LapisDeploy/site.go

213 lines
6.1 KiB
Go
Raw Permalink Normal View History

// package main is the main package for the LapisDeploy program
package main
import (
"errors"
"fmt"
"log"
"os"
"os/exec"
"strings"
"syscall"
"github.com/gogs/git-module"
lua "github.com/yuin/gopher-lua"
)
// 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
git struct {
branch 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)
if err != nil {
return Site{}, false, errors.New(fmt.Sprintf("Failed to get site '%s'", name))
}
if exists {
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.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.
func getAllSites() ([]Site, error) {
sites_dir := configuration.sites_dir
files, err := os.ReadDir(sites_dir)
var sites []Site
if err != nil {
return sites, errors.New(fmt.Sprintf("Failed to read sites: %s", err))
}
for _, file := range files {
if !file.IsDir() {
continue
}
site, exists, err := getSite(file.Name())
if err != nil || !exists {
return sites, errors.New(fmt.Sprintf("Failed to read sites: %s", err))
}
sites = append(sites, site)
2024-03-10 22:19:25 -05:00
}
return sites, nil
2024-03-10 22:19:25 -05:00
}
// startAllSites attempts to start every site
func startAllSites() error {
2024-03-10 22:19:25 -05:00
sites, err := getAllSites()
if err != nil {
2024-03-10 22:19:25 -05:00
return err
}
for _, site := range sites {
err = site.Start()
if err != nil {
return err
}
}
return nil
}
// Start attempts to start a Lapis server in the given repository.
func (site *Site) Start() error {
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)
cmd := exec.Command("lapis", "serve")
if err := cmd.Start(); err != nil {
return errors.New(fmt.Sprintf("Failed to start Lapis server in '%s': %s", site.getName(), err))
}
go func() {
cmd.Wait()
}()
return nil
}
2024-03-27 11:57:07 -05:00
// Stop attempts to stop a Lapis server in the given repository.
func (site *Site) Stop() error {
2024-03-27 11:57:07 -05:00
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 errors.New(fmt.Sprintf("Failed to stop Lapis server in '%s': %s", site.getName(), err))
2024-03-27 11:57:07 -05:00
}
go func() {
cmd.Wait()
}()
return nil
2024-03-27 11:57:07 -05:00
}
// 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()
if err != nil {
return errors.New(fmt.Sprintf("Failed to run 'lapis build' in '%s': %s", site.getName(), err))
}
log.Printf("[lapis build] %s", out)
if !strings.Contains(string(out), "HUP") {
return errors.New(fmt.Sprintf("Failed to restart Lapis server! (Lapis not running?): %s", string(out)))
}
return nil
}
// Update attempts to pull or clone a repository using the given name and url
func (site *Site) Update() error {
log.Printf("Pulling down repository %s...", site.getName())
repo, err := git.Open(configuration.sites_dir + "/" + site.name)
if err != nil {
return errors.New(fmt.Sprintf("Failed to open git repository '%s': %s", site.name, err))
}
if err = repo.Pull(); err != nil {
return errors.New(fmt.Sprintf("Failed to pull down git repository '%s': %s", site.getName(), err))
}
return nil
}
// CloneSite will clone a site and put it in the configured 'sites_dir'
func CloneSite(url string, name string) error {
log.Printf("Cloning repository %s...", name)
if err := git.Clone(url, configuration.sites_dir+"/"+name); err != nil {
return errors.New(fmt.Sprintf("Failed to pull down repository '%s': %s", name, err))
}
return nil
}