2023-12-13 11:52:56 +00:00
|
|
|
package sideload
|
2023-11-18 06:30:35 +00:00
|
|
|
|
|
|
|
import (
|
2023-12-10 11:19:40 +00:00
|
|
|
"context"
|
2024-10-03 12:59:18 +00:00
|
|
|
"fmt"
|
2024-09-29 15:03:11 +00:00
|
|
|
"git.solsynth.dev/goatworks/roadsign/pkg/warden"
|
2024-10-06 03:29:32 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-12-10 03:30:31 +00:00
|
|
|
"os"
|
2024-10-03 12:59:18 +00:00
|
|
|
"os/exec"
|
2023-12-10 03:30:31 +00:00
|
|
|
"path/filepath"
|
2024-10-03 12:59:18 +00:00
|
|
|
"strings"
|
2023-12-10 03:30:31 +00:00
|
|
|
|
2024-09-29 15:03:11 +00:00
|
|
|
"git.solsynth.dev/goatworks/roadsign/pkg/navi"
|
2023-11-18 06:30:35 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/google/uuid"
|
2023-12-10 06:52:00 +00:00
|
|
|
"github.com/samber/lo"
|
2023-12-10 11:19:40 +00:00
|
|
|
"github.com/saracen/fastzip"
|
2023-11-18 06:30:35 +00:00
|
|
|
)
|
|
|
|
|
2023-12-10 06:52:00 +00:00
|
|
|
func doPublish(c *fiber.Ctx) error {
|
2023-12-10 10:26:04 +00:00
|
|
|
var workdir string
|
2024-01-24 16:09:39 +00:00
|
|
|
var destination *navi.Destination
|
|
|
|
var application *warden.Application
|
|
|
|
for _, item := range navi.R.Regions {
|
2023-12-10 06:52:00 +00:00
|
|
|
if item.ID == c.Params("site") {
|
2024-01-24 16:09:39 +00:00
|
|
|
for _, location := range item.Locations {
|
|
|
|
for _, dest := range location.Destinations {
|
|
|
|
if dest.ID == c.Params("slug") {
|
|
|
|
destination = &dest
|
|
|
|
workdir, _ = dest.GetRawUri()
|
|
|
|
break
|
|
|
|
}
|
2023-12-10 10:26:04 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-24 16:09:39 +00:00
|
|
|
for _, app := range item.Applications {
|
|
|
|
if app.ID == c.Params("slug") {
|
|
|
|
application = &app
|
|
|
|
workdir = app.Workdir
|
2023-11-18 06:30:35 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-24 16:09:39 +00:00
|
|
|
var instance *warden.AppInstance
|
|
|
|
if application != nil {
|
|
|
|
if instance = warden.GetFromPool(application.ID); instance != nil {
|
2024-10-06 03:29:32 +00:00
|
|
|
if err := instance.Stop(); err != nil {
|
|
|
|
log.Warn().Err(err).Str("id", application.ID).Msg("Failed to stop application when publishing...")
|
|
|
|
}
|
2024-01-24 16:09:39 +00:00
|
|
|
}
|
|
|
|
} else if destination != nil && destination.GetType() != navi.DestinationStaticFile {
|
2023-11-18 06:30:35 +00:00
|
|
|
return fiber.ErrUnprocessableEntity
|
2024-10-02 15:12:52 +00:00
|
|
|
} else if destination == nil {
|
2024-01-24 16:09:39 +00:00
|
|
|
return fiber.ErrNotFound
|
2023-12-10 06:52:00 +00:00
|
|
|
}
|
|
|
|
|
2024-10-03 12:59:18 +00:00
|
|
|
if c.QueryBool("overwrite", true) {
|
2023-11-18 06:30:35 +00:00
|
|
|
files, _ := filepath.Glob(filepath.Join(workdir, "*"))
|
|
|
|
for _, file := range files {
|
|
|
|
_ = os.Remove(file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-10 06:52:00 +00:00
|
|
|
if form, err := c.MultipartForm(); err == nil {
|
2023-11-18 06:30:35 +00:00
|
|
|
files := form.File["attachments"]
|
|
|
|
for _, file := range files {
|
2023-12-10 06:52:00 +00:00
|
|
|
mimetype := lo.Ternary(len(c.Query("mimetype")) > 0, c.Query("mimetype"), file.Header["Content-Type"][0])
|
2023-11-18 06:30:35 +00:00
|
|
|
switch mimetype {
|
|
|
|
case "application/zip":
|
|
|
|
dst := filepath.Join(os.TempDir(), uuid.NewString()+".zip")
|
2023-12-10 06:52:00 +00:00
|
|
|
if err := c.SaveFile(file, dst); err != nil {
|
2023-11-18 06:30:35 +00:00
|
|
|
return err
|
|
|
|
} else {
|
2023-12-10 11:19:40 +00:00
|
|
|
if ex, err := fastzip.NewExtractor(dst, workdir); err != nil {
|
|
|
|
return err
|
|
|
|
} else if err = ex.Extract(context.Background()); err != nil {
|
|
|
|
defer ex.Close()
|
|
|
|
return err
|
|
|
|
}
|
2023-11-18 06:30:35 +00:00
|
|
|
}
|
2024-10-03 12:59:18 +00:00
|
|
|
_ = os.Remove(dst)
|
2023-11-18 06:30:35 +00:00
|
|
|
default:
|
|
|
|
dst := filepath.Join(workdir, file.Filename)
|
2023-12-10 06:52:00 +00:00
|
|
|
if err := c.SaveFile(file, dst); err != nil {
|
2023-11-18 06:30:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-03 12:59:18 +00:00
|
|
|
if postScript := c.FormValue("post-deploy-script", ""); len(postScript) > 0 {
|
|
|
|
cmd := exec.Command("sh", "-c", postScript)
|
|
|
|
cmd.Dir = filepath.Join(workdir)
|
|
|
|
cmd.Env = append(cmd.Env, strings.Split(c.FormValue("post-deploy-environment", ""), "\n")...)
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("post deploy script runs failed: %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-24 16:09:39 +00:00
|
|
|
if instance != nil {
|
2024-10-02 15:12:52 +00:00
|
|
|
_ = instance.Wake()
|
2024-01-24 16:09:39 +00:00
|
|
|
}
|
|
|
|
|
2023-12-10 06:52:00 +00:00
|
|
|
return c.SendStatus(fiber.StatusOK)
|
2023-11-18 06:30:35 +00:00
|
|
|
}
|