2023-03-03 20:37:09 +01:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/agent"
|
2023-03-28 20:43:45 +02:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/agent/controller/app/spec"
|
2023-04-11 12:05:19 +02:00
|
|
|
"forge.cadoles.com/arcad/edge/pkg/app"
|
2023-03-03 20:37:09 +01:00
|
|
|
"forge.cadoles.com/arcad/edge/pkg/bundle"
|
2023-03-13 10:44:58 +01:00
|
|
|
"github.com/mitchellh/hashstructure/v2"
|
2023-03-03 20:37:09 +01:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
|
|
)
|
|
|
|
|
2023-03-13 10:44:58 +01:00
|
|
|
type serverEntry struct {
|
2023-03-29 15:32:23 +02:00
|
|
|
AppDefHash uint64
|
|
|
|
Server *Server
|
2023-03-13 10:44:58 +01:00
|
|
|
}
|
|
|
|
|
2023-03-03 20:37:09 +01:00
|
|
|
type Controller struct {
|
2023-03-29 11:27:47 +02:00
|
|
|
client *http.Client
|
|
|
|
downloadDir string
|
|
|
|
dataDir string
|
|
|
|
servers map[string]*serverEntry
|
|
|
|
appRepository *AppRepository
|
2023-03-03 20:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name implements node.Controller.
|
|
|
|
func (c *Controller) Name() string {
|
|
|
|
return "app-controller"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reconcile implements node.Controller.
|
|
|
|
func (c *Controller) Reconcile(ctx context.Context, state *agent.State) error {
|
2023-03-28 20:43:45 +02:00
|
|
|
appSpec := spec.NewSpec()
|
2023-03-03 20:37:09 +01:00
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
if err := state.GetSpec(spec.Name, appSpec); err != nil {
|
2023-03-03 20:37:09 +01:00
|
|
|
if errors.Is(err, agent.ErrSpecNotFound) {
|
2023-03-13 10:44:58 +01:00
|
|
|
logger.Info(ctx, "could not find app spec")
|
2023-03-03 20:37:09 +01:00
|
|
|
|
2023-03-13 10:44:58 +01:00
|
|
|
c.stopAllApps(ctx, appSpec)
|
2023-03-03 20:37:09 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info(ctx, "retrieved spec", logger.F("spec", appSpec.SpecName()), logger.F("revision", appSpec.SpecRevision()))
|
|
|
|
|
|
|
|
c.updateApps(ctx, appSpec)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
func (c *Controller) stopAllApps(ctx context.Context, spec *spec.Spec) {
|
2023-03-13 10:44:58 +01:00
|
|
|
if len(c.servers) > 0 {
|
|
|
|
logger.Info(ctx, "stopping all apps")
|
|
|
|
}
|
|
|
|
|
|
|
|
for appID, entry := range c.servers {
|
2023-03-03 20:37:09 +01:00
|
|
|
logger.Info(ctx, "stopping app", logger.F("appID", appID))
|
|
|
|
|
2023-03-13 10:44:58 +01:00
|
|
|
if err := entry.Server.Stop(); err != nil {
|
2023-10-13 12:30:52 +02:00
|
|
|
err = errors.WithStack(err)
|
2023-03-03 20:37:09 +01:00
|
|
|
logger.Error(
|
|
|
|
ctx, "error while stopping app",
|
|
|
|
logger.F("appID", appID),
|
2023-10-19 22:09:18 +02:00
|
|
|
logger.CapturedE(err),
|
2023-03-03 20:37:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
delete(c.servers, appID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
func (c *Controller) updateApps(ctx context.Context, specs *spec.Spec) {
|
2023-03-03 20:37:09 +01:00
|
|
|
// Stop and remove obsolete apps
|
2023-03-28 20:43:45 +02:00
|
|
|
for appKey, server := range c.servers {
|
|
|
|
if _, exists := specs.Apps[appKey]; exists {
|
2023-03-03 20:37:09 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
logger.Info(ctx, "stopping app", logger.F("appKey", appKey))
|
2023-03-03 20:37:09 +01:00
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
if err := server.Server.Stop(); err != nil {
|
2023-10-13 12:30:52 +02:00
|
|
|
err = errors.WithStack(err)
|
2023-03-03 20:37:09 +01:00
|
|
|
logger.Error(
|
|
|
|
ctx, "error while stopping app",
|
2023-03-28 20:43:45 +02:00
|
|
|
logger.F("appKey", appKey),
|
2023-10-19 22:09:18 +02:00
|
|
|
logger.CapturedE(err),
|
2023-03-03 20:37:09 +01:00
|
|
|
)
|
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
delete(c.servers, appKey)
|
2023-03-03 20:37:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 23:21:43 +02:00
|
|
|
if err := c.updateAppRepository(ctx, specs); err != nil {
|
2023-10-13 12:30:52 +02:00
|
|
|
err = errors.WithStack(err)
|
2023-04-05 23:21:43 +02:00
|
|
|
logger.Error(
|
|
|
|
ctx, "could not update app repository",
|
2023-10-19 22:09:18 +02:00
|
|
|
logger.CapturedE(err),
|
2023-04-05 23:21:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2023-03-29 11:27:47 +02:00
|
|
|
|
|
|
|
// (Re)start apps if necessary
|
2023-03-28 20:43:45 +02:00
|
|
|
for appKey := range specs.Apps {
|
|
|
|
appCtx := logger.With(ctx, logger.F("appKey", appKey))
|
2023-03-03 20:37:09 +01:00
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
if err := c.updateApp(ctx, specs, appKey); err != nil {
|
2023-10-13 12:30:52 +02:00
|
|
|
err = errors.WithStack(err)
|
2023-10-19 22:09:18 +02:00
|
|
|
logger.Error(appCtx, "could not update app", logger.CapturedE(err))
|
2023-03-03 20:37:09 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2023-03-13 10:44:58 +01:00
|
|
|
}
|
2023-03-03 20:37:09 +01:00
|
|
|
|
2023-04-05 23:21:43 +02:00
|
|
|
func (c *Controller) updateAppRepository(ctx context.Context, specs *spec.Spec) error {
|
2023-03-29 11:27:47 +02:00
|
|
|
bundles := make([]string, 0, len(specs.Apps))
|
|
|
|
for appKey, app := range specs.Apps {
|
|
|
|
path := c.getAppBundlePath(appKey, app.Format)
|
|
|
|
bundles = append(bundles, path)
|
|
|
|
}
|
|
|
|
|
2023-04-05 23:21:43 +02:00
|
|
|
resolveAppURL, err := createResolveAppURL(specs)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.appRepository.Update(resolveAppURL, bundles)
|
2023-03-29 11:27:47 +02:00
|
|
|
|
2023-04-05 23:21:43 +02:00
|
|
|
return nil
|
2023-03-29 11:27:47 +02:00
|
|
|
}
|
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
func (c *Controller) updateApp(ctx context.Context, specs *spec.Spec, appKey string) (err error) {
|
|
|
|
appEntry := specs.Apps[appKey]
|
|
|
|
|
2023-03-29 15:32:23 +02:00
|
|
|
appDef := struct {
|
2023-04-05 23:21:43 +02:00
|
|
|
App spec.AppEntry
|
|
|
|
Config *spec.Config
|
2023-03-29 15:32:23 +02:00
|
|
|
}{
|
2023-04-05 23:21:43 +02:00
|
|
|
App: appEntry,
|
|
|
|
Config: specs.Config,
|
2023-03-29 15:32:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
newAppDefHash, err := hashstructure.Hash(appDef, hashstructure.FormatV2, nil)
|
2023-03-13 10:44:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
2023-03-03 20:37:09 +01:00
|
|
|
}
|
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
bundle, sha256sum, err := c.ensureAppBundle(ctx, appKey, appEntry)
|
2023-03-03 20:37:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not download app bundle")
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
server, exists := c.servers[appKey]
|
2023-03-03 20:37:09 +01:00
|
|
|
if !exists {
|
|
|
|
logger.Info(ctx, "app currently not running")
|
2023-03-28 20:43:45 +02:00
|
|
|
} else if sha256sum != appEntry.SHA256Sum {
|
2023-03-03 20:37:09 +01:00
|
|
|
logger.Info(
|
|
|
|
ctx, "bundle hash mismatch, stopping app",
|
|
|
|
logger.F("currentHash", sha256sum),
|
2023-03-28 20:43:45 +02:00
|
|
|
logger.F("specHash", appEntry.SHA256Sum),
|
2023-03-03 20:37:09 +01:00
|
|
|
)
|
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
if err := server.Server.Stop(); err != nil {
|
2023-03-03 20:37:09 +01:00
|
|
|
return errors.Wrap(err, "could not stop app")
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
server = nil
|
2023-03-03 20:37:09 +01:00
|
|
|
}
|
|
|
|
|
2023-04-05 23:21:43 +02:00
|
|
|
newServerEntry := func() (*serverEntry, error) {
|
2023-03-28 20:43:45 +02:00
|
|
|
options, err := c.getHandlerOptions(ctx, appKey, specs)
|
2023-03-03 20:37:09 +01:00
|
|
|
if err != nil {
|
2023-04-05 23:21:43 +02:00
|
|
|
return nil, errors.Wrap(err, "could not create handler options")
|
2023-03-28 20:43:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
server = &serverEntry{
|
2023-04-05 23:21:43 +02:00
|
|
|
Server: NewServer(bundle, specs.Config, options...),
|
2023-03-29 15:32:23 +02:00
|
|
|
AppDefHash: 0,
|
2023-03-13 10:44:58 +01:00
|
|
|
}
|
|
|
|
|
2023-04-05 23:21:43 +02:00
|
|
|
return server, nil
|
2023-03-13 10:44:58 +01:00
|
|
|
}
|
|
|
|
|
2023-04-05 23:21:43 +02:00
|
|
|
if server == nil {
|
|
|
|
serverEntry, err := newServerEntry()
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
2023-03-13 10:44:58 +01:00
|
|
|
|
2023-04-05 23:21:43 +02:00
|
|
|
c.servers[appKey] = serverEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
defChanged := newAppDefHash != server.AppDefHash
|
2023-03-29 15:32:23 +02:00
|
|
|
if server.Server.Running() && !defChanged {
|
2023-03-13 10:44:58 +01:00
|
|
|
return nil
|
2023-03-03 20:37:09 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 20:12:46 +02:00
|
|
|
ctx = logger.With(ctx,
|
|
|
|
logger.F("appKey", appKey),
|
|
|
|
logger.F("address", appEntry.Address),
|
|
|
|
)
|
|
|
|
|
2023-03-29 15:32:23 +02:00
|
|
|
if defChanged && server.AppDefHash != 0 {
|
2023-10-19 20:12:46 +02:00
|
|
|
logger.Info(ctx, "restarting app")
|
2023-04-05 23:21:43 +02:00
|
|
|
|
|
|
|
if err := server.Server.Stop(); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
serverEntry, err := newServerEntry()
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.servers[appKey] = serverEntry
|
2023-03-13 10:44:58 +01:00
|
|
|
} else {
|
2023-10-19 20:12:46 +02:00
|
|
|
logger.Info(ctx, "starting app")
|
2023-03-13 10:44:58 +01:00
|
|
|
}
|
2023-03-03 20:37:09 +01:00
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
if err := server.Server.Start(ctx, appEntry.Address); err != nil {
|
|
|
|
delete(c.servers, appKey)
|
2023-03-03 20:37:09 +01:00
|
|
|
|
|
|
|
return errors.Wrap(err, "could not start app")
|
|
|
|
}
|
|
|
|
|
2023-03-29 15:32:23 +02:00
|
|
|
server.AppDefHash = newAppDefHash
|
2023-03-13 10:44:58 +01:00
|
|
|
|
2023-03-03 20:37:09 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
func (c *Controller) ensureAppBundle(ctx context.Context, appID string, spec spec.AppEntry) (bundle.Bundle, string, error) {
|
2023-03-03 20:37:09 +01:00
|
|
|
if err := os.MkdirAll(c.downloadDir, os.ModePerm); err != nil {
|
|
|
|
return nil, "", errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
bundlePath := c.getAppBundlePath(appID, spec.Format)
|
2023-03-03 20:37:09 +01:00
|
|
|
|
|
|
|
_, err := os.Stat(bundlePath)
|
|
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
|
|
return nil, "", errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
if err := c.downloadFile(spec.URL, spec.SHA256Sum, bundlePath); err != nil {
|
|
|
|
return nil, "", errors.WithStack(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sha256sum, err := hash(bundlePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sha256sum == spec.SHA256Sum {
|
|
|
|
bdle, err := bundle.FromPath(bundlePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bdle, sha256sum, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info(ctx, "bundle hash mismatch, downloading app", logger.F("url", spec.URL))
|
|
|
|
|
|
|
|
if err := c.downloadFile(spec.URL, spec.SHA256Sum, bundlePath); err != nil {
|
|
|
|
return nil, "", errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bdle, err := bundle.FromPath(bundlePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2023-04-11 12:05:19 +02:00
|
|
|
manifest, err := app.LoadManifest(bdle)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
valid, err := validateManifest(manifest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
return nil, "", errors.New("bundle's manifest is invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
return bdle, spec.SHA256Sum, nil
|
2023-03-03 20:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) downloadFile(url string, sha256sum string, dest string) error {
|
|
|
|
res, err := c.client.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := res.Body.Close(); err != nil && !errors.Is(err, os.ErrClosed) {
|
|
|
|
panic(errors.WithStack(err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
tmp, err := os.CreateTemp(filepath.Dir(dest), "download_")
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := os.Remove(tmp.Name()); err != nil && !os.IsNotExist(err) {
|
|
|
|
panic(errors.WithStack(err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if _, err := io.Copy(tmp, res.Body); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpFileHash, err := hash(tmp.Name())
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tmpFileHash != sha256sum {
|
|
|
|
return errors.Errorf("sha256 sum mismatch: expected '%s', got '%s'", sha256sum, tmpFileHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Rename(tmp.Name(), dest); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) ensureAppDataDir(ctx context.Context, appID string) (string, error) {
|
|
|
|
dataDir := filepath.Join(c.dataDir, appID)
|
|
|
|
|
|
|
|
if err := os.MkdirAll(dataDir, os.ModePerm); err != nil {
|
|
|
|
return "", errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dataDir, nil
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:43:45 +02:00
|
|
|
func (c *Controller) getAppBundlePath(appKey string, format string) string {
|
|
|
|
return filepath.Join(c.downloadDir, appKey+"."+format)
|
|
|
|
}
|
|
|
|
|
2023-03-03 20:37:09 +01:00
|
|
|
func NewController(funcs ...OptionFunc) *Controller {
|
|
|
|
opts := defaultOptions()
|
|
|
|
for _, fn := range funcs {
|
|
|
|
fn(opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Controller{
|
2023-03-29 11:27:47 +02:00
|
|
|
client: opts.Client,
|
|
|
|
downloadDir: opts.DownloadDir,
|
|
|
|
dataDir: opts.DataDir,
|
|
|
|
servers: make(map[string]*serverEntry),
|
|
|
|
appRepository: NewAppRepository(),
|
2023-03-03 20:37:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ agent.Controller = &Controller{}
|