2021-11-03 08:20:40 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2022-01-18 13:13:20 +00:00
|
|
|
"context"
|
2021-11-03 08:20:40 +00:00
|
|
|
"fmt"
|
2021-12-28 01:31:21 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2021-11-03 08:20:40 +00:00
|
|
|
"strings"
|
|
|
|
|
2023-02-02 19:06:49 +00:00
|
|
|
"coopcloud.tech/abra/pkg/client"
|
2021-11-03 08:20:40 +00:00
|
|
|
"coopcloud.tech/abra/pkg/config"
|
2021-11-13 22:04:58 +00:00
|
|
|
"coopcloud.tech/abra/pkg/dns"
|
2021-12-28 00:24:23 +00:00
|
|
|
"coopcloud.tech/abra/pkg/formatter"
|
2021-12-23 23:43:24 +00:00
|
|
|
"coopcloud.tech/abra/pkg/git"
|
2021-12-25 23:00:19 +00:00
|
|
|
"coopcloud.tech/abra/pkg/lint"
|
2021-11-03 08:20:40 +00:00
|
|
|
"coopcloud.tech/abra/pkg/recipe"
|
2023-02-13 15:40:30 +00:00
|
|
|
"coopcloud.tech/abra/pkg/runtime"
|
2021-11-03 08:20:40 +00:00
|
|
|
"coopcloud.tech/abra/pkg/upstream/stack"
|
|
|
|
"github.com/AlecAivazis/survey/v2"
|
2023-04-14 14:12:31 +00:00
|
|
|
dockerClient "github.com/docker/docker/client"
|
2021-11-03 08:20:40 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-01-18 13:13:20 +00:00
|
|
|
"github.com/urfave/cli"
|
2021-11-03 08:20:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DeployAction is the main command-line action for this package
|
2023-02-02 19:06:49 +00:00
|
|
|
func DeployAction(c *cli.Context) error {
|
2021-11-03 08:20:40 +00:00
|
|
|
app := ValidateApp(c)
|
2023-03-07 11:23:40 +00:00
|
|
|
stackName := app.StackName()
|
2023-02-13 15:40:30 +00:00
|
|
|
conf := runtime.New()
|
2021-11-03 08:20:40 +00:00
|
|
|
|
2023-02-02 19:06:49 +00:00
|
|
|
cl, err := client.New(app.Server)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-01-05 18:21:41 +00:00
|
|
|
if !Chaos {
|
2023-02-14 23:49:22 +00:00
|
|
|
if err := recipe.EnsureUpToDate(app.Recipe, conf); err != nil {
|
2022-01-05 18:21:41 +00:00
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-12-27 03:07:52 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 15:40:30 +00:00
|
|
|
r, err := recipe.Get(app.Recipe, conf)
|
2021-12-25 22:35:45 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-12-25 23:00:19 +00:00
|
|
|
if err := lint.LintForErrors(r); err != nil {
|
2021-12-25 22:35:45 +00:00
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-03-07 11:23:40 +00:00
|
|
|
logrus.Debugf("checking whether %s is already deployed", stackName)
|
2021-11-03 08:20:40 +00:00
|
|
|
|
2023-03-07 11:23:40 +00:00
|
|
|
isDeployed, deployedVersion, err := stack.IsDeployed(context.Background(), cl, stackName)
|
2021-11-03 08:20:40 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if isDeployed {
|
2021-11-21 13:42:22 +00:00
|
|
|
if Force || Chaos {
|
2022-01-01 16:22:19 +00:00
|
|
|
logrus.Warnf("%s is already deployed but continuing (--force/--chaos)", app.Name)
|
2021-11-03 08:20:40 +00:00
|
|
|
} else {
|
2022-01-01 16:22:19 +00:00
|
|
|
logrus.Fatalf("%s is already deployed", app.Name)
|
2021-11-03 08:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
version := deployedVersion
|
2022-01-03 15:32:03 +00:00
|
|
|
if version == "unknown" && !Chaos {
|
2021-12-27 15:40:59 +00:00
|
|
|
catl, err := recipe.ReadRecipeCatalogue()
|
2021-11-19 14:50:29 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2022-01-25 11:37:13 +00:00
|
|
|
versions, err := recipe.GetRecipeCatalogueVersions(app.Recipe, catl)
|
2021-11-03 08:20:40 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(versions) > 0 {
|
|
|
|
version = versions[len(versions)-1]
|
2021-11-26 20:34:10 +00:00
|
|
|
logrus.Debugf("choosing %s as version to deploy", version)
|
2022-01-25 11:37:13 +00:00
|
|
|
if err := recipe.EnsureVersion(app.Recipe, version); err != nil {
|
2021-11-03 08:20:40 +00:00
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
2022-01-25 11:37:13 +00:00
|
|
|
head, err := git.GetRecipeHead(app.Recipe)
|
2021-12-23 23:43:24 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-12-29 23:41:21 +00:00
|
|
|
version = formatter.SmallSHA(head.String())
|
2021-11-03 08:20:40 +00:00
|
|
|
logrus.Warn("no versions detected, using latest commit")
|
2023-02-13 15:40:30 +00:00
|
|
|
if err := recipe.EnsureLatest(app.Recipe, conf); err != nil {
|
2021-11-03 08:20:40 +00:00
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 15:32:03 +00:00
|
|
|
if version == "unknown" && !Chaos {
|
2021-11-26 20:34:10 +00:00
|
|
|
logrus.Debugf("choosing %s as version to deploy", version)
|
2022-01-25 11:37:13 +00:00
|
|
|
if err := recipe.EnsureVersion(app.Recipe, version); err != nil {
|
2021-11-21 13:42:38 +00:00
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 15:32:03 +00:00
|
|
|
if version != "unknown" && !Chaos {
|
2022-01-25 11:37:13 +00:00
|
|
|
if err := recipe.EnsureVersion(app.Recipe, version); err != nil {
|
2021-11-03 08:20:40 +00:00
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if Chaos {
|
|
|
|
logrus.Warnf("chaos mode engaged")
|
|
|
|
var err error
|
2022-01-25 11:37:13 +00:00
|
|
|
version, err = recipe.ChaosVersion(app.Recipe)
|
2021-11-03 08:20:40 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:37:13 +00:00
|
|
|
abraShPath := fmt.Sprintf("%s/%s/%s", config.RECIPES_DIR, app.Recipe, "abra.sh")
|
2021-11-03 08:20:40 +00:00
|
|
|
abraShEnv, err := config.ReadAbraShEnvVars(abraShPath)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
for k, v := range abraShEnv {
|
|
|
|
app.Env[k] = v
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:37:13 +00:00
|
|
|
composeFiles, err := config.GetAppComposeFiles(app.Recipe, app.Env)
|
2021-11-03 08:20:40 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
deployOpts := stack.Deploy{
|
|
|
|
Composefiles: composeFiles,
|
2023-03-07 11:23:40 +00:00
|
|
|
Namespace: stackName,
|
2021-11-03 08:20:40 +00:00
|
|
|
Prune: false,
|
|
|
|
ResolveImage: stack.ResolveImageAlways,
|
|
|
|
}
|
|
|
|
compose, err := config.GetAppComposeConfig(app.Name, deployOpts, app.Env)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2023-03-07 11:23:40 +00:00
|
|
|
config.ExposeAllEnv(stackName, compose, app.Env)
|
|
|
|
config.SetRecipeLabel(compose, stackName, app.Recipe)
|
|
|
|
config.SetChaosLabel(compose, stackName, Chaos)
|
|
|
|
config.SetChaosVersionLabel(compose, stackName, version)
|
|
|
|
config.SetUpdateLabel(compose, stackName, app.Env)
|
2021-11-03 08:20:40 +00:00
|
|
|
|
|
|
|
if err := DeployOverview(app, version, "continue with deployment?"); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-12-21 22:57:20 +00:00
|
|
|
if !NoDomainChecks {
|
2023-01-23 00:11:07 +00:00
|
|
|
domainName, ok := app.Env["DOMAIN"]
|
|
|
|
if ok {
|
|
|
|
if _, err = dns.EnsureDomainsResolveSameIPv4(domainName, app.Server); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Warn("skipping domain checks as no DOMAIN=... configured for app")
|
2021-12-21 22:57:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Warn("skipping domain checks as requested")
|
2021-11-14 21:45:49 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 14:44:18 +00:00
|
|
|
stack.WaitTimeout, err = config.GetTimeoutFromLabel(compose, stackName)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
logrus.Debugf("set waiting timeout to %d s", stack.WaitTimeout)
|
|
|
|
|
2022-01-01 16:22:19 +00:00
|
|
|
if err := stack.RunDeploy(cl, deployOpts, compose, app.Name, DontWaitConverge); err != nil {
|
2021-11-03 08:20:40 +00:00
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-04-14 14:12:31 +00:00
|
|
|
postDeployCmds, ok := app.Env["POST_DEPLOY_CMDS"]
|
|
|
|
if ok && !DontWaitConverge {
|
|
|
|
logrus.Debugf("run the following post-deploy commands: %s", postDeployCmds)
|
|
|
|
if err := PostCmds(cl, app, postDeployCmds); err != nil {
|
|
|
|
logrus.Fatalf("attempting to run post deploy commands, saw: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostCmds parses a string of commands and executes them inside of the respective services
|
|
|
|
// the commands string must have the following format:
|
|
|
|
// "<service> <command> <arguments>|<service> <command> <arguments>|... "
|
|
|
|
func PostCmds(cl *dockerClient.Client, app config.App, commands string) error {
|
|
|
|
abraSh := path.Join(config.RECIPES_DIR, app.Recipe, "abra.sh")
|
|
|
|
if _, err := os.Stat(abraSh); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf(fmt.Sprintf("%s does not exist for %s?", abraSh, app.Name))
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, command := range strings.Split(commands, "|") {
|
|
|
|
commandParts := strings.Split(command, " ")
|
|
|
|
if len(commandParts) < 2 {
|
|
|
|
return fmt.Errorf(fmt.Sprintf("not enough arguments: %s", command))
|
|
|
|
}
|
|
|
|
targetServiceName := commandParts[0]
|
|
|
|
cmdName := commandParts[1]
|
|
|
|
parsedCmdArgs := ""
|
|
|
|
if len(commandParts) > 2 {
|
|
|
|
parsedCmdArgs = fmt.Sprintf("%s ", strings.Join(commandParts[2:], " "))
|
|
|
|
}
|
|
|
|
logrus.Infof("running post-command '%s %s' in container %s", cmdName, parsedCmdArgs, targetServiceName)
|
|
|
|
|
|
|
|
if err := EnsureCommand(abraSh, app.Recipe, cmdName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceNames, err := config.GetAppServiceNames(app.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
matchingServiceName := false
|
|
|
|
for _, serviceName := range serviceNames {
|
|
|
|
if serviceName == targetServiceName {
|
|
|
|
matchingServiceName = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !matchingServiceName {
|
|
|
|
return fmt.Errorf(fmt.Sprintf("no service %s for %s?", targetServiceName, app.Name))
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("running command %s %s within the context of %s_%s", cmdName, parsedCmdArgs, app.StackName(), targetServiceName)
|
|
|
|
|
|
|
|
if err := RunCmdRemote(cl, app, abraSh, targetServiceName, cmdName, parsedCmdArgs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-11-03 08:20:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeployOverview shows a deployment overview
|
|
|
|
func DeployOverview(app config.App, version, message string) error {
|
2022-01-27 11:24:20 +00:00
|
|
|
tableCol := []string{"server", "recipe", "config", "domain", "version"}
|
2021-12-28 00:24:23 +00:00
|
|
|
table := formatter.CreateTable(tableCol)
|
2021-11-03 08:20:40 +00:00
|
|
|
|
|
|
|
deployConfig := "compose.yml"
|
|
|
|
if composeFiles, ok := app.Env["COMPOSE_FILE"]; ok {
|
|
|
|
deployConfig = strings.Join(strings.Split(composeFiles, ":"), "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
server := app.Server
|
|
|
|
if app.Server == "default" {
|
|
|
|
server = "local"
|
|
|
|
}
|
|
|
|
|
2022-01-27 11:23:02 +00:00
|
|
|
table.Append([]string{server, app.Recipe, deployConfig, app.Domain, version})
|
2021-11-03 08:20:40 +00:00
|
|
|
table.Render()
|
|
|
|
|
|
|
|
if NoInput {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
response := false
|
|
|
|
prompt := &survey.Confirm{
|
|
|
|
Message: message,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := survey.AskOne(prompt, &response); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !response {
|
|
|
|
logrus.Fatal("exiting as requested")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewVersionOverview shows an upgrade or downgrade overview
|
2021-12-28 01:31:21 +00:00
|
|
|
func NewVersionOverview(app config.App, currentVersion, newVersion, releaseNotes string) error {
|
2022-01-27 11:24:20 +00:00
|
|
|
tableCol := []string{"server", "recipe", "config", "domain", "current version", "to be deployed"}
|
2021-12-28 00:24:23 +00:00
|
|
|
table := formatter.CreateTable(tableCol)
|
2021-11-03 08:20:40 +00:00
|
|
|
|
|
|
|
deployConfig := "compose.yml"
|
|
|
|
if composeFiles, ok := app.Env["COMPOSE_FILE"]; ok {
|
|
|
|
deployConfig = strings.Join(strings.Split(composeFiles, ":"), "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
server := app.Server
|
|
|
|
if app.Server == "default" {
|
|
|
|
server = "local"
|
|
|
|
}
|
|
|
|
|
2022-01-27 11:23:02 +00:00
|
|
|
table.Append([]string{server, app.Recipe, deployConfig, app.Domain, currentVersion, newVersion})
|
2021-11-03 08:20:40 +00:00
|
|
|
table.Render()
|
|
|
|
|
2021-12-28 01:31:21 +00:00
|
|
|
if releaseNotes == "" {
|
|
|
|
var err error
|
2022-01-25 11:37:13 +00:00
|
|
|
releaseNotes, err = GetReleaseNotes(app.Recipe, newVersion)
|
2021-12-28 01:31:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 15:13:42 +00:00
|
|
|
if releaseNotes != "" && newVersion != "" {
|
2021-12-28 01:31:21 +00:00
|
|
|
fmt.Println()
|
|
|
|
fmt.Println(fmt.Sprintf("%s release notes:\n\n%s", newVersion, releaseNotes))
|
|
|
|
} else {
|
|
|
|
logrus.Warnf("no release notes available for %s", newVersion)
|
|
|
|
}
|
|
|
|
|
2021-11-03 08:20:40 +00:00
|
|
|
if NoInput {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
response := false
|
|
|
|
prompt := &survey.Confirm{
|
|
|
|
Message: "continue with deployment?",
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := survey.AskOne(prompt, &response); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !response {
|
|
|
|
logrus.Fatal("exiting as requested")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-28 01:31:21 +00:00
|
|
|
|
|
|
|
// GetReleaseNotes prints release notes for a recipe version
|
|
|
|
func GetReleaseNotes(recipeName, version string) (string, error) {
|
2022-01-03 15:13:54 +00:00
|
|
|
if version == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2021-12-28 01:31:21 +00:00
|
|
|
fpath := path.Join(config.RECIPES_DIR, recipeName, "release", version)
|
|
|
|
|
|
|
|
if _, err := os.Stat(fpath); !os.IsNotExist(err) {
|
|
|
|
releaseNotes, err := ioutil.ReadFile(fpath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(releaseNotes), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|