// package main is the main package for the LapisDeploy program package main import ( "os/exec" "strings" "syscall" "log" "os" "fmt" "github.com/gogs/git-module" ) // startAllServers attempts to start every server in 'sites_dir' func startAllServers(sites_dir string) DeployError { files, err := os.ReadDir(sites_dir) if err != nil { return newDeployError(1, "startAllServers", fmt.Sprintf("Failed to read sites from '%s'", sites_dir), fmt.Sprint(err)) } for _, file := range files { if !file.IsDir() { continue } err := startServer(file.Name()) if err.code != 0 { return err } } return DeployError{} } // startServer attempts to start a Lapis server in the given repository. func startServer(name string) DeployError { log.Printf("Starting Lapis server in repository %s...", name) old_cwd, _ := os.Getwd() defer syscall.Chdir(old_cwd) syscall.Chdir(configuration.sites_dir+"/"+name) cmd := exec.Command("lapis", "serve") if err := cmd.Start(); err != nil { return newDeployError(1, "startServer", fmt.Sprintf("Failed to start Lapis server in repository '%s'", name), "") } go func() { cmd.Wait() }() return DeployError{} } // restartServer attempts to restart the 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(configuration.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! (Lapis server not running?)", 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(configuration.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, configuration.sites_dir+"/"+name); err != nil { return newDeployError(1, "pullRepo", fmt.Sprintf("Failed to pull down repository '%s'", name), "") } if err := startServer(name); err.code != 0 { return err } } else { log.Printf("Pulling down repository %s...", name) repo,err := git.Open(configuration.sites_dir+"/"+name) if err != nil { return newDeployError(1, "pullRepo", fmt.Sprintf("Failed to open git repository '%s'", name), "") } repo.Pull() } return DeployError{} }