Skip to content
This repository was archived by the owner on Nov 19, 2025. It is now read-only.

Commit 6de165f

Browse files
authored
Add task-def and file flags to local down/ps (#789)
1 parent a6b978a commit 6de165f

File tree

6 files changed

+96
-31
lines changed

6 files changed

+96
-31
lines changed

ecs-cli/integ/cmd/compose.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func TestServiceUp(t *testing.T, p *Project) {
8585
"up",
8686
"--cluster-config",
8787
p.ConfigName,
88+
"--create-log-groups",
8889
}
8990
cmd := integ.GetCommand(args)
9091

ecs-cli/modules/cli/local/create_app.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,39 @@ package local
1818
import (
1919
"fmt"
2020

21+
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/commands/flags"
2122
"github.com/urfave/cli"
2223
)
2324

25+
const (
26+
// taskDefinitionLabelType represents the type of option used to
27+
// transform a task definition to a compose file e.g. remoteFile, localFile.
28+
// taskDefinitionLabelValue represents the value of the option
29+
// e.g. file path, arn, family.
30+
taskDefinitionLabelType = "ecsLocalTaskDefType"
31+
taskDefinitionLabelValue = "ecsLocalTaskDefVal"
32+
)
33+
34+
const (
35+
localTaskDefType = "localFile"
36+
remoteTaskDefType = "remoteFile"
37+
)
38+
39+
const (
40+
ecsLocalDockerComposeFileName = "docker-compose.local.yml"
41+
)
42+
2443
func Create(c *cli.Context) {
2544
// 1. read in task def (from file or arn)
2645
// 2. parse task def into go object
2746
// 3. write to docker-compose.local.yml file
2847
fmt.Println("foo") // placeholder
2948
}
49+
50+
func validateOptions(c *cli.Context) error {
51+
if (c.String(flags.TaskDefinitionFileFlag) != "") && (c.String(flags.TaskDefinitionTaskFlag) != "") {
52+
return fmt.Errorf("%s and %s can not be used together",
53+
flags.TaskDefinitionTaskFlag, flags.TaskDefinitionFileFlag)
54+
}
55+
return nil
56+
}

ecs-cli/modules/cli/local/down_app.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package local
1515

1616
import (
17+
"fmt"
1718
"os"
1819
"os/exec"
1920
"path/filepath"
@@ -28,12 +29,6 @@ import (
2829
"golang.org/x/net/context"
2930
)
3031

31-
// TODO These labels should be defined part of the local.Create workflow.
32-
// Refactor to import these constants instead of re-defining them here.
33-
const (
34-
ecsLocalDockerComposeFileName = "docker-compose.local.yml"
35-
)
36-
3732
// Down stops and removes running local ECS tasks.
3833
// If the user stops the last running task in the local network then also remove the network.
3934
func Down(c *cli.Context) error {
@@ -42,8 +37,28 @@ func Down(c *cli.Context) error {
4237
network.Teardown(client)
4338
}()
4439

40+
if err := validateOptions(c); err != nil {
41+
logrus.Fatal(err.Error())
42+
}
43+
44+
if c.String(flags.TaskDefinitionFileFlag) != "" {
45+
return downLocalContainersWithFilters(filters.NewArgs(
46+
filters.Arg("label", fmt.Sprintf("%s=%s", taskDefinitionLabelValue,
47+
c.String(flags.TaskDefinitionFileFlag))),
48+
filters.Arg("label", fmt.Sprintf("%s=%s", taskDefinitionLabelType, localTaskDefType)),
49+
))
50+
}
51+
if c.String(flags.TaskDefinitionTaskFlag) != "" {
52+
return downLocalContainersWithFilters(filters.NewArgs(
53+
filters.Arg("label", fmt.Sprintf("%s=%s", taskDefinitionLabelValue,
54+
c.String(flags.TaskDefinitionTaskFlag))),
55+
filters.Arg("label", fmt.Sprintf("%s=%s", taskDefinitionLabelType, remoteTaskDefType)),
56+
))
57+
}
4558
if c.Bool(flags.AllFlag) {
46-
return downAllLocalContainers()
59+
return downLocalContainersWithFilters(filters.NewArgs(
60+
filters.Arg("label", taskDefinitionLabelValue),
61+
))
4762
}
4863
return downComposeLocalContainers()
4964
}
@@ -65,19 +80,17 @@ func downComposeLocalContainers() error {
6580
return nil
6681
}
6782

68-
func downAllLocalContainers() error {
83+
func downLocalContainersWithFilters(args filters.Args) error {
6984
ctx, cancel := context.WithTimeout(context.Background(), docker.TimeoutInS)
7085
defer cancel()
7186

7287
client := docker.NewClient()
7388
containers, err := client.ContainerList(ctx, types.ContainerListOptions{
74-
Filters: filters.NewArgs(
75-
filters.Arg("label", taskDefinitionLabelKey),
76-
),
77-
All: true,
89+
Filters: args,
90+
All: true,
7891
})
7992
if err != nil {
80-
logrus.Fatalf("Failed to list containers with label=%s due to %v", taskDefinitionLabelKey, err)
93+
logrus.Fatalf("Failed to list containers with filters %v due to %v", args, err)
8194
}
8295
if len(containers) == 0 {
8396
logrus.Warn("No running ECS local tasks found")

ecs-cli/modules/cli/local/ps_app.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,6 @@ import (
3131
"golang.org/x/net/context"
3232
)
3333

34-
// TODO These labels should be defined part of the local.Create workflow.
35-
// Refactor to import these constants instead of re-defining them here.
36-
// Docker object labels associated with containers created with "ecs-cli local".
37-
const (
38-
// taskDefinitionLabelKey represents the value of the option used to
39-
// transform a task definition to a compose file e.g. file path, arn, family.
40-
taskDefinitionLabelKey = "ecsLocalTaskDefinition"
41-
)
42-
4334
// Table formatting settings used by the Docker CLI.
4435
// See https://github.com/docker/cli/blob/0904fbfc77dbd4b6296c56e68be573b889d049e3/cli/command/formatter/formatter.go#L74
4536
const (
@@ -63,18 +54,34 @@ const (
6354
// If the --all flag is provided, then list all local ECS task containers.
6455
// If the --json flag is provided, then output the format as JSON instead.
6556
func Ps(c *cli.Context) {
57+
if err := validateOptions(c); err != nil {
58+
logrus.Fatal(err.Error())
59+
}
6660
containers := listContainers(c)
6761
displayContainers(c, containers)
6862
}
6963

7064
func listContainers(c *cli.Context) []types.Container {
71-
if !c.Bool(flags.AllFlag) {
72-
return listLocalComposeContainers()
65+
if c.String(flags.TaskDefinitionFileFlag) != "" {
66+
return listContainersWithFilters(filters.NewArgs(
67+
filters.Arg("label", fmt.Sprintf("%s=%s", taskDefinitionLabelValue,
68+
c.String(flags.TaskDefinitionFileFlag))),
69+
filters.Arg("label", fmt.Sprintf("%s=%s", taskDefinitionLabelType, localTaskDefType)),
70+
))
71+
}
72+
if c.String(flags.TaskDefinitionTaskFlag) != "" {
73+
return listContainersWithFilters(filters.NewArgs(
74+
filters.Arg("label", fmt.Sprintf("%s=%s", taskDefinitionLabelValue,
75+
c.String(flags.TaskDefinitionTaskFlag))),
76+
filters.Arg("label", fmt.Sprintf("%s=%s", taskDefinitionLabelType, remoteTaskDefType)),
77+
))
78+
}
79+
if c.Bool(flags.AllFlag) {
80+
return listContainersWithFilters(filters.NewArgs(
81+
filters.Arg("label", taskDefinitionLabelValue),
82+
))
7383
}
74-
// Task containers running locally all have a local label
75-
return listContainersWithFilters(filters.NewArgs(
76-
filters.Arg("label", taskDefinitionLabelKey),
77-
))
84+
return listLocalComposeContainers()
7885
}
7986

8087
func listLocalComposeContainers() []types.Container {
@@ -144,7 +151,7 @@ func displayAsTable(containers []types.Container) {
144151
container.Status,
145152
prettifyPorts(container.Ports),
146153
prettifyNames(container.Names),
147-
container.Labels[taskDefinitionLabelKey])
154+
container.Labels[taskDefinitionLabelValue])
148155
fmt.Fprintln(w, row)
149156
}
150157
w.Flush()

ecs-cli/modules/commands/flags/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ const (
150150
// Local
151151
TaskDefinitionFileFlag = "file"
152152
TaskDefinitionArnFlag = "arn"
153+
TaskDefinitionTaskFlag = "task-def"
153154
LocalOutputFlag = "output"
154155
JsonFlag = "json"
155156
AllFlag = "all"

ecs-cli/modules/commands/local/local_command.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,20 @@ func upCommand() cli.Command {
6060
func downCommand() cli.Command {
6161
return cli.Command{
6262
Name: "down",
63-
Usage: "Stop and remove a running local ECS task.",
63+
Usage: "Stop and remove a running ECS task container.",
6464
Action: local.Down,
6565
Flags: []cli.Flag{
6666
cli.BoolFlag{
6767
Name: flags.AllFlag,
68-
Usage: "Stop and remove all running local ECS tasks.",
68+
Usage: "Stops and removes all running containers",
69+
},
70+
cli.StringFlag{
71+
Name: flags.TaskDefinitionTaskFlag,
72+
Usage: "Stops and removes all running containers matching the task family or ARN",
73+
},
74+
cli.StringFlag{
75+
Name: flags.TaskDefinitionFileFlag,
76+
Usage: "Stops and removes all running containers matching the task definition file path",
6977
},
7078
},
7179
}
@@ -81,6 +89,14 @@ func psCommand() cli.Command {
8189
Name: flags.AllFlag,
8290
Usage: "Lists all running local ECS tasks.",
8391
},
92+
cli.StringFlag{
93+
Name: flags.TaskDefinitionTaskFlag,
94+
Usage: "Lists all running containers matching the task family or ARN",
95+
},
96+
cli.StringFlag{
97+
Name: flags.TaskDefinitionFileFlag,
98+
Usage: "Lists all running containers matching the task definition file path",
99+
},
84100
cli.BoolFlag{
85101
Name: flags.JsonFlag,
86102
Usage: "Output in JSON format.",

0 commit comments

Comments
 (0)