Save metadata endpoint
Unittests / unittests (push) Successful in 9s Details
Unittests / deploy-dev (push) Successful in 46s Details

This commit is contained in:
Adam Štrauch 2024-01-30 23:50:03 +01:00
parent a3d0ee92ce
commit 31ba1ce5a3
Signed by: cx
GPG Key ID: 7262DAFE292BCE20
4 changed files with 76 additions and 0 deletions

View File

@ -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()

View File

@ -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{

View File

@ -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
*/

View File

@ -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)