2022-02-05 23:01:05 +00:00
|
|
|
package glue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2022-04-21 20:31:58 +00:00
|
|
|
"strings"
|
2022-02-05 23:01:05 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
"github.com/rosti-cz/node-api/apps"
|
|
|
|
"github.com/rosti-cz/node-api/containers"
|
|
|
|
docker "github.com/rosti-cz/node-api/containers"
|
|
|
|
"github.com/rosti-cz/node-api/detector"
|
|
|
|
"github.com/rosti-cz/node-api/node"
|
|
|
|
)
|
|
|
|
|
2022-06-15 18:16:20 +00:00
|
|
|
// Wait for the container a little bit longer
|
|
|
|
const ENABLE_TECH_WAIT = 10
|
|
|
|
|
2022-02-05 23:01:05 +00:00
|
|
|
// Processor separates logic of apps, containers, detector and node from handlers.
|
|
|
|
// It defines common interface for both types of handlers, HTTP and the events.
|
|
|
|
type Processor struct {
|
|
|
|
AppName string
|
|
|
|
DB *gorm.DB
|
|
|
|
SnapshotProcessor *apps.SnapshotProcessor
|
2022-02-07 16:00:16 +00:00
|
|
|
NodeProcessor *node.Processor
|
2022-02-05 23:01:05 +00:00
|
|
|
WaitForAppLoops uint // each loop is five seconds
|
2022-02-05 23:49:32 +00:00
|
|
|
DockerSock string
|
|
|
|
BindIPHTTP string
|
|
|
|
BindIPSSH string
|
|
|
|
AppsPath string
|
2022-02-05 23:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return prepared Container instance
|
|
|
|
func (p *Processor) getContainer() (containers.Container, error) {
|
|
|
|
container := containers.Container{}
|
|
|
|
|
2022-02-07 16:00:16 +00:00
|
|
|
processor := p.getAppProcessor()
|
2022-02-05 23:01:05 +00:00
|
|
|
app, err := processor.Get(p.AppName)
|
|
|
|
if err != nil {
|
|
|
|
return container, err
|
|
|
|
}
|
|
|
|
|
|
|
|
container = docker.Container{
|
2022-02-05 23:49:32 +00:00
|
|
|
App: &app,
|
|
|
|
DockerSock: p.DockerSock,
|
|
|
|
BindIPHTTP: p.BindIPHTTP,
|
|
|
|
BindIPSSH: p.BindIPSSH,
|
|
|
|
AppsPath: p.AppsPath,
|
2022-02-05 23:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return container, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns instance of getAppProcessor
|
|
|
|
func (p *Processor) getAppProcessor() apps.AppsProcessor {
|
2022-02-07 16:00:16 +00:00
|
|
|
processor := apps.AppsProcessor{
|
2022-02-05 23:01:05 +00:00
|
|
|
DB: p.DB,
|
|
|
|
}
|
2022-02-07 16:00:16 +00:00
|
|
|
processor.Init()
|
|
|
|
return processor
|
2022-02-05 23:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// waits until app is ready
|
|
|
|
func (p *Processor) waitForApp() error {
|
|
|
|
sleepFor := 5 * time.Second
|
|
|
|
loops := 6
|
|
|
|
if p.WaitForAppLoops != 0 {
|
|
|
|
loops = int(p.WaitForAppLoops)
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:00:16 +00:00
|
|
|
statsProcessor := StatsProcessor{
|
2022-02-07 16:12:23 +00:00
|
|
|
DB: p.DB,
|
|
|
|
DockerSock: p.DockerSock,
|
2022-02-07 16:18:44 +00:00
|
|
|
BindIPHTTP: p.BindIPHTTP,
|
|
|
|
BindIPSSH: p.BindIPSSH,
|
|
|
|
AppsPath: p.AppsPath,
|
2022-02-07 16:00:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 23:01:05 +00:00
|
|
|
for i := 0; i < loops; i++ {
|
2022-02-07 16:00:16 +00:00
|
|
|
err := statsProcessor.UpdateState(p.AppName)
|
2022-02-05 23:01:05 +00:00
|
|
|
if err != nil {
|
|
|
|
time.Sleep(sleepFor)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
container, err := p.getContainer()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
status, err := container.Status()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-04 17:54:26 +00:00
|
|
|
if status.Status == "running" {
|
2022-02-05 23:01:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(sleepFor)
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("timeout reached")
|
|
|
|
}
|
|
|
|
|
|
|
|
// List returns list of apps
|
2022-05-06 16:40:22 +00:00
|
|
|
// noUpdate skips stats gathering to speed things up
|
|
|
|
func (p *Processor) List(noUpdate bool) (apps.Apps, error) {
|
2022-02-05 23:01:05 +00:00
|
|
|
appList := apps.Apps{}
|
|
|
|
|
2022-05-06 16:40:22 +00:00
|
|
|
if !noUpdate {
|
|
|
|
statsProcessor := StatsProcessor{
|
|
|
|
DB: p.DB,
|
|
|
|
DockerSock: p.DockerSock,
|
|
|
|
BindIPHTTP: p.BindIPHTTP,
|
|
|
|
BindIPSSH: p.BindIPSSH,
|
|
|
|
AppsPath: p.AppsPath,
|
|
|
|
}
|
2022-02-07 16:00:16 +00:00
|
|
|
|
2022-05-06 16:40:22 +00:00
|
|
|
err := statsProcessor.GatherStates()
|
|
|
|
if err != nil {
|
|
|
|
return appList, fmt.Errorf("backend error: %v", err)
|
|
|
|
}
|
2022-02-05 23:01:05 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 16:00:16 +00:00
|
|
|
processor := p.getAppProcessor()
|
2022-05-06 16:40:22 +00:00
|
|
|
appList, err := processor.List()
|
2022-02-05 23:01:05 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return appList, fmt.Errorf("backend error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return appList, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns one app
|
2022-05-27 16:24:39 +00:00
|
|
|
func (p *Processor) Get(noUpdate bool) (apps.App, error) {
|
2022-02-05 23:01:05 +00:00
|
|
|
app := apps.App{}
|
|
|
|
|
2022-05-27 16:24:39 +00:00
|
|
|
if !noUpdate {
|
|
|
|
statsProcessor := StatsProcessor{
|
|
|
|
DB: p.DB,
|
|
|
|
DockerSock: p.DockerSock,
|
|
|
|
BindIPHTTP: p.BindIPHTTP,
|
|
|
|
BindIPSSH: p.BindIPSSH,
|
|
|
|
AppsPath: p.AppsPath,
|
|
|
|
}
|
2022-02-07 16:00:16 +00:00
|
|
|
|
2022-05-27 16:24:39 +00:00
|
|
|
err := statsProcessor.UpdateState(p.AppName)
|
|
|
|
if err != nil {
|
|
|
|
return app, err
|
|
|
|
}
|
2022-02-05 23:01:05 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 16:00:16 +00:00
|
|
|
processor := p.getAppProcessor()
|
2022-05-27 16:24:39 +00:00
|
|
|
app, err := processor.Get(p.AppName)
|
2022-02-05 23:01:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return app, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gather runtime info about the container
|
2022-05-27 16:24:39 +00:00
|
|
|
if !noUpdate {
|
|
|
|
container := docker.Container{
|
|
|
|
App: &app,
|
|
|
|
DockerSock: p.DockerSock,
|
|
|
|
BindIPHTTP: p.BindIPHTTP,
|
|
|
|
BindIPSSH: p.BindIPSSH,
|
|
|
|
AppsPath: p.AppsPath,
|
2022-02-05 23:01:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 16:24:39 +00:00
|
|
|
status, err := container.Status()
|
2022-02-05 23:01:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return app, err
|
|
|
|
}
|
2022-10-04 17:54:26 +00:00
|
|
|
if status.Status == "running" {
|
2022-05-27 16:24:39 +00:00
|
|
|
var err error
|
|
|
|
app.Techs, err = container.GetTechs()
|
|
|
|
if err != nil {
|
|
|
|
return app, err
|
|
|
|
}
|
|
|
|
app.PrimaryTech, err = container.GetPrimaryTech()
|
|
|
|
if err != nil {
|
|
|
|
return app, err
|
|
|
|
}
|
|
|
|
|
|
|
|
processList, err := container.GetSystemProcesses()
|
|
|
|
if err != nil {
|
|
|
|
return app, err
|
|
|
|
}
|
|
|
|
|
|
|
|
flags, err := detector.Check(processList)
|
|
|
|
if err != nil {
|
|
|
|
return app, err
|
|
|
|
}
|
|
|
|
app.Flags = flags.String()
|
2022-02-05 23:01:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return app, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates a single app in the system
|
|
|
|
func (p *Processor) Create(appTemplate apps.App) error {
|
|
|
|
err := p.Register(appTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
container := docker.Container{
|
2022-02-06 01:02:30 +00:00
|
|
|
App: &appTemplate,
|
|
|
|
DockerSock: p.DockerSock,
|
|
|
|
BindIPHTTP: p.BindIPHTTP,
|
|
|
|
BindIPSSH: p.BindIPSSH,
|
|
|
|
AppsPath: p.AppsPath,
|
2022-02-05 23:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = container.Create()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore from snapshot if it's noted in the request
|
|
|
|
if len(appTemplate.Snapshot) > 0 {
|
|
|
|
log.Printf("App %s is going to be created from %s snapshot\n", appTemplate.Name, appTemplate.Snapshot)
|
|
|
|
|
|
|
|
// Restore the data
|
|
|
|
err = p.SnapshotProcessor.RestoreSnapshot(appTemplate.Snapshot, appTemplate.Name)
|
|
|
|
if err != nil {
|
2023-02-04 09:33:47 +00:00
|
|
|
return fmt.Errorf("failed to restore snapshot: %v", err)
|
2022-02-05 23:01:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-04 23:42:49 +00:00
|
|
|
err = container.Start()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to start container: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-02-04 09:33:47 +00:00
|
|
|
// Wait for the app to be created
|
|
|
|
err = p.waitForApp()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to wait for app: %v", err)
|
|
|
|
}
|
|
|
|
time.Sleep(5 * time.Second) // We wait for a little bit longer to make sure the container is fully started
|
|
|
|
|
|
|
|
// Setup SSH keys if it's noted in the request
|
|
|
|
if len(appTemplate.Setup.SSHKeys) > 0 && len(appTemplate.Snapshot) == 0 {
|
|
|
|
err = p.UpdateKeys(appTemplate.Setup.SSHKeys)
|
2023-01-28 10:46:59 +00:00
|
|
|
if err != nil {
|
2023-02-04 09:33:47 +00:00
|
|
|
return fmt.Errorf("failed to update keys: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup technology if it's noted in the request
|
|
|
|
if len(appTemplate.Setup.Tech) > 0 && len(appTemplate.Snapshot) == 0 {
|
|
|
|
err = p.EnableTech(appTemplate.Setup.Tech, appTemplate.Setup.TechVersion)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to enable tech: %v", err)
|
2023-01-28 10:46:59 +00:00
|
|
|
}
|
2023-02-04 09:33:47 +00:00
|
|
|
}
|
2023-01-28 10:46:59 +00:00
|
|
|
|
2023-02-04 09:33:47 +00:00
|
|
|
// Set password if it's noted in the request
|
|
|
|
if len(appTemplate.Setup.Password) > 0 && len(appTemplate.Snapshot) == 0 {
|
|
|
|
err = p.SetPassword(appTemplate.Setup.Password)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to set password: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Changes port of the app hosted inside the container
|
|
|
|
if appTemplate.AppPort != 0 && len(appTemplate.Snapshot) == 0 {
|
2023-01-28 10:46:59 +00:00
|
|
|
err = container.SetAppPort(appTemplate.AppPort)
|
|
|
|
if err != nil {
|
2023-02-04 09:33:47 +00:00
|
|
|
return fmt.Errorf("failed to change app port to %d: %v", appTemplate.AppPort, err)
|
2023-01-28 10:46:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = container.RestartProcess("nginx")
|
|
|
|
if err != nil {
|
2023-02-04 09:33:47 +00:00
|
|
|
return fmt.Errorf("failed to restart nginx: %v", err)
|
2023-01-28 10:46:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-04 23:42:49 +00:00
|
|
|
return nil
|
2022-02-05 23:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register registers app without creating a container for it
|
|
|
|
// Returns app name and an error
|
|
|
|
func (p *Processor) Register(appTemplate apps.App) error {
|
2022-02-07 16:00:16 +00:00
|
|
|
processor := p.getAppProcessor()
|
2022-02-05 23:01:05 +00:00
|
|
|
err := processor.New(appTemplate.Name, appTemplate.SSHPort, appTemplate.HTTPPort, appTemplate.Image, appTemplate.CPU, appTemplate.Memory)
|
|
|
|
if err != nil {
|
|
|
|
if validationError, ok := err.(apps.ValidationError); ok {
|
|
|
|
return fmt.Errorf("validation error: %v", validationError.Error())
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates application
|
|
|
|
func (p *Processor) Update(appTemplate apps.App) error {
|
2022-02-07 16:00:16 +00:00
|
|
|
processor := p.getAppProcessor()
|
2022-02-05 23:01:05 +00:00
|
|
|
app, err := processor.Update(appTemplate.Name, appTemplate.SSHPort, appTemplate.HTTPPort, appTemplate.Image, appTemplate.CPU, appTemplate.Memory)
|
|
|
|
if err != nil {
|
|
|
|
if validationError, ok := err.(apps.ValidationError); ok {
|
|
|
|
return fmt.Errorf("validation error: %v", validationError.Error())
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
container := docker.Container{
|
2022-02-06 01:02:30 +00:00
|
|
|
App: app,
|
|
|
|
DockerSock: p.DockerSock,
|
|
|
|
BindIPHTTP: p.BindIPHTTP,
|
|
|
|
BindIPSSH: p.BindIPSSH,
|
|
|
|
AppsPath: p.AppsPath,
|
2022-02-05 23:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = container.Destroy()
|
|
|
|
if err != nil && err.Error() == "no container found" {
|
|
|
|
// We don't care if the container didn't exist anyway
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = container.Create()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = container.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes app from the system
|
|
|
|
func (p *Processor) Delete() error {
|
2022-02-07 16:00:16 +00:00
|
|
|
processor := p.getAppProcessor()
|
2022-02-06 01:02:30 +00:00
|
|
|
container, err := p.getContainer()
|
2022-02-05 23:01:05 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("ERROR: delete app:", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
status, err := container.Status()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-04 17:54:26 +00:00
|
|
|
if status.Status != "no-container" {
|
2022-02-05 23:01:05 +00:00
|
|
|
// We stop the container first
|
|
|
|
err = container.Stop()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then delete it
|
|
|
|
err = container.Delete()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 01:02:30 +00:00
|
|
|
err = processor.Delete(p.AppName)
|
2022-02-05 23:01:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops app
|
|
|
|
func (p *Processor) Stop() error {
|
2022-02-06 01:02:30 +00:00
|
|
|
container, err := p.getContainer()
|
2022-05-27 17:12:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-05 23:01:05 +00:00
|
|
|
|
|
|
|
status, err := container.Status()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the container only when it exists
|
2022-10-04 17:54:26 +00:00
|
|
|
if status.Status != "no-container" {
|
2022-02-05 23:01:05 +00:00
|
|
|
err = container.Stop()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-27 17:12:19 +00:00
|
|
|
|
|
|
|
err = container.Destroy()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-05 23:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts app
|
|
|
|
func (p *Processor) Start() error {
|
2022-02-06 01:02:30 +00:00
|
|
|
container, err := p.getContainer()
|
2022-02-05 23:01:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
status, err := container.Status()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-04 17:54:26 +00:00
|
|
|
if status.Status == "no-container" {
|
2022-02-05 23:01:05 +00:00
|
|
|
err = container.Create()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = container.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restart restarts app
|
|
|
|
func (p *Processor) Restart() error {
|
2022-02-06 01:02:30 +00:00
|
|
|
container, err := p.getContainer()
|
2022-02-05 23:01:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = container.Restart()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateKeys uploads SSH keys into the container
|
|
|
|
// keys parameters is just a string, one key per line
|
|
|
|
func (p *Processor) UpdateKeys(keys string) error {
|
|
|
|
err := p.waitForApp()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
container, err := p.getContainer()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = container.SetFileContent(sshPubKeysLocation, keys+"\n", "0600")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPassword sets up a new password to access the SSH user
|
|
|
|
func (p *Processor) SetPassword(password string) error {
|
|
|
|
err := p.waitForApp()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
container, err := p.getContainer()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = container.SetPassword(password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-11 20:49:50 +00:00
|
|
|
// Generate SSH key and adds it into authorized_keys
|
|
|
|
// These pair of keys is used for deployment.
|
|
|
|
// Returns private key, pubkey and error.
|
|
|
|
// Keys are returned every time even if it was already generated
|
|
|
|
func (p *Processor) GenerateDeploySSHKeys() (string, string, error) {
|
2023-04-21 10:01:34 +00:00
|
|
|
container, err := p.getContainer()
|
2023-04-11 20:49:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2023-04-21 10:01:34 +00:00
|
|
|
// If the container is not running we skip this code
|
|
|
|
status, err := container.Status()
|
2023-04-11 20:49:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
2023-04-21 10:01:34 +00:00
|
|
|
if status.Status != "running" {
|
|
|
|
return "", "", nil
|
|
|
|
}
|
2023-04-11 20:49:50 +00:00
|
|
|
|
|
|
|
created, err := container.GenerateDeploySSHKeys()
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
privateKey, pubKey, err := container.GetDeploySSHKeys()
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if created {
|
2023-04-13 20:49:06 +00:00
|
|
|
err = container.AppendFile(sshPubKeysLocation, pubKey+"\n", "0600")
|
2023-04-11 20:49:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return privateKey, pubKey, nil
|
|
|
|
}
|
|
|
|
|
2023-04-11 20:56:15 +00:00
|
|
|
// Return SSH host key without hostname (first part of the line)
|
|
|
|
func (p *Processor) GetHostKey() (string, error) {
|
2023-04-21 10:01:34 +00:00
|
|
|
container, err := p.getContainer()
|
2023-04-11 20:56:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-04-21 10:01:34 +00:00
|
|
|
// If the container is not running we skip this code
|
|
|
|
status, err := container.Status()
|
2023-04-11 20:56:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-04-21 10:01:34 +00:00
|
|
|
if status.Status != "running" {
|
|
|
|
return "", nil
|
|
|
|
}
|
2023-04-11 20:56:15 +00:00
|
|
|
|
|
|
|
hostKey, err := container.GetHostKey()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return hostKey, nil
|
|
|
|
}
|
|
|
|
|
2022-02-05 23:01:05 +00:00
|
|
|
// Processes returns list of supervisord processes
|
|
|
|
func (p *Processor) Processes() ([]docker.Process, error) {
|
|
|
|
container, err := p.getContainer()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
processes, err := container.GetProcessList()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return processes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnableTech sets up runtime for new tech or new version a tech
|
|
|
|
func (p *Processor) EnableTech(service, version string) error {
|
|
|
|
err := p.waitForApp()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-15 18:16:20 +00:00
|
|
|
time.Sleep(ENABLE_TECH_WAIT * time.Second)
|
|
|
|
|
2022-02-05 23:01:05 +00:00
|
|
|
container, err := p.getContainer()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = container.SetTechnology(service, version)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rebuild recreates container for app
|
|
|
|
func (p *Processor) Rebuild() error {
|
|
|
|
container, err := p.getContainer()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = container.Destroy()
|
2022-04-21 20:31:58 +00:00
|
|
|
if err != nil && !strings.Contains(err.Error(), "no container found") {
|
2022-02-05 23:01:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = container.Create()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = container.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddLabel adds a label to the app
|
|
|
|
func (p *Processor) AddLabel(label string) error {
|
|
|
|
appProcessor := p.getAppProcessor()
|
|
|
|
err := appProcessor.AddLabel(p.AppName, label)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveLabel removes a label from the app
|
|
|
|
func (p *Processor) RemoveLabel(label string) error {
|
|
|
|
appProcessor := p.getAppProcessor()
|
|
|
|
err := appProcessor.RemoveLabel(p.AppName, label)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNote returns node's info
|
|
|
|
func (p *Processor) GetNode() (*node.Node, error) {
|
2022-02-07 16:00:16 +00:00
|
|
|
node, err := p.NodeProcessor.GetNodeInfo()
|
2022-02-05 23:01:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSnapshot creates a snapshot of given app
|
|
|
|
func (p *Processor) CreateSnapshot(labels []string) error {
|
|
|
|
_, err := p.SnapshotProcessor.CreateSnapshot(p.AppName, labels)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RestoreFromSnapshot restores app from given snapshot
|
|
|
|
func (p *Processor) RestoreFromSnapshot(snapshotName string) error {
|
|
|
|
container, err := p.getContainer()
|
|
|
|
|
|
|
|
// Stop the container
|
|
|
|
status, err := container.Status()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the container only when it exists
|
2022-10-04 17:54:26 +00:00
|
|
|
if status.Status != "no-container" {
|
2022-02-05 23:01:05 +00:00
|
|
|
err = container.Stop()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore the data
|
|
|
|
err = p.SnapshotProcessor.RestoreSnapshot(snapshotName, p.AppName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the container
|
|
|
|
status, err = container.Status()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-04 17:54:26 +00:00
|
|
|
if status.Status == "no-container" {
|
2022-02-05 23:01:05 +00:00
|
|
|
err = container.Create()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = container.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListSnapshots returns list of snapshots
|
|
|
|
func (p *Processor) ListSnapshots() (SnapshotsMetadata, error) {
|
|
|
|
snapshots, err := p.SnapshotProcessor.ListAppSnapshots(p.AppName)
|
|
|
|
if err != nil {
|
|
|
|
return SnapshotsMetadata{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var output SnapshotsMetadata
|
|
|
|
for _, snapshot := range snapshots {
|
|
|
|
output = append(output, SnapshotMetadata{
|
|
|
|
Key: snapshot.KeyName(p.SnapshotProcessor.IndexLabel),
|
|
|
|
Metadata: snapshot,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return output, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListAppsSnapshots returns list of snapshots for given app list
|
|
|
|
func (p *Processor) ListAppsSnapshots(appNames []string) (SnapshotsMetadata, error) {
|
|
|
|
snapshots, err := p.SnapshotProcessor.ListAppsSnapshots(appNames)
|
|
|
|
if err != nil {
|
|
|
|
return SnapshotsMetadata{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var output SnapshotsMetadata
|
|
|
|
for _, snapshot := range snapshots {
|
|
|
|
output = append(output, SnapshotMetadata{
|
|
|
|
Key: snapshot.KeyName(p.SnapshotProcessor.IndexLabel),
|
|
|
|
Metadata: snapshot,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return output, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListSnapshotsByLabel returns list of snapshots by given label
|
|
|
|
func (p *Processor) ListSnapshotsByLabel(label string) (SnapshotsMetadata, error) {
|
|
|
|
snapshots, err := p.SnapshotProcessor.ListAppsSnapshotsByLabel(label)
|
|
|
|
if err != nil {
|
|
|
|
return SnapshotsMetadata{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var output SnapshotsMetadata
|
|
|
|
for _, snapshot := range snapshots {
|
|
|
|
output = append(output, SnapshotMetadata{
|
|
|
|
Key: snapshot.KeyName(p.SnapshotProcessor.IndexLabel),
|
|
|
|
Metadata: snapshot,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return output, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSnapshot returns info about a single snapshot
|
|
|
|
func (p *Processor) GetSnapshot(snapshotName string) (SnapshotMetadata, error) {
|
|
|
|
snapshot, err := p.SnapshotProcessor.GetSnapshot(snapshotName)
|
|
|
|
if err != nil {
|
|
|
|
return SnapshotMetadata{}, err
|
|
|
|
}
|
|
|
|
output := SnapshotMetadata{
|
|
|
|
Key: snapshot.KeyName(p.SnapshotProcessor.IndexLabel),
|
|
|
|
Metadata: snapshot,
|
|
|
|
}
|
|
|
|
|
|
|
|
return output, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSnapshotDownloadLink return download link for a snapshot
|
|
|
|
func (p *Processor) GetSnapshotDownloadLink(snapshotName string) (string, error) {
|
|
|
|
link, err := p.SnapshotProcessor.GetDownloadLink(snapshotName)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return link, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteSnapshot deletes a snapshot
|
|
|
|
func (p *Processor) DeleteSnapshot(snapshotName string) error {
|
|
|
|
err := p.SnapshotProcessor.DeleteSnapshot(snapshotName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteAppSnapshots deletes all snapshot of given app
|
|
|
|
func (p *Processor) DeleteAppSnapshots() error {
|
|
|
|
err := p.SnapshotProcessor.DeleteAppSnapshots(p.AppName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-24 12:19:58 +00:00
|
|
|
|
|
|
|
// Returns active technology in the app
|
|
|
|
func (p *Processor) GetActiveTech() (*containers.TechInfo, error) {
|
|
|
|
container, err := p.getContainer()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tech, err := container.GetActiveTech()
|
|
|
|
if err != nil {
|
|
|
|
return tech, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tech, nil
|
|
|
|
}
|