Tweak tools update/upgrade to have a single 'target' arg for simpler routing

This commit is contained in:
Alexandre Aubin 2021-03-19 17:45:37 +01:00
parent 82d5be6802
commit 1fb9ddd42a
4 changed files with 63 additions and 33 deletions

View file

@ -1411,25 +1411,40 @@ tools:
### tools_update() ### tools_update()
update: update:
action_help: YunoHost update action_help: YunoHost update
api: PUT /update api: PUT /update/<target>
arguments: arguments:
target:
help: What to update, "apps" (application catalog) or "system" (fetch available package upgrades, equivalent to apt update), "all" for both
choices:
- apps
- system
- all
nargs: "?"
metavar: TARGET
default: all
--apps: --apps:
help: Fetch the application list to check which apps can be upgraded help: (Deprecated, see first positional arg) Fetch the application list to check which apps can be upgraded
action: store_true action: store_true
--system: --system:
help: Fetch available system packages upgrades (equivalent to apt update) help: (Deprecated, see first positional arg) Fetch available system packages upgrades (equivalent to apt update)
action: store_true action: store_true
### tools_upgrade() ### tools_upgrade()
upgrade: upgrade:
action_help: YunoHost upgrade action_help: YunoHost upgrade
api: PUT /upgrade api: PUT /upgrade/<target>
arguments: arguments:
target:
help: What to upgrade, either "apps" (all apps) or "system" (all system packages)
choices:
- apps
- system
nargs: "?"
--apps: --apps:
help: List of apps to upgrade (all by default) help: (Deprecated, see first positional arg) Upgrade all applications
nargs: "*" action: store_true
--system: --system:
help: Upgrade only the system packages help: (Deprecated, see first positional arg) Upgrade only the system packages
action: store_true action: store_true
### tools_shell() ### tools_shell()

View file

@ -147,7 +147,7 @@ def app_fetchlist():
) )
from yunohost.tools import tools_update from yunohost.tools import tools_update
tools_update(apps=True) tools_update(target="apps")
def app_list(full=False, installed=False, filter=None): def app_list(full=False, installed=False, filter=None):

View file

