2021-09-05 20:33:07 +00:00
|
|
|
package recipe
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"coopcloud.tech/abra/cli/internal"
|
|
|
|
"coopcloud.tech/abra/pkg/client"
|
|
|
|
"github.com/docker/distribution/reference"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var recipeSyncCommand = &cli.Command{
|
|
|
|
Name: "sync",
|
|
|
|
Usage: "Generate new recipe labels",
|
|
|
|
Aliases: []string{"s"},
|
|
|
|
Description: `
|
|
|
|
This command will generate labels for each service which correspond to the
|
|
|
|
following format:
|
|
|
|
|
|
|
|
coop-cloud.${STACK_NAME}.${SERVICE_NAME}.version=${IMAGE_TAG}-${IMAGE_DIGEST}
|
|
|
|
|
|
|
|
The <recipe> configuration will be updated on the local file system. These
|
|
|
|
labels are consumed by abra in other command invocations and used to determine
|
|
|
|
the versioning metadata of up-and-running containers are.
|
|
|
|
`,
|
|
|
|
ArgsUsage: "<recipe>",
|
|
|
|
Action: func(c *cli.Context) error {
|
2021-09-05 23:41:16 +00:00
|
|
|
recipe := internal.ValidateRecipe(c)
|
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-09-05 20:33:07 +00:00
|
|
|
if service.Name == "app" {
|
|
|
|
hasAppService = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hasAppService {
|
2021-09-10 22:54:02 +00:00
|
|
|
logrus.Fatal(fmt.Sprintf("no 'app' service defined in '%s'", recipe.Name))
|
2021-09-05 20:33:07 +00:00
|
|
|
}
|
|
|
|
|
2021-09-05 23:34:28 +00:00
|
|
|
for _, service := range recipe.Config.Services {
|
2021-09-05 23:15:59 +00:00
|
|
|
img, err := reference.ParseNormalizedNamed(service.Image)
|
2021-09-05 20:33:07 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-09-10 22:54:02 +00:00
|
|
|
logrus.Debugf("detected image '%s' for service '%s'", img, service.Name)
|
2021-09-05 20:33:07 +00:00
|
|
|
|
|
|
|
digest, err := client.GetTagDigest(img)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-09-10 22:54:02 +00:00
|
|
|
logrus.Debugf("retrieved digest '%s' for '%s'", digest, img)
|
2021-09-05 20:33:07 +00:00
|
|
|
|
|
|
|
tag := img.(reference.NamedTagged).Tag()
|
|
|
|
label := fmt.Sprintf("coop-cloud.${STACK_NAME}.%s.version=%s-%s", service.Name, tag, digest)
|
2021-09-05 23:34:28 +00:00
|
|
|
if err := recipe.UpdateLabel(service.Name, label); err != nil {
|
2021-09-05 20:33:07 +00:00
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-09-10 22:54:02 +00:00
|
|
|
logrus.Debugf("added label '%s' to service '%s'", label, service.Name)
|
2021-09-05 20:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|