package main import ( "encoding/json" "fmt" "log" "net/http" "os" "os/exec" "strings" "syscall" "github.com/gogs/git-module" ) const sites_dir = "./sites" type DeployError struct { code int where string details string command_output string } func newDeployError(code int, where string, details string, command_output string) DeployError { 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. 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(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") { return newDeployError(1, "restartServer", "'lapis build' command returned unexpected value!", 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(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, sites_dir+"/"+name); err != nil { return newDeployError(1, "pullRepo", fmt.Sprintf("Failed to pull down repository '%s'", name), "") } } else { log.Printf("Pulling down repository %s...", name) repo,err := git.Open(sites_dir+"/"+name) if err != nil { return newDeployError(1, "pullRepo", fmt.Sprintf("Failed to open git repository '%s'", name), "") } repo.Pull() } return DeployError{} } 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) } 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) } } func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){ var data map[string]interface{} err := json.NewDecoder(r.Body).Decode(&data) if err != nil { log.Panic(err) return } go handler(data) fmt.Fprint(w, "Received!") }) log.Fatal(http.ListenAndServe(":7575", nil)) }