Restart lapis server on receiving a webhook (fix #2)
This commit is contained in:
parent
a9e390bba4
commit
afcbcdd1a7
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
lapisdeploy
|
lapisdeploy
|
||||||
|
sites/
|
||||||
|
47
main.go
47
main.go
@ -6,15 +6,22 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"github.com/gogs/git-module"
|
"github.com/gogs/git-module"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const sites_dir = "./sites"
|
||||||
|
|
||||||
type DeployError struct {
|
type DeployError struct {
|
||||||
code int
|
code int
|
||||||
where string
|
where string
|
||||||
details string
|
details string
|
||||||
|
command_output string
|
||||||
}
|
}
|
||||||
func newDeployError(code int, where string, details string) DeployError {
|
func newDeployError(code int, where string, details string, command_output string) DeployError {
|
||||||
return DeployError{ code: code, where: where, details: details }
|
return DeployError{ code: code, where: where, details: details }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,26 +34,41 @@ func fileExists(path string) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// startServer attempts to start a Lapis server in the given repository.
|
// startServer attempts to start a Lapis server in the given repository.
|
||||||
func startServer(name string) DeployError {
|
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{}
|
return DeployError{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// pullRepo attempts to pull or clone a repository using the given name and url
|
// pullRepo attempts to pull or clone a repository using the given name and url
|
||||||
func pullRepo(url string, name string) DeployError {
|
func pullRepo(url string, name string) DeployError {
|
||||||
exists, err := fileExists(name)
|
exists, err := fileExists(sites_dir+"/"+name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return newDeployError(1, "fileExists",
|
return newDeployError(1, "fileExists",
|
||||||
fmt.Sprintf("Failed to check whether folder '%s' exists while pulling down repository '%s'!", name, name))
|
fmt.Sprintf("Failed to check whether folder '%s' exists while pulling down repository '%s'!", name, name), "")
|
||||||
}
|
}
|
||||||
if !exists {
|
if !exists {
|
||||||
if err = git.Clone(url, name); err != nil {
|
log.Printf("Cloning repository %s...", name)
|
||||||
return newDeployError(1, "pullRepo", fmt.Sprintf("Failed to pull down 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 {
|
} else {
|
||||||
repo,err := git.Open(name)
|
log.Printf("Pulling down repository %s...", name)
|
||||||
|
repo,err := git.Open(sites_dir+"/"+name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return newDeployError(1, "pullRepo", fmt.Sprintf("Failed to open git repository '%s'", name))
|
return newDeployError(1, "pullRepo", fmt.Sprintf("Failed to open git repository '%s'", name), "")
|
||||||
}
|
}
|
||||||
repo.Pull()
|
repo.Pull()
|
||||||
}
|
}
|
||||||
@ -56,15 +78,16 @@ func pullRepo(url string, name string) DeployError {
|
|||||||
func handler(data map[string]interface{}) {
|
func handler(data map[string]interface{}) {
|
||||||
repository := data["repository"].(map[string]interface{})
|
repository := data["repository"].(map[string]interface{})
|
||||||
log.Default().Printf("Repo: %s", repository["full_name"])
|
log.Default().Printf("Repo: %s", repository["full_name"])
|
||||||
log.Default().Printf("Ref: %s", data["ref"])
|
|
||||||
|
|
||||||
|
|
||||||
deploy_error := pullRepo(repository["ssh_url"].(string), repository["name"].(string))
|
deploy_error := pullRepo(repository["ssh_url"].(string), repository["name"].(string))
|
||||||
if deploy_error.code != 0 {
|
if deploy_error.code != 0 {
|
||||||
log.Default().Printf("Error in %s! [Code: %d] (%s)", deploy_error.where, deploy_error.code, deploy_error.details)
|
log.Default().Printf("Error in %s! [Code: %d] (%s)", deploy_error.where, deploy_error.code, deploy_error.details)
|
||||||
}
|
}
|
||||||
//syscall.Chdir(repository["name"].(string))
|
deploy_error = restartServer(repository["name"].(string))
|
||||||
//syscall.Exec("lapis", []string{"serve"}, []string{"production"})
|
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() {
|
func main() {
|
||||||
|
Loading…
Reference in New Issue
Block a user