decentral1se
6f43778691
Some checks failed
continuous-integration/drone/push Build is failing
Closes coop-cloud/organising#145.
231 lines
5.8 KiB
Go
231 lines
5.8 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"path"
|
|
|
|
abraFormatter "coopcloud.tech/abra/cli/formatter"
|
|
"coopcloud.tech/abra/cli/internal"
|
|
"coopcloud.tech/abra/pkg/catalogue"
|
|
"coopcloud.tech/abra/pkg/config"
|
|
"coopcloud.tech/abra/pkg/secret"
|
|
"github.com/AlecAivazis/survey/v2"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
type secrets map[string]string
|
|
|
|
var domain string
|
|
var domainFlag = &cli.StringFlag{
|
|
Name: "domain",
|
|
Aliases: []string{"d"},
|
|
Value: "",
|
|
Usage: "Choose a domain name",
|
|
Destination: &domain,
|
|
}
|
|
|
|
var newAppServer string
|
|
var newAppServerFlag = &cli.StringFlag{
|
|
Name: "server",
|
|
Aliases: []string{"s"},
|
|
Value: "",
|
|
Usage: "Show apps of a specific server",
|
|
Destination: &newAppServer,
|
|
}
|
|
|
|
var newAppName string
|
|
var newAppNameFlag = &cli.StringFlag{
|
|
Name: "app-name",
|
|
Aliases: []string{"a"},
|
|
Value: "",
|
|
Usage: "Choose an app name",
|
|
Destination: &newAppName,
|
|
}
|
|
|
|
var appNewDescription = `
|
|
This command takes a recipe and uses it to create a new app. This new app
|
|
configuration is stored in your ~/.abra directory under the appropriate server.
|
|
|
|
This command does not deploy your app for you. You will need to run "abra app
|
|
deploy <app>" to do so.
|
|
|
|
You can see what recipes are available (i.e. values for the <recipe> argument)
|
|
by running "abra recipe ls".
|
|
|
|
Passing the "--secrets/-S" flag will automatically generate secrets for your
|
|
app and store them encrypted at rest on the chosen target server. These
|
|
generated secrets are only visible at generation time, so please take care to
|
|
store them somewhere safe.
|
|
|
|
You can use the "--pass/-P" to store these generated passwords locally in a
|
|
pass store (see passwordstore.org for more). The pass command must be available
|
|
on your $PATH.
|
|
`
|
|
|
|
var appNewCommand = &cli.Command{
|
|
Name: "new",
|
|
Usage: "Create a new app",
|
|
Aliases: []string{"n"},
|
|
Description: appNewDescription,
|
|
Flags: []cli.Flag{
|
|
newAppServerFlag,
|
|
domainFlag,
|
|
newAppNameFlag,
|
|
internal.PassFlag,
|
|
internal.SecretsFlag,
|
|
},
|
|
ArgsUsage: "<recipe>",
|
|
Action: action,
|
|
BashComplete: func(c *cli.Context) {
|
|
catl, err := catalogue.ReadRecipeCatalogue()
|
|
if err != nil {
|
|
logrus.Warn(err)
|
|
}
|
|
if c.NArg() > 0 {
|
|
return
|
|
}
|
|
for name := range catl {
|
|
fmt.Println(name)
|
|
}
|
|
},
|
|
}
|
|
|
|
// ensureDomainFlag checks if the domain flag was used. if not, asks the user for it/
|
|
func ensureDomainFlag() error {
|
|
if domain == "" {
|
|
prompt := &survey.Input{
|
|
Message: "Specify app domain",
|
|
}
|
|
if err := survey.AskOne(prompt, &domain); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
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
|
|
}
|
|
|
|
if newAppServer == "" {
|
|
prompt := &survey.Select{
|
|
Message: "Select app server:",
|
|
Options: servers,
|
|
}
|
|
if err := survey.AskOne(prompt, &newAppServer); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// ensureServerFlag checks if the AppName flag was used. if not, asks the user for it.
|
|
func ensureAppNameFlag() error {
|
|
if newAppName == "" {
|
|
prompt := &survey.Input{
|
|
Message: "Specify app name:",
|
|
Default: config.SanitiseAppName(domain),
|
|
}
|
|
if err := survey.AskOne(prompt, &newAppName); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// createSecrets creates all secrets for a new app.
|
|
func createSecrets(sanitisedAppName string) (secrets, error) {
|
|
appEnvPath := path.Join(config.ABRA_DIR, "servers", newAppServer, fmt.Sprintf("%s.env", sanitisedAppName))
|
|
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 internal.Pass {
|
|
for secretName := range secrets {
|
|
secretValue := secrets[secretName]
|
|
if err := secret.PassInsertSecret(secretValue, secretName, sanitisedAppName, newAppServer); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
return secrets, nil
|
|
}
|
|
|
|
// action is the main command-line action for this package
|
|
func action(c *cli.Context) error {
|
|
recipe := internal.ValidateRecipe(c)
|
|
|
|
if err := config.EnsureAbraDirExists(); err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
|
|
if err := ensureServerFlag(); err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
|
|
if err := ensureDomainFlag(); err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
|
|
if err := ensureAppNameFlag(); err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
|
|
sanitisedAppName := config.SanitiseAppName(newAppName)
|
|
if len(sanitisedAppName) > 45 {
|
|
logrus.Fatalf("'%s' cannot be longer than 45 characters", sanitisedAppName)
|
|
}
|
|
logrus.Debugf("'%s' sanitised as '%s' for new app", newAppName, sanitisedAppName)
|
|
|
|
if err := config.TemplateAppEnvSample(recipe.Name, newAppName, newAppServer, domain, recipe.Name); err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
|
|
if internal.Secrets {
|
|
secrets, err := createSecrets(sanitisedAppName)
|
|
if err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
|
|
secretCols := []string{"Name", "Value"}
|
|
secretTable := abraFormatter.CreateTable(secretCols)
|
|
for secret := range secrets {
|
|
secretTable.Append([]string{secret, secrets[secret]})
|
|
}
|
|
|
|
if len(secrets) > 0 {
|
|
defer secretTable.Render()
|
|
}
|
|
}
|
|
|
|
tableCol := []string{"Name", "Domain", "Type", "Server"}
|
|
table := abraFormatter.CreateTable(tableCol)
|
|
table.Append([]string{sanitisedAppName, domain, recipe.Name, newAppServer})
|
|
|
|
fmt.Println("")
|
|
fmt.Println(fmt.Sprintf("New '%s' created! Here is your new app overview:", recipe.Name))
|
|
fmt.Println("")
|
|
table.Render()
|
|
fmt.Println("")
|
|
fmt.Println("You can configure this app by running the following:")
|
|
fmt.Println(fmt.Sprintf("\n abra app config %s", sanitisedAppName))
|
|
fmt.Println("")
|
|
fmt.Println("You can deploy this app by running the following:")
|
|
fmt.Println(fmt.Sprintf("\n abra app deploy %s", sanitisedAppName))
|
|
fmt.Println("")
|
|
|
|
return nil
|
|
}
|