From 651b57b3732b2c6a279812fd0cbb3c1f92a3a48a Mon Sep 17 00:00:00 2001 From: James Clark Date: Sat, 24 Dec 2022 21:00:26 -0700 Subject: [PATCH 1/2] Initial pull latest Docker image for custer+service command --- cmd/outback/pull_latest.go | 65 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 cmd/outback/pull_latest.go diff --git a/cmd/outback/pull_latest.go b/cmd/outback/pull_latest.go new file mode 100644 index 0000000..d6de919 --- /dev/null +++ b/cmd/outback/pull_latest.go @@ -0,0 +1,65 @@ +package cmd + +import ( + "fmt" + Outback "github.com/koala-labs/outback/pkg/outback" + "github.com/spf13/cobra" +) + +var pullLatestCmd = &cobra.Command{ + Use: "pull-latest", + Short: "Pull the latest docker image from a deployment", + Long: `A cluster must be specified via the --cluster flag, and a service must be specified via the --cluster flag. + The --verbose flag can be input to enable verbose output. + The --login flag can be input to login to AWS ECR.`, + RunE: runPullLatest, +} + +func runPullLatest(cmd *cobra.Command, args []string) error { + return pullLatest(flagCluster, flagService) +} + +func pullLatest(clusterName string, serviceName string) error { + outback := Outback.New(awsConfig) + + cluster, err := cfg.getCluster(clusterName) + if err != nil { + return err + } + + ecsCluster, err := outback.GetCluster(cluster.Name) + if err != nil { + return err + } + + ecsService, err := outback.GetService(ecsCluster, serviceName) + if err != nil { + return err + } + + // Get the Service's TaskDefinition + ecsTaskDef, err := outback.GetTaskDefinition(ecsCluster, ecsService) + if err != nil { + return err + } + + // Get the commit from the last TaskDefinition if it exists + commit, err := outback.GetLastDeployedCommit(*ecsTaskDef.TaskDefinitionArn) + if err != nil { + return err + } + + // Pull latest image + err = outback.LoginPullImage(cfg.Repo, commit) + if err != nil { + return err + } + + fmt.Printf("Successfully pulled the latest image from :\n\t%s:%s\n", cfg.Repo, commit) + + return nil +} + +func init() { + rootCmd.AddCommand(pullLatestCmd) +} From 54d076aaf67f4ee54a1f653a99f5b5d8339fdf93 Mon Sep 17 00:00:00 2001 From: James Clark Date: Sat, 24 Dec 2022 21:23:02 -0700 Subject: [PATCH 2/2] Add new errors for missing cluster or service in pull latest command --- cmd/outback/errors.go | 5 +++++ cmd/outback/pull_latest.go | 12 +++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/cmd/outback/errors.go b/cmd/outback/errors.go index d5d6698..2770993 100644 --- a/cmd/outback/errors.go +++ b/cmd/outback/errors.go @@ -32,6 +32,11 @@ var ( ErrCantFollowWithEndTime = errors.New("Could not follow logs because an end time was given") ) +var ( + ErrorMissingClusterInput = errors.New("A cluster name must be specified using the --cluster flag") + ErrorMissingServiceInput = errors.New("A service name must be specified using the --service flag") +) + // handleError is intended to be called with an error return to simplify error handling // Usage: // foo, err := GetFoo() diff --git a/cmd/outback/pull_latest.go b/cmd/outback/pull_latest.go index d6de919..3439481 100644 --- a/cmd/outback/pull_latest.go +++ b/cmd/outback/pull_latest.go @@ -8,7 +8,7 @@ import ( var pullLatestCmd = &cobra.Command{ Use: "pull-latest", - Short: "Pull the latest docker image from a deployment", + Short: "Pull the latest deployed docker image from a service", Long: `A cluster must be specified via the --cluster flag, and a service must be specified via the --cluster flag. The --verbose flag can be input to enable verbose output. The --login flag can be input to login to AWS ECR.`, @@ -20,6 +20,14 @@ func runPullLatest(cmd *cobra.Command, args []string) error { } func pullLatest(clusterName string, serviceName string) error { + if clusterName == "" { + return ErrorMissingClusterInput + } + + if serviceName == "" { + return ErrorMissingServiceInput + } + outback := Outback.New(awsConfig) cluster, err := cfg.getCluster(clusterName) @@ -49,6 +57,8 @@ func pullLatest(clusterName string, serviceName string) error { return err } + fmt.Printf("Found image at %s:%s\nPulling...\n", cfg.Repo, commit) + // Pull latest image err = outback.LoginPullImage(cfg.Repo, commit) if err != nil {