@ -43,7 +43,7 @@ class MyMigration(Migration):
# #
logger.info(m18n.n("migration_0015_patching_sources_list")) logger.info(m18n.n("migration_0015_patching_sources_list"))
self.patch_apt_sources_list() self.patch_apt_sources_list()
tools_update(system=True) tools_update(target="system")
# Tell libc6 it's okay to restart system stuff during the upgrade # Tell libc6 it's okay to restart system stuff during the upgrade
os.system( os.system(
@ -88,7 +88,7 @@ class MyMigration(Migration):
apps_packages = self.get_apps_equivs_packages() apps_packages = self.get_apps_equivs_packages()
self.hold(apps_packages) self.hold(apps_packages)
tools_upgrade(system=True, allow_yunohost_upgrade=False) tools_upgrade(target="system", allow_yunohost_upgrade=False)
if self.debian_major_version() == 9: if self.debian_major_version() == 9:
raise YunohostError("migration_0015_still_on_stretch_after_main_upgrade") raise YunohostError("migration_0015_still_on_stretch_after_main_upgrade")
@ -103,7 +103,7 @@ class MyMigration(Migration):
# #
logger.info(m18n.n("migration_0015_yunohost_upgrade")) logger.info(m18n.n("migration_0015_yunohost_upgrade"))
self.unhold(apps_packages) self.unhold(apps_packages)
tools_upgrade(system=True) tools_upgrade(target="system")
def debian_major_version(self): def debian_major_version(self):
# The python module "platform" and lsb_release are not reliable because # The python module "platform" and lsb_release are not reliable because
@ -141,7 +141,7 @@ class MyMigration(Migration):
# (but we don't if 'stretch' is already in the sources.list ... # (but we don't if 'stretch' is already in the sources.list ...
# which means maybe a previous upgrade crashed and we're re-running it) # which means maybe a previous upgrade crashed and we're re-running it)
if " buster " not in read_file("/etc/apt/sources.list"): if " buster " not in read_file("/etc/apt/sources.list"):
tools_update(system=True) tools_update(target="system")
upgradable_system_packages = list(_list_upgradable_apt_packages()) upgradable_system_packages = list(_list_upgradable_apt_packages())
if upgradable_system_packages: if upgradable_system_packages:
raise YunohostError("migration_0015_system_not_fully_up_to_date") raise YunohostError("migration_0015_system_not_fully_up_to_date")

View file

@ -404,22 +404,29 @@ def tools_regen_conf(
return regen_conf(names, with_diff, force, dry_run, list_pending) return regen_conf(names, with_diff, force, dry_run, list_pending)
def tools_update(apps=False, system=False): def tools_update(target=None, apps=False, system=False):
""" """
Update apps & system package cache Update apps & system package cache
Keyword arguments:
system -- Fetch available system packages upgrades (equivalent to apt update)
apps -- Fetch the application list to check which apps can be upgraded
""" """
# If neither --apps nor --system specified, do both # Legacy options (--system, --apps)
if not apps and not system: if apps or system:
apps = True logger.warning("Using 'yunohost tools update' with --apps / --system is deprecated, just write 'yunohost tools update apps system' (no -- prefix anymore)")
system = True if apps and system:
target = "all"
elif apps:
target = "apps"
else:
target = "system"
elif not target:
target = "all"
if target not in ["system", "apps", "all"]:
raise YunohostError("Unknown target %s, should be 'system', 'apps' or 'all'" % target, raw_msg=True)
upgradable_system_packages = [] upgradable_system_packages = []
if system: if target in ["system", "all"]:
# Update APT cache # Update APT cache
# LC_ALL=C is here to make sure the results are in english # LC_ALL=C is here to make sure the results are in english
@ -467,7 +474,7 @@ def tools_update(apps=False, system=False):
logger.debug(m18n.n("done")) logger.debug(m18n.n("done"))
upgradable_apps = [] upgradable_apps = []
if apps: if target in ["apps", "all"]:
try: try:
_update_apps_catalog() _update_apps_catalog()
except YunohostError as e: except YunohostError as e:
@ -518,7 +525,7 @@ def _list_upgradable_apps():
@is_unit_operation() @is_unit_operation()
def tools_upgrade( def tools_upgrade(
operation_logger, apps=None, system=False, allow_yunohost_upgrade=True operation_logger, target=None, apps=False, system=False, allow_yunohost_upgrade=True
): ):
""" """
Update apps & package cache, then display changelog Update apps & package cache, then display changelog
@ -536,26 +543,34 @@ def tools_upgrade(
if not packages.dpkg_lock_available(): if not packages.dpkg_lock_available():
raise YunohostError("dpkg_lock_not_available") raise YunohostError("dpkg_lock_not_available")
if system is not False and apps is not None: # Legacy options management (--system, --apps)
raise YunohostError("tools_upgrade_cant_both") if target is None:
if system is False and apps is None: logger.warning("Using 'yunohost tools upgrade' with --apps / --system is deprecated, just write 'yunohost tools upgrade apps' or 'system' (no -- prefix anymore)")
raise YunohostError("tools_upgrade_at_least_one")
if (system, apps) == (True, True):
raise YunohostError("tools_upgrade_cant_both")
if (system, apps) == (False, False):
raise YunohostError("tools_upgrade_at_least_one")
target = "apps" if apps else "system"
if target not in ["apps", "system"]:
raise Exception("Uhoh ?! tools_upgrade should have 'apps' or 'system' value for argument target")
# #
# Apps # Apps
# This is basically just an alias to yunohost app upgrade ... # This is basically just an alias to yunohost app upgrade ...
# #
if apps is not None: if target == "apps":
# Make sure there's actually something to upgrade # Make sure there's actually something to upgrade
upgradable_apps = [app["id"] for app in _list_upgradable_apps()] upgradable_apps = [app["id"] for app in _list_upgradable_apps()]
if not upgradable_apps or ( if not upgradable_apps:
len(apps) and all(app not in upgradable_apps for app in apps)
):
logger.info(m18n.n("apps_already_up_to_date")) logger.info(m18n.n("apps_already_up_to_date"))
return return
@ -573,7 +588,7 @@ def tools_upgrade(
# System # System
# #
if system is True: if target == "system":
# Check that there's indeed some packages to upgrade # Check that there's indeed some packages to upgrade
upgradables = list(_list_upgradable_apt_packages()) upgradables = list(_list_upgradable_apt_packages())