diff --git a/rtwcli/rtw_cmds/rtw_cmds/docker/verbs.py b/rtwcli/rtw_cmds/rtw_cmds/docker/verbs.py index 170ede4f..9cbd99eb 100644 --- a/rtwcli/rtw_cmds/rtw_cmds/docker/verbs.py +++ b/rtwcli/rtw_cmds/rtw_cmds/docker/verbs.py @@ -16,11 +16,61 @@ from rtwcli.docker_utils import ( docker_exec_interactive_bash, docker_start, + docker_stop, fix_missing_xauth_file, is_docker_container_running, ) from rtwcli.verb import VerbExtension -from rtwcli.workspace_utils import get_current_workspace +from rtwcli.workspace_utils import Workspace, get_current_workspace + + +def check_workspace(ws: Workspace) -> bool: + if not ws: + logger.info("No workspace is active.") + return False + + if not ws.ws_docker_support: + logger.info("The workspace does not support docker.") + return False + + if not ws.docker_container_name: + logger.error("The workspace is missing the docker_container_name attribute.") + return False + + return True + + +def container_start_routine(ws: Workspace) -> bool: + if not check_workspace(ws): + return False + + if not is_docker_container_running(ws.docker_container_name): + logger.info( + f"The docker container '{ws.docker_container_name}' is not running, starting it now." + ) + # fix missing .xauth file if it is not present + if not fix_missing_xauth_file(ws.docker_container_name): + logger.error(f"Failed to fix missing .xauth file for '{ws.docker_container_name}'.") + return False + + if not docker_start(ws.docker_container_name): + logger.error(f"Failed to start docker container '{ws.docker_container_name}'.") + return False + + return True + + +def container_stop_routine(ws: Workspace) -> bool: + if not check_workspace(ws): + return False + + if is_docker_container_running(ws.docker_container_name): + logger.info(f"Stopping docker container '{ws.docker_container_name}'.") + if not docker_stop(ws.docker_container_name): + logger.error(f"Failed to stop docker container '{ws.docker_container_name}'.") + return False + + return True class EnterVerb(VerbExtension): @@ -28,31 +78,30 @@ class EnterVerb(VerbExtension): def main(self, *, args): ws = get_current_workspace() - if not ws: - logger.info("No workspace is active.") + if not container_start_routine(ws): return + docker_exec_interactive_bash(ws.docker_container_name) - if not ws.ws_docker_support: - logger.info("The workspace does not support docker.") - return - if not ws.docker_container_name: - logger.error("The workspace is missing the docker_container_name attribute.") - return +class StartVerb(VerbExtension): + """Start docker container.""" - if not is_docker_container_running(ws.docker_container_name): - logger.info( - f"The docker container '{ws.docker_container_name}' is not running, starting it now." - ) - # fix missing .xauth file if it is not present - if not fix_missing_xauth_file(ws.docker_container_name): - logger.error( - f"Failed to fix missing .xauth file for '{ws.docker_container_name}'." - ) - return - - if not docker_start(ws.docker_container_name): - logger.error(f"Failed to start docker container '{ws.docker_container_name}'.") - return + def main(self, *, args): + container_start_routine(get_current_workspace()) - docker_exec_interactive_bash(ws.docker_container_name) + +class StopVerb(VerbExtension): + """Stop docker container.""" + + def main(self, *, args): + container_stop_routine(get_current_workspace()) + + +class RestartVerb(VerbExtension): + """Restart docker container.""" + + def main(self, *, args): + ws = get_current_workspace() + if not container_stop_routine(ws): + return + container_start_routine(ws) diff --git a/rtwcli/rtw_cmds/setup.py b/rtwcli/rtw_cmds/setup.py index bc92cc23..37d0514e 100644 --- a/rtwcli/rtw_cmds/setup.py +++ b/rtwcli/rtw_cmds/setup.py @@ -49,6 +49,9 @@ ], "rtw_cmds.docker.verbs": [ "enter = rtw_cmds.docker.verbs:EnterVerb", + "restart = rtw_cmds.docker.verbs:RestartVerb", + "start = rtw_cmds.docker.verbs:StartVerb", + "stop = rtw_cmds.docker.verbs:StopVerb", ], "rtw_cmds.pkg.verbs": [ "create = rtw_cmds.pkg.verbs:CreateVerb",