From 779d9ba95a954f8d21ac857bf6de2d2ce1ba6b53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C5=A0trauch?= Date: Sat, 4 Feb 2023 10:33:47 +0100 Subject: [PATCH] Set password, ssh keys and tech in single step during create --- apps/types.go | 8 ++++++++ glue/main.go | 46 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/apps/types.go b/apps/types.go index dee31f4..a05f072 100644 --- a/apps/types.go +++ b/apps/types.go @@ -101,6 +101,14 @@ type App struct { // This is not store in the database but used in create request when the app suppose to be created from an existing snapshot Snapshot string `json:"snapshot" gorm:"-"` + + // Fields to setup during creating of the app, this is not stored in the database + Setup struct { + SSHKeys string `json:"ssh_keys"` + Tech string `json:"tech"` + TechVersion string `json:"tech_version"` + Password string `json:"password"` + } `json:"setup,omitempty" gorm:"-"` } // Validate do basic checks of the struct values diff --git a/glue/main.go b/glue/main.go index b55fcc4..ef2bbcf 100644 --- a/glue/main.go +++ b/glue/main.go @@ -227,30 +227,56 @@ func (p *Processor) Create(appTemplate apps.App) error { // Restore the data err = p.SnapshotProcessor.RestoreSnapshot(appTemplate.Snapshot, appTemplate.Name) if err != nil { - return err + return fmt.Errorf("failed to restore snapshot: %v", err) + } + } + + // 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) + if err != nil { + 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) + } + } + + // 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 { - err = p.waitForApp() - if err != nil { - return err - } - + if appTemplate.AppPort != 0 && len(appTemplate.Snapshot) == 0 { err = container.SetAppPort(appTemplate.AppPort) if err != nil { - return err + return fmt.Errorf("failed to change app port to %d: %v", appTemplate.AppPort, err) } err = container.RestartProcess("nginx") if err != nil { - return err + return fmt.Errorf("failed to restart nginx: %v", err) } } err = container.Start() - return err + return fmt.Errorf("failed to start container: %v", err) } // Register registers app without creating a container for it