0
0
forked from toolshed/abra
This commit is contained in:
p4u1 2025-03-04 16:19:14 +01:00
parent 9827f9d470
commit 51ea98a910
5 changed files with 40 additions and 98 deletions

View File

@ -64,10 +64,8 @@ checkout as-is. Recipe commit hashes are also supported as values for
},
Run: func(cmd *cobra.Command, args []string) {
var (
deployWarnMessages []string
toDeployVersion string
isChaosCommit bool
toDeployChaosVersion = config.CHAOS_DEFAULT
deployWarnMessages []string
toDeployVersion string
)
app := internal.ValidateApp(args)
@ -100,28 +98,16 @@ checkout as-is. Recipe commit hashes are also supported as values for
log.Fatalf("%s is already deployed", app.Name)
}
toDeployVersion, toDeployChaosVersion, err = getDeployVersion(args, deployMeta, app)
toDeployVersion, err = getDeployVersion(args, deployMeta, app)
if err != nil {
log.Fatal(err)
}
if !internal.Chaos {
isChaosCommit, err = app.Recipe.EnsureVersion(toDeployVersion)
_, err = app.Recipe.EnsureVersion(toDeployVersion)
if err != nil {
log.Fatal(err)
}
if isChaosCommit {
log.Debugf("assuming chaos commit: %s", toDeployVersion)
internal.Chaos = true
toDeployChaosVersion = toDeployVersion
toDeployVersion, err = app.Recipe.GetVersionLabelLocal()
if err != nil {
log.Fatal(err)
}
}
}
if err := validateSecrets(cl, app); err != nil {
@ -154,16 +140,11 @@ checkout as-is. Recipe commit hashes are also supported as values for
log.Fatal(err)
}
toDeployChaosVersionLabel := toDeployChaosVersion
if app.Recipe.Dirty {
toDeployChaosVersionLabel = formatter.AddDirtyMarker(toDeployChaosVersionLabel)
}
appPkg.ExposeAllEnv(stackName, compose, app.Env)
appPkg.SetRecipeLabel(compose, stackName, app.Recipe.Name)
appPkg.SetChaosLabel(compose, stackName, internal.Chaos)
if internal.Chaos {
appPkg.SetChaosVersionLabel(compose, stackName, toDeployChaosVersionLabel)
appPkg.SetChaosVersionLabel(compose, stackName, toDeployVersion)
}
appPkg.SetUpdateLabel(compose, stackName, app.Env)
@ -197,19 +178,11 @@ checkout as-is. Recipe commit hashes are also supported as values for
deployedVersion = deployMeta.Version
}
toWriteVersion := toDeployVersion
if internal.Chaos || isChaosCommit {
toWriteVersion = toDeployChaosVersion
}
if err := internal.DeployOverview(
app,
deployWarnMessages,
deployedVersion,
deployMeta.ChaosVersion,
toDeployVersion,
toDeployChaosVersion,
toWriteVersion,
); err != nil {
log.Fatal(err)
}
@ -233,7 +206,7 @@ checkout as-is. Recipe commit hashes are also supported as values for
}
}
if err := app.WriteRecipeVersion(toWriteVersion, false); err != nil {
if err := app.WriteRecipeVersion(toDeployVersion, false); err != nil {
log.Fatalf("writing recipe version failed: %s", err)
}
},
@ -254,22 +227,22 @@ func getChaosVersion(app app.App, toDeployVersion, toDeployChaosVersion *string)
return nil
}
func getLatestVersionOrCommit(app app.App) (string, string, error) {
func getLatestVersionOrCommit(app app.App) (string, error) {
versions, err := app.Recipe.Tags()
if err != nil {
return "", "", err
return "", err
}
if len(versions) > 0 && !internal.Chaos {
return versions[len(versions)-1], "", nil
return versions[len(versions)-1], nil
}
head, err := app.Recipe.Head()
if err != nil {
return "", "", err
return "", err
}
return "", formatter.SmallSHA(head.String()), nil
return formatter.SmallSHA(head.String()), nil
}
// validateArgsAndFlags ensures compatible args/flags.
@ -296,49 +269,41 @@ func validateSecrets(cl *dockerClient.Client, app app.App) error {
return nil
}
func getDeployVersion(cliArgs []string, deployMeta stack.DeployMeta, app app.App) (string, string, error) {
func getDeployVersion(cliArgs []string, deployMeta stack.DeployMeta, app app.App) (string, error) {
// Chaos mode overrides everything
if internal.Chaos {
v, err := app.Recipe.ChaosVersion()
if err != nil {
return "", "", err
return "", err
}
// TODO: This seems wrong!?
cv, err := app.Recipe.GetVersionLabelLocal()
if err != nil {
return "", "", err
}
log.Debugf("version: taking chaos version: %s, %s", v, cv)
return v, cv, nil
log.Debugf("version: taking chaos version: %s", v)
return v, nil
}
// Check if the deploy version is set with a cli argument
if len(cliArgs) == 2 && cliArgs[1] != "" {
log.Debugf("version: taking version from cli arg: %s", cliArgs[1])
return cliArgs[1], "", nil
return cliArgs[1], nil
}
// Check if the recipe has a version in the .env file
if app.Recipe.EnvVersion != "" && !internal.IgnoreEnvVersion {
log.Debugf("version: taking version from .env file: %s", app.Recipe.EnvVersion)
return app.Recipe.EnvVersion, "", nil
return app.Recipe.EnvVersion, nil
}
// Take deployed version
if deployMeta.IsDeployed {
log.Debugf("version: taking deployed version: %s", deployMeta.Version)
return deployMeta.Version, "", nil
return deployMeta.Version, nil
}
v, vc, err := getLatestVersionOrCommit(app)
log.Debugf("version: taking new recipe versio: %s, %s", v, vc)
v, err := getLatestVersionOrCommit(app)
log.Debugf("version: taking new recipe version: %s", v)
if err != nil {
log.Fatal(err)
}
if v == "" {
return vc, vc, nil
}
return v, vc, nil
return v, nil
}
func init() {

View File

@ -209,16 +209,7 @@ var AppNewCommand = &cobra.Command{
log.Fatal(err)
}
if err := app.Recipe.IsDirty(); err != nil {
log.Fatal(err)
}
toWriteVersion := recipeVersion
if internal.Chaos || app.Recipe.Dirty {
toWriteVersion = chaosVersion
}
if err := app.WriteRecipeVersion(toWriteVersion, false); err != nil {
if err := app.WriteRecipeVersion(recipeVersion, false); err != nil {
log.Fatalf("writing recipe version failed: %s", err)
}
},

View File

@ -132,10 +132,7 @@ func DeployOverview(
app appPkg.App,
warnMessages []string,
deployedVersion string,
deployedChaosVersion string,
toDeployVersion,
toDeployChaosVersion string,
toWriteVersion string,
toDeployVersion string,
) error {
deployConfig := "compose.yml"
if composeFiles, ok := app.Env["COMPOSE_FILE"]; ok {
@ -152,11 +149,6 @@ func DeployOverview(
domain = config.NO_DOMAIN_DEFAULT
}
if app.Recipe.Dirty {
toWriteVersion = formatter.AddDirtyMarker(toWriteVersion)
toDeployChaosVersion = formatter.AddDirtyMarker(toDeployChaosVersion)
}
envVersion := app.Recipe.EnvVersionRaw
if envVersion == "" {
envVersion = config.NO_VERSION_DEFAULT
@ -170,15 +162,12 @@ func DeployOverview(
{"CURRENT DEPLOYMENT", "---"},
{"VERSION", formatter.BoldDirtyDefault(deployedVersion)},
{"CHAOS", formatter.BoldDirtyDefault(deployedChaosVersion)},
{"CURRENT ENV VERSION", "---"},
{"ENV VERSION", formatter.BoldDirtyDefault(envVersion)},
{"NEW DEPLOYMENT", "---"},
{"VERSION", formatter.BoldDirtyDefault(toDeployVersion)},
{"CHAOS", formatter.BoldDirtyDefault(toDeployChaosVersion)},
{fmt.Sprintf("%s.ENV", strings.ToUpper(app.Name)), "---"},
{"CURRENT VERSION", formatter.BoldDirtyDefault(envVersion)},
{"NEW VERSION", formatter.BoldDirtyDefault(toWriteVersion)},
}
overview := formatter.CreateOverview("DEPLOY OVERVIEW", rows)

View File

@ -7,6 +7,7 @@ import (
"sort"
"strings"
"coopcloud.tech/abra/pkg/config"
"coopcloud.tech/abra/pkg/formatter"
gitPkg "coopcloud.tech/abra/pkg/git"
"coopcloud.tech/abra/pkg/log"
@ -274,19 +275,14 @@ func (r Recipe) EnsureUpToDate() error {
return nil
}
// IsDirty checks whether a recipe is dirty or not. N.B., if you call IsDirty
// from another Recipe method, you should propagate the pointer reference (*).
func (r *Recipe) IsDirty() error {
// IsDirty checks whether a recipe is dirty or not.
func (r *Recipe) IsDirty() (bool, error) {
isClean, err := gitPkg.IsClean(r.Dir)
if err != nil {
return err
return false, err
}
if !isClean {
r.Dirty = true
}
return nil
return !isClean, nil
}
// ChaosVersion constructs a chaos mode recipe version.
@ -300,8 +296,12 @@ func (r *Recipe) ChaosVersion() (string, error) {
version = formatter.SmallSHA(head.String())
if err := r.IsDirty(); err != nil {
return version, err
dirty, err := r.IsDirty()
if err != nil {
return "", err
}
if dirty {
return fmt.Sprintf("%s%s", version, config.DIRTY_DEFAULT), nil
}
return version, nil

View File

@ -2,7 +2,6 @@ package recipe
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/url"
@ -20,7 +19,6 @@ import (
"coopcloud.tech/abra/pkg/log"
"coopcloud.tech/abra/pkg/web"
"coopcloud.tech/tagcmp"
"github.com/go-git/go-git/v5"
)
// RecipeCatalogueURL is the only current recipe catalogue available.
@ -122,7 +120,6 @@ type Features struct {
func Get(name string) Recipe {
version := ""
versionRaw := ""
dirty := false
if strings.Contains(name, ":") {
split := strings.Split(name, ":")
if len(split) > 2 {
@ -134,7 +131,6 @@ func Get(name string) Recipe {
versionRaw = version
if strings.HasSuffix(version, config.DIRTY_DEFAULT) {
version = strings.Replace(split[1], config.DIRTY_DEFAULT, "", 1)
dirty = true
log.Debugf("removed dirty suffix from .env version: %s -> %s", split[1], version)
}
}
@ -160,7 +156,6 @@ func Get(name string) Recipe {
Name: name,
EnvVersion: version,
EnvVersionRaw: versionRaw,
Dirty: dirty,
Dir: dir,
GitURL: gitURL,
SSHURL: sshURL,
@ -171,9 +166,11 @@ func Get(name string) Recipe {
AbraShPath: path.Join(dir, "abra.sh"),
}
if err := r.IsDirty(); err != nil && !errors.Is(err, git.ErrRepositoryNotExists) {
dirty, err := r.IsDirty()
if err != nil {
log.Fatalf("failed to check git status of %s: %s", r.Name, err)
}
r.Dirty = dirty
return r
}