Compare commits

...

9 Commits
v4 ... main

Author SHA1 Message Date
cx 9e82bfc2b5 Chagne node-x ip
Unittests / unittests (push) Successful in 1m23s Details
Unittests / deploy-dev (push) Successful in 1m15s Details
2024-04-15 10:30:05 +00:00
Adam Štrauch e0b5832e75
Fix deps and crashing when deleting nonexistant app
Unittests / unittests (push) Successful in 9s Details
Unittests / deploy-dev (push) Successful in 59s Details
2024-03-03 02:55:05 +01:00
Adam Štrauch 863d857283
Restart container when tech changes
Unittests / unittests (push) Successful in 10s Details
Unittests / deploy-dev (push) Successful in 47s Details
2024-02-01 22:02:22 +01:00
Adam Štrauch 1c5b8d8f50
Add better debug message to SetTechnology
Unittests / unittests (push) Successful in 9s Details
Unittests / deploy-dev (push) Successful in 46s Details
2024-02-01 21:57:34 +01:00
Adam Štrauch bc4b6c7bff
Possibility to set tech during update
Unittests / unittests (push) Successful in 9s Details
Unittests / deploy-dev (push) Successful in 53s Details
2024-02-01 20:57:13 +01:00
Adam Štrauch 45899f3b0c
Set owner of metadata
Unittests / unittests (push) Successful in 9s Details
Unittests / deploy-dev (push) Successful in 45s Details
2024-01-31 00:16:40 +01:00
Adam Štrauch 5513da35b3
Fix endpoint name for metadata
Unittests / unittests (push) Successful in 9s Details
Unittests / deploy-dev (push) Successful in 44s Details
2024-01-30 23:52:33 +01:00
Adam Štrauch 31ba1ce5a3
Save metadata endpoint
Unittests / unittests (push) Successful in 9s Details
Unittests / deploy-dev (push) Successful in 46s Details
2024-01-30 23:50:03 +01:00
Adam Štrauch a3d0ee92ce
Stats errors in Sentry
Unittests / unittests (push) Successful in 9s Details
Unittests / deploy-dev (push) Successful in 52s Details
2024-01-26 21:43:45 +01:00
11 changed files with 2054 additions and 578 deletions

View File

@ -38,7 +38,7 @@ jobs:
deploy-dev:
runs-on: [amd64, moon]
env:
NODES: "192.168.1.236"
NODES: "192.168.1.33"
steps:
- uses: actions/checkout@v4
- name: deploy

View File

@ -202,13 +202,13 @@ func (d *Driver) Remove(name string) error {
return err
}
timeout := time.Duration(dockerTimeout * time.Second)
err = cli.ContainerStop(context.TODO(), containerID, &timeout)
timeout := dockerTimeout
err = cli.ContainerStop(context.TODO(), containerID, container.StopOptions{Timeout: &timeout})
if err != nil {
return err
}
err = cli.ContainerRemove(context.TODO(), containerID, types.ContainerRemoveOptions{})
err = cli.ContainerRemove(context.TODO(), containerID, container.RemoveOptions{})
return err
}
@ -246,8 +246,8 @@ func (d *Driver) Stop(name string) error {
return err
}
timeout := time.Duration(dockerTimeout * time.Second)
err = cli.ContainerStop(context.TODO(), containerID, &timeout)
timeout := dockerTimeout
err = cli.ContainerStop(context.TODO(), containerID, container.StopOptions{Timeout: &timeout})
return err
}

View File

@ -430,7 +430,7 @@ func (c *Container) SetTechnology(tech string, version string) error {
output, err = driver.Exec(c.App.Name, []string{"su", "app", "-c", "rosti " + tech + " " + version}, "", []string{}, false)
}
log.Printf("DEBUG: enable tech for %s output: %s", c.App.Name, string(*output))
log.Printf("DEBUG: enable tech %s/%s for %s output: %s", tech, version, c.App.Name, string(*output))
return err
}

View File

@ -422,6 +422,25 @@ func (p *Processor) Update(appTemplate apps.App) error {
return err
}
// Setup technology if it's noted in the request
if len(appTemplate.Setup.Tech) > 0 {
err := p.waitForApp()
if err != nil {
return err
}
err = p.EnableTech(appTemplate.Setup.Tech, appTemplate.Setup.TechVersion)
if err != nil {
return fmt.Errorf("failed to enable tech: %v", err)
}
// We restart the container so everything can use the new tech
err = container.Restart()
if err != nil {
return err
}
}
return nil
}
@ -431,6 +450,7 @@ func (p *Processor) Delete() error {
container, err := p.getContainer()
if err != nil {
log.Println("ERROR: delete app:", err.Error())
return err
}
status, err := container.Status()
@ -634,6 +654,42 @@ 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
}
// Set permissions
err = os.Chmod(path.Join(volumePath, ".metadata.json"), 0600)
if err != nil {
return err
}
// Set owner
err = os.Chown(path.Join(volumePath, ".metadata.json"), ownerUID, ownerGID)
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

@ -3,6 +3,7 @@ package glue
import (
"log"
"github.com/getsentry/sentry-go"
"github.com/jinzhu/gorm"
"github.com/rosti-cz/node-api/apps"
docker "github.com/rosti-cz/node-api/containers"
@ -102,6 +103,7 @@ func (s *StatsProcessor) GatherStats() error {
for _, app := range appList {
err := s.UpdateUsage(app.Name)
if err != nil {
sentry.CaptureException(err)
log.Println("STATS ERROR:", err.Error())
}
}
@ -120,6 +122,7 @@ func (s *StatsProcessor) GatherStates() error {
for _, app := range appList {
err := s.UpdateState(app.Name)
if err != nil {
sentry.CaptureException(err)
log.Println("STATE ERROR:", err.Error())
}
}

View File

@ -2,6 +2,9 @@ package glue
import "github.com/rosti-cz/node-api/apps"
const ownerUID = 1000
const ownerGID = 1000
// Path where authorized keys are
const sshPubKeysLocation = "/srv/.ssh/authorized_keys"

23
go.mod
View File

@ -3,30 +3,31 @@ module github.com/rosti-cz/node-api
go 1.14
require (
github.com/Microsoft/go-winio v0.4.18 // indirect
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 // indirect
github.com/containerd/containerd v1.5.9 // indirect
github.com/docker/docker v20.10.12+incompatible
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/docker v25.0.3+incompatible
github.com/docker/go-connections v0.4.0
github.com/docker/go-units v0.5.0 // indirect
github.com/getsentry/sentry-go v0.26.0
github.com/gobuffalo/packr v1.30.1
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/jinzhu/gorm v1.9.14
github.com/kelseyhightower/envconfig v1.4.0
github.com/labstack/echo/v4 v4.10.0
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/minio/minio-go/v7 v7.0.14
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/nats-io/nats.go v1.23.0
github.com/opencontainers/image-spec v1.0.2
github.com/pkg/errors v0.9.1
github.com/satori/go.uuid v1.2.0
github.com/shirou/gopsutil v2.20.6+incompatible
github.com/stretchr/testify v1.8.2
google.golang.org/grpc v1.44.0 // indirect
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.24.0 // indirect
gorm.io/driver/mysql v1.4.7
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55
gotest.tools/v3 v3.1.0 // indirect
gotest.tools/v3 v3.5.1 // indirect
)

2481
go.sum

File diff suppressed because it is too large Load Diff

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/metadata", saveMetadataHandler)
// Adds new label
e.POST("/v1/apps/:name/labels", addLabelHandler)