LapisDeploy/site.go

110 lines
3.1 KiB
Go

// package main is the main package for the LapisDeploy program
package main
import (
"os/exec"
"strings"
"syscall"
"log"
"os"
"fmt"
"github.com/gogs/git-module"
)
// getAllSites gets every site installed
func getAllSites() ([]string, DeployError) {
sites_dir := configuration.sites_dir
files, err := os.ReadDir(sites_dir)
var sites []string
if err != nil {
return sites, newDeployError(1, "startAllSites", fmt.Sprintf("Failed to read sites from '%s'", sites_dir), fmt.Sprint(err))
}
for _, file := range files {
if !file.IsDir() {
continue
}
sites = append(sites, file.Name())
}
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 := startSite(site)
if err.code != 0 {
return err
}
}
return DeployError{}
}
// startSite attempts to start a Lapis server in the given repository.
func startSite(name string) DeployError {
log.Printf("Starting Lapis server in repository %s...", name)
old_cwd, _ := os.Getwd()
defer syscall.Chdir(old_cwd)
syscall.Chdir(configuration.sites_dir+"/"+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'", name), "")
}
go func() {
cmd.Wait()
}()
return DeployError{}
}
// restartSite attempts to restart the Lapis server in the given repository.
func restartSite(name string) DeployError {
log.Printf("Restarting Lapis server on repository %s...", name)
old_cwd, _ := os.Getwd()
defer syscall.Chdir(old_cwd)
syscall.Chdir(configuration.sites_dir+"/"+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'", 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{}
}
// pullRepo attempts to pull or clone a repository using the given name and url
func pullRepo(url string, name string) DeployError {
exists, err := fileExists(configuration.sites_dir+"/"+name)
if err != nil {
return newDeployError(1, "fileExists",
fmt.Sprintf("Failed to check whether folder '%s' exists while pulling down repository '%s'!", name, name), "")
}
if !exists {
log.Printf("Cloning repository %s...", name)
if err = git.Clone(url, configuration.sites_dir+"/"+name); err != nil {
return newDeployError(1, "pullRepo", fmt.Sprintf("Failed to pull down repository '%s'", name), "")
}
if err := startSite(name); err.code != 0 {
return err
}
} else {
log.Printf("Pulling down repository %s...", name)
repo,err := git.Open(configuration.sites_dir+"/"+name)
if err != nil {
return newDeployError(1, "pullRepo", fmt.Sprintf("Failed to open git repository '%s'", name), "")
}
repo.Pull()
}
return DeployError{}
}