2020-07-08 22:09:19 +00:00
|
|
|
package apps
|
|
|
|
|
2020-07-11 11:04:37 +00:00
|
|
|
import (
|
2021-10-02 18:00:35 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
2020-07-11 11:04:37 +00:00
|
|
|
)
|
2020-07-08 22:09:19 +00:00
|
|
|
|
2021-10-02 18:00:35 +00:00
|
|
|
// AppsProcessor encapsulates functions for apps manipulation
|
|
|
|
type AppsProcessor struct {
|
|
|
|
DB *gorm.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppsProcessor) Init() {
|
|
|
|
a.DB.AutoMigrate(Label{})
|
|
|
|
a.DB.AutoMigrate(App{})
|
2020-07-08 22:09:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns one app
|
2021-10-02 18:00:35 +00:00
|
|
|
func (a *AppsProcessor) Get(name string) (*App, error) {
|
2020-07-08 22:09:19 +00:00
|
|
|
var app App
|
|
|
|
|
2021-10-02 18:00:35 +00:00
|
|
|
err := a.DB.Preload("Labels").Where("name = ?", name).First(&app).Error
|
2020-07-08 22:09:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &app, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// List returns all apps located on this node
|
2021-10-02 18:00:35 +00:00
|
|
|
func (a *AppsProcessor) List() (*Apps, error) {
|
2020-07-30 20:46:09 +00:00
|
|
|
var apps Apps
|
2020-07-08 22:09:19 +00:00
|
|
|
|
2021-10-02 18:00:35 +00:00
|
|
|
err := a.DB.Preload("Labels").Find(&apps).Error
|
2020-07-08 22:09:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &apps, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates new record about application in the database
|
2021-10-02 18:00:35 +00:00
|
|
|
func (a *AppsProcessor) New(name string, SSHPort int, HTTPPort int, image string, CPU int, memory int) error {
|
2020-07-08 22:09:19 +00:00
|
|
|
app := App{
|
|
|
|
Name: name,
|
|
|
|
SSHPort: SSHPort,
|
|
|
|
HTTPPort: HTTPPort,
|
|
|
|
Image: image,
|
|
|
|
CPU: CPU,
|
|
|
|
Memory: memory,
|
|
|
|
}
|
|
|
|
|
2020-07-09 22:27:23 +00:00
|
|
|
validationErrors := app.Validate()
|
|
|
|
if len(validationErrors) != 0 {
|
|
|
|
return ValidationError{
|
|
|
|
Errors: validationErrors,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-02 18:00:35 +00:00
|
|
|
if err := a.DB.Create(&app).Error; err != nil {
|
2020-07-08 22:09:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update changes value about app in the database
|
2021-10-02 18:00:35 +00:00
|
|
|
func (a *AppsProcessor) Update(name string, SSHPort int, HTTPPort int, image string, CPU int, memory int) (*App, error) {
|
2020-07-08 22:09:19 +00:00
|
|
|
var app App
|
|
|
|
|
2021-10-02 18:00:35 +00:00
|
|
|
err := a.DB.Where("name = ?", name).First(&app).Error
|
2020-07-08 22:09:19 +00:00
|
|
|
if err != nil {
|
2020-07-11 11:04:37 +00:00
|
|
|
return &app, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update affected fields
|
|
|
|
if image != "" {
|
|
|
|
app.Image = image
|
2020-07-08 22:09:19 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 11:04:37 +00:00
|
|
|
if CPU != 0 {
|
|
|
|
app.CPU = CPU
|
|
|
|
}
|
|
|
|
|
|
|
|
if memory != 0 {
|
|
|
|
app.Memory = memory
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSH port and HTTP port cannot be turned off when they are once set
|
|
|
|
if SSHPort != 0 {
|
|
|
|
app.SSHPort = SSHPort
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSH port and HTTP port cannot be turned off when they are once set
|
|
|
|
if HTTPPort != 0 {
|
|
|
|
app.HTTPPort = HTTPPort
|
|
|
|
}
|
2020-07-09 22:27:23 +00:00
|
|
|
|
|
|
|
validationErrors := app.Validate()
|
|
|
|
if len(validationErrors) != 0 {
|
2020-07-11 11:04:37 +00:00
|
|
|
return &app, ValidationError{
|
2020-07-09 22:27:23 +00:00
|
|
|
Errors: validationErrors,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-11 11:04:37 +00:00
|
|
|
// Apply the changes
|
2021-10-02 18:00:35 +00:00
|
|
|
err = a.DB.Save(&app).Error
|
2020-07-11 11:04:37 +00:00
|
|
|
return &app, err
|
2020-07-08 22:09:19 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 22:34:16 +00:00
|
|
|
// UpdateResources updates various metrics saved in the database
|
2021-10-02 18:00:35 +00:00
|
|
|
func (a *AppsProcessor) UpdateResources(name string, state string, CPUUsage float64, memory int, diskUsageBytes int, diskUsageInodes int) error {
|
|
|
|
err := a.DB.Model(&App{}).Where("name = ?", name).Updates(App{
|
2020-07-16 17:05:38 +00:00
|
|
|
State: state,
|
|
|
|
CPUUsage: CPUUsage,
|
|
|
|
MemoryUsage: memory,
|
|
|
|
DiskUsageBytes: diskUsageBytes,
|
|
|
|
DiskUsageInodes: diskUsageInodes,
|
2020-07-23 21:50:20 +00:00
|
|
|
}).Error
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-25 22:34:16 +00:00
|
|
|
// UpdateState sets container's state
|
2021-10-02 18:00:35 +00:00
|
|
|
func (a *AppsProcessor) UpdateState(name string, state string) error {
|
|
|
|
err := a.DB.Model(&App{}).Where("name = ?", name).Updates(App{
|
2020-07-23 21:50:20 +00:00
|
|
|
State: state,
|
|
|
|
}).Error
|
2020-07-16 17:05:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-08 22:09:19 +00:00
|
|
|
// Delete removes records about one app from the database
|
2021-10-02 18:00:35 +00:00
|
|
|
func (a *AppsProcessor) Delete(name string) error {
|
|
|
|
app, err := a.Get(name)
|
2020-07-23 21:50:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-02 18:00:35 +00:00
|
|
|
err = a.DB.Where("app_id = ?", app.ID).Delete(&Label{}).Error
|
2020-07-23 21:50:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-02 18:00:35 +00:00
|
|
|
err = a.DB.Where("id = ?", app.ID).Delete(App{}).Error
|
2020-07-23 21:50:20 +00:00
|
|
|
|
2020-07-08 22:09:19 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-07-16 21:24:09 +00:00
|
|
|
|
|
|
|
// AddLabel adds label to existing application
|
2021-10-02 18:00:35 +00:00
|
|
|
func (a *AppsProcessor) AddLabel(appName string, label string) error {
|
2020-07-16 21:24:09 +00:00
|
|
|
var app App
|
|
|
|
|
2021-10-02 18:00:35 +00:00
|
|
|
err := a.DB.Where("name = ?", appName).First(&app).Error
|
2020-07-16 21:24:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if there is such label already
|
|
|
|
var count int
|
2021-10-02 18:00:35 +00:00
|
|
|
err = a.DB.Model(&Label{}).Where("value = ? AND app_id = ?", label, app.ID).Count(&count).Error
|
2020-07-16 21:24:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if count > 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// And create the label
|
2021-10-02 18:00:35 +00:00
|
|
|
return a.DB.Create(&Label{AppID: app.ID, Value: label}).Error
|
2020-07-16 21:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveLabel removes label from existing application
|
2021-10-02 18:00:35 +00:00
|
|
|
func (a *AppsProcessor) RemoveLabel(appName string, label string) error {
|
2020-07-16 21:24:09 +00:00
|
|
|
var app App
|
|
|
|
|
2021-10-02 18:00:35 +00:00
|
|
|
err := a.DB.Where("name = ?", appName).First(&app).Error
|
2020-07-16 21:24:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-02 18:00:35 +00:00
|
|
|
return a.DB.Where("label = ? AND app_id = ?", label, app.ID).Delete(&Label{}).Error
|
2020-07-16 21:24:09 +00:00
|
|
|
}
|