forked from toolshed/abra
		
	feat: support deploying with chaos mode
This commit is contained in:
		@ -10,6 +10,7 @@ import (
 | 
			
		||||
	"coopcloud.tech/abra/pkg/client"
 | 
			
		||||
	stack "coopcloud.tech/abra/pkg/client/stack"
 | 
			
		||||
	"coopcloud.tech/abra/pkg/config"
 | 
			
		||||
	"coopcloud.tech/abra/pkg/git"
 | 
			
		||||
	"coopcloud.tech/abra/pkg/recipe"
 | 
			
		||||
	"github.com/AlecAivazis/survey/v2"
 | 
			
		||||
	"github.com/sirupsen/logrus"
 | 
			
		||||
@ -22,6 +23,7 @@ var appDeployCommand = &cli.Command{
 | 
			
		||||
	Usage:   "Deploy an app",
 | 
			
		||||
	Flags: []cli.Flag{
 | 
			
		||||
		internal.ForceFlag,
 | 
			
		||||
		internal.ChaosFlag,
 | 
			
		||||
	},
 | 
			
		||||
	Description: `
 | 
			
		||||
This command deploys a new instance of an app. It does not support changing the
 | 
			
		||||
@ -50,14 +52,16 @@ hacking.
 | 
			
		||||
 | 
			
		||||
		if isDeployed {
 | 
			
		||||
			if internal.Force {
 | 
			
		||||
				logrus.Infof("continuing with deployment due to '--force/-f' being set")
 | 
			
		||||
				logrus.Warnf("'%s' already deployed but continuing (--force)", stackName)
 | 
			
		||||
			} else if internal.Chaos {
 | 
			
		||||
				logrus.Warnf("'%s' already deployed but continuing (--chaos)", stackName)
 | 
			
		||||
			} else {
 | 
			
		||||
				logrus.Fatalf("'%s' is already deployed", stackName)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		version := deployedVersion
 | 
			
		||||
		if version == "" {
 | 
			
		||||
		if version == "" && !internal.Chaos {
 | 
			
		||||
			versions, err := catalogue.GetRecipeCatalogueVersions(app.Type)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				logrus.Fatal(err)
 | 
			
		||||
@ -70,13 +74,32 @@ hacking.
 | 
			
		||||
				}
 | 
			
		||||
			} else {
 | 
			
		||||
				version = "latest commit"
 | 
			
		||||
				logrus.Warning("no versions detected, using latest commit")
 | 
			
		||||
				logrus.Warn("no versions detected, using latest commit")
 | 
			
		||||
				if err := recipe.EnsureLatest(app.Type); err != nil {
 | 
			
		||||
					logrus.Fatal(err)
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if internal.Chaos {
 | 
			
		||||
			logrus.Warnf("chaos mode engaged")
 | 
			
		||||
			head, err := git.GetRecipeHead(app.Type)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				logrus.Fatal(err)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			version = head.String()[:8]
 | 
			
		||||
 | 
			
		||||
			isClean, err := git.IsClean(app.Type)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				logrus.Fatal(err)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if !isClean {
 | 
			
		||||
				version = fmt.Sprintf("%s + unstaged changes", version)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		abraShPath := fmt.Sprintf("%s/%s/%s", config.APPS_DIR, app.Type, "abra.sh")
 | 
			
		||||
		abraShEnv, err := config.ReadAbraShEnvVars(abraShPath)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
 | 
			
		||||
@ -49,3 +49,15 @@ var ForceFlag = &cli.BoolFlag{
 | 
			
		||||
	Aliases:     []string{"f"},
 | 
			
		||||
	Destination: &Force,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Chaos engages chaos mode.
 | 
			
		||||
var Chaos bool
 | 
			
		||||
 | 
			
		||||
// ChaosFlag turns on/off chaos functionality.
 | 
			
		||||
var ChaosFlag = &cli.BoolFlag{
 | 
			
		||||
	Name:        "chaos",
 | 
			
		||||
	Value:       false,
 | 
			
		||||
	Aliases:     []string{"ch"},
 | 
			
		||||
	Usage:       "Deploy uncommitted recipes changes. Use with care!",
 | 
			
		||||
	Destination: &Chaos,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										48
									
								
								pkg/git/read.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								pkg/git/read.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,48 @@
 | 
			
		||||
package git
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"path"
 | 
			
		||||
 | 
			
		||||
	"coopcloud.tech/abra/pkg/config"
 | 
			
		||||
	"github.com/go-git/go-git/v5"
 | 
			
		||||
	"github.com/go-git/go-git/v5/plumbing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// GetRecipeHead retrieves latest HEAD metadata.
 | 
			
		||||
func GetRecipeHead(recipeName string) (*plumbing.Reference, error) {
 | 
			
		||||
	recipeDir := path.Join(config.ABRA_DIR, "apps", recipeName)
 | 
			
		||||
 | 
			
		||||
	repo, err := git.PlainOpen(recipeDir)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	head, err := repo.Head()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return head, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// IsClean checks if a repo has unstaged changes
 | 
			
		||||
func IsClean(recipeName string) (bool, error) {
 | 
			
		||||
	recipeDir := path.Join(config.ABRA_DIR, "apps", recipeName)
 | 
			
		||||
 | 
			
		||||
	repo, err := git.PlainOpen(recipeDir)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return false, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	worktree, err := repo.Worktree()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return false, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	status, err := worktree.Status()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return false, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return status.IsClean(), nil
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user