-
Notifications
You must be signed in to change notification settings - Fork 301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support "local ps" to list running local ECS task containers #779
Changes from 8 commits
073d262
199a3a7
b298f3a
834df74
940b9ca
614b179
181202d
43b7d32
d562e77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// +build integ | ||
|
||
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package e2e | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/amazon-ecs-cli/ecs-cli/integ" | ||
"github.com/aws/amazon-ecs-cli/ecs-cli/integ/stdout" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestECSLocal(t *testing.T) { | ||
t.Parallel() | ||
|
||
type commandTest struct { | ||
args []string | ||
execute func(t *testing.T, args []string) | ||
} | ||
|
||
tests := map[string]struct { | ||
sequence []commandTest | ||
}{ | ||
"clean state": { | ||
sequence: []commandTest{ | ||
{ | ||
args: []string{"local", "ps"}, | ||
execute: func(t *testing.T, args []string) { | ||
// Given | ||
cmd := integ.GetCommand(args) | ||
|
||
// When | ||
out, err := cmd.Output() | ||
require.NoErrorf(t, err, "Failed local ps", fmt.Sprintf("args=%v, stdout=%s, err=%v", args, string(out), err)) | ||
|
||
// Then | ||
stdout := stdout.Stdout(out) | ||
require.Equal(t, 1, len(stdout.Lines()), "Expected only the table header") | ||
stdout.TestHasAllSubstrings(t, []string{ | ||
"CONTAINER ID", | ||
"IMAGE", | ||
"STATUS", | ||
"PORTS", | ||
"NAMES", | ||
"TASKDEFINITIONARN", | ||
"TASKFILEPATH", | ||
}) | ||
}, | ||
}, | ||
{ | ||
args: []string{"local", "ps", "--json"}, | ||
execute: func(t *testing.T, args []string) { | ||
// Given | ||
cmd := integ.GetCommand(args) | ||
|
||
// When | ||
out, err := cmd.Output() | ||
require.NoErrorf(t, err, "Failed local ps", fmt.Sprintf("args=%v, stdout=%s, err=%v", args, string(out), err)) | ||
|
||
// Then | ||
stdout := stdout.Stdout(out) | ||
stdout.TestHasAllSubstrings(t, []string{"[]"}) | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
for _, cmd := range tc.sequence { | ||
cmd.execute(t, cmd.args) | ||
} | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2015-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
// Package local implements the subcommands to run ECS task definitions locally | ||
// (See: https://github.com/aws/containers-roadmap/issues/180). | ||
package local | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/urfave/cli" | ||
) | ||
|
||
func Create(c *cli.Context) { | ||
// 1. read in task def (from file or arn) | ||
// 2. parse task def into go object | ||
// 3. write to docker-compose.local.yml file | ||
fmt.Println("foo") // placeholder | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lol |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2015-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package local | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/docker/docker/client" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
const ( | ||
// minDockerAPIVersion is the oldest Docker API version supporting the operations used by "local" sub-commands. | ||
minDockerAPIVersion = "1.27" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How'd you pick this version? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I picked the same version that the endpoints container requires. The operations that we use so far in "ecs local" are all supported in v1.27. I can say that the endpoints container uses 1.27 and link to the code ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure 👍🏻 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually I'd prefer it if the comment also said that all the operations in ecs local are supported in v1.27 Its hypothetically possible that the "ecs local" commands would need a newer Docker client version than the endpoints container. The current comment might lead future maintainers to think that the commands and the endpoints container have to use the same Docker Client version. And that's not true. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 makes sense, done! |
||
) | ||
|
||
func newDockerClient() *client.Client { | ||
if os.Getenv("DOCKER_API_VERSION") == "" { | ||
// If the user does not explicitly set the API version, then the SDK can choose | ||
// an API version that's too new for the user's Docker engine. | ||
_ = os.Setenv("DOCKER_API_VERSION", minDockerAPIVersion) | ||
} | ||
|
||
client, err := client.NewEnvClient() | ||
if err != nil { | ||
logrus.Fatalf("Could not create a docker client due to %v", err) | ||
} | ||
return client | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright 2015-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package local | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"text/tabwriter" | ||
|
||
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/commands/flags" | ||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/api/types/filters" | ||
"github.com/docker/docker/client" | ||
"github.com/sirupsen/logrus" | ||
"github.com/urfave/cli" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
// TODO These labels should be defined part of the local.Create workflow. | ||
// Refactor to import these constants instead of re-defining them here. | ||
const ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SoManyHs These are the Docker object labels that I'm expecting to be part of each container. Let me know if you're planing to use a different value for them :) |
||
// ecsLocalLabelKey is the Docker object label associated with containers created with "ecs-cli local". | ||
ecsLocalLabelKey = "ECSLocalTask" | ||
|
||
// taskDefinitionARNLabelKey is the Docker object label present if the container was created with a task def ARN. | ||
taskDefinitionARNLabelKey = "taskDefinitionARN" | ||
|
||
// taskFilePathLabelKey is the Docker object label present if the container was created from a file. | ||
taskFilePathLabelKey = "taskFilePath" | ||
) | ||
|
||
// Table formatting settings used by the Docker CLI. | ||
// See https://github.com/docker/cli/blob/0904fbfc77dbd4b6296c56e68be573b889d049e3/cli/command/formatter/formatter.go#L74 | ||
const ( | ||
cellWidthInSpaces = 20 | ||
widthBetweenCellsInSpaces = 1 | ||
cellPaddingInSpaces = 3 | ||
paddingCharacter = ' ' | ||
noFormatting = 0 | ||
|
||
maxContainerIDLength = 12 | ||
) | ||
|
||
// JSON formatting settings. | ||
const ( | ||
jsonPrefix = "" | ||
jsonIndent = " " | ||
) | ||
|
||
// Ps lists the status of the ECS task containers running locally. | ||
// | ||
// Defaults to listing the container metadata in a table format to stdout. If the --json flag is provided, | ||
// then output the content as JSON instead. | ||
func Ps(c *cli.Context) { | ||
docker := newDockerClient() | ||
|
||
containers := listECSLocalContainers(docker) | ||
if c.Bool(flags.JsonFlag) { | ||
displayAsJSON(containers) | ||
} else { | ||
displayAsTable(containers) | ||
} | ||
} | ||
|
||
func listECSLocalContainers(docker *client.Client) []types.Container { | ||
// ECS Task containers running locally all have an ECS local label | ||
containers, err := docker.ContainerList(context.Background(), types.ContainerListOptions{ | ||
Filters: filters.NewArgs( | ||
filters.Arg("label", ecsLocalLabelKey), | ||
), | ||
}) | ||
if err != nil { | ||
logrus.Fatalf("Failed to list containers with label=%s due to %v", ecsLocalLabelKey, err) | ||
} | ||
return containers | ||
} | ||
|
||
func displayAsJSON(containers []types.Container) { | ||
data, err := json.MarshalIndent(containers, jsonPrefix, jsonIndent) | ||
if err != nil { | ||
logrus.Fatalf("Failed to marshal containers to JSON due to %v", err) | ||
} | ||
fmt.Fprintln(os.Stdout, string(data)) | ||
} | ||
|
||
func displayAsTable(containers []types.Container) { | ||
w := new(tabwriter.Writer) | ||
|
||
w.Init(os.Stdout, cellWidthInSpaces, widthBetweenCellsInSpaces, cellPaddingInSpaces, paddingCharacter, noFormatting) | ||
fmt.Fprintln(w, "CONTAINER ID\tIMAGE\tSTATUS\tPORTS\tNAMES\tTASKDEFINITIONARN\tTASKFILEPATH") | ||
for _, container := range containers { | ||
row := fmt.Sprintf("%s\t%s\t%s\t%s\t%s\t%s\t%s", | ||
container.ID[:maxContainerIDLength], | ||
container.Image, | ||
container.Status, | ||
prettifyPorts(container.Ports), | ||
prettifyNames(container.Names), | ||
container.Labels[taskDefinitionARNLabelKey], | ||
container.Labels[taskFilePathLabelKey]) | ||
fmt.Fprintln(w, row) | ||
} | ||
w.Flush() | ||
} | ||
|
||
func prettifyPorts(containerPorts []types.Port) string { | ||
var prettyPorts []string | ||
for _, port := range containerPorts { | ||
// See https://github.com/docker/cli/blob/0904fbfc77dbd4b6296c56e68be573b889d049e3/cli/command/formatter/container.go#L268 | ||
prettyPorts = append(prettyPorts, fmt.Sprintf("%s:%d->%d/%s", port.IP, port.PublicPort, port.PrivatePort, port.Type)) | ||
} | ||
return strings.Join(prettyPorts, ", ") | ||
} | ||
|
||
func prettifyNames(containerNames []string) string { | ||
return strings.Join(containerNames, ", ") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2015-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package local | ||
|
||
import ( | ||
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/local/network" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
// Stop stops a running local ECS task. | ||
// | ||
// If the user stops the last running task in the local network then also remove the network. | ||
func Stop(c *cli.Context) { | ||
docker := newDockerClient() | ||
defer network.Teardown(docker) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why this is a table driven test?
Looks like it only has one test case...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
soon™ we'll have full ecs-cli local workflows as integration tests :P.
This sequence for example will make up on test case: "ecs-cli local create --arn" -> "ecs-cli local up" -> "ecs-cli local ps" -> "ecs-cli local stop"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😎