LapisDeploy/site.go

137 lines
3.8 KiB
Go

// 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"
)
// Site contains information and methods used for management of a Lapis site.
type Site struct {
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);
if err != nil {
return Site{}, false, errors.New(fmt.Sprintf("Failed to get site '%s'", name))
}
if exists {
return Site{name: name}, true, nil
} else {
return Site{}, false, nil
}
}
// getAllSites gets every site installed.
func getAllSites() ([]Site, DeployError) {
sites_dir := configuration.sites_dir
files, err := os.ReadDir(sites_dir)
var sites []Site
if err != nil {
return sites, newDeployError(1, "startAllSites",
"Failed to read sites", fmt.Sprint(err))
}
for _, file := range files {
if !file.IsDir() {
continue
}
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)
}
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 {
err = site.Start()
if err.code != 0 {
return err
}
}
return 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)
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 newDeployError(1, "startSite",
fmt.Sprintf("Failed to start Lapis server in repository '%s'", site.name), "")
}
go func() {
cmd.Wait()
}()
return 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)
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 newDeployError(1, "restartSite",
fmt.Sprintf("Failed to run 'lapis build' in repository '%s'", site.name), 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))
}
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)
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))
}
return DeployError{}
}
// CloneSite will clone a site and put it in the configured 'sites_dir'
func CloneSite(url string, name string) DeployError {
log.Printf("Cloning repository %s...", name)
if err := git.Clone(url, configuration.sites_dir+"/"+name); err != nil {
return newDeployError(1, "cloneSite",
fmt.Sprintf("Failed to pull down repository '%s'", name), fmt.Sprint(err))
}
return DeployError{}
}