2021-11-03 08:10:13 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
|
2022-03-15 09:54:05 +00:00
|
|
|
"coopcloud.tech/abra/pkg/app"
|
2023-01-31 15:09:09 +00:00
|
|
|
"coopcloud.tech/abra/pkg/client"
|
2021-11-03 08:10:13 +00:00
|
|
|
"coopcloud.tech/abra/pkg/config"
|
2021-12-28 00:24:23 +00:00
|
|
|
"coopcloud.tech/abra/pkg/formatter"
|
2023-01-11 02:51:38 +00:00
|
|
|
"coopcloud.tech/abra/pkg/jsontable"
|
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"
|
2023-02-13 15:40:30 +00:00
|
|
|
"coopcloud.tech/abra/pkg/runtime"
|
2021-11-03 08:10:13 +00:00
|
|
|
"coopcloud.tech/abra/pkg/secret"
|
|
|
|
"github.com/AlecAivazis/survey/v2"
|
2023-01-31 15:09:09 +00:00
|
|
|
dockerClient "github.com/docker/docker/client"
|
2021-11-03 08:10:13 +00:00
|
|
|
"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.
|
2023-01-31 15:09:09 +00:00
|
|
|
func createSecrets(cl *dockerClient.Client, sanitisedAppName string) (AppSecrets, error) {
|
2022-01-25 12:48:04 +00:00
|
|
|
appEnvPath := path.Join(config.ABRA_DIR, "servers", NewAppServer, fmt.Sprintf("%s.env", Domain))
|
2021-11-03 08:10:13 +00:00
|
|
|
appEnv, err := config.ReadEnv(appEnvPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
secretEnvVars := secret.ReadSecretEnvVars(appEnv)
|
2023-01-31 15:09:09 +00:00
|
|
|
secrets, err := secret.GenerateSecrets(cl, secretEnvVars, sanitisedAppName, NewAppServer)
|
2021-11-03 08:10:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if Pass {
|
|
|
|
for secretName := range secrets {
|
|
|
|
secretValue := secrets[secretName]
|
2022-03-12 15:01:31 +00:00
|
|
|
if err := secret.PassInsertSecret(secretValue, secretName, Domain, NewAppServer); err != nil {
|
2021-11-03 08:10:13 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-12 15:47:19 +00:00
|
|
|
// promptForSecrets asks if we should generate secrets for a new app.
|
2022-03-15 09:54:05 +00:00
|
|
|
func promptForSecrets(appName string) error {
|
|
|
|
app, err := app.Get(appName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
secretEnvVars := secret.ReadSecretEnvVars(app.Env)
|
|
|
|
if len(secretEnvVars) == 0 {
|
|
|
|
logrus.Debugf("%s has no secrets to generate, skipping...", app.Recipe)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-12 15:47:19 +00:00
|
|
|
if !Secrets && !NoInput {
|
|
|
|
prompt := &survey.Confirm{
|
|
|
|
Message: "Generate app secrets?",
|
|
|
|
}
|
|
|
|
if err := survey.AskOne(prompt, &Secrets); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-03 08:10:13 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAction is the new app creation logic
|
|
|
|
func NewAction(c *cli.Context) error {
|
2023-02-13 15:40:30 +00:00
|
|
|
recipe := ValidateRecipeWithPrompt(c, runtime.WithEnsureRecipeLatest(false))
|
2021-11-03 08:10:13 +00:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-01-25 12:48:04 +00:00
|
|
|
sanitisedAppName := config.SanitiseAppName(Domain)
|
|
|
|
logrus.Debugf("%s sanitised as %s for new app", Domain, sanitisedAppName)
|
2021-11-03 08:10:13 +00:00
|
|
|
|
2022-01-25 12:48:04 +00:00
|
|
|
if err := config.TemplateAppEnvSample(recipe.Name, Domain, NewAppServer, Domain); err != nil {
|
2021-11-03 08:10:13 +00:00
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-03-18 10:10:15 +00:00
|
|
|
if err := promptForSecrets(Domain); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-01-31 15:09:09 +00:00
|
|
|
cl, err := client.New(NewAppServer)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-03-11 15:39:34 +00:00
|
|
|
var secrets AppSecrets
|
2023-01-11 02:51:38 +00:00
|
|
|
var secretTable *jsontable.JSONTable
|
2021-11-03 08:10:13 +00:00
|
|
|
if Secrets {
|
2023-01-31 15:09:09 +00:00
|
|
|
secrets, err := createSecrets(cl, sanitisedAppName)
|
2021-11-03 08:10:13 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
secretCols := []string{"Name", "Value"}
|
2022-03-11 15:39:34 +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 NewAppServer == "default" {
|
|
|
|
NewAppServer = "local"
|
|
|
|
}
|
|
|
|
|
2022-02-02 19:48:12 +00:00
|
|
|
tableCol := []string{"server", "recipe", "domain"}
|
2021-12-28 00:24:23 +00:00
|
|
|
table := formatter.CreateTable(tableCol)
|
2022-01-25 12:48:04 +00:00
|
|
|
table.Append([]string{NewAppServer, recipe.Name, Domain})
|
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-25 12:48:04 +00:00
|
|
|
fmt.Println(fmt.Sprintf("\n abra app config %s", Domain))
|
2021-11-03 08:10:13 +00:00
|
|
|
fmt.Println("")
|
|
|
|
fmt.Println("You can deploy this app by running the following:")
|
2022-01-25 12:48:04 +00:00
|
|
|
fmt.Println(fmt.Sprintf("\n abra app deploy %s", Domain))
|
2021-11-03 08:10:13 +00:00
|
|
|
fmt.Println("")
|
|
|
|
|
2022-03-11 15:39:34 +00:00
|
|
|
if len(secrets) > 0 {
|
|
|
|
fmt.Println("Here are your generated secrets:")
|
|
|
|
fmt.Println("")
|
|
|
|
secretTable.Render()
|
|
|
|
fmt.Println("")
|
|
|
|
logrus.Warn("generated secrets are not shown again, please take note of them *now*")
|
|
|
|
}
|
|
|
|
|
2021-11-03 08:10:13 +00:00
|
|
|
return nil
|
|
|
|
}
|