2024-01-24 16:09:39 +00:00
|
|
|
package sideload
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2024-09-29 15:03:11 +00:00
|
|
|
"git.solsynth.dev/goatworks/roadsign/pkg/navi"
|
|
|
|
"git.solsynth.dev/goatworks/roadsign/pkg/warden"
|
2024-01-24 16:09:39 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/pelletier/go-toml/v2"
|
|
|
|
"github.com/samber/lo"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getRegions(c *fiber.Ctx) error {
|
|
|
|
return c.JSON(navi.R.Regions)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRegionConfig(c *fiber.Ctx) error {
|
|
|
|
fp := filepath.Join(viper.GetString("paths.configs"), c.Params("id"))
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var data []byte
|
|
|
|
if data, err = os.ReadFile(fp + ".toml"); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Type("toml").SendString(string(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func doSync(c *fiber.Ctx) error {
|
|
|
|
req := string(c.Body())
|
|
|
|
|
|
|
|
id := c.Params("slug")
|
|
|
|
path := filepath.Join(viper.GetString("paths.configs"), fmt.Sprintf("%s.toml", id))
|
|
|
|
|
|
|
|
if file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755); err != nil {
|
|
|
|
return fiber.NewError(fiber.ErrInternalServerError.Code, err.Error())
|
|
|
|
} else {
|
2024-10-02 17:38:27 +00:00
|
|
|
var testOut map[string]any
|
|
|
|
if err := toml.Unmarshal([]byte(req), &testOut); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("invalid configuration: %v", err))
|
|
|
|
}
|
|
|
|
_, _ = file.Write([]byte(req))
|
2024-01-24 16:09:39 +00:00
|
|
|
defer file.Close()
|
|
|
|
}
|
2024-09-29 15:03:11 +00:00
|
|
|
|
2024-10-02 15:12:52 +00:00
|
|
|
var stopQueue, startQueue []*warden.AppInstance
|
|
|
|
// Getting things need to stop
|
2024-01-24 16:09:39 +00:00
|
|
|
if region, ok := lo.Find(navi.R.Regions, func(item *navi.Region) bool {
|
|
|
|
return item.ID == id
|
|
|
|
}); ok {
|
|
|
|
for _, application := range region.Applications {
|
|
|
|
if instance := warden.GetFromPool(application.ID); instance != nil {
|
2024-10-02 15:12:52 +00:00
|
|
|
stopQueue = append(stopQueue, instance)
|
2024-01-24 16:09:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reload
|
2024-10-02 15:12:52 +00:00
|
|
|
_ = navi.ReadInConfig(viper.GetString("paths.configs"))
|
|
|
|
|
|
|
|
// Getting things need to start
|
|
|
|
if region, ok := lo.Find(navi.R.Regions, func(item *navi.Region) bool {
|
|
|
|
return item.ID == id
|
|
|
|
}); ok {
|
|
|
|
for _, application := range region.Applications {
|
|
|
|
if instance := warden.GetFromPool(application.ID); instance != nil {
|
|
|
|
startQueue = append(startQueue, instance)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-29 15:03:11 +00:00
|
|
|
|
2024-01-24 16:09:39 +00:00
|
|
|
// Reboot
|
2024-10-02 15:12:52 +00:00
|
|
|
for _, instance := range stopQueue {
|
|
|
|
_ = instance.Stop()
|
|
|
|
}
|
|
|
|
for _, instance := range startQueue {
|
|
|
|
_ = instance.Start()
|
2024-01-24 16:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|