2021-11-03 08:10:13 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"coopcloud.tech/abra/pkg/config"
|
2021-12-28 00:24:23 +00:00
|
|
|
"coopcloud.tech/abra/pkg/formatter"
|
2021-11-03 08:10:13 +00:00
|
|
|
"coopcloud.tech/abra/pkg/recipe"
|
2021-12-27 03:07:52 +00:00
|
|
|
recipePkg "coopcloud.tech/abra/pkg/recipe"
|
2021-11-03 08:10:13 +00:00
|
|
|
"coopcloud.tech/abra/pkg/secret"
|
2021-11-09 16:43:24 +00:00
|
|
|
"coopcloud.tech/abra/pkg/ssh"
|
2021-11-03 08:10:13 +00:00
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
|
|
"github.com/sirupsen/logrus"
|
2022-01-18 13:13:20 +00:00
|
|
|
"github.com/urfave/cli"
|
2021-11-03 08:10:13 +00:00
|
|
|
)
|
|
|
|
|
2021-12-19 15:19:42 +00:00
|
|
|
// AppSecrets represents all app secrest
|
2021-11-03 08:10:13 +00:00
|
|
|
type AppSecrets map[string]string
|
|
|
|
|
2021-11-03 08:20:40 +00:00
|
|
|
// RecipeName is used for configuring recipe name programmatically
|
2021-11-03 08:10:13 +00:00
|
|
|
var RecipeName string
|
|
|
|
|
|
|
|
// createSecrets creates all secrets for a new app.
|
|
|
|
func createSecrets(sanitisedAppName string) (AppSecrets, error) {
|
2022-01-17 21:17:22 +00:00
|
|
|
appEnvPath := path.Join(config.ABRA_DIR, "servers", NewAppServer, fmt.Sprintf("%s.env", NewAppName))
|
2021-11-03 08:10:13 +00:00
|
|
|
appEnv, err := config.ReadEnv(appEnvPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
secretEnvVars := secret.ReadSecretEnvVars(appEnv)
|
|
|
|
secrets, err := secret.GenerateSecrets(secretEnvVars, sanitisedAppName, NewAppServer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if Pass {
|
|
|
|
for secretName := range secrets {
|
|
|
|
secretValue := secrets[secretName]
|
|
|
|
if err := secret.PassInsertSecret(secretValue, secretName, sanitisedAppName, NewAppServer); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return secrets, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensureDomainFlag checks if the domain flag was used. if not, asks the user for it/
|
|
|
|
func ensureDomainFlag(recipe recipe.Recipe, server string) error {
|
2021-11-03 08:41:20 +00:00
|
|
|
if Domain == "" && !NoInput {
|
2021-11-03 08:10:13 +00:00
|
|
|
prompt := &survey.Input{
|
|
|
|
Message: "Specify app domain",
|
|
|
|
Default: fmt.Sprintf("%s.%s", recipe.Name, server),
|
|
|
|
}
|
|
|
|
if err := survey.AskOne(prompt, &Domain); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-11-03 08:41:20 +00:00
|
|
|
|
|
|
|
if Domain == "" {
|
|
|
|
return fmt.Errorf("no domain provided")
|
|
|
|
}
|
|
|
|
|
2021-11-03 08:10:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensureServerFlag checks if the server flag was used. if not, asks the user for it.
|
|
|
|
func ensureServerFlag() error {
|
|
|
|
servers, err := config.GetServers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-03 08:41:20 +00:00
|
|
|
if NewAppServer == "" && !NoInput {
|
2021-11-03 08:10:13 +00:00
|
|
|
prompt := &survey.Select{
|
|
|
|
Message: "Select app server:",
|
|
|
|
Options: servers,
|
|
|
|
}
|
|
|
|
if err := survey.AskOne(prompt, &NewAppServer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 08:41:20 +00:00
|
|
|
if NewAppServer == "" {
|
|
|
|
return fmt.Errorf("no server provided")
|
|
|
|
}
|
|
|
|
|
2021-11-03 08:10:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensureServerFlag checks if the AppName flag was used. if not, asks the user for it.
|
|
|
|
func ensureAppNameFlag() error {
|
2021-11-03 08:41:20 +00:00
|
|
|
if NewAppName == "" && !NoInput {
|
2021-11-03 08:10:13 +00:00
|
|
|
prompt := &survey.Input{
|
|
|
|
Message: "Specify app name:",
|
2021-12-26 03:24:12 +00:00
|
|
|
Default: Domain,
|
2021-11-03 08:10:13 +00:00
|
|
|
}
|
|
|
|
if err := survey.AskOne(prompt, &NewAppName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-11-03 08:41:20 +00:00
|
|
|
|
|
|
|
if NewAppName == "" {
|
|
|
|
return fmt.Errorf("no app name provided")
|
|
|
|
}
|
|
|
|
|
2021-11-03 08:10:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAction is the new app creation logic
|
|
|
|
func NewAction(c *cli.Context) error {
|
|
|
|
recipe := ValidateRecipeWithPrompt(c)
|
|
|
|
|
2021-12-27 03:07:52 +00:00
|
|
|
if err := recipePkg.EnsureUpToDate(recipe.Name); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-11-03 08:10:13 +00:00
|
|
|
if err := ensureServerFlag(); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ensureDomainFlag(recipe, NewAppServer); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ensureAppNameFlag(); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sanitisedAppName := config.SanitiseAppName(NewAppName)
|
|
|
|
if len(sanitisedAppName) > 45 {
|
2021-12-22 19:08:15 +00:00
|
|
|
logrus.Fatalf("%s cannot be longer than 45 characters", sanitisedAppName)
|
2021-11-03 08:10:13 +00:00
|
|
|
}
|
2021-12-22 19:08:15 +00:00
|
|
|
logrus.Debugf("%s sanitised as %s for new app", NewAppName, sanitisedAppName)
|
2021-11-03 08:10:13 +00:00
|
|
|
|
2021-12-22 19:08:15 +00:00
|
|
|
if err := config.TemplateAppEnvSample(recipe.Name, NewAppName, NewAppServer, Domain); err != nil {
|
2021-11-03 08:10:13 +00:00
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if Secrets {
|
2021-11-09 16:43:24 +00:00
|
|
|
if err := ssh.EnsureHostKey(NewAppServer); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-11-03 08:10:13 +00:00
|
|
|
secrets, err := createSecrets(sanitisedAppName)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
secretCols := []string{"Name", "Value"}
|
2021-12-28 00:24:23 +00:00
|
|
|
secretTable := formatter.CreateTable(secretCols)
|
2021-11-03 08:10:13 +00:00
|
|
|
for secret := range secrets {
|
|
|
|
secretTable.Append([]string{secret, secrets[secret]})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(secrets) > 0 {
|
|
|
|
defer secretTable.Render()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if NewAppServer == "default" {
|
|
|
|
NewAppServer = "local"
|
|
|
|
}
|
|
|
|
|
2022-01-04 10:56:17 +00:00
|
|
|
tableCol := []string{"server", "type", "domain", "app name"}
|
2021-12-28 00:24:23 +00:00
|
|
|
table := formatter.CreateTable(tableCol)
|
2022-01-04 10:56:29 +00:00
|
|
|
table.Append([]string{NewAppServer, recipe.Name, Domain, NewAppName})
|
2021-11-03 08:10:13 +00:00
|
|
|
|
|
|
|
fmt.Println("")
|
2021-11-03 08:41:20 +00:00
|
|
|
fmt.Println(fmt.Sprintf("A new %s app has been created! Here is an overview:", recipe.Name))
|
2021-11-03 08:10:13 +00:00
|
|
|
fmt.Println("")
|
|
|
|
table.Render()
|
|
|
|
fmt.Println("")
|
|
|
|
fmt.Println("You can configure this app by running the following:")
|
2022-01-04 10:56:29 +00:00
|
|
|
fmt.Println(fmt.Sprintf("\n abra app config %s", NewAppName))
|
2021-11-03 08:10:13 +00:00
|
|
|
fmt.Println("")
|
|
|
|
fmt.Println("You can deploy this app by running the following:")
|
2022-01-04 10:56:29 +00:00
|
|
|
fmt.Println(fmt.Sprintf("\n abra app deploy %s", NewAppName))
|
2021-11-03 08:10:13 +00:00
|
|
|
fmt.Println("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|