2021-08-31 10:07:20 +00:00
|
|
|
# Copyright 2020 Canonical, Ltd.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2021-10-15 16:50:06 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
2021-08-31 10:07:20 +00:00
|
|
|
import logging
|
2021-11-05 20:29:33 +00:00
|
|
|
import re
|
|
|
|
import apt
|
|
|
|
import apt_pkg
|
2021-08-31 10:07:20 +00:00
|
|
|
|
|
|
|
from subiquity.common.errorreport import ErrorReportKind
|
2021-09-23 08:52:31 +00:00
|
|
|
from subiquity.common.types import ApplicationState
|
2021-10-05 14:53:53 +00:00
|
|
|
from subiquity.common.resources import get_users_and_groups
|
2021-09-23 08:52:31 +00:00
|
|
|
from subiquity.server.controller import SubiquityController
|
|
|
|
from subiquitycore.context import with_context
|
2021-11-03 11:35:25 +00:00
|
|
|
from subiquitycore.utils import arun_command
|
2021-10-04 10:55:25 +00:00
|
|
|
from system_setup.common.wsl_conf import wsl_config_update
|
2021-08-31 10:07:20 +00:00
|
|
|
|
2021-09-27 12:36:25 +00:00
|
|
|
log = logging.getLogger("system_setup.server.controllers.configure")
|
2021-08-31 10:07:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConfigureController(SubiquityController):
|
|
|
|
|
|
|
|
def __init__(self, app):
|
|
|
|
super().__init__(app)
|
2021-09-20 06:37:55 +00:00
|
|
|
self.app = app
|
2021-08-31 10:07:20 +00:00
|
|
|
self.model = app.base_model
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
self.install_task = self.app.aio_loop.create_task(self.configure())
|
|
|
|
|
2021-11-05 20:29:33 +00:00
|
|
|
def _update_locale_gen(self, localeGenPath, lang):
|
|
|
|
""" Uncomments the line in locale.gen file where lang is found,
|
|
|
|
if found commented. A fully qualified language is expected,
|
|
|
|
since that would have passed thru the Locale
|
|
|
|
controller validation. e.g. en_UK.UTF-8. """
|
|
|
|
|
|
|
|
fileContents: str
|
|
|
|
locGenNeedsWrite = False
|
|
|
|
with open(localeGenPath, "r") as f:
|
|
|
|
fileContents = f.read()
|
|
|
|
lineFound = fileContents.find(lang)
|
|
|
|
if lineFound == -1:
|
|
|
|
# An unsupported locale coming from our UI is a bug
|
|
|
|
raise AssertionError("Selected language {} not supported."
|
|
|
|
.format(lang))
|
|
|
|
|
|
|
|
pattern = r'#+\s*({}.*)'.format(lang)
|
|
|
|
commented = re.compile(pattern)
|
|
|
|
(fileContents, n) = commented.subn(r'\1', fileContents, count=1)
|
|
|
|
locGenNeedsWrite = n == 1
|
|
|
|
|
|
|
|
if locGenNeedsWrite:
|
|
|
|
with open(localeGenPath, "wt") as f:
|
|
|
|
f.write(fileContents)
|
|
|
|
|
|
|
|
async def _install_check_lang_support_packages(self, lang, env):
|
|
|
|
""" Installs any packages recommended by
|
|
|
|
check-language-support command. """
|
|
|
|
|
|
|
|
clsCommand = "check-language-support"
|
|
|
|
|
|
|
|
cp = await arun_command([clsCommand, "-l", lang[0:5]], env=env)
|
|
|
|
if cp.returncode != 0:
|
|
|
|
log.error('Command \"%s\" failed with return code %d',
|
|
|
|
cp.args, cp.returncode)
|
|
|
|
return
|
|
|
|
|
|
|
|
packages = cp.stdout.strip('\n').split(' ')
|
|
|
|
if len(packages) == 0:
|
|
|
|
log.debug("%s didn't recommend any packages. Nothing to do.",
|
|
|
|
clsCommand)
|
|
|
|
return
|
|
|
|
|
|
|
|
cache = apt.Cache()
|
|
|
|
if self.app.opts.dry_run:
|
|
|
|
for package in packages:
|
|
|
|
# Downloading instead of installing. Doesn't require root.
|
|
|
|
packs_dir = os.path.join(self.model.root,
|
|
|
|
apt_pkg.config
|
|
|
|
.find_dir("Dir::Cache::Archives")[1:])
|
|
|
|
cmd = ["wget", cache[package].candidate.uri, "-O", packs_dir]
|
|
|
|
os.makedirs(packs_dir, exist_ok=True)
|
|
|
|
await arun_command(cmd, env=env)
|
|
|
|
else:
|
|
|
|
cache.update()
|
|
|
|
cache.open(None)
|
|
|
|
with cache.actiongroup():
|
|
|
|
for package in packages:
|
|
|
|
cache[package].mark_install()
|
|
|
|
cache.commit()
|
|
|
|
|
|
|
|
async def _activate_locale(self, lang, env):
|
|
|
|
""" Final commands to run for locale support """
|
|
|
|
|
|
|
|
cmds = [["locale-gen"],
|
|
|
|
["update-locale", "LANG={}".format(lang)]]
|
|
|
|
if self.app.opts.dry_run:
|
|
|
|
for cmd in cmds:
|
|
|
|
# TODO Figure out about mocks.
|
|
|
|
# It would be good if they offered a -P prefix option,
|
|
|
|
# but they don't.
|
|
|
|
# Chroot'ing is not an option.
|
|
|
|
log.debug('Would run: %s', ' '.join(cmd))
|
|
|
|
else:
|
|
|
|
for cmd in cmds:
|
|
|
|
cp = await arun_command(cmd, env=env)
|
|
|
|
if cp.returncode != 0:
|
|
|
|
raise SystemError('Command {} failed with return code {}'
|
|
|
|
.format(cp.args, cp.returncode))
|
|
|
|
|
2021-08-31 10:07:20 +00:00
|
|
|
@with_context(
|
|
|
|
description="final system configuration", level="INFO",
|
|
|
|
childlevel="DEBUG")
|
|
|
|
async def configure(self, *, context):
|
2021-09-01 08:48:44 +00:00
|
|
|
context.set('is-install-context', True)
|
2021-08-31 10:07:20 +00:00
|
|
|
try:
|
|
|
|
|
|
|
|
self.app.update_state(ApplicationState.WAITING)
|
|
|
|
|
2021-09-01 08:48:44 +00:00
|
|
|
await self.model.wait_install()
|
|
|
|
|
2021-08-31 10:07:20 +00:00
|
|
|
self.app.update_state(ApplicationState.NEEDS_CONFIRMATION)
|
|
|
|
|
|
|
|
self.app.update_state(ApplicationState.RUNNING)
|
|
|
|
|
2021-09-01 08:48:44 +00:00
|
|
|
await self.model.wait_postinstall()
|
|
|
|
|
2021-08-31 10:07:20 +00:00
|
|
|
self.app.update_state(ApplicationState.POST_WAIT)
|
|
|
|
|
|
|
|
# TODO WSL:
|
|
|
|
# 1. Use self.model to get all data to commit
|
2021-08-31 11:26:49 +00:00
|
|
|
# 2. Write directly (without wsl utilities) to wsl.conf and other
|
|
|
|
# fstab files
|
2021-08-31 13:56:08 +00:00
|
|
|
# 3. If not in reconfigure mode: create User, otherwise just write
|
|
|
|
# wsl.conf files.
|
2021-08-31 10:07:20 +00:00
|
|
|
# This must not use subprocesses.
|
|
|
|
# If dry-run: write in .subiquity
|
|
|
|
|
|
|
|
self.app.update_state(ApplicationState.POST_RUNNING)
|
|
|
|
|
2021-11-05 20:29:33 +00:00
|
|
|
localeGenPath = "/etc/locale.gen"
|
2021-09-23 08:52:31 +00:00
|
|
|
dryrun = self.app.opts.dry_run
|
2021-09-14 08:16:06 +00:00
|
|
|
variant = self.app.variant
|
2021-10-06 14:10:56 +00:00
|
|
|
root_dir = self.model.root
|
2021-10-11 07:10:06 +00:00
|
|
|
username = None
|
2021-09-28 12:48:30 +00:00
|
|
|
if variant == "wsl_setup":
|
2021-10-04 07:23:02 +00:00
|
|
|
wsl_id = self.model.identity.user
|
2021-10-11 07:10:06 +00:00
|
|
|
username = wsl_id.username
|
2021-10-15 16:50:06 +00:00
|
|
|
create_user_base = []
|
|
|
|
assign_grp_base = []
|
|
|
|
usergroups_list = get_users_and_groups()
|
2021-11-05 20:29:33 +00:00
|
|
|
lang = self.model.locale.selected_language
|
2021-09-23 08:52:31 +00:00
|
|
|
if dryrun:
|
2021-10-15 16:50:06 +00:00
|
|
|
log.debug("creating a mock-up env for user %s", username)
|
|
|
|
# creating folders and files for dryrun
|
|
|
|
etc_dir = os.path.join(root_dir, "etc")
|
|
|
|
os.makedirs(etc_dir, exist_ok=True)
|
|
|
|
home_dir = os.path.join(root_dir, "home")
|
|
|
|
os.makedirs(home_dir, exist_ok=True)
|
|
|
|
pseudo_files = ["passwd", "shadow", "gshadow", "group",
|
|
|
|
"subgid", "subuid"]
|
2021-11-05 20:29:33 +00:00
|
|
|
|
|
|
|
# locale
|
|
|
|
testLocGenPath = os.path.join(etc_dir,
|
|
|
|
os.path
|
|
|
|
.basename(localeGenPath))
|
|
|
|
shutil.copy(localeGenPath, testLocGenPath)
|
|
|
|
shutil.copy(localeGenPath,
|
|
|
|
"{}-".format(testLocGenPath))
|
|
|
|
# After copies are done, overwrite the original path
|
|
|
|
# because there is code outside of this 'if'
|
|
|
|
# depending on 'localeGenPath' being initialized.
|
|
|
|
localeGenPath = testLocGenPath
|
|
|
|
|
2021-10-15 16:50:06 +00:00
|
|
|
for file in pseudo_files:
|
|
|
|
filepath = os.path.join(etc_dir, file)
|
|
|
|
open(filepath, "a").close()
|
|
|
|
# mimic groupadd
|
|
|
|
group_id = 1000
|
|
|
|
for group in usergroups_list:
|
|
|
|
group_filepath = os.path.join(etc_dir, "group")
|
|
|
|
gshadow_filepath = os.path.join(etc_dir, "gshadow")
|
|
|
|
shutil.copy(group_filepath,
|
|
|
|
"{}-".format(group_filepath))
|
|
|
|
with open(group_filepath, "a") as group_file:
|
|
|
|
group_file.write("{}:x:{}:\n".
|
|
|
|
format(group, group_id))
|
|
|
|
group_id += 1
|
|
|
|
shutil.copy(gshadow_filepath,
|
|
|
|
"{}-".format(gshadow_filepath))
|
|
|
|
with open(gshadow_filepath, "a") as gshadow_file:
|
|
|
|
gshadow_file.write("{}:!::\n".format(group))
|
|
|
|
|
2021-11-03 18:53:02 +00:00
|
|
|
create_user_base = ["-P", root_dir]
|
2021-10-15 16:50:06 +00:00
|
|
|
assign_grp_base = ["-P", root_dir]
|
|
|
|
|
2021-11-05 20:29:33 +00:00
|
|
|
env = os.environ.copy()
|
|
|
|
self._update_locale_gen(localeGenPath, lang)
|
|
|
|
await self._install_check_lang_support_packages(lang, env)
|
|
|
|
await self._activate_locale(lang, env)
|
|
|
|
|
2021-10-15 16:50:06 +00:00
|
|
|
create_user_cmd = ["useradd"] + create_user_base + \
|
|
|
|
["-m", "-s", "/bin/bash",
|
|
|
|
"-c", wsl_id.realname,
|
|
|
|
"-p", wsl_id.password, username]
|
|
|
|
assign_grp_cmd = ["usermod"] + assign_grp_base + \
|
|
|
|
["-a", "-G", ",".join(usergroups_list),
|
|
|
|
username]
|
|
|
|
|
2021-11-03 11:35:25 +00:00
|
|
|
create_user_proc = await arun_command(create_user_cmd)
|
2021-10-15 16:50:06 +00:00
|
|
|
if create_user_proc.returncode != 0:
|
|
|
|
raise Exception("Failed to create user %s: %s"
|
|
|
|
% (username, create_user_proc.stderr))
|
|
|
|
log.debug("created user %s", username)
|
|
|
|
|
2021-11-03 11:35:25 +00:00
|
|
|
assign_grp_proc = await arun_command(assign_grp_cmd)
|
2021-10-15 16:50:06 +00:00
|
|
|
if assign_grp_proc.returncode != 0:
|
|
|
|
raise Exception(("Failed to assign group to user %s: %s")
|
|
|
|
% (username, assign_grp_proc.stderr))
|
2021-09-28 12:48:30 +00:00
|
|
|
else:
|
2021-10-04 10:55:25 +00:00
|
|
|
wsl_config_update(self.model.wslconfadvanced.wslconfadvanced,
|
2021-10-06 14:10:56 +00:00
|
|
|
root_dir)
|
2021-09-30 16:00:32 +00:00
|
|
|
|
2021-10-08 12:21:16 +00:00
|
|
|
# update advanced config when it is in autoinstall mode
|
|
|
|
if self.app.opts.autoinstall is not None and \
|
|
|
|
self.model.wslconfadvanced.wslconfadvanced is not None:
|
|
|
|
wsl_config_update(self.model.wslconfadvanced.wslconfadvanced,
|
|
|
|
root_dir)
|
|
|
|
|
2021-10-11 07:10:06 +00:00
|
|
|
wsl_config_update(self.model.wslconfbase.wslconfbase, root_dir,
|
|
|
|
default_user=username)
|
2021-09-28 12:48:30 +00:00
|
|
|
|
2021-08-31 10:07:20 +00:00
|
|
|
self.app.update_state(ApplicationState.DONE)
|
|
|
|
except Exception:
|
|
|
|
kw = {}
|
|
|
|
self.app.make_apport_report(
|
|
|
|
ErrorReportKind.INSTALL_FAIL, "configuration failed", **kw)
|
|
|
|
raise
|
|
|
|
|
|
|
|
def stop_uu(self):
|
|
|
|
# This is a no-op to allow Shutdown controller to depend on this one
|
2021-08-31 11:26:49 +00:00
|
|
|
pass
|