Add per-site optional configuration (fix #15)

This commit is contained in:
Noah 2024-03-15 17:51:45 -05:00
parent 9dc85805e3
commit 25512974d2
3 changed files with 65 additions and 23 deletions

View File

@ -2,9 +2,9 @@
package main package main
import ( import (
"os"
"encoding/json" "encoding/json"
"log" "log"
"os"
) )
// Configuration stores information retrieved from a configuration file // Configuration stores information retrieved from a configuration file
@ -39,4 +39,3 @@ func parseConfig(path string, config *Configuration) {
config.matrix.password = matrix["password"].(string) config.matrix.password = matrix["password"].(string)
config.matrix.room_id = matrix["room_id"].(string) config.matrix.room_id = matrix["room_id"].(string)
} }

View File

@ -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 {

68
site.go
View File

@ -2,6 +2,7 @@
package main package main
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"log" "log"
@ -16,22 +17,63 @@ import (
// 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.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. // getAllSites gets every site installed.
func getAllSites() ([]Site, DeployError) { func getAllSites() ([]Site, DeployError) {
sites_dir := configuration.sites_dir sites_dir := configuration.sites_dir
@ -71,16 +113,16 @@ 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)
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 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()
@ -91,36 +133,36 @@ func (site *Site) Start() 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)
syscall.Chdir(configuration.sites_dir+"/"+site.name) 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 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",
fmt.Sprintf("Failed to open git repository '%s'", site.name), fmt.Sprint(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 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{}
} }