2021-09-04 23:55:10 +00:00
|
|
|
package recipe
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path"
|
2021-09-05 23:15:59 +00:00
|
|
|
"path/filepath"
|
2021-09-04 23:55:10 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-09-05 23:15:59 +00:00
|
|
|
"coopcloud.tech/abra/pkg/client/stack"
|
|
|
|
loader "coopcloud.tech/abra/pkg/client/stack"
|
2021-09-05 23:34:28 +00:00
|
|
|
"coopcloud.tech/abra/pkg/compose"
|
2021-09-05 19:37:03 +00:00
|
|
|
"coopcloud.tech/abra/pkg/config"
|
2021-09-10 22:54:02 +00:00
|
|
|
gitPkg "coopcloud.tech/abra/pkg/git"
|
2021-09-05 23:15:59 +00:00
|
|
|
composetypes "github.com/docker/cli/cli/compose/types"
|
2021-09-04 23:55:10 +00:00
|
|
|
"github.com/go-git/go-git/v5"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
2021-09-10 22:54:02 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-09-04 23:55:10 +00:00
|
|
|
)
|
|
|
|
|
2021-09-05 23:34:28 +00:00
|
|
|
// 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) {
|
2021-09-05 23:41:16 +00:00
|
|
|
if err := EnsureExists(recipeName); err != nil {
|
|
|
|
return Recipe{}, err
|
|
|
|
}
|
|
|
|
|
2021-09-05 23:34:28 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-09-04 23:55:10 +00:00
|
|
|
// EnsureExists checks whether a recipe has been cloned locally or not.
|
|
|
|
func EnsureExists(recipe string) error {
|
|
|
|
recipeDir := path.Join(config.ABRA_DIR, "apps", strings.ToLower(recipe))
|
2021-09-10 22:54:02 +00:00
|
|
|
url := fmt.Sprintf("%s/%s.git", config.REPOS_BASE_URL, recipe)
|
|
|
|
if err := gitPkg.Clone(recipeDir, url); err != nil {
|
|
|
|
return err
|
2021-09-04 23:55:10 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnsureVersion checks whether a specific version exists for a recipe.
|
2021-09-07 06:12:37 +00:00
|
|
|
func EnsureVersion(recipeName, version string) error {
|
|
|
|
recipeDir := path.Join(config.ABRA_DIR, "apps", recipeName)
|
2021-09-04 23:55:10 +00:00
|
|
|
|
|
|
|
repo, err := git.PlainOpen(recipeDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tags, err := repo.Tags()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 22:54:02 +00:00
|
|
|
logrus.Debugf("read '%s' as tags for recipe '%s'", tags, recipeName)
|
|
|
|
|
2021-09-04 23:55:10 +00:00
|
|
|
var tagRef plumbing.ReferenceName
|
|
|
|
if err := tags.ForEach(func(ref *plumbing.Reference) (err error) {
|
|
|
|
if ref.Name().Short() == version {
|
|
|
|
tagRef = ref.Name()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if tagRef.String() == "" {
|
|
|
|
return fmt.Errorf("%s is not available?", version)
|
|
|
|
}
|
|
|
|
|
|
|
|
worktree, err := repo.Worktree()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := &git.CheckoutOptions{Branch: tagRef, Keep: true}
|
|
|
|
if err := worktree.Checkout(opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-10 22:54:02 +00:00
|
|
|
logrus.Debugf("successfully checked '%s' out to '%s' in '%s'", recipeName, tagRef, recipeDir)
|
|
|
|
|
2021-09-04 23:55:10 +00:00
|
|
|
return nil
|
|
|
|
}
|