refactor: consolidate recipe in-place editing functions
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
b5d8fb1270
commit
6a1ecd0f85
|
@ -5,8 +5,6 @@ import (
|
||||||
|
|
||||||
"coopcloud.tech/abra/cli/internal"
|
"coopcloud.tech/abra/cli/internal"
|
||||||
"coopcloud.tech/abra/pkg/client"
|
"coopcloud.tech/abra/pkg/client"
|
||||||
"coopcloud.tech/abra/pkg/compose"
|
|
||||||
"coopcloud.tech/abra/pkg/config"
|
|
||||||
"coopcloud.tech/abra/pkg/recipe"
|
"coopcloud.tech/abra/pkg/recipe"
|
||||||
"github.com/docker/distribution/reference"
|
"github.com/docker/distribution/reference"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
@ -31,13 +29,13 @@ the versioning metadata of up-and-running containers are.
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
recipeName := internal.ValidateRecipe(c)
|
recipeName := internal.ValidateRecipe(c)
|
||||||
|
|
||||||
composeConfig, err := recipe.GetComposeConfig(recipeName)
|
recipe, err := recipe.Get(recipeName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
hasAppService := false
|
hasAppService := false
|
||||||
for _, service := range composeConfig.Services {
|
for _, service := range recipe.Config.Services {
|
||||||
if service.Name == "app" {
|
if service.Name == "app" {
|
||||||
hasAppService = true
|
hasAppService = true
|
||||||
}
|
}
|
||||||
|
@ -47,7 +45,7 @@ the versioning metadata of up-and-running containers are.
|
||||||
logrus.Fatal(fmt.Sprintf("No 'app' service defined in '%s', cannot proceed", recipeName))
|
logrus.Fatal(fmt.Sprintf("No 'app' service defined in '%s', cannot proceed", recipeName))
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, service := range composeConfig.Services {
|
for _, service := range recipe.Config.Services {
|
||||||
img, err := reference.ParseNormalizedNamed(service.Image)
|
img, err := reference.ParseNormalizedNamed(service.Image)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
|
@ -60,8 +58,7 @@ the versioning metadata of up-and-running containers are.
|
||||||
|
|
||||||
tag := img.(reference.NamedTagged).Tag()
|
tag := img.(reference.NamedTagged).Tag()
|
||||||
label := fmt.Sprintf("coop-cloud.${STACK_NAME}.%s.version=%s-%s", service.Name, tag, digest)
|
label := fmt.Sprintf("coop-cloud.${STACK_NAME}.%s.version=%s-%s", service.Name, tag, digest)
|
||||||
pattern := fmt.Sprintf("%s/%s/compose**yml", config.APPS_DIR, recipeName)
|
if err := recipe.UpdateLabel(service.Name, label); err != nil {
|
||||||
if err := compose.UpdateLabel(pattern, service.Name, label); err != nil {
|
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,6 @@ import (
|
||||||
"coopcloud.tech/abra/cli/internal"
|
"coopcloud.tech/abra/cli/internal"
|
||||||
"coopcloud.tech/abra/pkg/catalogue"
|
"coopcloud.tech/abra/pkg/catalogue"
|
||||||
"coopcloud.tech/abra/pkg/client"
|
"coopcloud.tech/abra/pkg/client"
|
||||||
"coopcloud.tech/abra/pkg/compose"
|
|
||||||
"coopcloud.tech/abra/pkg/config"
|
|
||||||
"coopcloud.tech/abra/pkg/recipe"
|
"coopcloud.tech/abra/pkg/recipe"
|
||||||
"coopcloud.tech/tagcmp"
|
"coopcloud.tech/tagcmp"
|
||||||
"github.com/AlecAivazis/survey/v2"
|
"github.com/AlecAivazis/survey/v2"
|
||||||
|
@ -38,12 +36,12 @@ This is step 1 of upgrading a recipe. Step 2 is running "abra recipe sync
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
recipeName := internal.ValidateRecipe(c)
|
recipeName := internal.ValidateRecipe(c)
|
||||||
|
|
||||||
composeConfig, err := recipe.GetComposeConfig(recipeName)
|
recipe, err := recipe.Get(recipeName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, service := range composeConfig.Services {
|
for _, service := range recipe.Config.Services {
|
||||||
catlVersions, err := catalogue.VersionsOfService(recipeName, service.Name)
|
catlVersions, err := catalogue.VersionsOfService(recipeName, service.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
|
@ -129,8 +127,7 @@ This is step 1 of upgrading a recipe. Step 2 is running "abra recipe sync
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
pattern := fmt.Sprintf("%s/%s/compose**yml", config.APPS_DIR, recipeName)
|
if err := recipe.UpdateTag(image, upgradeTag); err != nil {
|
||||||
if err := compose.UpdateTag(pattern, image, upgradeTag); err != nil {
|
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,12 +9,55 @@ import (
|
||||||
|
|
||||||
"coopcloud.tech/abra/pkg/client/stack"
|
"coopcloud.tech/abra/pkg/client/stack"
|
||||||
loader "coopcloud.tech/abra/pkg/client/stack"
|
loader "coopcloud.tech/abra/pkg/client/stack"
|
||||||
|
"coopcloud.tech/abra/pkg/compose"
|
||||||
"coopcloud.tech/abra/pkg/config"
|
"coopcloud.tech/abra/pkg/config"
|
||||||
composetypes "github.com/docker/cli/cli/compose/types"
|
composetypes "github.com/docker/cli/cli/compose/types"
|
||||||
"github.com/go-git/go-git/v5"
|
"github.com/go-git/go-git/v5"
|
||||||
"github.com/go-git/go-git/v5/plumbing"
|
"github.com/go-git/go-git/v5/plumbing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Recipe represents a recipe.
|
||||||
|
type Recipe struct {
|
||||||
|
Name string
|
||||||
|
Config *composetypes.Config
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateLabel updates a recipe label
|
||||||
|
func (r Recipe) UpdateLabel(serviceName, label string) error {
|
||||||
|
pattern := fmt.Sprintf("%s/%s/compose**yml", config.APPS_DIR, r.Name)
|
||||||
|
if err := compose.UpdateLabel(pattern, serviceName, label); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateTag updates a recipe tag
|
||||||
|
func (r Recipe) UpdateTag(image, tag string) error {
|
||||||
|
pattern := fmt.Sprintf("%s/%s/compose**yml", config.APPS_DIR, r.Name)
|
||||||
|
if err := compose.UpdateTag(pattern, image, tag); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get retrieves a recipe.
|
||||||
|
func Get(recipeName string) (Recipe, error) {
|
||||||
|
pattern := fmt.Sprintf("%s/%s/compose**yml", config.APPS_DIR, recipeName)
|
||||||
|
composeFiles, err := filepath.Glob(pattern)
|
||||||
|
if err != nil {
|
||||||
|
return Recipe{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
opts := stack.Deploy{Composefiles: composeFiles}
|
||||||
|
emptyEnv := make(map[string]string)
|
||||||
|
config, err := loader.LoadComposefile(opts, emptyEnv)
|
||||||
|
if err != nil {
|
||||||
|
return Recipe{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return Recipe{Name: recipeName, Config: config}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// EnsureExists checks whether a recipe has been cloned locally or not.
|
// EnsureExists checks whether a recipe has been cloned locally or not.
|
||||||
func EnsureExists(recipe string) error {
|
func EnsureExists(recipe string) error {
|
||||||
recipeDir := path.Join(config.ABRA_DIR, "apps", strings.ToLower(recipe))
|
recipeDir := path.Join(config.ABRA_DIR, "apps", strings.ToLower(recipe))
|
||||||
|
@ -70,21 +113,3 @@ func EnsureVersion(version string) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetComposeConfig merges and loads a recipe compose configuration.
|
|
||||||
func GetComposeConfig(recipeName string) (*composetypes.Config, error) {
|
|
||||||
pattern := fmt.Sprintf("%s/%s/compose**yml", config.APPS_DIR, recipeName)
|
|
||||||
composeFiles, err := filepath.Glob(pattern)
|
|
||||||
if err != nil {
|
|
||||||
return &composetypes.Config{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
opts := stack.Deploy{Composefiles: composeFiles}
|
|
||||||
emptyEnv := make(map[string]string)
|
|
||||||
compose, err := loader.LoadComposefile(opts, emptyEnv)
|
|
||||||
if err != nil {
|
|
||||||
return &composetypes.Config{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return compose, nil
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue