2024-03-07 15:11:25 -06:00
|
|
|
package main
|
|
|
|
|
2024-03-07 16:43:06 -06:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-03-07 22:37:09 -06:00
|
|
|
"fmt"
|
2024-03-07 16:43:06 -06:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-03-07 22:37:09 -06:00
|
|
|
"os"
|
2024-03-08 14:51:58 -06:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
2024-03-07 22:37:09 -06:00
|
|
|
"github.com/gogs/git-module"
|
2024-03-07 16:43:06 -06:00
|
|
|
)
|
2024-03-07 15:11:25 -06:00
|
|
|
|
2024-03-08 15:16:33 -06:00
|
|
|
type Configuration struct {
|
|
|
|
sites_dir string
|
|
|
|
}
|
|
|
|
var configuration = Configuration{}
|
2024-03-08 14:51:58 -06:00
|
|
|
|
2024-03-07 22:37:09 -06:00
|
|
|
type DeployError struct {
|
|
|
|
code int
|
|
|
|
where string
|
|
|
|
details string
|
2024-03-08 14:51:58 -06:00
|
|
|
command_output string
|
2024-03-07 22:37:09 -06:00
|
|
|
}
|
2024-03-08 14:51:58 -06:00
|
|
|
func newDeployError(code int, where string, details string, command_output string) DeployError {
|
2024-03-07 22:37:09 -06:00
|
|
|
return DeployError{ code: code, where: where, details: details }
|
|
|
|
}
|
|
|
|
|
|
|
|
// fileExists returns whether the given file or directory exists.
|
|
|
|
func fileExists(path string) (bool, error) {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err == nil { return true, nil }
|
|
|
|
if os.IsNotExist(err) { return false, nil }
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// startServer attempts to start a Lapis server in the given repository.
|
2024-03-08 14:51:58 -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)
|
2024-03-08 15:16:33 -06:00
|
|
|
syscall.Chdir(configuration.sites_dir+"/"+name)
|
2024-03-08 14:51:58 -06:00
|
|
|
|
|
|
|
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") {
|
|
|
|
return newDeployError(1, "restartServer", "'lapis build' command returned unexpected value!", string(out))
|
|
|
|
}
|
2024-03-07 22:37:09 -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 {
|
2024-03-08 15:16:33 -06:00
|
|
|
exists, err := fileExists(configuration.sites_dir+"/"+name)
|
2024-03-07 22:37:09 -06:00
|
|
|
if err != nil {
|
|
|
|
return newDeployError(1, "fileExists",
|
2024-03-08 14:51:58 -06:00
|
|
|
fmt.Sprintf("Failed to check whether folder '%s' exists while pulling down repository '%s'!", name, name), "")
|
2024-03-07 22:37:09 -06:00
|
|
|
}
|
2024-03-08 14:51:58 -06:00
|
|
|
if !exists {
|
|
|
|
log.Printf("Cloning repository %s...", name)
|
2024-03-08 15:16:33 -06:00
|
|
|
if err = git.Clone(url, configuration.sites_dir+"/"+name); err != nil {
|
2024-03-08 14:51:58 -06:00
|
|
|
return newDeployError(1, "pullRepo", fmt.Sprintf("Failed to pull down repository '%s'", name), "")
|
2024-03-07 22:37:09 -06:00
|
|
|
}
|
|
|
|
} else {
|
2024-03-08 14:51:58 -06:00
|
|
|
log.Printf("Pulling down repository %s...", name)
|
2024-03-08 15:16:33 -06:00
|
|
|
repo,err := git.Open(configuration.sites_dir+"/"+name)
|
2024-03-07 22:37:09 -06:00
|
|
|
if err != nil {
|
2024-03-08 14:51:58 -06:00
|
|
|
return newDeployError(1, "pullRepo", fmt.Sprintf("Failed to open git repository '%s'", name), "")
|
2024-03-07 22:37:09 -06:00
|
|
|
}
|
|
|
|
repo.Pull()
|
|
|
|
}
|
|
|
|
return DeployError{}
|
|
|
|
}
|
|
|
|
|
2024-03-08 15:16:33 -06:00
|
|
|
// parseConfig parses the JSON configuration file at 'path' and stores the contents in 'config'
|
|
|
|
func parseConfig(path string, config *Configuration) {
|
|
|
|
file, _ := os.Open(path)
|
|
|
|
var data map[string]interface{}
|
|
|
|
err := json.NewDecoder(file).Decode(&data)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic("Failed to parse config:", err)
|
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
config.sites_dir = data["sites_dir"].(string)
|
|
|
|
}
|
|
|
|
|
2024-03-07 22:37:09 -06:00
|
|
|
func handler(data map[string]interface{}) {
|
|
|
|
repository := data["repository"].(map[string]interface{})
|
|
|
|
log.Default().Printf("Repo: %s", repository["full_name"])
|
|
|
|
|
|
|
|
deploy_error := pullRepo(repository["ssh_url"].(string), repository["name"].(string))
|
|
|
|
if deploy_error.code != 0 {
|
|
|
|
log.Default().Printf("Error in %s! [Code: %d] (%s)", deploy_error.where, deploy_error.code, deploy_error.details)
|
|
|
|
}
|
2024-03-08 14:51:58 -06:00
|
|
|
deploy_error = restartServer(repository["name"].(string))
|
|
|
|
if deploy_error.code != 0 {
|
|
|
|
log.Printf("Error in %s! [Code: %d] (%s) {Command output: '%s'}",
|
|
|
|
deploy_error.where, deploy_error.code, deploy_error.details, deploy_error.command_output)
|
|
|
|
}
|
2024-03-07 22:37:09 -06:00
|
|
|
}
|
|
|
|
|
2024-03-07 15:11:25 -06:00
|
|
|
func main() {
|
2024-03-08 15:16:33 -06:00
|
|
|
parseConfig("config.json", &configuration)
|
2024-03-07 16:43:06 -06:00
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){
|
|
|
|
var data map[string]interface{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&data)
|
|
|
|
if err != nil {
|
2024-03-07 22:37:09 -06:00
|
|
|
log.Panic(err)
|
2024-03-07 16:43:06 -06:00
|
|
|
return
|
|
|
|
}
|
2024-03-07 22:37:09 -06:00
|
|
|
|
|
|
|
go handler(data)
|
|
|
|
fmt.Fprint(w, "Received!")
|
2024-03-07 16:43:06 -06:00
|
|
|
})
|
2024-03-07 22:37:09 -06:00
|
|
|
log.Fatal(http.ListenAndServe(":7575", nil))
|
2024-03-07 15:11:25 -06:00
|
|
|
}
|