Skip to content

Commit

Permalink
[rtwcli] rtw docker restart/start/stop (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
muritane authored Nov 1, 2024
1 parent 2da48fd commit 2860b2d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 24 deletions.
97 changes: 73 additions & 24 deletions rtwcli/rtw_cmds/rtw_cmds/docker/verbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,92 @@
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):
"""Enter docker container."""

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)
3 changes: 3 additions & 0 deletions rtwcli/rtw_cmds/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 2860b2d

Please sign in to comment.