2024-03-08 15:36:11 -06:00
|
|
|
// package main is the main package for the LapisDeploy program
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-03-11 17:19:10 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2024-03-08 15:36:11 -06:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/gogs/git-module"
|
2024-03-27 11:44:09 -05:00
|
|
|
lua "github.com/yuin/gopher-lua"
|
2024-03-08 15:36:11 -06:00
|
|
|
)
|
|
|
|
|
2024-03-11 17:19:10 -05:00
|
|
|
// Site contains information and methods used for management of a Lapis site.
|
|
|
|
type Site struct {
|
2024-03-15 17:51:45 -05:00
|
|
|
name string
|
|
|
|
config SiteConfig
|
|
|
|
}
|
|
|
|
|
2024-03-27 11:44:09 -05:00
|
|
|
// SiteConfig contains parsed data from a site's configuration file.
|
2024-03-15 17:51:45 -05:00
|
|
|
type SiteConfig struct {
|
|
|
|
port int64
|
2024-03-11 17:19:10 -05:00
|
|
|
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) {
|
2024-03-15 17:51:45 -05:00
|
|
|
site_path := configuration.sites_dir + "/" + name
|
|
|
|
exists, err := fileExists(site_path)
|
2024-03-11 17:19:10 -05:00
|
|
|
if err != nil {
|
|
|
|
return Site{}, false, errors.New(fmt.Sprintf("Failed to get site '%s'", name))
|
|
|
|
}
|
|
|
|
if exists {
|
2024-03-15 17:51:45 -05:00
|
|
|
site := Site{name: name}
|
|
|
|
site.getConfig() // Get per-site config
|
|
|
|
return site, true, nil
|
2024-03-11 17:19:10 -05:00
|
|
|
} else {
|
|
|
|
return Site{}, false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-27 11:44:09 -05:00
|
|
|
// getConfig parses a site's configuration file.
|
2024-03-15 17:51:45 -05:00
|
|
|
func (site *Site) getConfig() (bool, error) {
|
2024-03-27 11:44:09 -05:00
|
|
|
path := fmt.Sprintf("%s/%s/deploy_config.lua", configuration.sites_dir, site.name)
|
|
|
|
// Make sure config file exists
|
2024-03-15 17:51:45 -05:00
|
|
|
if exists, _ := fileExists(path); !exists {
|
|
|
|
return false, nil
|
|
|
|
}
|
2024-03-27 11:44:09 -05:00
|
|
|
// 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
|
|
|
|
|
2024-03-15 17:51:45 -05:00
|
|
|
// Parse the config
|
|
|
|
var config SiteConfig
|
2024-03-27 11:44:09 -05:00
|
|
|
log.Printf("Loading per-site config %s", path)
|
|
|
|
|
|
|
|
getStringFromTable(L, table, "name", &config.name)
|
|
|
|
getIntFromTable(L, table, "port", &config.port)
|
2024-03-15 17:51:45 -05:00
|
|
|
|
|
|
|
site.config = config
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2024-03-27 11:44:09 -05:00
|
|
|
// getName gets a website's configured name (falls back to technical name if one isn't configured).
|
2024-03-15 17:51:45 -05:00
|
|
|
func (site *Site) getName() string {
|
|
|
|
if site.config.name == "" {
|
|
|
|
return site.name
|
|
|
|
} else {
|
|
|
|
return site.config.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-11 17:19:10 -05:00
|
|
|
// getAllSites gets every site installed.
|
|
|
|
func getAllSites() ([]Site, DeployError) {
|
2024-03-10 22:10:00 -05:00
|
|
|
sites_dir := configuration.sites_dir
|
2024-03-10 14:06:10 -05:00
|
|
|
files, err := os.ReadDir(sites_dir)
|
2024-03-11 17:19:10 -05:00
|
|
|
var sites []Site
|
2024-03-10 14:06:10 -05:00
|
|
|
if err != nil {
|
2024-03-11 17:19:10 -05:00
|
|
|
return sites, newDeployError(1, "startAllSites",
|
|
|
|
"Failed to read sites", fmt.Sprint(err))
|
2024-03-10 14:06:10 -05:00
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
if !file.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
2024-03-11 17:19:10 -05:00
|
|
|
site, exists, err := getSite(file.Name())
|
|
|
|
if err != nil || !exists {
|
|
|
|
return sites, newDeployError(1, "getAllSites", "Failed to read sites", fmt.Sprint(err))
|
|
|
|
}
|
|
|
|
sites = append(sites, site)
|
2024-03-10 22:19:25 -05:00
|
|
|
}
|
|
|
|
return sites, DeployError{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// startAllSites attempts to start every site
|
|
|
|
func startAllSites() DeployError {
|
|
|
|
sites, err := getAllSites()
|
|
|
|
if err.code != 0 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, site := range sites {
|
2024-03-11 17:19:10 -05:00
|
|
|
err = site.Start()
|
2024-03-10 14:06:10 -05:00
|
|
|
if err.code != 0 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return DeployError{}
|
|
|
|
}
|
|
|
|
|
2024-03-11 17:19:10 -05:00
|
|
|
// Start attempts to start a Lapis server in the given repository.
|
|
|
|
func (site *Site) Start() DeployError {
|
2024-03-15 17:51:45 -05:00
|
|
|
log.Printf("Starting Lapis server in %s...", site.getName())
|
2024-03-10 14:06:10 -05:00
|
|
|
|
|
|
|
old_cwd, _ := os.Getwd()
|
|
|
|
defer syscall.Chdir(old_cwd)
|
2024-03-15 17:51:45 -05:00
|
|
|
syscall.Chdir(configuration.sites_dir + "/" + site.name)
|
2024-03-10 14:06:10 -05:00
|
|
|
|
|
|
|
cmd := exec.Command("lapis", "serve")
|
|
|
|
if err := cmd.Start(); err != nil {
|
2024-03-11 17:19:10 -05:00
|
|
|
return newDeployError(1, "startSite",
|
2024-03-15 17:51:45 -05:00
|
|
|
fmt.Sprintf("Failed to start Lapis server in '%s'", site.getName()), "")
|
2024-03-10 14:06:10 -05:00
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
cmd.Wait()
|
|
|
|
}()
|
2024-03-15 17:51:45 -05:00
|
|
|
|
2024-03-10 14:06:10 -05:00
|
|
|
return DeployError{}
|
|
|
|
}
|
|
|
|
|
2024-03-27 11:57:07 -05:00
|
|
|
// 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{}
|
|
|
|
}
|
|
|
|
|
2024-03-11 17:19:10 -05:00
|
|
|
// Restart attempts to restart the Lapis server in the given repository.
|
|
|
|
func (site *Site) Restart() DeployError {
|
2024-03-15 17:51:45 -05:00
|
|
|
log.Printf("Restarting Lapis server in %s...", site.getName())
|
2024-03-08 15:36:11 -06:00
|
|
|
|
|
|
|
old_cwd, _ := os.Getwd()
|
|
|
|
defer syscall.Chdir(old_cwd)
|
2024-03-15 17:51:45 -05:00
|
|
|
syscall.Chdir(configuration.sites_dir + "/" + site.name)
|
2024-03-08 15:36:11 -06:00
|
|
|
|
|
|
|
out, err := exec.Command("lapis", "build").CombinedOutput()
|
|
|
|
if err != nil {
|
2024-03-11 17:19:10 -05:00
|
|
|
return newDeployError(1, "restartSite",
|
2024-03-15 17:51:45 -05:00
|
|
|
fmt.Sprintf("Failed to run 'lapis build' in '%s'", site.getName()), string(out))
|
2024-03-08 15:36:11 -06:00
|
|
|
}
|
|
|
|
log.Printf("[lapis build] %s", out)
|
|
|
|
if !strings.Contains(string(out), "HUP") {
|
2024-03-11 17:19:10 -05:00
|
|
|
return newDeployError(1, "restartSite",
|
2024-03-15 17:51:45 -05:00
|
|
|
"Failed to restart Lapis server! (Lapis not running?)", string(out))
|
2024-03-08 15:36:11 -06:00
|
|
|
}
|
|
|
|
return DeployError{}
|
|
|
|
}
|
|
|
|
|
2024-03-11 17:19:10 -05:00
|
|
|
// Update attempts to pull or clone a repository using the given name and url
|
2024-03-15 17:51:45 -05:00
|
|
|
func (site *Site) Update() DeployError {
|
|
|
|
log.Printf("Pulling down repository %s...", site.getName())
|
|
|
|
repo, err := git.Open(configuration.sites_dir + "/" + site.name)
|
2024-03-08 15:36:11 -06:00
|
|
|
if err != nil {
|
2024-03-11 17:19:10 -05:00
|
|
|
return newDeployError(1, "updateSite",
|
|
|
|
fmt.Sprintf("Failed to open git repository '%s'", site.name), fmt.Sprint(err))
|
2024-03-08 15:36:11 -06:00
|
|
|
}
|
2024-03-10 23:13:43 -05:00
|
|
|
if err = repo.Pull(); err != nil {
|
2024-03-11 17:19:10 -05:00
|
|
|
return newDeployError(1, "updateSite",
|
2024-03-15 17:51:45 -05:00
|
|
|
fmt.Sprintf("Failed to pull down git repository '%s'", site.getName()), fmt.Sprint(err))
|
2024-03-10 23:13:43 -05:00
|
|
|
}
|
|
|
|
return DeployError{}
|
|
|
|
}
|
|
|
|
|
2024-03-11 17:19:10 -05:00
|
|
|
// CloneSite will clone a site and put it in the configured 'sites_dir'
|
|
|
|
func CloneSite(url string, name string) DeployError {
|
2024-03-10 23:13:43 -05:00
|
|
|
log.Printf("Cloning repository %s...", name)
|
|
|
|
if err := git.Clone(url, configuration.sites_dir+"/"+name); err != nil {
|
2024-03-11 17:19:10 -05:00
|
|
|
return newDeployError(1, "cloneSite",
|
|
|
|
fmt.Sprintf("Failed to pull down repository '%s'", name), fmt.Sprint(err))
|
2024-03-08 15:36:11 -06:00
|
|
|
}
|
|
|
|
return DeployError{}
|
|
|
|
}
|