Refactor - drop external_temp_file (#985)

Move unattended-upgrades to part of postinstall, so it can be done
before the restore_apt_config umount, which lets us get rid of the
redundant external_temp_file logic.
This commit is contained in:
Dan Bungert 2021-06-28 17:11:49 -06:00 committed by GitHub
parent edb101cf2a
commit 90b1bb9eed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 24 deletions

View File

@ -41,7 +41,7 @@ for answers in examples/answers*.yaml; do
# The --foreground is important to avoid subiquity getting SIGTTOU-ed.
timeout --foreground 60 sh -c "LANG=C.UTF-8 python3 -m subiquity.cmd.tui --answers $answers --dry-run --snaps-from-examples --machine-config $config $opts" < $tty
validate
grep -q 'finish: subiquity/Install/install/run_unattended_upgrades: SUCCESS: downloading and installing security updates' .subiquity/subiquity-server-debug.log
grep -q 'finish: subiquity/Install/install/postinstall/run_unattended_upgrades: SUCCESS: downloading and installing security updates' .subiquity/subiquity-server-debug.log
done
clean
@ -61,14 +61,14 @@ grep -q 'finish: subiquity/Install/install/postinstall/install_package1: SUCCESS
grep -q 'finish: subiquity/Install/install/postinstall/install_package2: SUCCESS: installing package2' \
.subiquity/subiquity-server-debug.log
grep -q 'switching subiquity to edge' .subiquity/subiquity-server-debug.log
grep -q 'finish: subiquity/Install/install/run_unattended_upgrades: SUCCESS: downloading and installing all updates' \
grep -q 'finish: subiquity/Install/install/postinstall/run_unattended_upgrades: SUCCESS: downloading and installing all updates' \
.subiquity/subiquity-server-debug.log
clean
timeout --foreground 60 sh -c "LANG=C.UTF-8 python3 -m subiquity.cmd.tui --autoinstall examples/autoinstall-user-data.yaml \
--dry-run --machine-config examples/simple.json --kernel-cmdline 'autoinstall'"
validate
grep -q 'finish: subiquity/Install/install/run_unattended_upgrades: SUCCESS: downloading and installing security updates' .subiquity/subiquity-server-debug.log
grep -q 'finish: subiquity/Install/install/postinstall/run_unattended_upgrades: SUCCESS: downloading and installing security updates' .subiquity/subiquity-server-debug.log
python3 -m subiquity.cmd.schema > "$testschema"
diff -u "autoinstall-schema.json" "$testschema"

View File

@ -14,7 +14,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import asyncio
import contextlib
import datetime
import logging
import os
@ -232,12 +231,6 @@ class InstallController(SubiquityController):
await self.postinstall(context=context)
if self.model.network.has_network:
self.app.update_state(ApplicationState.UU_RUNNING)
policy = self.model.updates.updates
await self.run_unattended_upgrades(context=context,
policy=policy)
self.app.update_state(ApplicationState.DONE)
except Exception:
kw = {}
@ -273,6 +266,11 @@ class InstallController(SubiquityController):
packages.extend(self.app.base_model.packages)
for package in packages:
await self.install_package(context=context, package=package)
if self.model.network.has_network:
self.app.update_state(ApplicationState.UU_RUNNING)
policy = self.model.updates.updates
await self.run_unattended_upgrades(context=context, policy=policy)
await self.restore_apt_config(context=context)
@with_context(description="configuring cloud-init")
@ -315,11 +313,11 @@ class InstallController(SubiquityController):
async def run_unattended_upgrades(self, context, policy):
if self.app.opts.dry_run:
command = ["sleep", str(5/self.app.scale_factor)]
aptdir = os.path.join(self.model.target, "tmp")
aptdir = self.tpath("tmp")
else:
command = [sys.executable, "-m", "curtin", "in-target",
"-t", "/target", "--", "unattended-upgrades", "-v"]
aptdir = os.path.join(self.model.target, "etc/apt/apt.conf.d")
aptdir = self.tpath("etc/apt/apt.conf.d")
os.makedirs(aptdir, exist_ok=True)
apt_conf_contents = uu_apt_conf
if policy == 'all':
@ -327,7 +325,7 @@ class InstallController(SubiquityController):
else:
apt_conf_contents += uu_apt_conf_update_security
fname = 'zzzz-temp-installer-unattended-upgrade'
with external_temp_file(aptdir, fname) as apt_conf:
with open(os.path.join(aptdir, fname), 'wb') as apt_conf:
apt_conf.write(apt_conf_contents)
apt_conf.close()
self.unattended_upgrades_ctx = context
@ -353,17 +351,6 @@ class InstallController(SubiquityController):
]), check=True)
@contextlib.contextmanager
def external_temp_file(dir, fname):
path = os.path.join(dir, fname)
fid = open(path, 'wb')
try:
yield fid
finally:
fid.close()
os.remove(path)
uu_apt_conf = b"""\
# Config for the unattended-upgrades run to avoid failing on battery power or
# a metered connection.