2020-07-08 22:09:19 +00:00
|
|
|
package apps
|
|
|
|
|
2020-07-09 22:27:23 +00:00
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ValidationError is error that holds multiple validation error messages
|
|
|
|
type ValidationError struct {
|
|
|
|
Errors []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v ValidationError) Error() string {
|
|
|
|
return strings.Join(v.Errors, "\n")
|
|
|
|
}
|
2020-07-08 22:09:19 +00:00
|
|
|
|
|
|
|
// Label holds metadata about the application
|
|
|
|
type Label struct {
|
|
|
|
Value string `json:"value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// App keeps info about hosted application
|
|
|
|
type App struct {
|
|
|
|
gorm.Model
|
|
|
|
|
|
|
|
Name string `json:"name" gorm:"primary_key"`
|
|
|
|
SSHPort int `json:"ssh_port"`
|
|
|
|
HTTPPort int `json:"http_port"`
|
|
|
|
Image string `json:"image"`
|
2020-07-09 22:27:23 +00:00
|
|
|
CPU int `json:"cpu"` // percentage, 200 means two CPU
|
2020-07-08 22:09:19 +00:00
|
|
|
Memory int `json:"memory"` // Limit in MB
|
|
|
|
Labels []Label `json:"labels"` // username:cx or user_id:1
|
|
|
|
|
2020-07-09 22:27:23 +00:00
|
|
|
Status string `json:"status"` // running, data-only (no container, data only), stopped
|
|
|
|
MemoryUsage int `json:"memory_usage"` // Usage in MB
|
|
|
|
DiskUsage int `json:"disk_usage"` // Usage in MB
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate do basic checks of the struct values
|
|
|
|
func (a *App) Validate() []string {
|
|
|
|
var errors []string
|
|
|
|
|
|
|
|
nameRegExp := regexp.MustCompile(`^[a-z0-9_]{3,48}$`)
|
|
|
|
if !nameRegExp.MatchString(a.Name) {
|
|
|
|
errors = append(errors, "name can contain only characters, numbers and underscores and has to be between 3 and 48 characters")
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.SSHPort < 0 && a.SSHPort > 65536 {
|
|
|
|
errors = append(errors, "SSH port has to be between 0 and 65536, where 0 means disabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.HTTPPort < 1 && a.HTTPPort > 65536 {
|
|
|
|
errors = append(errors, "HTTP port has to be between 1 and 65536")
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.Image == "" {
|
|
|
|
errors = append(errors, "image cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.CPU < 10 && a.CPU > 800 {
|
|
|
|
errors = append(errors, "CPU value has be between 10 and 800")
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.Memory < 32 && a.Memory > 16384 {
|
|
|
|
errors = append(errors, "Memory value has be between 32 and 16384")
|
|
|
|
}
|
2020-07-08 22:09:19 +00:00
|
|
|
|
2020-07-09 22:27:23 +00:00
|
|
|
return errors
|
2020-07-08 22:09:19 +00:00
|
|
|
}
|