From b698377fe0534c595b09521007394d3923584877 Mon Sep 17 00:00:00 2001 From: Travis Thieman Date: Wed, 17 Feb 2016 17:17:22 -0500 Subject: [PATCH] Add dusty shutdown command --- dusty/cli/__init__.py | 6 ++++-- dusty/cli/shutdown.py | 14 ++++++++++++++ dusty/commands/run.py | 10 +++++++++- dusty/systems/virtualbox/__init__.py | 7 +++++++ 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 dusty/cli/shutdown.py diff --git a/dusty/cli/__init__.py b/dusty/cli/__init__.py index 063e1417..c5802d8d 100644 --- a/dusty/cli/__init__.py +++ b/dusty/cli/__init__.py @@ -17,6 +17,7 @@ scripts Execute predefined scripts inside running containers setup Configure Dusty after installation shell Open a shell inside a running container + shutdown Shut down the Dusty VM status Show info on activated apps, services and libs stop Stop Dusty-managed containers test Run test scripts in isolated environments @@ -44,8 +45,8 @@ from ..config import get_config_value from ..log import configure_client_logging, log_to_client from ..payload import Payload -from . import (assets, bundles, config, cp, dump, disk, env, logs, repos, restart, scripts, shell, stop, - up, upgrade, validate, version, setup, test, status) +from . import (assets, bundles, config, cp, dump, disk, env, logs, repos, restart, scripts, shell, + shutdown, stop, up, upgrade, validate, version, setup, test, status) from .. import constants MODULE_MAP = { @@ -62,6 +63,7 @@ 'scripts': scripts, 'setup': setup, 'shell': shell, + 'shutdown': shutdown, 'status': status, 'stop': stop, 'test': test, diff --git a/dusty/cli/shutdown.py b/dusty/cli/shutdown.py new file mode 100644 index 00000000..f22b7c89 --- /dev/null +++ b/dusty/cli/shutdown.py @@ -0,0 +1,14 @@ +"""Shut down the Dusty VM. + +Usage: + shutdown +""" + +from docopt import docopt + +from ..payload import Payload +from ..commands.run import shutdown_dusty_vm + +def main(argv): + args = docopt(__doc__, argv) + return Payload(shutdown_dusty_vm) diff --git a/dusty/commands/run.py b/dusty/commands/run.py index e2129c41..6a934155 100644 --- a/dusty/commands/run.py +++ b/dusty/commands/run.py @@ -7,6 +7,7 @@ from ..systems.docker import compose from ..systems.docker.config import (get_authed_registries, registry_from_image, log_in_to_registry) +from ..systems.virtualbox import docker_vm_is_running, shut_down_docker_vm from ..log import log_to_client from .repos import update_managed_repos from .. import constants @@ -14,7 +15,7 @@ from ..source import Repo from ..payload import daemon_command from ..warnings import daemon_warnings -from ..subprocess import check_call_demoted +from ..subprocess import check_call_demoted, check_and_log_output_and_error_demoted @daemon_command def prep_for_start_local_env(pull_repos): @@ -139,3 +140,10 @@ def restart_apps_by_repo(repo_names): if spec_assembler.get_same_container_repos_from_spec(app_spec).intersection(resolved_repos): apps_with_repos.add(app_spec.name) restart_apps_or_services(apps_with_repos) + +@daemon_command +def shutdown_dusty_vm(): + if docker_vm_is_running(): + stop_apps_or_services() + log_to_client('Shutting down Dusty VM') + shut_down_docker_vm() diff --git a/dusty/systems/virtualbox/__init__.py b/dusty/systems/virtualbox/__init__.py index 1947f4fe..6614572d 100644 --- a/dusty/systems/virtualbox/__init__.py +++ b/dusty/systems/virtualbox/__init__.py @@ -164,6 +164,13 @@ def initialize_docker_vm(): _ensure_cp_dir_exists() _ensure_assets_dir_exists() +def shut_down_docker_vm(): + if docker_vm_is_running(): + check_call_demoted(['docker-machine', 'stop', constants.VM_MACHINE_NAME]) + log_to_client('Dusty VM was shut down') + else: + log_to_client('Dusty VM is already shut down') + @memoized def get_docker_vm_ip(): ssh_port = None