Skip to content

Commit

Permalink
cli/upgrade: Initial support for offline upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
joebonrichie committed Mar 27, 2024
1 parent 13c2898 commit 2bf001f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
13 changes: 13 additions & 0 deletions eopkg-offline-update.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Unit]
Description=Update the operating system whilst offline

DefaultDependencies=no
Requires=sysinit.target dbus.socket
After=sysinit.target dbus.socket systemd-journald.socket system-update-pre.target
Before=shutdown.target system-update.target

[Service]
Type=oneshot
ExecStart=eopkg up --bypass-update-repo --yes-all

FailureAction=reboot
50 changes: 49 additions & 1 deletion pisi/cli/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
#

import optparse
import os
import subprocess

from pisi import translate as _

import pisi.cli.command as command
import pisi.context as ctx
import pisi.util as util
import pisi.api
import pisi.db

Expand Down Expand Up @@ -62,6 +65,7 @@ def options(self):
type="string", default=None, help=_('Name of the to be upgraded packages\' repository'))
group.add_option("-f", "--fetch-only", action="store_true",
default=False, help=_("Fetch upgrades but do not install."))
group.add_option("--offline", action="store_true", default=False, help=_("Perform upgrades offline"))
group.add_option("-x", "--exclude", action="append",
default=None, help=_("When upgrading system, ignore packages and components whose basenames match pattern."))
group.add_option("--exclude-from", action="store",
Expand All @@ -74,11 +78,19 @@ def options(self):

def run(self):

if self.options.fetch_only:
if self.options.fetch_only or self.options.offline:
self.init(database=True, write=False)
else:
self.init()

if self.options.offline:
if os.path.exists('/system-update'):
ctx.ui.warning(_('An offline update is already prepared'))
if ctx.ui.confirm(_('Do you wish to clear the previously prepared offline update?')):
os.remove('/system-update')
else:
return

if not ctx.get_option('bypass_update_repo'):
ctx.ui.info(_('Updating repositories'))
repos = pisi.api.list_repos()
Expand All @@ -100,3 +112,39 @@ def run(self):
packages.extend(self.args)

pisi.api.upgrade(packages, repository)

# https://www.freedesktop.org/software/systemd/man/latest/systemd.offline-updates.html
if self.options.offline:

upgradable_pkgs = pisi.api.list_upgradable()
upgradable_pkgs.sort()

if not upgradable_pkgs:
return

# a bit of a hard coded safety hack here
if not os.path.exists('/usr/lib/systemd/system/system-update.target.wants/eopkg-offline-update.service'):
ctx.ui.error(_("eopkg-offline-update.service doesn't exist, check your installation"))
return

packagedb = pisi.db.packagedb.PackageDB()

# Nothing special about this file but we need to create some sort of symlink to /system-update
# to instruct systemd to perform an offline update
offline_file = os.path.join(ctx.config.history_dir(), 'prepared-offline-update')

try:
with open(offline_file, 'w') as f:
for pkg in upgradable_pkgs:
info = packagedb.get_package(pkg)
f.write(unicode(info))
os.symlink(offline_file, '/system-update')
ctx.ui.debug(_('Created symlink from %s to /system-update') % offline_file)
except IOError:
ctx.ui.error(_('Failed to prepare offline upgrade'))
return
finally:
ctx.ui.info(util.colorize(_('Successfully prepared offline upgrade'), 'green'))

if ctx.ui.confirm(_('The updates will be applied on next reboot. Do you wish to reboot now?')):
subprocess.Popen(["systemctl", "soft-reboot"])
2 changes: 1 addition & 1 deletion pisi/operations/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def upgrade(A=[], repo=None):
paths.append(install_op.package_fname)

# fetch to be upgraded packages but do not install them.
if ctx.get_option('fetch_only'):
if ctx.get_option('fetch_only') or ctx.get_option('offline'):
return

if conflicts:
Expand Down

0 comments on commit 2bf001f

Please sign in to comment.