-
Notifications
You must be signed in to change notification settings - Fork 302
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 down" to stop a running local task #783
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2622bd2
Rename "local stop" to "local down"
efekarakus 1fc99a2
Simplify success logs for network teardown
efekarakus 86a7f59
Support "local down" to stop and remove a running local ECS task
efekarakus 58c7c55
Skip network removal if it doesn't exist
efekarakus 1511e3b
Add integ test for "local down" when there are no tasks running
efekarakus 28f58a0
Add comment explaining priority of flags for "local down"
efekarakus cc77415
Move docker.go to its own sub-package
efekarakus 7aad85d
Update comment to explain we don't count the endpoints container
efekarakus 4af9e8a
Rename docker.NewDockerClient to docker.NewClient
efekarakus c39bdb1
Update "local down" to have only the "--all" flag
efekarakus 887f190
Add test case for calling "local up" and "local down"
efekarakus e7a7166
Update buildspec to pull the endpoints container image
efekarakus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// 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" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/local/docker" | ||
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/local/network" | ||
"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/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 ( | ||
ecsLocalDockerComposeFileName = "docker-compose.local.yml" | ||
) | ||
|
||
// Down stops and removes running local ECS tasks. | ||
// If the user stops the last running task in the local network then also remove the network. | ||
func Down(c *cli.Context) error { | ||
defer func() { | ||
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. Nice use of defer 👍🏻 |
||
client := docker.NewClient() | ||
network.Teardown(client) | ||
}() | ||
|
||
if c.Bool(flags.AllFlag) { | ||
return downAllLocalContainers() | ||
} | ||
return downComposeLocalContainers() | ||
} | ||
|
||
func downComposeLocalContainers() error { | ||
wd, _ := os.Getwd() | ||
if _, err := os.Stat(filepath.Join(wd, ecsLocalDockerComposeFileName)); os.IsNotExist(err) { | ||
logrus.Warnf("Compose file %s does not exist in current directory", ecsLocalDockerComposeFileName) | ||
return nil | ||
} | ||
|
||
logrus.Infof("Running Compose down on %s", ecsLocalDockerComposeFileName) | ||
cmd := exec.Command("docker-compose", "-f", ecsLocalDockerComposeFileName, "down") | ||
_, err := cmd.CombinedOutput() | ||
if err != nil { | ||
logrus.Fatalf("Failed to run docker-compose down due to %v", err) | ||
} | ||
|
||
logrus.Info("Stopped and removed containers successfully") | ||
return nil | ||
} | ||
|
||
func downAllLocalContainers() error { | ||
ctx, cancel := context.WithTimeout(context.Background(), docker.TimeoutInS) | ||
defer cancel() | ||
|
||
client := docker.NewClient() | ||
containers, err := client.ContainerList(ctx, types.ContainerListOptions{ | ||
Filters: filters.NewArgs( | ||
filters.Arg("label", ecsLocalLabelKey), | ||
), | ||
All: true, | ||
}) | ||
if err != nil { | ||
logrus.Fatalf("Failed to list containers with label=%s due to %v", ecsLocalLabelKey, err) | ||
} | ||
if len(containers) == 0 { | ||
logrus.Warn("No running ECS local tasks found") | ||
return nil | ||
} | ||
|
||
logrus.Infof("Stop and remove %d container(s)", len(containers)) | ||
for _, container := range containers { | ||
if err = client.ContainerStop(ctx, container.ID, nil); err != nil { | ||
logrus.Fatalf("Failed to stop container %s due to %v", container.ID[:maxContainerIDLength], err) | ||
} | ||
logrus.Infof("Stopped container with id %s", container.ID[:maxContainerIDLength]) | ||
|
||
if err = client.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{}); err != nil { | ||
logrus.Fatalf("Failed to remove container %s due to %v", container.ID[:maxContainerIDLength], err) | ||
} | ||
logrus.Infof("Removed container with id %s", container.ID[:maxContainerIDLength]) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
👍🏻