Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
e72171f1ef | |||
85ab2a963b | |||
25512974d2 | |||
9dc85805e3 |
13
config.go
13
config.go
@ -2,20 +2,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Configuration stores information retrieved from a configuration file
|
||||
type Configuration struct {
|
||||
port int
|
||||
port int
|
||||
sites_dir string
|
||||
matrix struct {
|
||||
matrix struct {
|
||||
homeserver string
|
||||
username string
|
||||
password string
|
||||
room_id string
|
||||
username string
|
||||
password string
|
||||
room_id string
|
||||
}
|
||||
}
|
||||
|
||||
@ -39,4 +39,3 @@ func parseConfig(path string, config *Configuration) {
|
||||
config.matrix.password = matrix["password"].(string)
|
||||
config.matrix.room_id = matrix["room_id"].(string)
|
||||
}
|
||||
|
||||
|
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"
|
||||
}
|
||||
}
|
3
main.go
3
main.go
@ -58,7 +58,8 @@ func handler(data map[string]interface{}) {
|
||||
}
|
||||
sendMessage(MatrixMessage{text: "⚪️ Starting server..."})
|
||||
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()
|
||||
}
|
||||
if deploy_error := site.Start(); deploy_error.code != 0 {
|
||||
|
68
matrix.go
68
matrix.go
@ -3,15 +3,15 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
"strings"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"maunium.net/go/mautrix"
|
||||
"maunium.net/go/mautrix/event"
|
||||
"maunium.net/go/mautrix/id"
|
||||
"maunium.net/go/mautrix/format"
|
||||
"maunium.net/go/mautrix/id"
|
||||
)
|
||||
|
||||
// MatrixMessage stores information about a matrix message.
|
||||
@ -19,10 +19,13 @@ type MatrixMessage struct {
|
||||
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
|
||||
type MatrixCommand struct {
|
||||
cmd string
|
||||
callback func(ctx context.Context, evt *event.Event) string
|
||||
cmd string
|
||||
callback MatrixCommandCallback
|
||||
description string
|
||||
}
|
||||
|
||||
@ -38,10 +41,10 @@ func sendMessage(msg MatrixMessage) {
|
||||
}
|
||||
|
||||
// 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{
|
||||
cmd: cmd,
|
||||
callback: callback,
|
||||
cmd: cmd,
|
||||
callback: callback,
|
||||
description: description,
|
||||
})
|
||||
}
|
||||
@ -53,9 +56,9 @@ func initMatrix() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
resp, err := client.Login(context.Background(), &mautrix.ReqLogin{
|
||||
Type: "m.login.password",
|
||||
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: configuration.matrix.username},
|
||||
Password: configuration.matrix.password,
|
||||
Type: "m.login.password",
|
||||
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: configuration.matrix.username},
|
||||
Password: configuration.matrix.password,
|
||||
StoreCredentials: true,
|
||||
})
|
||||
if err != nil {
|
||||
@ -81,14 +84,16 @@ func initMatrix() {
|
||||
})
|
||||
syncer.OnEventType(event.EventMessage, func(ctx context.Context, evt *event.Event) {
|
||||
if evt.RoomID.String() == configuration.matrix.room_id {
|
||||
prefixes := []string{"!", configuration.matrix.username+" "}
|
||||
for _,prefix := range prefixes {
|
||||
prefixes := []string{"!", configuration.matrix.username + " "}
|
||||
for _, prefix := range prefixes {
|
||||
if strings.HasPrefix(evt.Content.AsMessage().Body, prefix) {
|
||||
msg := evt.Content.AsMessage().Body
|
||||
cmd := strings.TrimPrefix(strings.Split(msg, " ")[0], prefix)
|
||||
found_command := false
|
||||
for _, command := range Chatcommands {
|
||||
if command.cmd == strings.TrimPrefix(evt.Content.AsMessage().Body, prefix) {
|
||||
if command.cmd == cmd {
|
||||
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.SetReply(evt)
|
||||
client.SendMessageEvent(ctx, evt.RoomID, event.EventMessage, content)
|
||||
@ -103,30 +108,49 @@ 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 := ""
|
||||
for _,cmd := range Chatcommands {
|
||||
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 {
|
||||
registerChatCommand("sites", "Display all available sites", func(ctx context.Context, evt *event.Event, args []string) string {
|
||||
text := "⚪️ Getting sites...\n"
|
||||
sites, err := getAllSites()
|
||||
if err.code != 0 {
|
||||
return text+"🔴 Failed to get sites!"
|
||||
return text + "🔴 Failed to get sites!"
|
||||
}
|
||||
for _, site := range sites {
|
||||
text = fmt.Sprintf("%s- %s\n", text, site.name)
|
||||
}
|
||||
if len(sites) == 0 {
|
||||
text = text+"*No sites found*"
|
||||
text = text + "*No sites found*"
|
||||
}
|
||||
return text
|
||||
})
|
||||
|
||||
go func () {
|
||||
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."
|
||||
})
|
||||
|
||||
go func() {
|
||||
for range time.Tick(time.Second * 6) {
|
||||
log.Printf("Scanning for queued messages...")
|
||||
for _, msg := range MatrixOut {
|
||||
|
70
site.go
70
site.go
@ -2,6 +2,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
@ -15,23 +16,64 @@ import (
|
||||
|
||||
// Site contains information and methods used for management of a Lapis site.
|
||||
type Site struct {
|
||||
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.
|
||||
func getSite(name string) (Site, bool, error) {
|
||||
site_path := configuration.sites_dir+"/"+name
|
||||
exists, err := fileExists(site_path);
|
||||
site_path := configuration.sites_dir + "/" + name
|
||||
exists, err := fileExists(site_path)
|
||||
if err != nil {
|
||||
return Site{}, false, errors.New(fmt.Sprintf("Failed to get site '%s'", name))
|
||||
}
|
||||
if exists {
|
||||
return Site{name: name}, true, nil
|
||||
site := Site{name: name}
|
||||
site.getConfig() // Get per-site config
|
||||
return site, true, nil
|
||||
} else {
|
||||
return Site{}, false, nil
|
||||
}
|
||||
}
|
||||
|
||||
// getConfig parses a site's configuration file
|
||||
func (site *Site) getConfig() (bool, error) {
|
||||
path := fmt.Sprintf("%s/%s/deploy_config.json", configuration.sites_dir, site.name)
|
||||
if exists, _ := fileExists(path); !exists {
|
||||
return false, nil
|
||||
}
|
||||
file, _ := os.Open(path)
|
||||
var data map[string]interface{}
|
||||
err := json.NewDecoder(file).Decode(&data)
|
||||
if err != nil {
|
||||
log.Panic("Failed to parse config:", err)
|
||||
}
|
||||
file.Close()
|
||||
// Parse the config
|
||||
var config SiteConfig
|
||||
config.name = data["name"].(string)
|
||||
config.port = int64(data["port"].(float64))
|
||||
|
||||
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.
|
||||
func getAllSites() ([]Site, DeployError) {
|
||||
sites_dir := configuration.sites_dir
|
||||
@ -71,16 +113,16 @@ func startAllSites() DeployError {
|
||||
|
||||
// Start attempts to start a Lapis server in the given repository.
|
||||
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()
|
||||
defer syscall.Chdir(old_cwd)
|
||||
syscall.Chdir(configuration.sites_dir+"/"+site.name)
|
||||
syscall.Chdir(configuration.sites_dir + "/" + site.name)
|
||||
|
||||
cmd := exec.Command("lapis", "serve")
|
||||
if err := cmd.Start(); err != nil {
|
||||
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() {
|
||||
cmd.Wait()
|
||||
@ -91,36 +133,36 @@ func (site *Site) Start() DeployError {
|
||||
|
||||
// Restart attempts to restart the Lapis server in the given repository.
|
||||
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()
|
||||
defer syscall.Chdir(old_cwd)
|
||||
syscall.Chdir(configuration.sites_dir+"/"+site.name)
|
||||
syscall.Chdir(configuration.sites_dir + "/" + site.name)
|
||||
|
||||
out, err := exec.Command("lapis", "build").CombinedOutput()
|
||||
if err != nil {
|
||||
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)
|
||||
if !strings.Contains(string(out), "HUP") {
|
||||
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{}
|
||||
}
|
||||
|
||||
// Update attempts to pull or clone a repository using the given name and url
|
||||
func (site *Site) Update() DeployError {
|
||||
log.Printf("Pulling down repository %s...", site.name)
|
||||
repo,err := git.Open(configuration.sites_dir+"/"+site.name)
|
||||
func (site *Site) Update() DeployError {
|
||||
log.Printf("Pulling down repository %s...", site.getName())
|
||||
repo, err := git.Open(configuration.sites_dir + "/" + site.name)
|
||||
if err != nil {
|
||||
return newDeployError(1, "updateSite",
|
||||
fmt.Sprintf("Failed to open git repository '%s'", site.name), fmt.Sprint(err))
|
||||
}
|
||||
if err = repo.Pull(); err != nil {
|
||||
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{}
|
||||
}
|
||||
|
Reference in New Issue
Block a user