2024-03-08 15:36:11 -06:00
|
|
|
// package main is the main package for the LapisDeploy program
|
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-14 11:50:53 -05:00
|
|
|
"github.com/akamensky/argparse"
|
2024-03-07 16:43:06 -06:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-03-07 22:37:09 -06:00
|
|
|
"os"
|
2024-03-07 16:43:06 -06:00
|
|
|
)
|
2024-03-07 15:11:25 -06:00
|
|
|
|
2024-03-08 15:36:11 -06:00
|
|
|
// Create a configuration struct
|
2024-03-08 15:16:33 -06:00
|
|
|
var configuration = Configuration{}
|
2024-03-08 14:51:58 -06:00
|
|
|
|
2024-03-07 22:37:09 -06:00
|
|
|
// fileExists returns whether the given file or directory exists.
|
|
|
|
func fileExists(path string) (bool, error) {
|
2024-03-14 11:50:53 -05:00
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
2024-03-07 22:37:09 -06:00
|
|
|
}
|
|
|
|
|
2024-03-08 15:36:11 -06:00
|
|
|
// handler is in charge of handling requests after the JSON has been parsed
|
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"])
|
2024-03-10 21:22:05 -05:00
|
|
|
sendMessage(MatrixMessage{text: fmt.Sprintf("⚪️ Handling webhook for `%s`...", repository["full_name"])})
|
2024-03-07 22:37:09 -06:00
|
|
|
|
2024-03-10 23:13:43 -05:00
|
|
|
repo_name := repository["name"].(string)
|
|
|
|
|
2024-03-11 17:19:10 -05:00
|
|
|
site, exists, err := getSite(repo_name)
|
2024-03-10 23:13:43 -05:00
|
|
|
if err != nil {
|
2024-03-11 17:19:10 -05:00
|
|
|
deploy_error := newDeployError(1, "handler", fmt.Sprintf("Failed to check if site '%s' exists", repo_name), fmt.Sprint(err))
|
2024-03-14 11:50:53 -05:00
|
|
|
deploy_error.SendOverMatrix()
|
|
|
|
return
|
2024-03-07 22:37:09 -06:00
|
|
|
}
|
2024-03-10 23:13:43 -05:00
|
|
|
if exists {
|
2024-03-11 17:55:13 -05:00
|
|
|
sendMessage(MatrixMessage{text: "⚪️ Updating repository..."})
|
2024-03-11 17:19:10 -05:00
|
|
|
if deploy_error := site.Update(); deploy_error.code != 0 {
|
2024-03-14 11:50:53 -05:00
|
|
|
deploy_error.SendOverMatrix()
|
|
|
|
return
|
2024-03-10 23:13:43 -05:00
|
|
|
}
|
2024-03-11 17:55:13 -05:00
|
|
|
sendMessage(MatrixMessage{text: "⚪️ Restarting server..."})
|
2024-03-11 17:19:10 -05:00
|
|
|
if deploy_error := site.Restart(); deploy_error.code != 0 {
|
2024-03-14 11:50:53 -05:00
|
|
|
deploy_error.SendOverMatrix()
|
|
|
|
return
|
2024-03-10 23:13:43 -05:00
|
|
|
}
|
2024-03-11 17:55:13 -05:00
|
|
|
} else {
|
|
|
|
sendMessage(MatrixMessage{text: "⚪️ Cloning repository..."})
|
2024-03-11 17:19:10 -05:00
|
|
|
if deploy_error := CloneSite(repository["ssh_url"].(string), repo_name); deploy_error.code != 0 {
|
2024-03-14 11:50:53 -05:00
|
|
|
deploy_error.SendOverMatrix()
|
|
|
|
return
|
2024-03-10 23:13:43 -05:00
|
|
|
}
|
2024-03-11 17:55:13 -05:00
|
|
|
sendMessage(MatrixMessage{text: "⚪️ Starting server..."})
|
2024-03-11 17:19:10 -05:00
|
|
|
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 {
|
2024-03-14 11:50:53 -05:00
|
|
|
deploy_error.SendOverMatrix()
|
|
|
|
return
|
2024-03-10 23:13:43 -05:00
|
|
|
}
|
2024-03-11 17:55:13 -05:00
|
|
|
defer sendMessage(MatrixMessage{text: "🚀 Launched for the first time!"})
|
2024-03-10 21:22:05 -05:00
|
|
|
}
|
|
|
|
sendMessage(MatrixMessage{text: "🟢 Deployed successfully!"})
|
2024-03-07 22:37:09 -06:00
|
|
|
}
|
|
|
|
|
2024-03-08 15:36:11 -06:00
|
|
|
// main is the starting point of the program
|
2024-03-14 11:50:53 -05:00
|
|
|
func main() {
|
2024-03-08 15:50:30 -06:00
|
|
|
// Parse arguments
|
|
|
|
parser := argparse.NewParser("lapisdeploy", "Easily deploy Lapis web applications through Gitea webhooks")
|
|
|
|
config_path := parser.String("c", "config", &argparse.Options{Required: false, Help: "Configuration file", Default: "./config.json"})
|
|
|
|
if err := parser.Parse(os.Args); err != nil { // Parse arguments
|
|
|
|
fmt.Print(parser.Usage(err)) // Show usage if there's an error
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
parseConfig(*config_path, &configuration) // Parse JSON configuration file
|
|
|
|
|
2024-03-14 11:50:53 -05:00
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { // Webhook receiver
|
2024-03-07 16:43:06 -06:00
|
|
|
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
|
|
|
|
2024-03-08 15:50:30 -06:00
|
|
|
go handler(data) // Run the handler
|
2024-03-07 22:37:09 -06:00
|
|
|
fmt.Fprint(w, "Received!")
|
2024-03-07 16:43:06 -06:00
|
|
|
})
|
2024-03-08 15:55:13 -06:00
|
|
|
log.Printf("Starting Lapis Deploy on port %d...", configuration.port)
|
2024-03-10 14:06:10 -05:00
|
|
|
|
2024-03-14 11:50:53 -05:00
|
|
|
startAllSites() // Start all the servers
|
|
|
|
go initMatrix() // Start Matrix stuff in a goroutine
|
2024-03-10 14:06:10 -05:00
|
|
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", configuration.port), nil)) // Start HTTP server
|
2024-03-07 15:11:25 -06:00
|
|
|
}
|