2024-03-08 15:36:11 -06:00
|
|
|
// 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"
|
|
|
|
)
|
|
|
|
|
2024-03-10 14:06:10 -05:00
|
|
|
// startAllServers attempts to start every server in 'sites_dir'
|
|
|
|
func startAllServers(sites_dir string) DeployError {
|
|
|
|
files, err := os.ReadDir(sites_dir)
|
|
|
|
if err != nil {
|
|
|
|
return newDeployError(1, "startAllServers",
|
|
|
|
fmt.Sprintf("Failed to read sites from '%s'", sites_dir), fmt.Sprint(err))
|
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
if !file.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err := startServer(file.Name())
|
|
|
|
if err.code != 0 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return DeployError{}
|
|
|
|
}
|
|
|
|
|
2024-03-08 15:36:11 -06:00
|
|
|
// startServer attempts to start a Lapis server in the given repository.
|
2024-03-10 14:06:10 -05:00
|
|
|
func startServer(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, "startServer", fmt.Sprintf("Failed to start Lapis server in repository '%s'", name), "")
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
cmd.Wait()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return DeployError{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// restartServer attempts to restart the Lapis server in the given repository.
|
2024-03-08 15:36:11 -06:00
|
|
|
func restartServer(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, "restartServer", 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") {
|
2024-03-10 14:06:10 -05:00
|
|
|
return newDeployError(1, "restartServer", "'lapis build' command returned unexpected value! (Lapis server not running?)", string(out))
|
2024-03-08 15:36:11 -06:00
|
|
|
}
|
|
|
|
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), "")
|
|
|
|
}
|
2024-03-10 14:06:10 -05:00
|
|
|
if err := startServer(name); err.code != 0 {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-08 15:36:11 -06:00
|
|
|
} 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{}
|
|
|
|
}
|