From 31ba1ce5a307b59d9cf34b8054d37150d81c489c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C5=A0trauch?= Date: Tue, 30 Jan 2024 23:50:03 +0100 Subject: [PATCH] Save metadata endpoint --- glue/main.go | 24 ++++++++++++++++++++++++ handlers.go | 28 ++++++++++++++++++++++++++++ handlers_nats.go | 21 +++++++++++++++++++++ main.go | 3 +++ 4 files changed, 76 insertions(+) diff --git a/glue/main.go b/glue/main.go index 3861205..4f4b3bf 100644 --- a/glue/main.go +++ b/glue/main.go @@ -634,6 +634,30 @@ func (p *Processor) GetHostKey() (string, error) { return hostKey, nil } +// Save meta data about app into a file +func (p *Processor) SaveMetadata(metadata string) error { + container, err := p.getContainer() + if err != nil { + return err + } + + volumePath := container.VolumeHostPath() + + f, err := os.Create(path.Join(volumePath, ".metadata.json")) + if err != nil { + return err + } + + defer f.Close() + _, err = f.Write([]byte(metadata)) + + if err != nil { + return err + } + + return nil +} + // Processes returns list of supervisord processes func (p *Processor) Processes() ([]docker.Process, error) { container, err := p.getContainer() diff --git a/handlers.go b/handlers.go index a5986ff..7313a7f 100644 --- a/handlers.go +++ b/handlers.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "io" "io/ioutil" "log" "net/http" @@ -362,6 +363,33 @@ func getOrphansHander(c echo.Context) error { return c.JSON(http.StatusOK, []string{}) } +// Save metadata for the app +func saveMetadataHandler(c echo.Context) error { + name := c.Param("name") + + processor := glue.Processor{ + AppName: name, + DB: common.GetDBConnection(), + SnapshotProcessor: &snapshotProcessor, + DockerSock: config.DockerSocket, + BindIPHTTP: config.AppsBindIPHTTP, + BindIPSSH: config.AppsBindIPSSH, + AppsPath: config.AppsPath, + } + + body, err := io.ReadAll(c.Request().Body) + if err != nil { + return fmt.Errorf("error reading request body: %v", err) + } + + err = processor.SaveMetadata(string(body)) + if err != nil { + return fmt.Errorf("error while save metadata: %v", err.Error()) + } + + return nil +} + // Return info about the node including performance index func getNodeInfoHandler(c echo.Context) error { processor := glue.Processor{ diff --git a/handlers_nats.go b/handlers_nats.go index 2654f37..b12b12d 100644 --- a/handlers_nats.go +++ b/handlers_nats.go @@ -63,6 +63,7 @@ func _messageHandler(m *nats.Msg) error { "add_label": addLabelEventHandler, "remove_label": removeLabelEventHandler, "list_orphans": listOrphansEventHandler, + "save_metadata": saveMetadataEventHandler, "node": getNodeEventHandler, "create_snapshot": createSnapshotEventHandler, "restore_from_snapshot": restoreFromSnapshotEventHandler, @@ -719,6 +720,26 @@ func listOrphansEventHandler(m *nats.Msg, message *RequestMessage) error { return nil } +// Save metadata for the app +func saveMetadataEventHandler(m *nats.Msg, message *RequestMessage) error { + processor := glue.Processor{ + AppName: message.AppName, + DB: common.GetDBConnection(), + SnapshotProcessor: &snapshotProcessor, + DockerSock: config.DockerSocket, + BindIPHTTP: config.AppsBindIPHTTP, + BindIPSSH: config.AppsBindIPSSH, + AppsPath: config.AppsPath, + } + + err := processor.SaveMetadata(message.Payload) + if err != nil { + return fmt.Errorf("error while save metadata: %v", err.Error()) + } + + return nil +} + /* getNodeEventHandler returns info about the node including performance index */ diff --git a/main.go b/main.go index 3f01e57..7cbb7f3 100644 --- a/main.go +++ b/main.go @@ -183,6 +183,9 @@ func main() { // Rebuilds existing app, it keeps the data but creates the container again e.PUT("/v1/apps/:name/rebuild", rebuildAppHandler) + // Save metadata about app + e.POST("/v1/apps/:name", saveMetadataHandler) + // Adds new label e.POST("/v1/apps/:name/labels", addLabelHandler)