forked from opensearch-project/opensearch-migrations
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved Docker "run" command logging (opensearch-project#109)
* Improved Docker "run" command logging * Added a new log statement that generates an equivalent 'docker run' command to what the Docker Python SDK executes Signed-off-by: Chris Helma <chelma+github@amazon.com> * Minor changes per PR comments opensearch-project#109 Signed-off-by: Chris Helma <chelma+github@amazon.com> * More minor changes per PR comments opensearch-project#109 Signed-off-by: Chris Helma <chelma+github@amazon.com> --------- Signed-off-by: Chris Helma <chelma+github@amazon.com>
- Loading branch information
Showing
6 changed files
with
174 additions
and
5 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
cluster_migration_core/cluster_migration_core/cluster_management/docker_command_gen.py
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,58 @@ | ||
from typing import Dict, List | ||
|
||
from docker.types import Ulimit | ||
|
||
""" | ||
The code in this module can be used to generate Docker CLI commands equivalent to those run by the Python Docker SDK. | ||
This is useful for debugging when things go wrong, as the SDK does not print the full commands at any log level. The | ||
generation functions will mirror the arguments list of their corresponding SDK command. | ||
Eventually, we may end up using this module to replace the Docker SDK by running these commands directly. The SDK has | ||
a number of rough edges, like the fact that it doesn't log the actual commands it is running, which makes it tough to | ||
use. | ||
""" | ||
|
||
|
||
def gen_docker_run(image: str, name: str, network: str, ports: Dict[str, str], volumes: Dict[str, Dict[str, str]], | ||
ulimits: List[Ulimit], detach: bool, environment: Dict[str, str], | ||
extra_hosts: Dict[str, str]) -> str: | ||
prefix = "docker run" | ||
name_section = f"--name {name}" | ||
network_section = f"--network {network}" | ||
publish_strs = [f"--publish {host_port}:{container_port}" for container_port, host_port in ports.items()] | ||
publish_section = " ".join(publish_strs) | ||
volumes_section = " ".join([f"--volume {k}:{v['bind']}:{v['mode']}" for k, v in volumes.items()]) | ||
ulimits_section = " ".join([f"--ulimit {u.name}={u.soft}:{u.hard}" for u in ulimits]) | ||
environment_section = " ".join([f"--env {k}='{v}'" for k, v in environment.items()]) | ||
extra_hosts_section = " ".join([f"--add-host {k}:{v}" for k, v in extra_hosts.items()]) | ||
detach_section = "--detach" if detach else "" | ||
image_section = image | ||
|
||
command_sections = [ | ||
prefix, | ||
name_section, | ||
network_section, | ||
publish_section, | ||
volumes_section, | ||
ulimits_section, | ||
environment_section, | ||
extra_hosts_section, | ||
detach_section, | ||
image_section # Needs to be last | ||
] | ||
|
||
return _pretty_join(command_sections, " ") | ||
|
||
|
||
def _pretty_join(command_sections: List[str], delimiter: str) -> str: | ||
""" | ||
A quick function to handle empty sections better than the default .join() method. Avoids inserting spaces when a | ||
section is blank. | ||
""" | ||
non_empty_sections = [] | ||
|
||
for section in command_sections: | ||
if section.strip(): # not empty | ||
non_empty_sections.append(section) | ||
|
||
return delimiter.join(non_empty_sections) |
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
65 changes: 65 additions & 0 deletions
65
cluster_migration_core/unit_tests/cluster_management/test_docker_command_gen.py
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,65 @@ | ||
from docker.types import Ulimit | ||
|
||
import cluster_migration_core.cluster_management.docker_command_gen as dcg | ||
|
||
|
||
def test_WHEN_gen_run_command_THEN_as_expected(): | ||
# Set up our test | ||
test_args = { | ||
"image": "image", | ||
"name": "container_name", | ||
"network": "network_name", | ||
"ports": {9200: 80, 6160: 42}, | ||
"volumes": { | ||
"/mydir1": {"bind": "/path", "mode": "ro"}, | ||
"volume1": {"bind": "/path2", "mode": "rw"} | ||
}, | ||
"ulimits": [ | ||
Ulimit(name='limit', soft=1, hard=2) | ||
], | ||
"detach": True, | ||
"environment": { | ||
"a": "b", | ||
"c": "d" | ||
}, | ||
"extra_hosts": { | ||
"name": "host" | ||
} | ||
} | ||
|
||
# Run our test | ||
generated_command = dcg.gen_docker_run(**test_args) | ||
|
||
# Check our results | ||
expected_command = ("docker run --name container_name --network network_name --publish 80:9200 --publish 42:6160" | ||
" --volume /mydir1:/path:ro --volume volume1:/path2:rw --ulimit limit=1:2 --env a='b'" | ||
" --env c='d' --add-host name:host --detach image") | ||
assert expected_command == generated_command | ||
|
||
|
||
def test_WHEN_gen_run_command_2_THEN_as_expected(): | ||
# Set up our test | ||
test_args = { | ||
"image": "image", | ||
"name": "container_name", | ||
"network": "network_name", | ||
"ports": {9200: 80, 6160: 42}, | ||
"volumes": {}, | ||
"ulimits": [ | ||
Ulimit(name='limit', soft=1, hard=2) | ||
], | ||
"detach": False, | ||
"environment": { | ||
"a": "b", | ||
"c": "d" | ||
}, | ||
"extra_hosts": {} | ||
} | ||
|
||
# Run our test | ||
generated_command = dcg.gen_docker_run(**test_args) | ||
|
||
# Check our results | ||
expected_command = ("docker run --name container_name --network network_name --publish 80:9200 --publish 42:6160" | ||
" --ulimit limit=1:2 --env a='b' --env c='d' image") | ||
assert expected_command == generated_command |
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