Add Site structure (and convert helper functions to methods) (fix #17)

This commit is contained in:
Noah 2024-03-11 17:19:10 -05:00
parent fef65ea991
commit 818e95c33f
3 changed files with 75 additions and 36 deletions

17
main.go
View File

@ -29,22 +29,27 @@ func handler(data map[string]interface{}) {
repo_name := repository["name"].(string) repo_name := repository["name"].(string)
exists, err := fileExists(configuration.sites_dir+"/"+repo_name) site, exists, err := getSite(repo_name)
if err != nil { if err != nil {
newDeployError(1, "handler", "Failed to check if site '%s' exists", fmt.Sprint(err)) deploy_error := newDeployError(1, "handler", fmt.Sprintf("Failed to check if site '%s' exists", repo_name), fmt.Sprint(err))
deploy_error.SendOverMatrix(); return
} }
if exists { if exists {
if deploy_error := updateSite(repo_name); deploy_error.code != 0 { if deploy_error := site.Update(); deploy_error.code != 0 {
deploy_error.SendOverMatrix(); return deploy_error.SendOverMatrix(); return
} }
if deploy_error := restartSite(repo_name); deploy_error.code != 0 { if deploy_error := site.Restart(); deploy_error.code != 0 {
deploy_error.SendOverMatrix(); return deploy_error.SendOverMatrix(); return
} }
} else { } else {
if deploy_error := cloneSite(repository["ssh_url"].(string), repo_name); deploy_error.code != 0 { if deploy_error := CloneSite(repository["ssh_url"].(string), repo_name); deploy_error.code != 0 {
deploy_error.SendOverMatrix(); return deploy_error.SendOverMatrix(); return
} }
if deploy_error := startSite(repo_name); deploy_error.code != 0 { if site, exists, err = getSite(repo_name); err != nil {
deploy_error := newDeployError(1, "handler", "Failed to get site '%s' after creation!", fmt.Sprint(err))
deploy_error.SendOverMatrix()
}
if deploy_error := site.Start(); deploy_error.code != 0 {
deploy_error.SendOverMatrix(); return deploy_error.SendOverMatrix(); return
} }
} }

View File

@ -117,7 +117,10 @@ func initMatrix() {
return text+"🔴 Failed to get sites!" return text+"🔴 Failed to get sites!"
} }
for _, site := range sites { for _, site := range sites {
text = fmt.Sprintf("%s- %s\n", text, site) text = fmt.Sprintf("%s- %s\n", text, site.name)
}
if len(sites) == 0 {
text = text+"*No sites found*"
} }
return text return text
}) })

89
site.go
View File

