Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
7ccd29bfb1 | |||
852874a12a | |||
09ed6cc8c4 | |||
ba71b42369 | |||
fc4f0affc2 | |||
84961bda58 | |||
2edf00073f | |||
e72171f1ef | |||
85ab2a963b | |||
25512974d2 | |||
9dc85805e3 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
lapisdeploy
|
lapisdeploy
|
||||||
config.json
|
config.json
|
||||||
|
config.lua
|
||||||
sites/
|
sites/
|
||||||
|
80
config.go
80
config.go
@ -2,14 +2,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"errors"
|
||||||
"encoding/json"
|
|
||||||
"log"
|
"github.com/yuin/gopher-lua"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Configuration stores information retrieved from a configuration file
|
// Configuration stores information retrieved from a configuration file
|
||||||
type Configuration struct {
|
type Configuration struct {
|
||||||
port int
|
port int64
|
||||||
sites_dir string
|
sites_dir string
|
||||||
matrix struct {
|
matrix struct {
|
||||||
homeserver string
|
homeserver string
|
||||||
@ -19,24 +19,64 @@ type Configuration struct {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getStringFromTable retrieves a string from a table on the lua stack using the key passed in.
|
||||||
|
func getStringFromTable(L *lua.LState, lobj lua.LValue, key string, out *string) error {
|
||||||
|
lv := L.GetTable(lobj, lua.LString(key))
|
||||||
|
if text, ok := lv.(lua.LString); ok {
|
||||||
|
*out = string(text)
|
||||||
|
} else {
|
||||||
|
return errors.New("Failed to get string from table using key '" + key + "'")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getIntFromTable retrieves an integer from a table on the Lua stack using the key passed in.
|
||||||
|
func getIntFromTable(L *lua.LState, lobj lua.LValue, key string, out *int64) error {
|
||||||
|
lv := L.GetTable(lobj, lua.LString(key))
|
||||||
|
if int, ok := lv.(lua.LNumber); ok {
|
||||||
|
*out = int64(int)
|
||||||
|
} else {
|
||||||
|
return errors.New("Failed to get integer from table using key '" + key + "'")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getTableFromTable retrieves a nested table from the Lua stack using the key passed in.
|
||||||
|
func getTableFromTable(L *lua.LState, lobj lua.LValue, key string, out *lua.LValue) {
|
||||||
|
*out = L.GetTable(lobj, lua.LString(key))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// parseConfig parses the JSON configuration file at 'path' and stores the contents in 'config'
|
// parseConfig parses the JSON configuration file at 'path' and stores the contents in 'config'
|
||||||
func parseConfig(path string, config *Configuration) {
|
func parseConfig(path string, config *Configuration) {
|
||||||
file, _ := os.Open(path)
|
// Lua state setup
|
||||||
var data map[string]interface{}
|
L := lua.NewState()
|
||||||
err := json.NewDecoder(file).Decode(&data)
|
defer L.Close()
|
||||||
if err != nil {
|
|
||||||
log.Panic("Failed to parse config:", err)
|
|
||||||
}
|
|
||||||
file.Close()
|
|
||||||
// Main configuration
|
|
||||||
config.port = int(data["port"].(float64)) // this conversion makes no sense
|
|
||||||
config.sites_dir = data["sites_dir"].(string)
|
|
||||||
|
|
||||||
// Matrix configuration
|
L.DoFile(path)
|
||||||
matrix := data["matrix"].(map[string]interface{})
|
table := L.Get(-1) // Get the table returned by the config file
|
||||||
config.matrix.homeserver = matrix["homeserver"].(string)
|
|
||||||
config.matrix.username = matrix["username"].(string)
|
// Main config
|
||||||
config.matrix.password = matrix["password"].(string)
|
if err := getStringFromTable(L, table, "sites_dir", &config.sites_dir); err != nil {
|
||||||
config.matrix.room_id = matrix["room_id"].(string)
|
panic(err)
|
||||||
|
}
|
||||||
|
if err := getIntFromTable(L, table, "port", &config.port); err != nil {
|
||||||
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Matrix config
|
||||||
|
var matrix lua.LValue
|
||||||
|
getTableFromTable(L, table, "matrix", &matrix)
|
||||||
|
if err := getStringFromTable(L, matrix, "homeserver", &config.matrix.homeserver); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if err := getStringFromTable(L, matrix, "username", &config.matrix.username); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if err := getStringFromTable(L, matrix, "password", &config.matrix.password); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if err := getStringFromTable(L, matrix, "room_id", &config.matrix.room_id); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
10
config.json
10
config.json
@ -1,10 +0,0 @@
|
|||||||
{
|
|
||||||
"port": 7575,
|
|
||||||
"sites_dir": "./sites",
|
|
||||||
"matrix": {
|
|
||||||
"homeserver": "https://retroedge.tech",
|
|
||||||
"username": "@lapisdeploy:retroedge.tech",
|
|
||||||
"password": "orange73",
|
|
||||||
"room_id": "!CVyqZMOFBXFdExgnug:retroedge.tech"
|
|
||||||
}
|
|
||||||
}
|
|
10
go.mod
10
go.mod
@ -3,8 +3,13 @@ module code.retroedge.tech/noah/lapisdeploy
|
|||||||
go 1.22.0
|
go 1.22.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/akamensky/argparse v1.4.0 // indirect
|
github.com/akamensky/argparse v1.4.0
|
||||||
github.com/gogs/git-module v1.8.3 // indirect
|
github.com/gogs/git-module v1.8.3
|
||||||
|
github.com/yuin/gopher-lua v1.1.1
|
||||||
|
maunium.net/go/mautrix v0.17.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75 // indirect
|
github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75 // indirect
|
||||||
@ -22,5 +27,4 @@ require (
|
|||||||
golang.org/x/sys v0.18.0 // indirect
|
golang.org/x/sys v0.18.0 // indirect
|
||||||
golang.org/x/tools v0.19.0 // indirect
|
golang.org/x/tools v0.19.0 // indirect
|
||||||
maunium.net/go/maulogger/v2 v2.4.1 // indirect
|
maunium.net/go/maulogger/v2 v2.4.1 // indirect
|
||||||
maunium.net/go/mautrix v0.17.0 // indirect
|
|
||||||
)
|
)
|
||||||
|
2
go.sum
2
go.sum
@ -37,6 +37,8 @@ github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
|
|||||||
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
|
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
|
||||||
github.com/yuin/goldmark v1.6.0 h1:boZcn2GTjpsynOsC0iJHnBWa4Bi0qzfJjthwauItG68=
|
github.com/yuin/goldmark v1.6.0 h1:boZcn2GTjpsynOsC0iJHnBWa4Bi0qzfJjthwauItG68=
|
||||||
github.com/yuin/goldmark v1.6.0/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
github.com/yuin/goldmark v1.6.0/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
|
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
|
||||||
|
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
|
||||||
go.mau.fi/util v0.4.0 h1:S2X3qU4pUcb/vxBRfAuZjbrR9xVMAXSjQojNBLPBbhs=
|
go.mau.fi/util v0.4.0 h1:S2X3qU4pUcb/vxBRfAuZjbrR9xVMAXSjQojNBLPBbhs=
|
||||||
go.mau.fi/util v0.4.0/go.mod h1:leeiHtgVBuN+W9aDii3deAXnfC563iN3WK6BF8/AjNw=
|
go.mau.fi/util v0.4.0/go.mod h1:leeiHtgVBuN+W9aDii3deAXnfC563iN3WK6BF8/AjNw=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
12
main.go
12
main.go
@ -58,7 +58,8 @@ func handler(data map[string]interface{}) {
|
|||||||
}
|
}
|
||||||
sendMessage(MatrixMessage{text: "⚪️ Starting server..."})
|
sendMessage(MatrixMessage{text: "⚪️ Starting server..."})
|
||||||
if site, exists, err = getSite(repo_name); err != nil {
|
if site, exists, err = getSite(repo_name); err != nil {
|
||||||
deploy_error := newDeployError(1, "handler", "Failed to get site '%s' after creation!", fmt.Sprint(err))
|
deploy_error := newDeployError(1, "handler",
|
||||||
|
fmt.Sprintf("Failed to get site '%s' after creation!", repo_name), fmt.Sprint(err))
|
||||||
deploy_error.SendOverMatrix()
|
deploy_error.SendOverMatrix()
|
||||||
}
|
}
|
||||||
if deploy_error := site.Start(); deploy_error.code != 0 {
|
if deploy_error := site.Start(); deploy_error.code != 0 {
|
||||||
@ -74,13 +75,20 @@ func handler(data map[string]interface{}) {
|
|||||||
func main() {
|
func main() {
|
||||||
// Parse arguments
|
// Parse arguments
|
||||||
parser := argparse.NewParser("lapisdeploy", "Easily deploy Lapis web applications through Gitea webhooks")
|
parser := argparse.NewParser("lapisdeploy", "Easily deploy Lapis web applications through Gitea webhooks")
|
||||||
config_path := parser.String("c", "config", &argparse.Options{Required: false, Help: "Configuration file", Default: "./config.json"})
|
config_path := parser.String("c", "config", &argparse.Options{Required: false, Help: "Configuration file", Default: "./config.lua"})
|
||||||
disable_matrix := parser.Flag("M", "disable-matrix", &argparse.Options{Required: false, Help: "Disable Matrix chat bot", Default: false})
|
disable_matrix := parser.Flag("M", "disable-matrix", &argparse.Options{Required: false, Help: "Disable Matrix chat bot", Default: false})
|
||||||
if err := parser.Parse(os.Args); err != nil { // Parse arguments
|
if err := parser.Parse(os.Args); err != nil { // Parse arguments
|
||||||
fmt.Print(parser.Usage(err)) // Show usage if there's an error
|
fmt.Print(parser.Usage(err)) // Show usage if there's an error
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if exists, err := fileExists(*config_path); !exists {
|
||||||
|
fmt.Printf("Invalid config file '%s'\n", *config_path)
|
||||||
|
os.Exit(1)
|
||||||
|
} else if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
parseConfig(*config_path, &configuration) // Parse JSON configuration file
|
parseConfig(*config_path, &configuration) // Parse JSON configuration file
|
||||||
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { // Webhook receiver
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { // Webhook receiver
|
||||||
|
83
matrix.go
83
matrix.go
@ -3,15 +3,15 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
|
||||||
"time"
|
|
||||||
"strings"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"maunium.net/go/mautrix"
|
"maunium.net/go/mautrix"
|
||||||
"maunium.net/go/mautrix/event"
|
"maunium.net/go/mautrix/event"
|
||||||
"maunium.net/go/mautrix/id"
|
|
||||||
"maunium.net/go/mautrix/format"
|
"maunium.net/go/mautrix/format"
|
||||||
|
"maunium.net/go/mautrix/id"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MatrixMessage stores information about a matrix message.
|
// MatrixMessage stores information about a matrix message.
|
||||||
@ -19,10 +19,13 @@ type MatrixMessage struct {
|
|||||||
text string
|
text string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MatrixCommandCallback is a function that gets called when the bot is issued a command over Matrix
|
||||||
|
type MatrixCommandCallback func(ctx context.Context, evt *event.Event, args []string) string
|
||||||
|
|
||||||
// MatrixCommand stores information about a matrix chat command
|
// MatrixCommand stores information about a matrix chat command
|
||||||
type MatrixCommand struct {
|
type MatrixCommand struct {
|
||||||
cmd string
|
cmd string
|
||||||
callback func(ctx context.Context, evt *event.Event) string
|
callback MatrixCommandCallback
|
||||||
description string
|
description string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,7 +41,7 @@ func sendMessage(msg MatrixMessage) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// registerChatCommand
|
// registerChatCommand
|
||||||
func registerChatCommand(cmd string, description string, callback func(ctx context.Context, evt *event.Event) string) {
|
func registerChatCommand(cmd string, description string, callback MatrixCommandCallback) {
|
||||||
Chatcommands = append(Chatcommands, MatrixCommand{
|
Chatcommands = append(Chatcommands, MatrixCommand{
|
||||||
cmd: cmd,
|
cmd: cmd,
|
||||||
callback: callback,
|
callback: callback,
|
||||||
@ -84,11 +87,13 @@ func initMatrix() {
|
|||||||
prefixes := []string{"!", configuration.matrix.username + " "}
|
prefixes := []string{"!", configuration.matrix.username + " "}
|
||||||
for _, prefix := range prefixes {
|
for _, prefix := range prefixes {
|
||||||
if strings.HasPrefix(evt.Content.AsMessage().Body, prefix) {
|
if strings.HasPrefix(evt.Content.AsMessage().Body, prefix) {
|
||||||
|
msg := evt.Content.AsMessage().Body
|
||||||
|
cmd := strings.TrimPrefix(strings.Split(msg, " ")[0], prefix)
|
||||||
found_command := false
|
found_command := false
|
||||||
for _, command := range Chatcommands {
|
for _, command := range Chatcommands {
|
||||||
if command.cmd == strings.TrimPrefix(evt.Content.AsMessage().Body, prefix) {
|
if command.cmd == cmd {
|
||||||
found_command = true
|
found_command = true
|
||||||
out := command.callback(ctx, evt)
|
out := command.callback(ctx, evt, strings.Split(strings.TrimPrefix(msg, prefix+cmd), " "))
|
||||||
content := format.RenderMarkdown(out, true, false)
|
content := format.RenderMarkdown(out, true, false)
|
||||||
content.SetReply(evt)
|
content.SetReply(evt)
|
||||||
client.SendMessageEvent(ctx, evt.RoomID, event.EventMessage, content)
|
client.SendMessageEvent(ctx, evt.RoomID, event.EventMessage, content)
|
||||||
@ -103,7 +108,7 @@ func initMatrix() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
registerChatCommand("help", "Show the help dialouge", func(ctx context.Context, evt *event.Event) string {
|
registerChatCommand("help", "Show the help dialouge", func(ctx context.Context, evt *event.Event, args []string) string {
|
||||||
text := ""
|
text := ""
|
||||||
for _, cmd := range Chatcommands {
|
for _, cmd := range Chatcommands {
|
||||||
text = fmt.Sprintf("%s- %s\n %s\n", text, cmd.cmd, cmd.description)
|
text = fmt.Sprintf("%s- %s\n %s\n", text, cmd.cmd, cmd.description)
|
||||||
@ -111,14 +116,14 @@ func initMatrix() {
|
|||||||
return text
|
return text
|
||||||
})
|
})
|
||||||
|
|
||||||
registerChatCommand("sites", "Display all available sites", func(ctx context.Context, evt *event.Event) string {
|
registerChatCommand("sites", "Display all available sites", func(ctx context.Context, evt *event.Event, args []string) string {
|
||||||
text := "⚪️ Getting sites...\n"
|
text := "⚪️ Getting sites...\n"
|
||||||
sites, err := getAllSites()
|
sites, err := getAllSites()
|
||||||
if err.code != 0 {
|
if err.code != 0 {
|
||||||
return text + "🔴 Failed to get sites!"
|
return text + "🔴 Failed to get sites!"
|
||||||
}
|
}
|
||||||
for _, site := range sites {
|
for _, site := range sites {
|
||||||
text = fmt.Sprintf("%s- %s\n", text, site.name)
|
text = fmt.Sprintf("%s- %s\n", text, site.getName())
|
||||||
}
|
}
|
||||||
if len(sites) == 0 {
|
if len(sites) == 0 {
|
||||||
text = text + "*No sites found*"
|
text = text + "*No sites found*"
|
||||||
@ -126,6 +131,62 @@ func initMatrix() {
|
|||||||
return text
|
return text
|
||||||
})
|
})
|
||||||
|
|
||||||
|
registerChatCommand("restart", "Restart a site.", func(ctx context.Context, evt *event.Event, args []string) string {
|
||||||
|
if len(args) < 2 {
|
||||||
|
return "🔴 Invalid site."
|
||||||
|
}
|
||||||
|
site, found, err := getSite(args[1])
|
||||||
|
if err != nil {
|
||||||
|
return "🔴 Failed to get site " + args[2] + "!"
|
||||||
|
} else if !found {
|
||||||
|
return "🔴 Invalid site."
|
||||||
|
}
|
||||||
|
text := "⚪️ Restarting server..."
|
||||||
|
derr := site.Restart()
|
||||||
|
if derr.code != 0 {
|
||||||
|
derr.SendOverMatrix()
|
||||||
|
return text + "\n🔴 Failed to restart site!"
|
||||||
|
}
|
||||||
|
return text + "\n🟢 Successfully restarted site."
|
||||||
|
})
|
||||||
|
|
||||||
|
registerChatCommand("start", "Start a site.", func(ctx context.Context, evt *event.Event, args []string) string {
|
||||||
|
if len(args) < 2 {
|
||||||
|
return "🔴 Invalid site."
|
||||||
|
}
|
||||||
|
site, found, err := getSite(args[1])
|
||||||
|
if err != nil {
|
||||||
|
return "🔴 Failed to get site " + args[2] + "!"
|
||||||
|
} else if !found {
|
||||||
|
return "🔴 Invalid site."
|
||||||
|
}
|
||||||
|
|
||||||
|
text := "⚪️ Starting server..."
|
||||||
|
if derr := site.Start(); derr.code != 0 {
|
||||||
|
derr.SendOverMatrix()
|
||||||
|
return text + "\n🔴 Failed to stop site!"
|
||||||
|
}
|
||||||
|
return text + "\n🟢 Successfully started site."
|
||||||
|
})
|
||||||
|
|
||||||
|
registerChatCommand("stop", "Stop a site.", func(ctx context.Context, evt *event.Event, args []string) string {
|
||||||
|
if len(args) < 2 {
|
||||||
|
return "🔴 Invalid site."
|
||||||
|
}
|
||||||
|
site, found, err := getSite(args[1])
|
||||||
|
if err != nil {
|
||||||
|
return "🔴 Failed to get site " + args[2] + "!"
|
||||||
|
} else if !found {
|
||||||
|
return "🔴 Invalid site."
|
||||||
|
}
|
||||||
|
text := "⚪️ Stopping server..."
|
||||||
|
if derr := site.Stop(); derr.code != 0 {
|
||||||
|
derr.SendOverMatrix()
|
||||||
|
return text + "\n🔴 Failed to stop site!"
|
||||||
|
}
|
||||||
|
return text + "\n🟢 Successfully stopped site."
|
||||||
|
})
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for range time.Tick(time.Second * 6) {
|
for range time.Tick(time.Second * 6) {
|
||||||
log.Printf("Scanning for queued messages...")
|
log.Printf("Scanning for queued messages...")
|
||||||
|
82
site.go
82
site.go
@ -11,27 +11,72 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/gogs/git-module"
|
"github.com/gogs/git-module"
|
||||||
|
lua "github.com/yuin/gopher-lua"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Site contains information and methods used for management of a Lapis site.
|
// Site contains information and methods used for management of a Lapis site.
|
||||||
type Site struct {
|
type Site struct {
|
||||||
name string
|
name string
|
||||||
|
config SiteConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// SiteConfig contains parsed data from a site's configuration file.
|
||||||
|
type SiteConfig struct {
|
||||||
|
port int64
|
||||||
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
// getSite gets a specific site and returns a Site struct and a bool indicating whether a site was found.
|
// getSite gets a specific site and returns a Site struct and a bool indicating whether a site was found.
|
||||||
func getSite(name string) (Site, bool, error) {
|
func getSite(name string) (Site, bool, error) {
|
||||||
site_path := configuration.sites_dir + "/" + name
|
site_path := configuration.sites_dir + "/" + name
|
||||||
exists, err := fileExists(site_path);
|
exists, err := fileExists(site_path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Site{}, false, errors.New(fmt.Sprintf("Failed to get site '%s'", name))
|
return Site{}, false, errors.New(fmt.Sprintf("Failed to get site '%s'", name))
|
||||||
}
|
}
|
||||||
if exists {
|
if exists {
|
||||||
return Site{name: name}, true, nil
|
site := Site{name: name}
|
||||||
|
site.getConfig() // Get per-site config
|
||||||
|
return site, true, nil
|
||||||
} else {
|
} else {
|
||||||
return Site{}, false, nil
|
return Site{}, false, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getConfig parses a site's configuration file.
|
||||||
|
func (site *Site) getConfig() (bool, error) {
|
||||||
|
path := fmt.Sprintf("%s/%s/deploy_config.lua", configuration.sites_dir, site.name)
|
||||||
|
// Make sure config file exists
|
||||||
|
if exists, _ := fileExists(path); !exists {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
// Lua state setup
|
||||||
|
L := lua.NewState()
|
||||||
|
defer L.Close()
|
||||||
|
|
||||||
|
L.DoFile(path) // Load the config
|
||||||
|
table := L.Get(-1) // Get the table returned by the config file
|
||||||
|
|
||||||
|
// Parse the config
|
||||||
|
var config SiteConfig
|
||||||
|
log.Printf("Loading per-site config %s", path)
|
||||||
|
|
||||||
|
getStringFromTable(L, table, "name", &config.name)
|
||||||
|
getIntFromTable(L, table, "port", &config.port)
|
||||||
|
|
||||||
|
site.config = config
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getName gets a website's configured name (falls back to technical name if one isn't configured).
|
||||||
|
func (site *Site) getName() string {
|
||||||
|
if site.config.name == "" {
|
||||||
|
return site.name
|
||||||
|
} else {
|
||||||
|
return site.config.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// getAllSites gets every site installed.
|
// getAllSites gets every site installed.
|
||||||
func getAllSites() ([]Site, DeployError) {
|
func getAllSites() ([]Site, DeployError) {
|
||||||
sites_dir := configuration.sites_dir
|
sites_dir := configuration.sites_dir
|
||||||
@ -71,7 +116,7 @@ func startAllSites() DeployError {
|
|||||||
|
|
||||||
// Start attempts to start a Lapis server in the given repository.
|
// Start attempts to start a Lapis server in the given repository.
|
||||||
func (site *Site) Start() DeployError {
|
func (site *Site) Start() DeployError {
|
||||||
log.Printf("Starting Lapis server in repository %s...", site.name)
|
log.Printf("Starting Lapis server in %s...", site.getName())
|
||||||
|
|
||||||
old_cwd, _ := os.Getwd()
|
old_cwd, _ := os.Getwd()
|
||||||
defer syscall.Chdir(old_cwd)
|
defer syscall.Chdir(old_cwd)
|
||||||
@ -80,7 +125,7 @@ func (site *Site) Start() DeployError {
|
|||||||
cmd := exec.Command("lapis", "serve")
|
cmd := exec.Command("lapis", "serve")
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
return newDeployError(1, "startSite",
|
return newDeployError(1, "startSite",
|
||||||
fmt.Sprintf("Failed to start Lapis server in repository '%s'", site.name), "")
|
fmt.Sprintf("Failed to start Lapis server in '%s'", site.getName()), "")
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
cmd.Wait()
|
cmd.Wait()
|
||||||
@ -89,9 +134,28 @@ func (site *Site) Start() DeployError {
|
|||||||
return DeployError{}
|
return DeployError{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Stop attempts to stop a Lapis server in the given repository.
|
||||||
|
func (site *Site) Stop() DeployError {
|
||||||
|
log.Printf("Stopping Lapis server %s...", site.getName())
|
||||||
|
|
||||||
|
old_cwd, _ := os.Getwd()
|
||||||
|
defer syscall.Chdir(old_cwd)
|
||||||
|
syscall.Chdir(configuration.sites_dir + "/" + site.name)
|
||||||
|
|
||||||
|
cmd := exec.Command("lapis", "term")
|
||||||
|
if err := cmd.Start(); err != nil {
|
||||||
|
return newDeployError(1, "stopSite",
|
||||||
|
fmt.Sprintf("Failed to stop Lapis server in '%s'", site.getName()), "")
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
cmd.Wait()
|
||||||
|
}()
|
||||||
|
return DeployError{}
|
||||||
|
}
|
||||||
|
|
||||||
// Restart attempts to restart the Lapis server in the given repository.
|
// Restart attempts to restart the Lapis server in the given repository.
|
||||||
func (site *Site) Restart() DeployError {
|
func (site *Site) Restart() DeployError {
|
||||||
log.Printf("Restarting Lapis server on repository %s...", site.name)
|
log.Printf("Restarting Lapis server in %s...", site.getName())
|
||||||
|
|
||||||
old_cwd, _ := os.Getwd()
|
old_cwd, _ := os.Getwd()
|
||||||
defer syscall.Chdir(old_cwd)
|
defer syscall.Chdir(old_cwd)
|
||||||
@ -100,19 +164,19 @@ func (site *Site) Restart() DeployError {
|
|||||||
out, err := exec.Command("lapis", "build").CombinedOutput()
|
out, err := exec.Command("lapis", "build").CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return newDeployError(1, "restartSite",
|
return newDeployError(1, "restartSite",
|
||||||
fmt.Sprintf("Failed to run 'lapis build' in repository '%s'", site.name), string(out))
|
fmt.Sprintf("Failed to run 'lapis build' in '%s'", site.getName()), string(out))
|
||||||
}
|
}
|
||||||
log.Printf("[lapis build] %s", out)
|
log.Printf("[lapis build] %s", out)
|
||||||
if !strings.Contains(string(out), "HUP") {
|
if !strings.Contains(string(out), "HUP") {
|
||||||
return newDeployError(1, "restartSite",
|
return newDeployError(1, "restartSite",
|
||||||
"'lapis build' command returned unexpected value! (Lapis server not running?)", string(out))
|
"Failed to restart Lapis server! (Lapis not running?)", string(out))
|
||||||
}
|
}
|
||||||
return DeployError{}
|
return DeployError{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update attempts to pull or clone a repository using the given name and url
|
// Update attempts to pull or clone a repository using the given name and url
|
||||||
func (site *Site) Update() DeployError {
|
func (site *Site) Update() DeployError {
|
||||||
log.Printf("Pulling down repository %s...", site.name)
|
log.Printf("Pulling down repository %s...", site.getName())
|
||||||
repo, err := git.Open(configuration.sites_dir + "/" + site.name)
|
repo, err := git.Open(configuration.sites_dir + "/" + site.name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return newDeployError(1, "updateSite",
|
return newDeployError(1, "updateSite",
|
||||||
@ -120,7 +184,7 @@ func (site *Site) Update() DeployError {
|
|||||||
}
|
}
|
||||||
if err = repo.Pull(); err != nil {
|
if err = repo.Pull(); err != nil {
|
||||||
return newDeployError(1, "updateSite",
|
return newDeployError(1, "updateSite",
|
||||||
fmt.Sprintf("Failed to pull down git repository '%s'", site.name), fmt.Sprint(err))
|
fmt.Sprintf("Failed to pull down git repository '%s'", site.getName()), fmt.Sprint(err))
|
||||||
}
|
}
|
||||||
return DeployError{}
|
return DeployError{}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user