Skip to content

Commit

Permalink
refactor(system): add response for docker commands and service commands
Browse files Browse the repository at this point in the history
  • Loading branch information
sassanh committed Aug 24, 2024
1 parent eb225b6 commit 0841e57
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 67 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Version 0.15.10

- refactor(core): use `dpkg-query` instead of `apt` python api as loading `Cache` in `apt` is slow and use it in docker service
- refactor(system): add response for docker commands and service commands

## Version 0.15.9

Expand Down
3 changes: 1 addition & 2 deletions ubo_app/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@
DEBUG_MODE_TEST_UUID = str_to_bool(os.environ.get('UBO_DEBUG_TEST_UUID', 'False')) == 1

PICOVOICE_ACCESS_KEY = 'PICOVOICE_ACCESS_KEY'
DOCKER_CREDENTIALS_TEMPLATE = 'DOCKER_CREDENTIALS_{}'

DEBUG_MODE_DOCKER = str_to_bool(os.environ.get('UBO_DEBUG_DOCKER', 'False')) == 1
DOCKER_CREDENTIALS_TEMPLATE = 'DOCKER_CREDENTIALS_{}'
DOCKER_PREFIX = os.environ.get('UBO_DOCKER_PREFIX', '')
DOCKER_INSTALLATION_LOCK_FILE = Path('/var/run/ubo/docker_installation.lock')

CONFIG_PATH = platformdirs.user_config_path(appname='ubo', ensure_exists=True)
SECRETS_PATH = CONFIG_PATH / '.secrets.env'
Expand Down
10 changes: 3 additions & 7 deletions ubo_app/store/update_manager/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,8 @@ def _(state: _UpdateManagerServiceState) -> None:
id=UPDATE_MANAGER_NOTIFICATION_ID,
title='Update in progress',
content="""\
Please keep the device powered on.
This may take around 20 minutes to complete.""",
Please keep the device powered on.
This may take around 20 minutes to complete.""",
importance=Importance.LOW,
icon='󰚰',
display_type=NotificationDisplayType.BACKGROUND
Expand All @@ -376,8 +376,4 @@ def _(state: _UpdateManagerServiceState) -> None:
),
)
else:
dispatch(
NotificationsClearByIdAction(
id=UPDATE_MANAGER_NOTIFICATION_ID,
),
)
dispatch(NotificationsClearByIdAction(id=UPDATE_MANAGER_NOTIFICATION_ID))
103 changes: 49 additions & 54 deletions ubo_app/system/system_manager/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,55 @@
import subprocess
from pathlib import Path

from ubo_app.constants import DOCKER_INSTALLATION_LOCK_FILE, USERNAME
from ubo_app.constants import USERNAME


def docker_handler(command: str) -> None:
def docker_handler(command: str) -> str | None:
"""Install and start Docker on the host machine."""
Path.touch(
DOCKER_INSTALLATION_LOCK_FILE,
mode=0o666,
exist_ok=True,
)
try:
if command == 'install':
subprocess.run( # noqa: S603
Path(__file__).parent.parent.joinpath('install_docker.sh'),
env={'USERNAME': USERNAME},
check=False,
)
elif command == 'start':
subprocess.run( # noqa: S603
[
'/usr/bin/env',
'systemctl',
'start',
'docker.socket',
],
check=False,
)
subprocess.run( # noqa: S603
[
'/usr/bin/env',
'systemctl',
'start',
'docker.service',
],
check=False,
)
elif command == 'stop':
subprocess.run( # noqa: S603
[
'/usr/bin/env',
'systemctl',
'stop',
'docker.socket',
],
check=False,
)
subprocess.run( # noqa: S603
[
'/usr/bin/env',
'systemctl',
'stop',
'docker.service',
],
check=False,
)
finally:
DOCKER_INSTALLATION_LOCK_FILE.unlink(missing_ok=True)
if command == 'install':
subprocess.run( # noqa: S603
Path(__file__).parent.parent.joinpath('install_docker.sh'),
env={'USERNAME': USERNAME},
check=False,
)
return 'installed'

if command == 'start':
subprocess.run( # noqa: S603
[
'/usr/bin/env',
'systemctl',
'start',
'docker.socket',
],
check=False,
)
subprocess.run( # noqa: S603
[
'/usr/bin/env',
'systemctl',
'start',
'docker.service',
],
check=False,
)
elif command == 'stop':
subprocess.run( # noqa: S603
[
'/usr/bin/env',
'systemctl',
'stop',
'docker.socket',
],
check=False,
)
subprocess.run( # noqa: S603
[
'/usr/bin/env',
'systemctl',
'stop',
'docker.service',
],
check=False,
)
return 'done'
2 changes: 1 addition & 1 deletion ubo_app/system/system_manager/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def handle_command(command: str) -> str | None:
if header == 'led':
led_manager.run_command_thread_safe(arguments)
elif header == 'docker':
docker_handler(arguments[0])
return docker_handler(arguments[0])
elif header == 'service':
return service_handler(arguments[0], arguments[1])
elif header == 'package':
Expand Down
1 change: 1 addition & 0 deletions ubo_app/system/system_manager/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ubo_app.utils.apt import install_package, uninstall_package

PACKAGE_WHITELIST = [
'lightdm',
'rpi-connect',
]

Expand Down
8 changes: 5 additions & 3 deletions ubo_app/system/system_manager/service_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def ssh_handler(command: str) -> str | None:
)
result.check_returncode()
return result.stdout

if command == 'clear_all_temporary_accounts':
subprocess.run( # noqa: S603
Path(__file__).parent.joinpath('clear_all_temporary_accounts.sh'),
Expand Down Expand Up @@ -47,10 +48,10 @@ def ssh_handler(command: str) -> str | None:
else:
msg = f'Invalid ssh command "{command}"'
raise ValueError(msg)
return None
return 'done'


def lightdm_handler(command: str) -> None:
def lightdm_handler(command: str) -> str | None:
"""Handle LightDM commands."""
if command == 'start':
subprocess.run( # noqa: S603
Expand All @@ -73,8 +74,9 @@ def lightdm_handler(command: str) -> None:
check=True,
)
else:
msg = f'Invalid ssh command "{command}"'
msg = f'Invalid LightDM command "{command}"'
raise ValueError(msg)
return 'done'


def service_handler(service: str, command: str) -> str | None:
Expand Down

0 comments on commit 0841e57

Please sign in to comment.