Skip to content

Commit

Permalink
add util method to get Docker container names (localstack#1641)
Browse files Browse the repository at this point in the history
  • Loading branch information
whummer authored Oct 10, 2019
1 parent b90c9ad commit 3975728
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
9 changes: 7 additions & 2 deletions localstack/utils/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,16 @@ def load_plugins(scope=None):


def docker_container_running(container_name):
output = to_str(run("docker ps --format '{{.Names}}'"))
container_names = re.split(r'\s+', output.replace('\n', ' '))
container_names = get_docker_container_names()
return container_name in container_names


def get_docker_container_names():
output = to_str(run("docker ps --format '{{.Names}}'"))
container_names = re.split(r'\s+', output.strip().replace('\n', ' '))
return container_names


def setup_logging():
# determine and set log level
log_level = logging.DEBUG if is_debug() else logging.INFO
Expand Down
12 changes: 4 additions & 8 deletions localstack/utils/cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import re
from docopt import docopt
from localstack import config
from localstack.utils.bootstrap import (
start_infra_in_docker, start_infra_locally, run, to_str, MAIN_CONTAINER_NAME)
start_infra_in_docker, start_infra_locally, run, MAIN_CONTAINER_NAME, docker_container_running)

# Note: make sure we don't have imports at the root level here

Expand Down Expand Up @@ -67,13 +66,10 @@ def cmd_ssh(argv, args):
Options:
"""
args.update(docopt(cmd_ssh.__doc__.strip(), argv=argv))
lines = to_str(run('docker ps')).split('\n')[1:]
lines = [l for l in lines if MAIN_CONTAINER_NAME in l]
if len(lines) != 1:
raise Exception('Expected 1 running "%s" container, but found %s' % (MAIN_CONTAINER_NAME, len(lines)))
cid = re.split(r'\s', lines[0])[0]
if not docker_container_running(MAIN_CONTAINER_NAME):
raise Exception('Expected 1 running "%s" container, but found none' % MAIN_CONTAINER_NAME)
try:
process = run('docker exec -it %s bash' % cid, tty=True)
process = run('docker exec -it %s bash' % MAIN_CONTAINER_NAME, tty=True)
process.wait()
except KeyboardInterrupt:
pass
4 changes: 4 additions & 0 deletions localstack/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ def has_docker():
return False


def get_docker_container_names():
return bootstrap.get_docker_container_names()


def is_port_open(port_or_url, http_path=None, expect_success=True, protocols=['tcp']):
port = port_or_url
host = 'localhost'
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ forbiddenfruit==0.1.3
jsonpath-rw==1.4.0
localstack-ext[full]>=0.10.2
localstack-ext>=0.10.2 #basic-lib
localstack-client>=0.12 #basic-lib
localstack-client>=0.14 #basic-lib
moto-ext>=1.3.14.1
nose>=1.3.7
nose-timer>=0.7.5
Expand Down

0 comments on commit 3975728

Please sign in to comment.