Skip to content

Commit

Permalink
fix(core): update manager downloads the latest install.sh and runs …
Browse files Browse the repository at this point in the history
…it to do the update - closes #152
  • Loading branch information
sassanh committed Aug 10, 2024
1 parent ae8e343 commit 058e211
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 29 deletions.
4 changes: 4 additions & 0 deletions ubo_app/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
from str_to_bool import str_to_bool

USERNAME = os.environ.get('UBO_USERNAME', 'ubo')
INSTALLER_URL = os.environ.get(
'UBO_INSTALLER_URL',
'https://raw.githubusercontent.com/ubopod/ubo-app/main/ubo_app/system/install.sh',
)
INSTALLATION_PATH = os.environ.get('UBO_INSTALLATION_PATH', '/opt/ubo')
DEBUG_MODE = str_to_bool(os.environ.get('UBO_DEBUG', 'False')) == 1
LOG_LEVEL = os.environ.get('UBO_LOG_LEVEL', 'DEBUG' if DEBUG_MODE else None)
Expand Down
78 changes: 51 additions & 27 deletions ubo_app/store/update_manager/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
from ubo_gui.constants import DANGER_COLOR, INFO_COLOR, SUCCESS_COLOR
from ubo_gui.menu.types import ActionItem, Item

from ubo_app.constants import INSTALLATION_PATH
from ubo_app.constants import INSTALLATION_PATH, INSTALLER_URL
from ubo_app.logging import logger
from ubo_app.store.core import RebootEvent
from ubo_app.store.main import autorun, dispatch
from ubo_app.store.services.notifications import (
Chime,
Expand Down Expand Up @@ -71,32 +72,63 @@ async def check_version() -> None:
async def update() -> None:
"""Update the Ubo app."""
logger.info('Updating Ubo app...')
dispatch(
NotificationsAddAction(
notification=Notification(
id='ubo:update_manager',
title='Updating...',
content='Fetching the latest version of Ubo app...',
display_type=NotificationDisplayType.BACKGROUND,
color=INFO_COLOR,
icon='󰇚',
blink=False,
progress=0,
),
),
)

async def download_files() -> None:
dispatch(
NotificationsAddAction(
notification=Notification(
id='ubo:update_manager',
title='Updating...',
content='Downloading the latest version of the install script...',
display_type=NotificationDisplayType.BACKGROUND,
color=INFO_COLOR,
icon='󰇚',
blink=False,
progress=0,
),
),
)

target_path = Path(f'{INSTALLATION_PATH}/_update/')
shutil.rmtree(target_path, ignore_errors=True)
target_path.mkdir(parents=True, exist_ok=True)

packages_count_path = f'{INSTALLATION_PATH}/.packages-count'

try:
packages_count = int(Path(packages_count_path).read_text(encoding='utf-8'))
except FileNotFoundError:
packages_count = 55
packages_count *= 2
packages_count += 1
counter = 0

process = await asyncio.create_subprocess_exec(
'/usr/bin/env',
'curl',
'-Lk',
INSTALLER_URL,
'--output',
f'{target_path}/install.sh',
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
await process.wait()
Path(f'{target_path}/install.sh').chmod(0o755)

dispatch(
NotificationsAddAction(
notification=Notification(
id='ubo:update_manager',
title='Updating...',
content='Fetching the latest version of Ubo app...',
display_type=NotificationDisplayType.BACKGROUND,
color=INFO_COLOR,
icon='󰇚',
blink=False,
progress=1 / packages_count,
),
),
)

process = await asyncio.create_subprocess_exec(
'/usr/bin/env',
Expand Down Expand Up @@ -127,7 +159,7 @@ async def download_files() -> None:
UpdateManagerSetStatusAction(status=UpdateStatus.CHECKING),
)
return
counter = 0

while True:
line = (await process.stdout.readline()).decode()
if not line:
Expand All @@ -144,7 +176,7 @@ async def download_files() -> None:
color=INFO_COLOR,
icon='󰇚',
blink=False,
progress=min(counter / (packages_count * 2), 1),
progress=min((counter + 1) / packages_count, 1),
),
),
)
Expand Down Expand Up @@ -177,15 +209,7 @@ async def download_files() -> None:

await asyncio.sleep(2)

process = await asyncio.create_subprocess_exec(
'/usr/bin/env',
'systemctl',
'reboot',
'-i',
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
await process.wait()
dispatch(RebootEvent())
except Exception:
logger.exception('Failed to update')
dispatch(
Expand Down
4 changes: 2 additions & 2 deletions ubo_app/system/services/update.service
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ Type=oneshot
User=root
Environment=INSTALLATION_PATH={{INSTALLATION_PATH}}
Environment=USERNAME={{USERNAME}}
ExecStartPre=/bin/bash -c '[[ $(ls {{INSTALLATION_PATH}}/_update/update_is_ready) ]]'
ExecStart={{INSTALLATION_PATH}}/env/lib/python3.11/site-packages/ubo_app/system/install.sh --update
ExecStartPre=/bin/bash -c '[[ -e /opt/ubo/_update/update_is_ready ]]'
ExecStart={{INSTALLATION_PATH}}/_update/install.sh --update
RemainAfterExit=no

0 comments on commit 058e211

Please sign in to comment.