forked from toolshed/abra
test: moar integration tests [ci skip]
This commit is contained in:
@ -5,7 +5,6 @@ import (
|
||||
|
||||
"coopcloud.tech/abra/pkg/config"
|
||||
"coopcloud.tech/abra/pkg/recipe"
|
||||
"coopcloud.tech/abra/pkg/runtime"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
@ -28,12 +27,7 @@ func AppNameComplete(c *cli.Context) {
|
||||
|
||||
// RecipeNameComplete completes recipe names.
|
||||
func RecipeNameComplete(c *cli.Context) {
|
||||
// defaults since we can't take arguments here... this means auto-completion
|
||||
// of recipe names always access the network if e.g. the catalogue needs
|
||||
// cloning / updating
|
||||
conf := runtime.New()
|
||||
|
||||
catl, err := recipe.ReadRecipeCatalogue(conf)
|
||||
catl, err := recipe.ReadRecipeCatalogue(false)
|
||||
if err != nil {
|
||||
logrus.Warn(err)
|
||||
}
|
||||
|
@ -8,7 +8,6 @@ import (
|
||||
|
||||
"coopcloud.tech/abra/pkg/config"
|
||||
gitPkg "coopcloud.tech/abra/pkg/git"
|
||||
"coopcloud.tech/abra/pkg/runtime"
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
@ -54,13 +53,9 @@ var CatalogueSkipList = map[string]bool{
|
||||
}
|
||||
|
||||
// EnsureCatalogue ensures that the catalogue is cloned locally & present.
|
||||
func EnsureCatalogue(conf *runtime.Config) error {
|
||||
func EnsureCatalogue() error {
|
||||
catalogueDir := path.Join(config.ABRA_DIR, "catalogue")
|
||||
if _, err := os.Stat(catalogueDir); err != nil && os.IsNotExist(err) {
|
||||
if conf.Offline {
|
||||
return fmt.Errorf("no local copy of the catalogue available, network access required")
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/%s.git", config.REPOS_BASE_URL, config.CATALOGUE_JSON_REPO_NAME)
|
||||
if err := gitPkg.Clone(catalogueDir, url); err != nil {
|
||||
return err
|
||||
@ -72,9 +67,8 @@ func EnsureCatalogue(conf *runtime.Config) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// EnsureUpToDate ensures that the local catalogue has no unstaged changes as
|
||||
// is up to date. This is useful to run before doing catalogue generation.
|
||||
func EnsureUpToDate(conf *runtime.Config) error {
|
||||
// EnsureIsClean makes sure that the catalogue has no unstaged changes.
|
||||
func EnsureIsClean() error {
|
||||
isClean, err := gitPkg.IsClean(config.CATALOGUE_DIR)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -85,11 +79,11 @@ func EnsureUpToDate(conf *runtime.Config) error {
|
||||
return fmt.Errorf(msg, config.CATALOGUE_DIR)
|
||||
}
|
||||
|
||||
if conf.Offline {
|
||||
logrus.Debug("attempting to use local catalogue without access network (\"--offline\")")
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EnsureUpToDate ensures that the local catalogue is up to date.
|
||||
func EnsureUpToDate() error {
|
||||
repo, err := git.PlainOpen(config.CATALOGUE_DIR)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -326,7 +326,7 @@ func TemplateAppEnvSample(recipeName, appName, server, domain string) error {
|
||||
}
|
||||
|
||||
appEnvPath := path.Join(ABRA_DIR, "servers", server, fmt.Sprintf("%s.env", appName))
|
||||
if _, err := os.Stat(appEnvPath); os.IsExist(err) {
|
||||
if _, err := os.Stat(appEnvPath); !os.IsNotExist(err) {
|
||||
return fmt.Errorf("%s already exists?", appEnvPath)
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,6 @@ import (
|
||||
"coopcloud.tech/abra/pkg/config"
|
||||
"coopcloud.tech/abra/pkg/recipe"
|
||||
recipePkg "coopcloud.tech/abra/pkg/recipe"
|
||||
"coopcloud.tech/abra/pkg/runtime"
|
||||
"coopcloud.tech/tagcmp"
|
||||
"github.com/docker/distribution/reference"
|
||||
"github.com/go-git/go-git/v5"
|
||||
@ -334,11 +333,7 @@ func LintImagePresent(recipe recipe.Recipe) (bool, error) {
|
||||
}
|
||||
|
||||
func LintHasPublishedVersion(recipe recipe.Recipe) (bool, error) {
|
||||
// defaults since we can't take arguments here... this means this lint rule
|
||||
// always access the network if e.g. the catalogue needs cloning / updating
|
||||
conf := runtime.New()
|
||||
|
||||
catl, err := recipePkg.ReadRecipeCatalogue(conf)
|
||||
catl, err := recipePkg.ReadRecipeCatalogue(false)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
@ -17,7 +17,6 @@ import (
|
||||
"coopcloud.tech/abra/pkg/formatter"
|
||||
gitPkg "coopcloud.tech/abra/pkg/git"
|
||||
"coopcloud.tech/abra/pkg/limit"
|
||||
"coopcloud.tech/abra/pkg/runtime"
|
||||
"coopcloud.tech/abra/pkg/upstream/stack"
|
||||
loader "coopcloud.tech/abra/pkg/upstream/stack"
|
||||
"coopcloud.tech/abra/pkg/web"
|
||||
@ -206,8 +205,8 @@ func (r Recipe) Tags() ([]string, error) {
|
||||
}
|
||||
|
||||
// Get retrieves a recipe.
|
||||
func Get(recipeName string, conf *runtime.Config) (Recipe, error) {
|
||||
if err := EnsureExists(recipeName, conf); err != nil {
|
||||
func Get(recipeName string, offline bool) (Recipe, error) {
|
||||
if err := EnsureExists(recipeName); err != nil {
|
||||
return Recipe{}, err
|
||||
}
|
||||
|
||||
@ -233,7 +232,7 @@ func Get(recipeName string, conf *runtime.Config) (Recipe, error) {
|
||||
return Recipe{}, err
|
||||
}
|
||||
|
||||
meta, err := GetRecipeMeta(recipeName, conf)
|
||||
meta, err := GetRecipeMeta(recipeName, offline)
|
||||
if err != nil {
|
||||
switch err.(type) {
|
||||
case RecipeMissingFromCatalogue:
|
||||
@ -251,19 +250,10 @@ func Get(recipeName string, conf *runtime.Config) (Recipe, error) {
|
||||
}
|
||||
|
||||
// EnsureExists ensures that a recipe is locally cloned
|
||||
func EnsureExists(recipeName string, conf *runtime.Config) error {
|
||||
if !conf.RecipeExists {
|
||||
logrus.Debug("skipping ensuring recipe locally exists")
|
||||
return nil
|
||||
}
|
||||
|
||||
func EnsureExists(recipeName string) error {
|
||||
recipeDir := path.Join(config.RECIPES_DIR, recipeName)
|
||||
|
||||
if _, err := os.Stat(recipeDir); os.IsNotExist(err) {
|
||||
if conf.Offline {
|
||||
return fmt.Errorf("no local copy of %s available, network access required", recipeName)
|
||||
}
|
||||
|
||||
logrus.Debugf("%s does not exist, attemmpting to clone", recipeDir)
|
||||
url := fmt.Sprintf("%s/%s.git", config.REPOS_BASE_URL, recipeName)
|
||||
if err := gitPkg.Clone(recipeDir, url); err != nil {
|
||||
@ -282,15 +272,6 @@ func EnsureExists(recipeName string, conf *runtime.Config) error {
|
||||
func EnsureVersion(recipeName, version string) error {
|
||||
recipeDir := path.Join(config.RECIPES_DIR, recipeName)
|
||||
|
||||
isClean, err := gitPkg.IsClean(recipeDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !isClean {
|
||||
return fmt.Errorf("%s has locally unstaged changes", recipeName)
|
||||
}
|
||||
|
||||
if err := gitPkg.EnsureGitRepo(recipeDir); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -342,30 +323,31 @@ func EnsureVersion(recipeName, version string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// EnsureLatest makes sure the latest commit is checked out for a local recipe repository
|
||||
func EnsureLatest(recipeName string, conf *runtime.Config) error {
|
||||
if !conf.RecipeLatest {
|
||||
logrus.Debug("skipping ensuring recipe is synced with remote")
|
||||
return nil
|
||||
}
|
||||
|
||||
// EnsureIsClean makes sure that the recipe repository has no unstaged changes.
|
||||
func EnsureIsClean(recipeName string) error {
|
||||
recipeDir := path.Join(config.RECIPES_DIR, recipeName)
|
||||
|
||||
isClean, err := gitPkg.IsClean(recipeDir)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("unable to check git clean status in %s: %s", recipeDir, err)
|
||||
}
|
||||
|
||||
if !isClean {
|
||||
return fmt.Errorf("%s has locally unstaged changes", recipeName)
|
||||
msg := "%s (%s) has locally unstaged changes? please commit/remove your changes before proceeding"
|
||||
return fmt.Errorf(msg, recipeName, recipeDir)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// EnsureLatest makes sure the latest commit is checked out for a local recipe repository
|
||||
func EnsureLatest(recipeName string) error {
|
||||
recipeDir := path.Join(config.RECIPES_DIR, recipeName)
|
||||
|
||||
if err := gitPkg.EnsureGitRepo(recipeDir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logrus.Debugf("attempting to open git repository in %s", recipeDir)
|
||||
|
||||
repo, err := git.PlainOpen(recipeDir)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -376,24 +358,9 @@ func EnsureLatest(recipeName string, conf *runtime.Config) error {
|
||||
return err
|
||||
}
|
||||
|
||||
meta, err := GetRecipeMeta(recipeName, conf)
|
||||
branch, err := gitPkg.GetDefaultBranch(repo, recipeDir)
|
||||
if err != nil {
|
||||
switch err.(type) {
|
||||
case RecipeMissingFromCatalogue:
|
||||
meta = RecipeMeta{}
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
var branch plumbing.ReferenceName
|
||||
if meta.DefaultBranch != "" {
|
||||
branch = plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", meta.DefaultBranch))
|
||||
} else {
|
||||
branch, err = gitPkg.GetDefaultBranch(repo, recipeDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
checkOutOpts := &git.CheckoutOptions{
|
||||
@ -598,29 +565,9 @@ func GetStringInBetween(recipeName, str, start, end string) (result string, err
|
||||
}
|
||||
|
||||
// EnsureUpToDate ensures that the local repo is synced to the remote
|
||||
func EnsureUpToDate(recipeName string, conf *runtime.Config) error {
|
||||
if !conf.RecipeLatest {
|
||||
logrus.Debug("skipping ensuring recipe is synced with remote")
|
||||
return nil
|
||||
}
|
||||
|
||||
func EnsureUpToDate(recipeName string) error {
|
||||
recipeDir := path.Join(config.RECIPES_DIR, recipeName)
|
||||
|
||||
isClean, err := gitPkg.IsClean(recipeDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to check git clean status in %s: %s", recipeDir, err)
|
||||
}
|
||||
|
||||
if !isClean {
|
||||
msg := "%s (%s) has locally unstaged changes? please commit/remove your changes before proceeding"
|
||||
return fmt.Errorf(msg, recipeName, recipeDir)
|
||||
}
|
||||
|
||||
if conf.Offline {
|
||||
logrus.Debug("attempting to use local recipe without access network (\"--offline\")")
|
||||
return nil
|
||||
}
|
||||
|
||||
repo, err := git.PlainOpen(recipeDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to open %s: %s", recipeDir, err)
|
||||
@ -671,15 +618,17 @@ func EnsureUpToDate(recipeName string, conf *runtime.Config) error {
|
||||
}
|
||||
|
||||
// ReadRecipeCatalogue reads the recipe catalogue.
|
||||
func ReadRecipeCatalogue(conf *runtime.Config) (RecipeCatalogue, error) {
|
||||
func ReadRecipeCatalogue(offline bool) (RecipeCatalogue, error) {
|
||||
recipes := make(RecipeCatalogue)
|
||||
|
||||
if err := catalogue.EnsureCatalogue(conf); err != nil {
|
||||
if err := catalogue.EnsureCatalogue(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := catalogue.EnsureUpToDate(conf); err != nil {
|
||||
return nil, err
|
||||
if !offline {
|
||||
if err := catalogue.EnsureUpToDate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if err := readRecipeCatalogueFS(&recipes); err != nil {
|
||||
@ -706,10 +655,10 @@ func readRecipeCatalogueFS(target interface{}) error {
|
||||
}
|
||||
|
||||
// VersionsOfService lists the version of a service.
|
||||
func VersionsOfService(recipe, serviceName string, conf *runtime.Config) ([]string, error) {
|
||||
func VersionsOfService(recipe, serviceName string, offline bool) ([]string, error) {
|
||||
var versions []string
|
||||
|
||||
catalogue, err := ReadRecipeCatalogue(conf)
|
||||
catalogue, err := ReadRecipeCatalogue(offline)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -743,8 +692,8 @@ func (r RecipeMissingFromCatalogue) Error() string {
|
||||
}
|
||||
|
||||
// GetRecipeMeta retrieves the recipe metadata from the recipe catalogue.
|
||||
func GetRecipeMeta(recipeName string, conf *runtime.Config) (RecipeMeta, error) {
|
||||
catl, err := ReadRecipeCatalogue(conf)
|
||||
func GetRecipeMeta(recipeName string, offline bool) (RecipeMeta, error) {
|
||||
catl, err := ReadRecipeCatalogue(offline)
|
||||
if err != nil {
|
||||
return RecipeMeta{}, err
|
||||
}
|
||||
@ -756,10 +705,6 @@ func GetRecipeMeta(recipeName string, conf *runtime.Config) (RecipeMeta, error)
|
||||
}
|
||||
}
|
||||
|
||||
if err := EnsureExists(recipeName, conf); err != nil {
|
||||
return RecipeMeta{}, err
|
||||
}
|
||||
|
||||
logrus.Debugf("recipe metadata retrieved for %s", recipeName)
|
||||
|
||||
return recipeMeta, nil
|
||||
@ -843,11 +788,7 @@ type InternalTracker struct {
|
||||
type RepoCatalogue map[string]RepoMeta
|
||||
|
||||
// ReadReposMetadata retrieves coop-cloud/... repo metadata from Gitea.
|
||||
func ReadReposMetadata(conf *runtime.Config) (RepoCatalogue, error) {
|
||||
if conf.Offline {
|
||||
return nil, fmt.Errorf("network access required to query recipes metadata")
|
||||
}
|
||||
|
||||
func ReadReposMetadata() (RepoCatalogue, error) {
|
||||
reposMeta := make(RepoCatalogue)
|
||||
|
||||
pageIdx := 1
|
||||
@ -882,7 +823,7 @@ func ReadReposMetadata(conf *runtime.Config) (RepoCatalogue, error) {
|
||||
}
|
||||
|
||||
// GetRecipeVersions retrieves all recipe versions.
|
||||
func GetRecipeVersions(recipeName string, conf *runtime.Config) (RecipeVersions, error) {
|
||||
func GetRecipeVersions(recipeName string, offline bool) (RecipeVersions, error) {
|
||||
versions := RecipeVersions{}
|
||||
recipeDir := path.Join(config.RECIPES_DIR, recipeName)
|
||||
|
||||
@ -920,7 +861,7 @@ func GetRecipeVersions(recipeName string, conf *runtime.Config) (RecipeVersions,
|
||||
|
||||
logrus.Debugf("successfully checked out %s in %s", ref.Name(), recipeDir)
|
||||
|
||||
recipe, err := Get(recipeName, conf)
|
||||
recipe, err := Get(recipeName, offline)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -1027,11 +968,7 @@ func GetRecipeCatalogueVersions(recipeName string, catl RecipeCatalogue) ([]stri
|
||||
}
|
||||
|
||||
// UpdateRepositories clones and updates all recipe repositories locally.
|
||||
func UpdateRepositories(repos RepoCatalogue, recipeName string, conf *runtime.Config) error {
|
||||
if conf.Offline {
|
||||
return fmt.Errorf("network access required to update recipes")
|
||||
}
|
||||
|
||||
func UpdateRepositories(repos RepoCatalogue, recipeName string) error {
|
||||
var barLength int
|
||||
if recipeName != "" {
|
||||
barLength = 1
|
||||
@ -1065,10 +1002,6 @@ func UpdateRepositories(repos RepoCatalogue, recipeName string, conf *runtime.Co
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
if err := EnsureUpToDate(rm.Name, conf); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
ch <- rm.Name
|
||||
retrieveBar.Add(1)
|
||||
}(repoMeta)
|
||||
|
@ -1,59 +0,0 @@
|
||||
package runtime
|
||||
|
||||
import "github.com/sirupsen/logrus"
|
||||
|
||||
// Config is a runtime behaviour modifier.
|
||||
type Config struct {
|
||||
Offline bool // Whether or not Abra should prefer local / offline access
|
||||
RecipeExists bool // Whether or not Abra should ensure the recipe is locally cloned
|
||||
RecipeLatest bool // Whether or not Abra should ensure the recipe has the latest commit
|
||||
}
|
||||
|
||||
// Option is a runtime configuration option.
|
||||
type Option func(c *Config)
|
||||
|
||||
// New creates a new runtime configuration.
|
||||
func New(opts ...Option) *Config {
|
||||
conf := &Config{
|
||||
Offline: false,
|
||||
RecipeExists: true,
|
||||
RecipeLatest: true,
|
||||
}
|
||||
|
||||
for _, optFunc := range opts {
|
||||
optFunc(conf)
|
||||
}
|
||||
|
||||
return conf
|
||||
}
|
||||
|
||||
// WithOffline ensures Abra attempts to prefer local file system and offline
|
||||
// access.
|
||||
func WithOffline(offline bool) Option {
|
||||
return func(c *Config) {
|
||||
if offline {
|
||||
logrus.Debugf("runtime config: attempting to run in offline mode")
|
||||
}
|
||||
c.Offline = offline
|
||||
}
|
||||
}
|
||||
|
||||
// WithEnsureRecipeExists ensures recipe exists locally.
|
||||
func WithEnsureRecipeExists(exists bool) Option {
|
||||
return func(c *Config) {
|
||||
if exists {
|
||||
logrus.Debugf("runtime config: ensuring recipe exists")
|
||||
}
|
||||
c.RecipeExists = exists
|
||||
}
|
||||
}
|
||||
|
||||
// WithEnsureRecipeUpToDate ensures recipe is synced with the remote.
|
||||
func WithEnsureRecipeUpToDate(upToDate bool) Option {
|
||||
return func(c *Config) {
|
||||
if upToDate {
|
||||
logrus.Debugf("runtime config: ensuring recipe is synced with remote")
|
||||
}
|
||||
c.RecipeLatest = upToDate
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user