Post deploy script

This commit is contained in:
2024-10-03 20:59:18 +08:00
parent 11a9a4a929
commit 34f20e02ae
6 changed files with 41 additions and 14 deletions

View File

@ -60,7 +60,7 @@ func (v *Destination) GetType() DestinationType {
func (v *Destination) GetRawUri() (string, url.Values) {
uri := strings.SplitN(v.Uri, "://", 2)[1]
data := strings.SplitN(uri, "?", 2)
data = append(data, " ") // Make the data array least have two elements
data = append(data, " ") // Make the data array at least have two elements
qs, _ := url.ParseQuery(data[1])
return data[0], qs

View File

@ -2,9 +2,12 @@ package sideload
import (
"context"
"fmt"
"git.solsynth.dev/goatworks/roadsign/pkg/warden"
"os"
"os/exec"
"path/filepath"
"strings"
"git.solsynth.dev/goatworks/roadsign/pkg/navi"
"github.com/gofiber/fiber/v2"
@ -50,7 +53,7 @@ func doPublish(c *fiber.Ctx) error {
return fiber.ErrNotFound
}
if c.Query("overwrite", "yes") == "yes" {
if c.QueryBool("overwrite", true) {
files, _ := filepath.Glob(filepath.Join(workdir, "*"))
for _, file := range files {
_ = os.Remove(file)
@ -74,6 +77,7 @@ func doPublish(c *fiber.Ctx) error {
return err
}
}
_ = os.Remove(dst)
default:
dst := filepath.Join(workdir, file.Filename)
if err := c.SaveFile(file, dst); err != nil {
@ -83,6 +87,15 @@ func doPublish(c *fiber.Ctx) error {
}
}
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))
}
}
if instance != nil {
_ = instance.Wake()
}

View File

@ -77,7 +77,7 @@ func doSync(c *fiber.Ctx) error {
_ = instance.Stop()
}
for _, instance := range startQueue {
_ = instance.Start()
_ = instance.Wake()
}
return c.SendStatus(fiber.StatusOK)

View File

@ -62,14 +62,8 @@ func (v *AppInstance) Wake() error {
}
if v.Cmd.ProcessState.Exited() {
return v.Start()
} else if v.Cmd.ProcessState.Exited() {
return fmt.Errorf("process already dead")
}
if v.Cmd.ProcessState.Exited() {
return fmt.Errorf("cannot start process")
} else {
return nil
}
return nil
}
func (v *AppInstance) Start() error {
@ -93,7 +87,7 @@ func (v *AppInstance) Start() error {
} else if v.Cmd != nil && v.Cmd.ProcessState == nil {
v.Status = AppStarted
} else {
v.Status = lo.Ternary(v.Cmd == nil, AppExited, AppFailure)
v.Status = AppFailure
v.Cmd = nil
return
}
@ -119,6 +113,7 @@ func (v *AppInstance) Stop() error {
}
}
v.Status = AppExited
return nil
}