Skip to content

Commit 0f4893b

Browse files
committed
allow overriding workdir for containers
1 parent eb10bc3 commit 0f4893b

File tree

6 files changed

+15
-8
lines changed

6 files changed

+15
-8
lines changed

cmd/deploy.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ If deployment failed, then rolls back to the previous stack definition.`,
4343
viper.GetString("image_tag"),
4444
viper.GetStringSlice("image_tags"),
4545
viper.GetStringSlice("deploy.services"),
46+
viper.GetString("workdir"),
4647
)
4748
if err != nil {
4849
log.WithError(err).Errorf("Deployment failed with code %d", exitCode)

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ func init() {
4848
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "Show debug output")
4949
rootCmd.PersistentFlags().StringP("cluster", "c", "", "name of cluster (required)")
5050
rootCmd.PersistentFlags().StringP("profile", "p", "", "name of AWS profile to use, which is set in ~/.aws/config")
51+
rootCmd.PersistentFlags().StringP("workdir", "w", "", "Set working directory")
5152
rootCmd.PersistentFlags().StringP("image_tag", "", "", "Overrides the docker image tag in all container definitions. Overrides \"--image-tags\" flag.")
5253
rootCmd.PersistentFlags().StringSliceP("image_tags", "", []string{}, "Modifies the docker image tags in container definitions. Can be specified several times, one for each container definition. Also takes comma-separated values in one tag. I.e. if there are 2 containers and --image-tags is set once to \"new\", then the image tag of the first container will be modified, leaving the second one untouched. Gets overridden by \"--image-tag\". If you have 3 container definitions and want to modify tags for the 1st and the 3rd, but leave the 2nd unchanged, specify it as \"--image_tags first_tag,,last_tag\".")
5354

5455
viper.BindPFlag("profile", rootCmd.PersistentFlags().Lookup("profile"))
5556
viper.BindPFlag("cluster", rootCmd.PersistentFlags().Lookup("cluster"))
57+
viper.BindPFlag("workdir", rootCmd.PersistentFlags().Lookup("workdir"))
5658
viper.BindPFlag("image_tag", rootCmd.PersistentFlags().Lookup("image_tag"))
5759
viper.BindPFlag("image_tags", rootCmd.PersistentFlags().Lookup("image_tags"))
5860

cmd/run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ It can modify the container command.
3535
viper.GetString("task_definition"),
3636
viper.GetString("image_tag"),
3737
viper.GetStringSlice("image_tags"),
38+
viper.GetString("workdir"),
3839
containerName,
3940
viper.GetString("log_group"),
4041
viper.GetString("run.launch_type"),

lib/deploy.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
// DeployServices deploys specified services in parallel
14-
func DeployServices(profile, cluster, imageTag string, imageTags, services []string) (exitCode int, err error) {
14+
func DeployServices(profile, cluster, imageTag string, imageTags, services []string, workDir string) (exitCode int, err error) {
1515
ctx := log.WithFields(log.Fields{
1616
"cluster": cluster,
1717
"image_tag": imageTag,
@@ -30,7 +30,7 @@ func DeployServices(profile, cluster, imageTag string, imageTags, services []str
3030
wg.Add(1)
3131
go func() {
3232
defer wg.Done()
33-
deployService(ctx, cluster, imageTag, imageTags, service, exits, rollback, &wg)
33+
deployService(ctx, cluster, imageTag, imageTags, workDir, service, exits, rollback, &wg)
3434
}()
3535
}
3636

@@ -52,7 +52,7 @@ func DeployServices(profile, cluster, imageTag string, imageTags, services []str
5252
return
5353
}
5454

55-
func deployService(ctx log.Interface, cluster, imageTag string, imageTags []string, service string, exitChan chan int, rollback chan bool, wg *sync.WaitGroup) {
55+
func deployService(ctx log.Interface, cluster, imageTag string, imageTags []string, workDir, service string, exitChan chan int, rollback chan bool, wg *sync.WaitGroup) {
5656
ctx = ctx.WithFields(log.Fields{
5757
"service": service,
5858
})
@@ -90,7 +90,7 @@ func deployService(ctx log.Interface, cluster, imageTag string, imageTags []stri
9090

9191
taskDefinition := describeTaskResult.TaskDefinition
9292
// replace the image tag if there is any
93-
if err := modifyContainerDefinitionImages(imageTag, imageTags, taskDefinition.ContainerDefinitions, ctx); err != nil {
93+
if err := modifyContainerDefinitionImages(imageTag, imageTags, workDir, taskDefinition.ContainerDefinitions, ctx); err != nil {
9494
ctx.WithError(err).Error("Can't modify container definition images")
9595
exitChan <- 1
9696
}

lib/run.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
// RunTask runs the specified one-off task in the cluster using the task definition
12-
func RunTask(profile, cluster, service, taskDefinitionName, imageTag string, imageTags []string, containerName, awslogGroup, launchType string, args []string) (exitCode int, err error) {
12+
func RunTask(profile, cluster, service, taskDefinitionName, imageTag string, imageTags []string, workDir, containerName, awslogGroup, launchType string, args []string) (exitCode int, err error) {
1313
err = makeSession(profile)
1414
if err != nil {
1515
return 1, err
@@ -28,7 +28,7 @@ func RunTask(profile, cluster, service, taskDefinitionName, imageTag string, ima
2828
taskDefinition := describeResult.TaskDefinition
2929

3030
var foundContainerName bool
31-
if err := modifyContainerDefinitionImages(imageTag, imageTags, taskDefinition.ContainerDefinitions, ctx); err != nil {
31+
if err := modifyContainerDefinitionImages(imageTag, imageTags, workDir, taskDefinition.ContainerDefinitions, ctx); err != nil {
3232
return 1, err
3333
}
3434
for n, containerDefinition := range taskDefinition.ContainerDefinitions {

lib/util.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func fetchCloudWatchLog(cluster, containerName, awslogGroup, taskUUID string, de
9494
return printCloudWatchLogs(awslogGroup, streamName)
9595
}
9696

97-
func modifyContainerDefinitionImages(imageTag string, imageTags []string, containerDefinitions []*ecs.ContainerDefinition, ctx log.Interface) error {
97+
func modifyContainerDefinitionImages(imageTag string, imageTags []string, workDir string, containerDefinitions []*ecs.ContainerDefinition, ctx log.Interface) error {
9898

9999
for n, containerDefinition := range containerDefinitions {
100100
ctx := ctx.WithField("container_name", aws.StringValue(containerDefinition.Name))
@@ -120,10 +120,13 @@ func modifyContainerDefinitionImages(imageTag string, imageTags []string, contai
120120
"old_tag": imageWithTag[1],
121121
}).Debug("Image tag changed")
122122
}
123-
124123
} else {
125124
ctx.Debug("Container doesn't seem to have a tag in the image. It's safer to not do anything.")
126125
}
126+
if workDir != "" {
127+
containerDefinitions[n].WorkingDirectory = aws.String(workDir)
128+
ctx.WithField("workdir", workDir).Debug("Workdir changed")
129+
}
127130

128131
}
129132
return nil

0 commit comments

Comments
 (0)