From 5f80da8cbd5eba37bcdda7013c04f613ceba7f05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C5=A0trauch?= Date: Sat, 25 May 2024 14:19:03 +0200 Subject: [PATCH] Is password set field --- apps/main.go | 3 ++- apps/main_test.go | 3 ++- apps/types.go | 4 +++- containers/types.go | 21 +++++++++++++++++++++ glue/stats.go | 1 + 5 files changed, 29 insertions(+), 3 deletions(-) diff --git a/apps/main.go b/apps/main.go index b82a98a..32da32d 100644 --- a/apps/main.go +++ b/apps/main.go @@ -115,7 +115,7 @@ func (a *AppsProcessor) Update(name string, SSHPort int, HTTPPort int, image str } // UpdateResources updates various metrics saved in the database -func (a *AppsProcessor) UpdateResources(name string, state string, OOMKilled bool, CPUUsage float64, memory int, diskUsageBytes int, diskUsageInodes int, flags detector.Flags) error { +func (a *AppsProcessor) UpdateResources(name string, state string, OOMKilled bool, CPUUsage float64, memory int, diskUsageBytes int, diskUsageInodes int, flags detector.Flags, isPasswordSet bool) error { err := a.DB.Model(&App{}).Where("name = ?", name).Updates(App{ State: state, OOMKilled: OOMKilled, @@ -124,6 +124,7 @@ func (a *AppsProcessor) UpdateResources(name string, state string, OOMKilled boo DiskUsageBytes: diskUsageBytes, DiskUsageInodes: diskUsageInodes, Flags: flags.String(), + IsPasswordSet: isPasswordSet, }).Error return err } diff --git a/apps/main_test.go b/apps/main_test.go index e7e2b94..9820605 100644 --- a/apps/main_test.go +++ b/apps/main_test.go @@ -95,7 +95,7 @@ func TestAppsProcessorUpdateResources(t *testing.T) { err := processor.New("updateresources_1224", 1002, 1003, "testimage", 2, 256) assert.Nil(t, err) - err = processor.UpdateResources("updateresources_1224", "running", true, 1000, 256, 100, 200, detector.Flags{"test"}) + err = processor.UpdateResources("updateresources_1224", "running", true, 1000, 256, 100, 200, detector.Flags{"test"}, true) assert.Nil(t, err) app, err := processor.Get("updateresources_1224") @@ -107,6 +107,7 @@ func TestAppsProcessorUpdateResources(t *testing.T) { assert.Equal(t, 256, app.MemoryUsage) assert.Equal(t, 100, app.DiskUsageBytes) assert.Equal(t, 200, app.DiskUsageInodes) + assert.Equal(t, true, app.IsPasswordSet) assert.Contains(t, app.Flags, "test") } diff --git a/apps/types.go b/apps/types.go index f768a01..8ac8ff4 100644 --- a/apps/types.go +++ b/apps/types.go @@ -38,6 +38,7 @@ type AppState struct { DiskUsageBytes int `json:"disk_usage_bytes"` DiskUsageInodes int `json:"disk_usage_inodes"` Flags detector.Flags `json:"flags"` + IsPasswordSet bool `json:"is_password_set"` } // Apps is list of applications @@ -94,7 +95,8 @@ type App struct { // Disk usage in inodes DiskUsageInodes int `json:"disk_usage_inodes"` // Flags from detector of problems in the container - Flags string `json:"flags"` // flags are separated by comma + Flags string `json:"flags"` // flags are separated by comma + IsPasswordSet bool `json:"is_password_set"` // True if the password is set in the container (file with the password exists) // this is gathered in docker package and has to be assembled externally Techs AppTechs `json:"techs,omitempty" gorm:"-"` // list of available technologies in the image diff --git a/containers/types.go b/containers/types.go index 30e71c0..53c127f 100644 --- a/containers/types.go +++ b/containers/types.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "log" + "os" "path" "strconv" "strings" @@ -17,6 +18,7 @@ import ( const appUsername = "app" const owner = "app:app" const passwordFile = "/srv/.rosti" +const passwordFileNoPath = ".rosti" const deployKeyType = "ed25519" const deployKeyPrefix = "rosti_deploy" @@ -96,6 +98,11 @@ func (c *Container) GetState() (*apps.AppState, error) { return nil, err } + isPasswordSet, err := c.IsPasswordSet() + if err != nil { + return nil, err + } + state := apps.AppState{ State: status.Status, OOMKilled: status.OOMKilled, @@ -106,6 +113,7 @@ func (c *Container) GetState() (*apps.AppState, error) { DiskUsageBytes: bytes, DiskUsageInodes: inodes, Flags: flags, + IsPasswordSet: isPasswordSet, } return &state, nil @@ -135,6 +143,19 @@ func (c *Container) DiskUsage() (int, int, error) { return du(c.VolumeHostPath()) } +// IsPasswordSet returns true if the password is set for the container (file with the password exists) +func (c *Container) IsPasswordSet() (bool, error) { + _, err := os.Stat(path.Join(c.VolumeHostPath(), passwordFileNoPath)) + if err != nil && os.IsNotExist(err) { + return false, nil + } + if err != nil { + return false, err + } + + return true, nil +} + // ResourceUsage returns amount of memory in B and CPU in % that the app occupies func (c *Container) ResourceUsage() (float64, int, error) { driver := c.getDriver() diff --git a/glue/stats.go b/glue/stats.go index fe9b377..0bd8d54 100644 --- a/glue/stats.go +++ b/glue/stats.go @@ -59,6 +59,7 @@ func (s *StatsProcessor) UpdateUsage(name string) error { state.DiskUsageBytes, state.DiskUsageInodes, state.Flags, + state.IsPasswordSet, ) return err