forked from toolshed/abra
		
	
		
			
				
	
	
		
			66 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			66 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
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)
 | 
						|
 | 
						|
		hasAppService := false
 | 
						|
		for _, service := range recipe.Config.Services {
 | 
						|
			if service.Name == "app" {
 | 
						|
				hasAppService = true
 | 
						|
			}
 | 
						|
		}
 | 
						|
 | 
						|
		if !hasAppService {
 | 
						|
			logrus.Fatal(fmt.Sprintf("no 'app' service defined in '%s'", recipe.Name))
 | 
						|
		}
 | 
						|
 | 
						|
		for _, service := range recipe.Config.Services {
 | 
						|
			img, err := reference.ParseNormalizedNamed(service.Image)
 | 
						|
			if err != nil {
 | 
						|
				logrus.Fatal(err)
 | 
						|
			}
 | 
						|
			logrus.Debugf("detected image '%s' for service '%s'", img, service.Name)
 | 
						|
 | 
						|
			digest, err := client.GetTagDigest(img)
 | 
						|
			if err != nil {
 | 
						|
				logrus.Fatal(err)
 | 
						|
			}
 | 
						|
			logrus.Debugf("retrieved digest '%s' for '%s'", digest, img)
 | 
						|
 | 
						|
			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 {
 | 
						|
				logrus.Fatal(err)
 | 
						|
			}
 | 
						|
			logrus.Debugf("added label '%s' to service '%s'", label, service.Name)
 | 
						|
		}
 | 
						|
 | 
						|
		return nil
 | 
						|
	},
 | 
						|
}
 |