2021-09-05 20:33:07 +00:00
|
|
|
package recipe
|
|
|
|
|
|
|
|
import (
|
2021-10-08 07:51:47 +00:00
|
|
|
"errors"
|
2021-09-05 20:33:07 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"coopcloud.tech/abra/cli/internal"
|
2021-10-05 08:28:09 +00:00
|
|
|
"coopcloud.tech/abra/pkg/catalogue"
|
|
|
|
"github.com/AlecAivazis/survey/v2"
|
2021-09-05 20:33:07 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var recipeSyncCommand = &cli.Command{
|
2021-10-08 07:51:47 +00:00
|
|
|
Name: "sync",
|
|
|
|
Usage: "Ensure recipe version labels are up-to-date",
|
|
|
|
Aliases: []string{"s"},
|
2021-11-06 21:38:29 +00:00
|
|
|
ArgsUsage: "<recipe> [<version>]",
|
2021-09-05 20:33:07 +00:00
|
|
|
Description: `
|
2021-10-08 07:51:47 +00:00
|
|
|
This command will generate labels for the main recipe service (i.e. by
|
|
|
|
convention, typically the service named "app") which corresponds to the
|
|
|
|
following format:
|
2021-09-05 20:33:07 +00:00
|
|
|
|
2021-10-08 07:51:47 +00:00
|
|
|
coop-cloud.${STACK_NAME}.version=<version>
|
2021-09-05 20:33:07 +00:00
|
|
|
|
2021-10-08 07:51:47 +00:00
|
|
|
The <version> is determined by the recipe maintainer and is specified on the
|
|
|
|
command-line. The <recipe> configuration will be updated on the local file
|
|
|
|
system.
|
2021-09-05 20:33:07 +00:00
|
|
|
`,
|
2021-10-05 08:28:09 +00:00
|
|
|
BashComplete: func(c *cli.Context) {
|
|
|
|
catl, err := catalogue.ReadRecipeCatalogue()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warn(err)
|
|
|
|
}
|
|
|
|
if c.NArg() > 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for name := range catl {
|
|
|
|
fmt.Println(name)
|
|
|
|
}
|
|
|
|
},
|
2021-09-05 20:33:07 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2021-10-08 07:51:47 +00:00
|
|
|
if c.Args().Len() != 2 {
|
|
|
|
internal.ShowSubcommandHelpAndError(c, errors.New("missing <recipe>/<version> arguments?"))
|
|
|
|
}
|
|
|
|
|
2021-09-05 23:41:16 +00:00
|
|
|
recipe := internal.ValidateRecipe(c)
|
2021-09-05 20:33:07 +00:00
|
|
|
|
2021-10-08 07:51:47 +00:00
|
|
|
// TODO: validate with tagcmp when new commits come in
|
|
|
|
// See https://git.coopcloud.tech/coop-cloud/abra/pulls/109
|
|
|
|
nextTag := c.Args().Get(1)
|
2021-10-05 08:28:09 +00:00
|
|
|
|
2021-10-08 07:51:47 +00:00
|
|
|
mainService := "app"
|
2021-10-05 08:28:09 +00:00
|
|
|
var services []string
|
2021-09-05 20:33:07 +00:00
|
|
|
hasAppService := false
|
2021-09-05 23:34:28 +00:00
|
|
|
for _, service := range recipe.Config.Services {
|
2021-10-05 08:28:09 +00:00
|
|
|
services = append(services, service.Name)
|
2021-09-05 20:33:07 +00:00
|
|
|
if service.Name == "app" {
|
|
|
|
hasAppService = true
|
2021-10-05 08:28:09 +00:00
|
|
|
logrus.Debugf("detected app service in '%s'", recipe.Name)
|
2021-09-05 20:33:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hasAppService {
|
2021-10-05 08:28:09 +00:00
|
|
|
logrus.Warnf("no 'app' service defined in '%s'", recipe.Name)
|
|
|
|
var chosenService string
|
|
|
|
prompt := &survey.Select{
|
|
|
|
Message: fmt.Sprintf("what is the main service name for '%s'?", recipe.Name),
|
|
|
|
Options: services,
|
2021-09-05 20:33:07 +00:00
|
|
|
}
|
2021-10-05 08:28:09 +00:00
|
|
|
if err := survey.AskOne(prompt, &chosenService); err != nil {
|
2021-09-05 20:33:07 +00:00
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-10-05 08:28:09 +00:00
|
|
|
mainService = chosenService
|
|
|
|
}
|
2021-09-05 20:33:07 +00:00
|
|
|
|
2021-10-05 08:28:09 +00:00
|
|
|
logrus.Debugf("selecting '%s' as the service to sync version labels", mainService)
|
|
|
|
|
2021-10-08 07:51:47 +00:00
|
|
|
label := fmt.Sprintf("coop-cloud.${STACK_NAME}.version=%s", nextTag)
|
2021-10-05 08:28:09 +00:00
|
|
|
if err := recipe.UpdateLabel(mainService, label); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-10-08 07:51:47 +00:00
|
|
|
logrus.Infof("synced label '%s' to service '%s'", label, mainService)
|
2021-10-05 08:28:09 +00:00
|
|
|
|
2021-09-05 20:33:07 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|