Add Site structure (and convert helper functions to methods) (fix #17)
This commit is contained in:
parent
fef65ea991
commit
818e95c33f
17
main.go
17
main.go
@ -29,22 +29,27 @@ func handler(data map[string]interface{}) {
|
||||
|
||||
repo_name := repository["name"].(string)
|
||||
|
||||
exists, err := fileExists(configuration.sites_dir+"/"+repo_name)
|
||||
site, exists, err := getSite(repo_name)
|
||||
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 deploy_error := updateSite(repo_name); deploy_error.code != 0 {
|
||||
if deploy_error := site.Update(); deploy_error.code != 0 {
|
||||
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
|
||||
}
|
||||
} 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
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -117,7 +117,10 @@ func initMatrix() {
|
||||
return text+"🔴 Failed to get 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
|
||||
})
|
||||
|
89
site.go
89
site.go
@ -2,29 +2,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"syscall"
|
||||
"log"
|
||||
"os"
|
||||
"fmt"
|
||||
|
||||
"github.com/gogs/git-module"
|
||||
)
|
||||
|
||||
// getAllSites gets every site installed
|
||||
func getAllSites() ([]string, DeployError) {
|
||||
// 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 []string
|
||||
var sites []Site
|
||||
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 {
|
||||
if !file.IsDir() {
|
||||
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{}
|
||||
}
|
||||
@ -36,7 +61,7 @@ func startAllSites() DeployError {
|
||||
return err
|
||||
}
|
||||
for _, site := range sites {
|
||||
err := startSite(site)
|
||||
err = site.Start()
|
||||
if err.code != 0 {
|
||||
return err
|
||||
}
|
||||
@ -44,17 +69,18 @@ func startAllSites() DeployError {
|
||||
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)
|
||||
// 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+"/"+name)
|
||||
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'", name), "")
|
||||
return newDeployError(1, "startSite",
|
||||
fmt.Sprintf("Failed to start Lapis server in repository '%s'", site.name), "")
|
||||
}
|
||||
go func() {
|
||||
cmd.Wait()
|
||||
@ -63,43 +89,48 @@ func startSite(name string) DeployError {
|
||||
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)
|
||||
// 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+"/"+name)
|
||||
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'", 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)
|
||||
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{}
|
||||
}
|
||||
|
||||
// updateSite attempts to pull or clone a repository using the given name and url
|
||||
func updateSite(name string) DeployError {
|
||||
log.Printf("Pulling down repository %s...", name)
|
||||
repo,err := git.Open(configuration.sites_dir+"/"+name)
|
||||
// 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'", 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 {
|
||||
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{}
|
||||
}
|
||||
|
||||
// cloneSite
|
||||
func cloneSite(url string, name string) 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 newDeployError(1, "cloneSite",
|
||||
fmt.Sprintf("Failed to pull down repository '%s'", name), fmt.Sprint(err))
|
||||
}
|
||||
return DeployError{}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user