Compare commits
29 Commits
Author | SHA1 | Date | |
---|---|---|---|
ba045a947f | |||
d54fd47375 | |||
a122598484 | |||
20b955231b | |||
70fffcd25b | |||
dea25cd2f9 | |||
a4ae6f77c7 | |||
476ac5a175 | |||
698d472ca1 | |||
b15b5b7be1 | |||
e855c3e630 | |||
c19cb14ade | |||
2e5c3dbed6 | |||
e62d7fe0c2 | |||
4ecd4b06b8 | |||
8875a40fea | |||
fc91613a66 | |||
401f4a89f8 | |||
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/
|
||||||
|
146
config.go
146
config.go
@ -2,41 +2,139 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"errors"
|
||||||
"encoding/json"
|
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
lua "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 {
|
trusted_servers []string
|
||||||
|
matrix struct {
|
||||||
homeserver string
|
homeserver string
|
||||||
username string
|
username string
|
||||||
password string
|
password string
|
||||||
room_id string
|
room_id string
|
||||||
|
disabled bool
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getStringFromTable retrieves a string from a table on the lua stack using the key passed in.
|
||||||
|
//
|
||||||
|
// If the value is nil, 'out' is unchanged.
|
||||||
|
func getStringFromTable(L *lua.LState, lobj lua.LValue, key string, out *string) (bool, error) {
|
||||||
|
lv := L.GetTable(lobj, lua.LString(key))
|
||||||
|
if text, ok := lv.(lua.LString); ok { // Value is string
|
||||||
|
*out = string(text)
|
||||||
|
} else if _, ok := lv.(*lua.LNilType); !ok { // Value is not a string, and is not nil
|
||||||
|
return true, errors.New("'" + key + "' is not a string")
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getIntFromTable retrieves an integer from a table on the Lua stack using the key passed in.
|
||||||
|
//
|
||||||
|
// If the value is nil, 'out' is unchanged.
|
||||||
|
func getIntFromTable(L *lua.LState, lobj lua.LValue, key string, out *int64) (bool, error) {
|
||||||
|
lv := L.GetTable(lobj, lua.LString(key))
|
||||||
|
if int, ok := lv.(lua.LNumber); ok { // Value is number
|
||||||
|
*out = int64(int)
|
||||||
|
} else if _, ok := lv.(*lua.LNilType); !ok { // Value is not a number, and is not nil
|
||||||
|
return true, errors.New("'" + key + "' is not a number")
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getBoolFromTable retrieves a bool from a table on the Lua stack using the key passed in.
|
||||||
|
//
|
||||||
|
// If the value is nil, 'out' is unchanged.
|
||||||
|
func getBoolFromTable(L *lua.LState, lobj lua.LValue, key string, out *bool) (bool, error) {
|
||||||
|
lv := L.GetTable(lobj, lua.LString(key))
|
||||||
|
if boolean, ok := lv.(lua.LBool); ok { // Value is boolean
|
||||||
|
*out = bool(boolean)
|
||||||
|
} else if _, ok := lv.(*lua.LNilType); !ok { // Value is not a bool, and is not nil
|
||||||
|
return true, errors.New("'" + key + "' is not a bool")
|
||||||
|
}
|
||||||
|
return true, 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) bool {
|
||||||
|
lv := L.GetTable(lobj, lua.LString(key))
|
||||||
|
*out = lv
|
||||||
|
if _, ok := lv.(*lua.LNilType); ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// 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)
|
log.Printf("Loading config from : %s ...", path)
|
||||||
var data map[string]interface{}
|
// Lua state setup
|
||||||
err := json.NewDecoder(file).Decode(&data)
|
L := lua.NewState()
|
||||||
if err != nil {
|
defer L.Close()
|
||||||
log.Panic("Failed to parse config:", err)
|
|
||||||
|
L.DoFile(path)
|
||||||
|
table := L.Get(-1) // Get the table returned by the config file
|
||||||
|
if table.Type().String() != "table" { // Make sure value returned is of type 'table'
|
||||||
|
log.Fatalf("config is of type : '%s', not 'table'", table.Type().String())
|
||||||
}
|
}
|
||||||
file.Close()
|
|
||||||
// Main configuration
|
|
||||||
config.port = int(data["port"].(float64)) // this conversion makes no sense
|
|
||||||
config.sites_dir = data["sites_dir"].(string)
|
|
||||||
|
|
||||||
// Matrix configuration
|
// Main config
|
||||||
matrix := data["matrix"].(map[string]interface{})
|
if _, err := getStringFromTable(L, table, "sites_dir", &config.sites_dir); err != nil {
|
||||||
config.matrix.homeserver = matrix["homeserver"].(string)
|
panic(err)
|
||||||
config.matrix.username = matrix["username"].(string)
|
}
|
||||||
config.matrix.password = matrix["password"].(string)
|
if _, err := getIntFromTable(L, table, "port", &config.port); err != nil {
|
||||||
config.matrix.room_id = matrix["room_id"].(string)
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trusted servers
|
||||||
|
var trusted_servers lua.LValue
|
||||||
|
getTableFromTable(L, table, "trusted_servers", &trusted_servers) // Get the 'trusted_servers' table
|
||||||
|
// Make sure 'config.trusted_servers' is of type 'table'
|
||||||
|
if trusted_servers_type := trusted_servers.Type().String(); trusted_servers_type != "table" &&
|
||||||
|
trusted_servers_type != "nil" {
|
||||||
|
log.Fatalf("config.trusted_servers is of type : '%s', not 'table'", trusted_servers_type)
|
||||||
|
} else if trusted_servers_type != "nil" { // If type is not nil (has to be table or it would've been caught already)
|
||||||
|
for i := 1; i <= L.ObjLen(trusted_servers); i++ { // Loop through every trusted server
|
||||||
|
lv := L.GetTable(trusted_servers, lua.LNumber(i))
|
||||||
|
if text, ok := lv.(lua.LString); ok { // Value is string
|
||||||
|
config.trusted_servers = append(config.trusted_servers, string(text))
|
||||||
|
} else if _, ok := lv.(*lua.LNilType); !ok { // Value is not a string, and is not nil
|
||||||
|
log.Fatalf("config.trusted_servers[%d] is of type : '%s', not 'string'", i, lv.Type().String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Matrix config
|
||||||
|
var matrix lua.LValue
|
||||||
|
getTableFromTable(L, table, "matrix", &matrix) // Get the 'matrix' table
|
||||||
|
// Make sure 'config.matrix' is of type 'table'
|
||||||
|
if matrix_type := matrix.Type().String(); matrix_type != "table" && matrix_type != "nil" {
|
||||||
|
log.Fatalf("config.matrix is of type : '%s', not 'table'", matrix_type)
|
||||||
|
} else if matrix_type != "nil" {
|
||||||
|
if _, err := getBoolFromTable(L, matrix, "disabled", &config.matrix.disabled); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if !config.matrix.disabled { // Only load the rest of the matrix config if matrix isn't disabled
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if matrix_type == "nil" { // Assume config.matrix.disabled is true if 'matrix' is missing from the config file
|
||||||
|
config.matrix.disabled = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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"
|
|
||||||
}
|
|
||||||
}
|
|
13
config.lua.example
Normal file
13
config.lua.example
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
return {
|
||||||
|
port = 7575,
|
||||||
|
sites_dir = "./sites",
|
||||||
|
trusted_servers = { -- IPs of servers that are allowed to send Gitea webhooks
|
||||||
|
},
|
||||||
|
matrix = {
|
||||||
|
homeserver = "https://matrix.org",
|
||||||
|
username = "bot:matrix.org",
|
||||||
|
password = "Sup@rS3cure",
|
||||||
|
room_id = "", -- Room ID can be found under "room settings" in most clients
|
||||||
|
disabled = false
|
||||||
|
}
|
||||||
|
}
|
@ -1,27 +0,0 @@
|
|||||||
// package main is the main package for the LapisDeploy program
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
// DeployError contains important information about an error if something went wrong.
|
|
||||||
type DeployError struct {
|
|
||||||
code int
|
|
||||||
where string
|
|
||||||
details string
|
|
||||||
command_output string
|
|
||||||
}
|
|
||||||
|
|
||||||
// SendOverMatrix provides an easy way to send the contents of a DeployError over Matrix
|
|
||||||
func (deploy_error *DeployError) SendOverMatrix() {
|
|
||||||
sendMessage(MatrixMessage{text: fmt.Sprintf("🔴 **Error in **`%s`**!**\n- *%s*\n- Code: %d",
|
|
||||||
deploy_error.where, deploy_error.details, deploy_error.code),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// newDeployError creates a DeployError
|
|
||||||
func newDeployError(code int, where string, details string, command_output string) DeployError {
|
|
||||||
return DeployError{ code: code, where: where, details: details, command_output: command_output }
|
|
||||||
}
|
|
||||||
|
|
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=
|
||||||
|
104
main.go
104
main.go
@ -3,15 +3,24 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/akamensky/argparse"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/akamensky/argparse"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Create a configuration struct
|
// Circle emoji
|
||||||
var configuration = Configuration{}
|
var Circles = map[string]string{
|
||||||
|
"white": "⚪️ ",
|
||||||
|
"green": "🟢 ",
|
||||||
|
"yellow": "🟡 ",
|
||||||
|
"red": "🔴 ",
|
||||||
|
"orange": "🟠 ",
|
||||||
|
}
|
||||||
|
|
||||||
// fileExists returns whether the given file or directory exists.
|
// fileExists returns whether the given file or directory exists.
|
||||||
func fileExists(path string) (bool, error) {
|
func fileExists(path string) (bool, error) {
|
||||||
@ -25,79 +34,120 @@ func fileExists(path string) (bool, error) {
|
|||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a configuration struct
|
||||||
|
var configuration = Configuration{}
|
||||||
|
|
||||||
// handler is in charge of handling requests after the JSON has been parsed
|
// handler is in charge of handling requests after the JSON has been parsed
|
||||||
func handler(data map[string]interface{}) {
|
func handler(data map[string]interface{}) {
|
||||||
repository := data["repository"].(map[string]interface{})
|
repository := data["repository"].(map[string]interface{})
|
||||||
log.Default().Printf("Repo: %s", repository["full_name"])
|
log.Default().Printf("Repo: %s", repository["full_name"])
|
||||||
sendMessage(MatrixMessage{text: fmt.Sprintf("⚪️ Handling webhook for `%s`...", repository["full_name"])})
|
sendInfo(fmt.Sprintf("Handling webhook for `%s`...", repository["full_name"]))
|
||||||
|
|
||||||
repo_name := repository["name"].(string)
|
repo_name := repository["name"].(string)
|
||||||
|
|
||||||
site, exists, err := getSite(repo_name)
|
site, exists, err := getSite(repo_name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
deploy_error := newDeployError(1, "handler", fmt.Sprintf("Failed to check if site '%s' exists", repo_name), fmt.Sprint(err))
|
sendError(errors.New(fmt.Sprintf("Failed to check if site '%s' exists: %s", repo_name, err)))
|
||||||
deploy_error.SendOverMatrix()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if exists {
|
if exists {
|
||||||
sendMessage(MatrixMessage{text: "⚪️ Updating repository..."})
|
sendInfo("Updating repository...")
|
||||||
if deploy_error := site.Update(); deploy_error.code != 0 {
|
if err := site.Update(); err != nil {
|
||||||
deploy_error.SendOverMatrix()
|
sendError(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sendMessage(MatrixMessage{text: "⚪️ Restarting server..."})
|
new_site, exists, err := getSite(site.name)
|
||||||
if deploy_error := site.Restart(); deploy_error.code != 0 {
|
if err != nil || !exists {
|
||||||
deploy_error.SendOverMatrix()
|
sendError(errors.New("Failed to update site data on a site we just updated: " + repo_name))
|
||||||
|
}
|
||||||
|
site = new_site
|
||||||
|
sendInfo("Restarting server...")
|
||||||
|
if error := site.Restart(); error != nil {
|
||||||
|
sendError(error)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
sendMessage(MatrixMessage{text: "⚪️ Cloning repository..."})
|
sendInfo("Cloning repositiory...")
|
||||||
if deploy_error := CloneSite(repository["ssh_url"].(string), repo_name); deploy_error.code != 0 {
|
if error := CloneSite(repository["ssh_url"].(string), repo_name); error != nil {
|
||||||
deploy_error.SendOverMatrix()
|
sendError(error)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sendMessage(MatrixMessage{text: "⚪️ Starting server..."})
|
sendInfo("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))
|
sendError(err)
|
||||||
deploy_error.SendOverMatrix()
|
return
|
||||||
}
|
}
|
||||||
if deploy_error := site.Start(); deploy_error.code != 0 {
|
if error := site.Start(); error != nil {
|
||||||
deploy_error.SendOverMatrix()
|
sendError(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer sendMessage(MatrixMessage{text: "🚀 Launched for the first time!"})
|
defer sendMessage(MatrixMessage{text: "🚀 Launched for the first time!"})
|
||||||
}
|
}
|
||||||
sendMessage(MatrixMessage{text: "🟢 Deployed successfully!"})
|
sendMessage(MatrixMessage{text: Circles["green"] + "Deployed successfully!"})
|
||||||
}
|
}
|
||||||
|
|
||||||
// main is the starting point of the program
|
// main is the starting point of the program
|
||||||
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
|
||||||
|
// Disable Matrix if it's disabled in either the config or specified on CLI
|
||||||
|
configuration.matrix.disabled = *disable_matrix || configuration.matrix.disabled
|
||||||
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { // Webhook receiver
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { // Webhook receiver
|
||||||
|
log.Print("Recieved request!")
|
||||||
var data map[string]interface{}
|
var data map[string]interface{}
|
||||||
err := json.NewDecoder(r.Body).Decode(&data)
|
err := json.NewDecoder(r.Body).Decode(&data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
check_ips := []string{ // IPs of client to check against trusted_servers
|
||||||
go handler(data) // Run the handler
|
strings.Split(r.RemoteAddr, ":")[0],
|
||||||
fmt.Fprint(w, "Received!")
|
r.Header.Get("x-forwarded-for"),
|
||||||
|
}
|
||||||
|
found_trusted_server := false
|
||||||
|
for _, server := range configuration.trusted_servers {
|
||||||
|
if found_trusted_server {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
for _, ip := range check_ips {
|
||||||
|
if ip == server {
|
||||||
|
log.Printf("Request IP matches a trusted server (%s)", ip)
|
||||||
|
go handler(data) // Run the handler
|
||||||
|
fmt.Fprint(w, "Received!")
|
||||||
|
found_trusted_server = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found_trusted_server {
|
||||||
|
log.Print("Request IP doesn't match a trusted server! Dropping...")
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
// Attempt to create sites_dir
|
||||||
|
if exists, _ := fileExists(configuration.sites_dir); !exists {
|
||||||
|
os.Mkdir(configuration.sites_dir, 640) // Permissions are 640 (Owner: rw, Group: r, All: none)
|
||||||
|
}
|
||||||
|
|
||||||
log.Printf("Starting Lapis Deploy on port %d...", configuration.port)
|
log.Printf("Starting Lapis Deploy on port %d...", configuration.port)
|
||||||
|
|
||||||
startAllSites() // Start all the servers
|
startAllSites() // Start all the servers
|
||||||
if !*disable_matrix { // Only start Matrix bot if --disable-matrix isn't passed
|
if !configuration.matrix.disabled { // Only start Matrix bot if --disable-matrix isn't passed
|
||||||
go initMatrix() // Start Matrix stuff in a goroutine
|
go initMatrix() // Start Matrix stuff in a goroutine
|
||||||
} else {
|
} else {
|
||||||
log.Print("Matrix bot is disabled!")
|
log.Print("Matrix bot is disabled!")
|
||||||
|
123
matrix.go
123
matrix.go
@ -3,46 +3,59 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
|
||||||
"time"
|
|
||||||
"strings"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"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.
|
||||||
type MatrixMessage struct {
|
type MatrixMessage struct {
|
||||||
text string
|
text string
|
||||||
|
level int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output queue
|
// Output queue.
|
||||||
var MatrixOut []MatrixMessage
|
var MatrixOut struct {
|
||||||
|
queue []MatrixMessage
|
||||||
// ChatCommands
|
mux sync.Mutex
|
||||||
var Chatcommands []MatrixCommand
|
|
||||||
|
|
||||||
// sendMessage sends a message through Matrix
|
|
||||||
func sendMessage(msg MatrixMessage) {
|
|
||||||
MatrixOut = append(MatrixOut, msg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// registerChatCommand
|
// sendMessage sends a message through Matrix.
|
||||||
func registerChatCommand(cmd string, description string, callback func(ctx context.Context, evt *event.Event) string) {
|
func sendMessage(msg MatrixMessage) {
|
||||||
Chatcommands = append(Chatcommands, MatrixCommand{
|
log.Print(msg.text)
|
||||||
cmd: cmd,
|
MatrixOut.mux.Lock()
|
||||||
callback: callback,
|
MatrixOut.queue = append(MatrixOut.queue, msg)
|
||||||
description: description,
|
MatrixOut.mux.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// sendError sends an error through Matrix.
|
||||||
|
func sendError(err error) {
|
||||||
|
sendMessage(MatrixMessage{
|
||||||
|
text: fmt.Sprintf("%s %s", Circles["red"], err),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// sendInfo sends a bit of info text through Matrix.
|
||||||
|
func sendInfo(text string) {
|
||||||
|
sendMessage(MatrixMessage{
|
||||||
|
text: fmt.Sprintf("%s %s", Circles["white"], text),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,9 +66,9 @@ func initMatrix() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
resp, err := client.Login(context.Background(), &mautrix.ReqLogin{
|
resp, err := client.Login(context.Background(), &mautrix.ReqLogin{
|
||||||
Type: "m.login.password",
|
Type: "m.login.password",
|
||||||
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: configuration.matrix.username},
|
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: configuration.matrix.username},
|
||||||
Password: configuration.matrix.password,
|
Password: configuration.matrix.password,
|
||||||
StoreCredentials: true,
|
StoreCredentials: true,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -81,21 +94,23 @@ func initMatrix() {
|
|||||||
})
|
})
|
||||||
syncer.OnEventType(event.EventMessage, func(ctx context.Context, evt *event.Event) {
|
syncer.OnEventType(event.EventMessage, func(ctx context.Context, evt *event.Event) {
|
||||||
if evt.RoomID.String() == configuration.matrix.room_id {
|
if evt.RoomID.String() == configuration.matrix.room_id {
|
||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !found_command {
|
if !found_command {
|
||||||
client.SendText(ctx, evt.RoomID, "🔴 Command not found")
|
client.SendText(ctx, evt.RoomID, Circles["red"]+"Command not found")
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -103,41 +118,31 @@ func initMatrix() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
registerChatCommand("help", "Show the help dialouge", func(ctx context.Context, evt *event.Event) string {
|
registerChatCommands() // Register all the chat commands defined inside matrix_commands.go
|
||||||
text := ""
|
|
||||||
for _,cmd := range Chatcommands {
|
|
||||||
text = fmt.Sprintf("%s- %s\n %s\n", text, cmd.cmd, cmd.description)
|
|
||||||
}
|
|
||||||
return text
|
|
||||||
})
|
|
||||||
|
|
||||||
registerChatCommand("sites", "Display all available sites", func(ctx context.Context, evt *event.Event) string {
|
go func() {
|
||||||
text := "⚪️ Getting sites...\n"
|
for range time.Tick(time.Second * 2) {
|
||||||
sites, err := getAllSites()
|
MatrixOut.mux.Lock()
|
||||||
if err.code != 0 {
|
if len(MatrixOut.queue) > 0 {
|
||||||
return text+"🔴 Failed to get sites!"
|
text := ""
|
||||||
}
|
for i, msg := range MatrixOut.queue {
|
||||||
for _, site := range sites {
|
text += msg.text
|
||||||
text = fmt.Sprintf("%s- %s\n", text, site.name)
|
if i != len(MatrixOut.queue) {
|
||||||
}
|
text += "\n"
|
||||||
if len(sites) == 0 {
|
}
|
||||||
text = text+"*No sites found*"
|
}
|
||||||
}
|
MatrixOut.queue = []MatrixMessage{}
|
||||||
return text
|
_, err := client.SendMessageEvent(context.Background(),
|
||||||
})
|
id.RoomID(configuration.matrix.room_id),
|
||||||
|
event.EventMessage,
|
||||||
go func () {
|
format.RenderMarkdown(text, true, false),
|
||||||
for range time.Tick(time.Second * 6) {
|
)
|
||||||
log.Printf("Scanning for queued messages...")
|
|
||||||
for _, msg := range MatrixOut {
|
|
||||||
_, err := client.SendMessageEvent(context.Background(), id.RoomID(configuration.matrix.room_id), event.EventMessage, format.RenderMarkdown(msg.text, true, false))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[Matrix] ERROR : %s", err)
|
log.Printf("[Matrix] ERROR : %s", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
log.Print("[Matrix] Sent queued message successfully.")
|
|
||||||
}
|
}
|
||||||
MatrixOut = []MatrixMessage{}
|
MatrixOut.mux.Unlock()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
140
matrix_commands.go
Normal file
140
matrix_commands.go
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"maunium.net/go/mautrix/event"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Chatcommands = []MatrixCommand{}
|
||||||
|
|
||||||
|
// getSitesByString is a helper function for chat commands.
|
||||||
|
// If text is "all" it will get all sites. If text is a specific site name it will get that site.
|
||||||
|
// Returns a list of strings that are textual representations of errors.
|
||||||
|
func getSitesByString(text string) ([]Site, []string) {
|
||||||
|
errors := []string{} // errors holds any errors we encounter in this function
|
||||||
|
sites := []Site{} // sites holds every site we need
|
||||||
|
// Determine which sites to get
|
||||||
|
if text == "all" { // If we need to get all sites, (!restart all)
|
||||||
|
var err error
|
||||||
|
sites, err = getAllSites() // Get all sites
|
||||||
|
if err != nil { // Check for errors
|
||||||
|
errors = append(errors, "Failed to get all sites!")
|
||||||
|
}
|
||||||
|
} else { // If we need to get a specific site, (!restart LapisDeploy-TestApp)
|
||||||
|
site, found, err := getSite(text) // (Attempt to) get that site.
|
||||||
|
// Check if something went wrong (site doesn't exist, error occured)
|
||||||
|
if err != nil { // Error occured.
|
||||||
|
errors = append(errors, "Failed to get site "+text+"!")
|
||||||
|
} else if !found { // Site doesn't exist.
|
||||||
|
errors = append(errors, "Invalid site.")
|
||||||
|
}
|
||||||
|
sites = append(sites, site) // Add this site to the list of sites needed
|
||||||
|
}
|
||||||
|
return sites, errors
|
||||||
|
}
|
||||||
|
|
||||||
|
func registerChatCommands() {
|
||||||
|
Chatcommands = []MatrixCommand{
|
||||||
|
{
|
||||||
|
cmd: "help",
|
||||||
|
description: "Show the help dialouge",
|
||||||
|
callback: func(ctx context.Context, evt *event.Event, args []string) string {
|
||||||
|
text := ""
|
||||||
|
for _, cmd := range Chatcommands {
|
||||||
|
text = fmt.Sprintf("%s- %s\n %s\n", text, cmd.cmd, cmd.description)
|
||||||
|
}
|
||||||
|
return text
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
cmd: "sites",
|
||||||
|
description: "Display all available sites",
|
||||||
|
callback: func(ctx context.Context, evt *event.Event, args []string) string {
|
||||||
|
text := Circles["white"] + "Getting sites...\n"
|
||||||
|
sites, err := getAllSites()
|
||||||
|
if err != nil {
|
||||||
|
return text + Circles["red"] + "Failed to get sites!"
|
||||||
|
}
|
||||||
|
for _, site := range sites {
|
||||||
|
text = fmt.Sprintf("%s%s", text, site.getName())
|
||||||
|
if site.config.name != "" { // Only add technical name to sites with a configured name.
|
||||||
|
text += " (" + site.name + ")"
|
||||||
|
}
|
||||||
|
text += "\n"
|
||||||
|
}
|
||||||
|
if len(sites) == 0 {
|
||||||
|
text = text + "*No sites found*"
|
||||||
|
}
|
||||||
|
return text
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
cmd: "restart",
|
||||||
|
description: "Restart either a specific site, or all sites.",
|
||||||
|
callback: func(ctx context.Context, evt *event.Event, args []string) string {
|
||||||
|
if len(args) < 2 { // If no arguments were given
|
||||||
|
return Circles["red"] + "Invalid site."
|
||||||
|
}
|
||||||
|
sites, errors := getSitesByString(args[1])
|
||||||
|
text := "" // text contains the entire message that will be sent.
|
||||||
|
for _, error := range errors {
|
||||||
|
text += Circles["red"] + error + "\n"
|
||||||
|
}
|
||||||
|
for _, site := range sites { // For every site queued to be restarted,
|
||||||
|
if err := site.Restart(); err != nil { // Attempt to restart it
|
||||||
|
text += fmt.Sprintf("%sRestarting '%s'... failed!\n", Circles["red"], site.getName())
|
||||||
|
} else { // Otherwise, success.
|
||||||
|
text += fmt.Sprintf("%sRestarting '%s'... success", Circles["green"], site.getName())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return text // Return the message.
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
cmd: "start",
|
||||||
|
description: "Start a site.",
|
||||||
|
callback: func(ctx context.Context, evt *event.Event, args []string) string {
|
||||||
|
if len(args) < 2 {
|
||||||
|
return Circles["red"] + "Invalid site."
|
||||||
|
}
|
||||||
|
sites, errors := getSitesByString(args[1])
|
||||||
|
text := "" // text contains the entire message that will be sent.
|
||||||
|
for _, error := range errors {
|
||||||
|
text += Circles["red"] + error + "\n"
|
||||||
|
}
|
||||||
|
for _, site := range sites {
|
||||||
|
if err := site.Start(); err != nil {
|
||||||
|
text += fmt.Sprintf("%sStarting '%s'... failed!\n", Circles["red"], site.getName())
|
||||||
|
} else {
|
||||||
|
text += fmt.Sprintf("%sStarting '%s'... success!\n", Circles["green"], site.getName())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return text
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
cmd: "stop",
|
||||||
|
description: "Stop a site.",
|
||||||
|
callback: func(ctx context.Context, evt *event.Event, args []string) string {
|
||||||
|
if len(args) < 2 {
|
||||||
|
return Circles["red"] + "Invalid site."
|
||||||
|
}
|
||||||
|
sites, errors := getSitesByString(args[1])
|
||||||
|
text := "" // text contains the entire message that will be sent.
|
||||||
|
for _, error := range errors {
|
||||||
|
text += Circles["red"] + error + "\n"
|
||||||
|
}
|
||||||
|
for _, site := range sites {
|
||||||
|
if err := site.Stop(); err != nil {
|
||||||
|
text += fmt.Sprintf("%sStopping '%s'... failed: %s", Circles["red"], site.getName(), err)
|
||||||
|
} else {
|
||||||
|
text += fmt.Sprintf("%sStopping '%s'... success", Circles["green"], site.getName())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return text
|
||||||
|
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
154
site.go
154
site.go
@ -11,35 +11,99 @@ 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
|
||||||
|
config SiteConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// SiteConfig contains parsed data from a site's configuration file.
|
||||||
|
type SiteConfig struct {
|
||||||
|
port int64
|
||||||
name string
|
name string
|
||||||
|
git struct {
|
||||||
|
branch 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)
|
||||||
|
warning := fmt.Sprintf("%s%s : Site config warning : ", Circles["orange"], site.getName())
|
||||||
|
// Make sure config file exists
|
||||||
|
if exists, _ := fileExists(path); !exists {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
log.Printf("Loading per-site config %s", path)
|
||||||
|
// 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
|
||||||
|
if table.Type().String() != "table" { // Make sure 'config' is of type 'table'
|
||||||
|
err := fmt.Sprintf("Value returned by config is of type '%s', not 'table'", table.Type().String())
|
||||||
|
sendMessage(MatrixMessage{text: warning + err})
|
||||||
|
return false, errors.New(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the config
|
||||||
|
config := SiteConfig{}
|
||||||
|
|
||||||
|
if _, err := getStringFromTable(L, table, "name", &config.name); err != nil { // name
|
||||||
|
sendMessage(MatrixMessage{text: warning + fmt.Sprint(err)})
|
||||||
|
}
|
||||||
|
if _, err := getIntFromTable(L, table, "port", &config.port); err != nil { // port
|
||||||
|
sendMessage(MatrixMessage{text: warning + fmt.Sprint(err)})
|
||||||
|
}
|
||||||
|
raw_gitt := L.GetTable(table, lua.LString("git")) // git
|
||||||
|
if gitt, ok := L.GetTable(table, lua.LString("git")).(*lua.LTable); ok {
|
||||||
|
if _, err := getStringFromTable(L, gitt, "branch", &config.git.branch); err != nil { // git.branch
|
||||||
|
sendMessage(MatrixMessage{text: warning + fmt.Sprint(err)})
|
||||||
|
}
|
||||||
|
} else if _, ok := raw_gitt.(*lua.LNilType); !ok { // If 'git' is neither a table or nil (undefined),
|
||||||
|
sendMessage(MatrixMessage{text: warning + "'git' is neither a table or nil!"})
|
||||||
|
}
|
||||||
|
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, error) {
|
||||||
sites_dir := configuration.sites_dir
|
sites_dir := configuration.sites_dir
|
||||||
files, err := os.ReadDir(sites_dir)
|
files, err := os.ReadDir(sites_dir)
|
||||||
var sites []Site
|
var sites []Site
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return sites, newDeployError(1, "startAllSites",
|
return sites, errors.New(fmt.Sprintf("Failed to read sites: %s", err))
|
||||||
"Failed to read sites", fmt.Sprint(err))
|
|
||||||
}
|
}
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
if !file.IsDir() {
|
if !file.IsDir() {
|
||||||
@ -47,90 +111,102 @@ func getAllSites() ([]Site, DeployError) {
|
|||||||
}
|
}
|
||||||
site, exists, err := getSite(file.Name())
|
site, exists, err := getSite(file.Name())
|
||||||
if err != nil || !exists {
|
if err != nil || !exists {
|
||||||
return sites, newDeployError(1, "getAllSites", "Failed to read sites", fmt.Sprint(err))
|
return sites, errors.New(fmt.Sprintf("Failed to read sites: %s", err))
|
||||||
}
|
}
|
||||||
sites = append(sites, site)
|
sites = append(sites, site)
|
||||||
}
|
}
|
||||||
return sites, DeployError{}
|
return sites, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// startAllSites attempts to start every site
|
// startAllSites attempts to start every site
|
||||||
func startAllSites() DeployError {
|
func startAllSites() error {
|
||||||
sites, err := getAllSites()
|
sites, err := getAllSites()
|
||||||
if err.code != 0 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, site := range sites {
|
for _, site := range sites {
|
||||||
err = site.Start()
|
err = site.Start()
|
||||||
if err.code != 0 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return DeployError{}
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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() error {
|
||||||
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)
|
||||||
syscall.Chdir(configuration.sites_dir+"/"+site.name)
|
syscall.Chdir(configuration.sites_dir + "/" + site.name)
|
||||||
|
|
||||||
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 errors.New(fmt.Sprintf("Failed to start Lapis server in '%s': %s", site.getName(), err))
|
||||||
fmt.Sprintf("Failed to start Lapis server in repository '%s'", site.name), "")
|
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
cmd.Wait()
|
cmd.Wait()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return DeployError{}
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restart attempts to restart the Lapis server in the given repository.
|
// Stop attempts to stop a Lapis server in the given repository.
|
||||||
func (site *Site) Restart() DeployError {
|
func (site *Site) Stop() error {
|
||||||
log.Printf("Restarting Lapis server on repository %s...", site.name)
|
log.Printf("Stopping Lapis server %s...", site.getName())
|
||||||
|
|
||||||
old_cwd, _ := os.Getwd()
|
old_cwd, _ := os.Getwd()
|
||||||
defer syscall.Chdir(old_cwd)
|
defer syscall.Chdir(old_cwd)
|
||||||
syscall.Chdir(configuration.sites_dir+"/"+site.name)
|
syscall.Chdir(configuration.sites_dir + "/" + site.name)
|
||||||
|
|
||||||
|
cmd := exec.Command("lapis", "term")
|
||||||
|
if err := cmd.Start(); err != nil {
|
||||||
|
return errors.New(fmt.Sprintf("Failed to stop Lapis server in '%s': %s", site.getName(), err))
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
cmd.Wait()
|
||||||
|
}()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restart attempts to restart the Lapis server in the given repository.
|
||||||
|
func (site *Site) Restart() error {
|
||||||
|
log.Printf("Restarting Lapis server in %s...", site.getName())
|
||||||
|
|
||||||
|
old_cwd, _ := os.Getwd()
|
||||||
|
defer syscall.Chdir(old_cwd)
|
||||||
|
syscall.Chdir(configuration.sites_dir + "/" + site.name)
|
||||||
|
|
||||||
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 errors.New(fmt.Sprintf("Failed to run 'lapis build' in '%s': %s", site.getName(), err))
|
||||||
fmt.Sprintf("Failed to run 'lapis build' in repository '%s'", site.name), 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 errors.New(fmt.Sprintf("Failed to restart Lapis server! (Lapis not running?): %s", string(out)))
|
||||||
"'lapis build' command returned unexpected value! (Lapis server not running?)", string(out))
|
|
||||||
}
|
}
|
||||||
return DeployError{}
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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() error {
|
||||||
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 errors.New(fmt.Sprintf("Failed to open git repository '%s': %s", site.name, err))
|
||||||
fmt.Sprintf("Failed to open git repository '%s'", site.name), fmt.Sprint(err))
|
|
||||||
}
|
}
|
||||||
if err = repo.Pull(); err != nil {
|
if err = repo.Pull(); err != nil {
|
||||||
return newDeployError(1, "updateSite",
|
return errors.New(fmt.Sprintf("Failed to pull down git repository '%s': %s", site.getName(), err))
|
||||||
fmt.Sprintf("Failed to pull down git repository '%s'", site.name), fmt.Sprint(err))
|
|
||||||
}
|
}
|
||||||
return DeployError{}
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CloneSite will clone a site and put it in the configured 'sites_dir'
|
// CloneSite will clone a site and put it in the configured 'sites_dir'
|
||||||
func CloneSite(url string, name string) DeployError {
|
func CloneSite(url string, name string) error {
|
||||||
log.Printf("Cloning repository %s...", name)
|
log.Printf("Cloning repository %s...", name)
|
||||||
if err := git.Clone(url, configuration.sites_dir+"/"+name); err != nil {
|
if err := git.Clone(url, configuration.sites_dir+"/"+name); err != nil {
|
||||||
return newDeployError(1, "cloneSite",
|
return errors.New(fmt.Sprintf("Failed to pull down repository '%s': %s", name, err))
|
||||||
fmt.Sprintf("Failed to pull down repository '%s'", name), fmt.Sprint(err))
|
|
||||||
}
|
}
|
||||||
return DeployError{}
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user