abra/cli/recipe/sync.go

66 lines
1.8 KiB
Go
Raw Normal View History

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 {
recipe := internal.ValidateRecipe(c)
2021-09-05 20:33:07 +00:00
hasAppService := false
for _, service := range recipe.Config.Services {
2021-09-05 20:33:07 +00:00
if service.Name == "app" {
hasAppService = true
}
}
if !hasAppService {
logrus.Fatal(fmt.Sprintf("no 'app' service defined in '%s'", recipe.Name))
2021-09-05 20:33:07 +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)
}
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)
}
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)
if err := recipe.UpdateLabel(service.Name, label); err != nil {
2021-09-05 20:33:07 +00:00
logrus.Fatal(err)
}
logrus.Debugf("added label '%s' to service '%s'", label, service.Name)
2021-09-05 20:33:07 +00:00
}
return nil
},
}