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-06-11 14:00:32 -05:00
|
|
|
"errors"
|
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-04-16 11:36:51 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/akamensky/argparse"
|
2024-03-07 16:43:06 -06:00
|
|
|
)
|
2024-03-07 15:11:25 -06:00
|
|
|
|
2024-06-11 14:00:32 -05:00
|
|
|
// Circle emoji
|
2024-04-12 11:19:43 -05:00
|
|
|
var Circles = map[string]string{
|
|
|
|
"white": "⚪️ ",
|
|
|
|
"green": "🟢 ",
|
|
|
|
"yellow": "🟡 ",
|
|
|
|
"red": "🔴 ",
|
|
|
|
"orange": "🟠 ",
|
|
|
|
}
|
|
|
|
|
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-06-11 14:00:32 -05:00
|
|
|
// Create a configuration struct
|
|
|
|
var configuration = Configuration{}
|
|
|
|
|
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-06-11 14:00:32 -05:00
|
|
|
sendInfo(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-06-11 14:00:32 -05:00
|
|
|
sendError(errors.New(fmt.Sprintf("Failed to check if site '%s' exists: %s", repo_name, err)))
|
2024-03-14 11:50:53 -05:00
|
|
|
return
|
2024-03-07 22:37:09 -06:00
|
|
|
}
|
2024-03-10 23:13:43 -05:00
|
|
|
if exists {
|
2024-06-11 14:00:32 -05:00
|
|
|
sendInfo("Updating repository...")
|
|
|
|
if err := site.Update(); err != nil {
|
|
|
|
sendError(err)
|
2024-03-14 11:50:53 -05:00
|
|
|
return
|
2024-03-10 23:13:43 -05:00
|
|
|
}
|
2024-04-08 15:29:07 -05:00
|
|
|
new_site, exists, err := getSite(site.name)
|
|
|
|
if err != nil || !exists {
|
2024-06-11 14:00:32 -05:00
|
|
|
sendError(errors.New("Failed to update site data on a site we just updated: " + repo_name))
|
2024-04-08 15:29:07 -05:00
|
|
|
}
|
|
|
|
site = new_site
|
2024-06-11 14:00:32 -05:00
|
|
|
sendInfo("Restarting server...")
|
|
|
|
if error := site.Restart(); error != nil {
|
|
|
|
sendError(error)
|
2024-03-14 11:50:53 -05:00
|
|
|
return
|
2024-03-10 23:13:43 -05:00
|
|
|
}
|
2024-03-11 17:55:13 -05:00
|
|
|
} else {
|
2024-06-11 14:00:32 -05:00
|
|
|
sendInfo("Cloning repositiory...")
|
|
|
|
if error := CloneSite(repository["ssh_url"].(string), repo_name); error != nil {
|
|
|
|
sendError(error)
|
2024-03-14 11:50:53 -05:00
|
|
|
return
|
2024-03-10 23:13:43 -05:00
|
|
|
}
|
2024-06-11 14:00:32 -05:00
|
|
|
sendInfo("Starting server...")
|
2024-03-11 17:19:10 -05:00
|
|
|
if site, exists, err = getSite(repo_name); err != nil {
|
2024-06-11 14:00:32 -05:00
|
|
|
sendError(err)
|
|
|
|
return
|
2024-03-11 17:19:10 -05:00
|
|
|
}
|
2024-06-11 14:00:32 -05:00
|
|
|
if error := site.Start(); error != nil {
|
|
|
|
sendError(err)
|
2024-03-14 11:50:53 -05:00
|
|
|
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
|
|
|
}
|
2024-04-12 11:19:43 -05:00
|
|
|
sendMessage(MatrixMessage{text: Circles["green"] + "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")
|
2024-03-27 11:45:37 -05:00
|
|
|
config_path := parser.String("c", "config", &argparse.Options{Required: false, Help: "Configuration file", Default: "./config.lua"})
|
2024-03-15 15:57:32 -05:00
|
|
|
disable_matrix := parser.Flag("M", "disable-matrix", &argparse.Options{Required: false, Help: "Disable Matrix chat bot", Default: false})
|
2024-03-08 15:50:30 -06:00
|
|
|
if err := parser.Parse(os.Args); err != nil { // Parse arguments
|
|
|
|
fmt.Print(parser.Usage(err)) // Show usage if there's an error
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-27 10:21:56 -05:00
|
|
|
if exists, err := fileExists(*config_path); !exists {
|
|
|
|
fmt.Printf("Invalid config file '%s'\n", *config_path)
|
|
|
|
os.Exit(1)
|
|
|
|
} else if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-03-08 15:50:30 -06:00
|
|
|
parseConfig(*config_path, &configuration) // Parse JSON configuration file
|
2024-04-16 10:21:34 -05:00
|
|
|
// Disable Matrix if it's disabled in either the config or specified on CLI
|
|
|
|
configuration.matrix.disabled = *disable_matrix || configuration.matrix.disabled
|
2024-03-08 15:50:30 -06:00
|
|
|
|
2024-03-14 11:50:53 -05:00
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { // Webhook receiver
|
2024-04-16 11:36:51 -05:00
|
|
|
log.Print("Recieved request!")
|
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-04-16 11:36:51 -05:00
|
|
|
check_ips := []string{ // IPs of client to check against trusted_servers
|
|
|
|
strings.Split(r.RemoteAddr, ":")[0],
|
|
|
|
r.Header.Get("x-forwarded-for"),
|
|
|
|
}
|
|
|
|
found_trusted_server := false
|
|
|
|
for _, server := range configuration.trusted_servers {
|
|
|
|
if found_trusted_server {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for _, ip := range check_ips {
|
|
|
|
if ip == server {
|
|
|
|
log.Printf("Request IP matches a trusted server (%s)", ip)
|
|
|
|
go handler(data) // Run the handler
|
|
|
|
fmt.Fprint(w, "Received!")
|
|
|
|
found_trusted_server = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found_trusted_server {
|
|
|
|
log.Print("Request IP doesn't match a trusted server! Dropping...")
|
|
|
|
}
|
2024-03-07 16:43:06 -06:00
|
|
|
})
|
2024-04-02 21:10:51 -05:00
|
|
|
// Attempt to create sites_dir
|
|
|
|
if exists, _ := fileExists(configuration.sites_dir); !exists {
|
|
|
|
os.Mkdir(configuration.sites_dir, 640) // Permissions are 640 (Owner: rw, Group: r, All: none)
|
|
|
|
}
|
|
|
|
|
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-04-16 10:21:34 -05:00
|
|
|
startAllSites() // Start all the servers
|
|
|
|
if !configuration.matrix.disabled { // Only start Matrix bot if --disable-matrix isn't passed
|
2024-03-15 15:57:32 -05:00
|
|
|
go initMatrix() // Start Matrix stuff in a goroutine
|
|
|
|
} else {
|
|
|
|
log.Print("Matrix bot is disabled!")
|
|
|
|
}
|
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
|
|
|
}
|