@ -2,29 +2,54 @@
package main package main
import ( import (
"errors"
"fmt"
"log"
"os"
"os/exec" "os/exec"
"strings" "strings"
"syscall" "syscall"
"log"
"os"
"fmt"
"github.com/gogs/git-module" "github.com/gogs/git-module"
) )
// getAllSites gets every site installed // Site contains information and methods used for management of a Lapis site.
func getAllSites() ([]string, DeployError) { 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 sites_dir := configuration.sites_dir
files, err := os.ReadDir(sites_dir) files, err := os.ReadDir(sites_dir)
var sites []string var sites []Site
if err != nil { if err != nil {
return sites, newDeployError(1, "startAllSites", fmt.Sprintf("Failed to read sites from '%s'", sites_dir), fmt.Sprint(err)) return sites, newDeployError(1, "startAllSites",
"Failed to read sites", fmt.Sprint(err))
} }
for _, file := range files { for _, file := range files {
if !file.IsDir() { if !file.IsDir() {
continue continue
} }
sites = append(sites, file.Name()) 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{} return sites, DeployError{}
} }
@ -36,7 +61,7 @@ func startAllSites() DeployError {
return err return err
} }
for _, site := range sites { for _, site := range sites {
err := startSite(site) err = site.Start()
if err.code != 0 { if err.code != 0 {
return err return err
} }
@ -44,17 +69,18 @@ func startAllSites() DeployError {
return DeployError{} return DeployError{}
} }
// startSite attempts to start a Lapis server in the given repository. // Start attempts to start a Lapis server in the given repository.
func startSite(name string) DeployError { func (site *Site) Start() DeployError {
log.Printf("Starting Lapis server in repository %s...", name) log.Printf("Starting Lapis server in repository %s...", site.name)
old_cwd, _ := os.Getwd() old_cwd, _ := os.Getwd()
defer syscall.Chdir(old_cwd) defer syscall.Chdir(old_cwd)
syscall.Chdir(configuration.sites_dir+"/"+name) syscall.Chdir(configuration.sites_dir+"/"+site.name)
cmd := exec.Command("lapis", "serve") cmd := exec.Command("lapis", "serve")
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
return newDeployError(1, "startSite", fmt.Sprintf("Failed to start Lapis server in repository '%s'", name), "") return newDeployError(1, "startSite",
fmt.Sprintf("Failed to start Lapis server in repository '%s'", site.name), "")
} }
go func() { go func() {
cmd.Wait() cmd.Wait()
@ -63,43 +89,48 @@ func startSite(name string) DeployError {
return DeployError{} return DeployError{}
} }
// restartSite attempts to restart the Lapis server in the given repository. // Restart attempts to restart the Lapis server in the given repository.
func restartSite(name string) DeployError { func (site *Site) Restart() DeployError {
log.Printf("Restarting Lapis server on repository %s...", name) log.Printf("Restarting Lapis server on repository %s...", site.name)
old_cwd, _ := os.Getwd() old_cwd, _ := os.Getwd()
defer syscall.Chdir(old_cwd) defer syscall.Chdir(old_cwd)
syscall.Chdir(configuration.sites_dir+"/"+name) syscall.Chdir(configuration.sites_dir+"/"+site.name)
out, err := exec.Command("lapis", "build").CombinedOutput() out, err := exec.Command("lapis", "build").CombinedOutput()
if err != nil { if err != nil {
return newDeployError(1, "restartSite", fmt.Sprintf("Failed to run 'lapis build' in repository '%s'", name), string(out)) return newDeployError(1, "restartSite",
fmt.Sprintf("Failed to run 'lapis build' in repository '%s'", site.name), string(out))
} }
log.Printf("[lapis build] %s", out) log.Printf("[lapis build] %s", out)
if !strings.Contains(string(out), "HUP") { if !strings.Contains(string(out), "HUP") {
return newDeployError(1, "restartSite", "'lapis build' command returned unexpected value! (Lapis server not running?)", string(out)) return newDeployError(1, "restartSite",
"'lapis build' command returned unexpected value! (Lapis server not running?)", string(out))
} }
return DeployError{} return DeployError{}
} }
// updateSite attempts to pull or clone a repository using the given name and url // Update attempts to pull or clone a repository using the given name and url
func updateSite(name string) DeployError { func (site *Site) Update() DeployError {
log.Printf("Pulling down repository %s...", name) log.Printf("Pulling down repository %s...", site.name)
repo,err := git.Open(configuration.sites_dir+"/"+name) repo,err := git.Open(configuration.sites_dir+"/"+site.name)
if err != nil { if err != nil {
return newDeployError(1, "updateSite", fmt.Sprintf("Failed to open git repository '%s'", name), fmt.Sprint(err)) return newDeployError(1, "updateSite",
fmt.Sprintf("Failed to open git repository '%s'", site.name), fmt.Sprint(err))
} }
if err = repo.Pull(); err != nil { if err = repo.Pull(); err != nil {
return newDeployError(1, "updateSite", fmt.Sprintf("Failed to pull down git repository '%s'", name), fmt.Sprint(err)) return newDeployError(1, "updateSite",
fmt.Sprintf("Failed to pull down git repository '%s'", site.name), fmt.Sprint(err))
} }
return DeployError{} return DeployError{}
} }
// cloneSite // CloneSite will clone a site and put it in the configured 'sites_dir'
func cloneSite(url string, name string) DeployError { func CloneSite(url string, name string) DeployError {
log.Printf("Cloning repository %s...", name) log.Printf("Cloning repository %s...", name)
if err := git.Clone(url, configuration.sites_dir+"/"+name); err != nil { 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 newDeployError(1, "cloneSite",
fmt.Sprintf("Failed to pull down repository '%s'", name), fmt.Sprint(err))
} }
return DeployError{} return DeployError{}
} }