Merge pull request #1050 from canonical/wsl_oobe_win_locale

DE-100 - Locale/Base/Advanced models initialization
This commit is contained in:
Didier Roche 2021-09-27 17:24:50 +02:00 committed by GitHub
commit f947847e31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 163 additions and 36 deletions

View File

@ -0,0 +1,94 @@
#!/usr/bin/env python3
# Copyright 2015-2021 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/>.
import os
import logging
import subprocess
log = logging.getLogger("subiquity.system_setup.common.wsl_utils")
config_ref = {
"wsl": {
"automount": {
"enabled": "automount",
"mountfstab": "mountfstab",
"root": "custom_path",
"options": "custom_mount_opt",
},
"network": {
"generatehosts": "gen_host",
"generateresolvconf": "gen_resolvconf",
},
"interop": {
"enabled": "interop_enabled",
"appendwindowspath": "interop_appendwindowspath",
}
},
"ubuntu": {
"GUI": {
"theme": "gui_theme",
"followwintheme": "gui_followwintheme",
},
"Interop": {
"guiintegration": "legacy_gui",
"audiointegration": "legacy_audio",
"advancedipdetection": "adv_ip_detect",
},
"Motd": {
"wslnewsenabled": "wsl_motd_news",
}
}
}
def is_reconfigure(is_dryrun):
if is_dryrun and \
os.getenv("DRYRUN_RECONFIG") == "true":
return True
if_normaluser = False
with open('/etc/passwd', 'r') as f:
for line in f:
# check every normal user except nobody (65534)
if int(line.split(':')[2]) >= 1000 and \
int(line.split(':')[2]) != 65534:
if_normaluser = True
break
return not is_dryrun and if_normaluser
def get_windows_locale():
windows_locale_failed_msg = (
"Cannot determine Windows locale, fallback to default."
" Reason of failure: "
)
try:
process = subprocess.run(["powershell.exe", "-NonInteractive",
"-NoProfile", "-Command",
"(Get-Culture).Name"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if process.returncode:
log.info(windows_locale_failed_msg +
process.stderr.decode("utf-8"))
return None
tmp_code = process.stdout.rstrip().decode("utf-8")
tmp_code = tmp_code.replace("-", "_")
return tmp_code
except OSError as e:
log.info(windows_locale_failed_msg + e.strerror)
return None

View File

@ -18,10 +18,10 @@ from subiquity.server.controllers.cmdlist import (
LateController,
ErrorController,
)
from subiquity.server.controllers.locale import LocaleController
from subiquity.server.controllers.reporting import ReportingController
from subiquity.server.controllers.userdata import UserdataController
from .identity import IdentityController
from .locale import WSLLocaleController
from .wslconfbase import WSLConfigurationBaseController
from .wslconfadvanced import WSLConfigurationAdvancedController
from .configure import ConfigureController
@ -32,7 +32,7 @@ __all__ = [
'ErrorController',
'IdentityController',
'LateController',
'LocaleController',
'WSLLocaleController',
'ReportingController',
'SetupShutdownController',
'UserdataController',

View File

@ -26,7 +26,7 @@ from subiquity.common.types import (
ApplicationState,
)
log = logging.getLogger("subiquity.system_setup.controllers.configure")
log = logging.getLogger("system_setup.server.controllers.configure")
class ConfigureController(SubiquityController):

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python3
# Copyright 2015-2021 Canonical, Ltd.
# Copyright 2021 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
@ -15,18 +14,16 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
from system_setup.common.wsl_utils import get_windows_locale
from subiquity.server.controllers.locale import LocaleController
def is_reconfigure(is_dryrun):
if is_dryrun and \
os.getenv("DRYRUN_RECONFIG") == "true":
return True
if_normaluser = False
with open('/etc/passwd', 'r') as f:
for line in f:
# check every normal user except nobody (65534)
if int(line.split(':')[2]) >= 1000 and \
int(line.split(':')[2]) != 65534:
if_normaluser = True
break
return not is_dryrun and if_normaluser
class WSLLocaleController(LocaleController):
def start(self):
win_lang = get_windows_locale()
self.model.selected_language = os.environ.get("LANG") \
or self.autoinstall_default
if win_lang:
self.model.selected_language = win_lang + ".UTF-8"
self.app.aio_loop.create_task(self.configured())

View File

@ -19,7 +19,7 @@ from subiquitycore.context import with_context
from subiquity.common.types import ShutdownMode
from subiquity.server.controllers import ShutdownController
log = logging.getLogger("system_setup.controllers.restart")
log = logging.getLogger("system_setup.server.controllers.restart")
class SetupShutdownController(ShutdownController):

View File

@ -24,8 +24,10 @@ from subiquity.common.apidef import API
from subiquity.common.types import WSLConfigurationAdvanced
from subiquity.server.controller import SubiquityController
from system_setup.common.wsl_utils import config_ref
log = logging.getLogger(
'subiquity.server.controllers.wsl_configuration_advanced')
'system_setup.server.controllers.wsl_configuration_advanced')
class WSLConfigurationAdvancedController(SubiquityController):
@ -88,24 +90,26 @@ class WSLConfigurationAdvancedController(SubiquityController):
if path.exists('/etc/wsl.conf'):
wslconfig = configparser.ConfigParser()
wslconfig.read('/etc/wsl.conf')
for a in wslconfig:
if a in self.config_ref['wsl']:
a_x = wslconfig[a]
for b in a_x:
if b in self.config_ref['wsl'][a]:
data[self.config_ref['wsl'][a][b]] = a_x[b]
for conf_sec in wslconfig:
if conf_sec in config_ref['wsl']:
conf_sec_list = wslconfig[conf_sec]
for conf_item in conf_sec_list:
if conf_item in config_ref['wsl'][conf_sec]:
data[config_ref['wsl'][conf_sec][conf_item]] = \
conf_sec_list[conf_item]
if path.exists('/etc/ubuntu-wsl.conf'):
ubuntuconfig = configparser.ConfigParser()
ubuntuconfig.read('/etc/ubuntu-wsl.conf')
for a in ubuntuconfig:
if a in self.config_ref['ubuntu']:
a_x = ubuntuconfig[a]
for b in a_x:
if b in self.config_ref['ubuntu'][a]:
data[self.config_ref['ubuntu'][a][b]] = a_x[b]
for conf_sec in ubuntuconfig:
if conf_sec in self.config_ref['ubuntu']:
conf_sec_list = ubuntuconfig[conf_sec]
for conf_item in conf_sec_list:
if conf_item in config_ref['ubuntu'][conf_sec]:
data[config_ref['ubuntu'][conf_sec][conf_item]] = \
conf_sec_list[conf_item]
if data:
def bool_converter(x):
return x == 'true'
return x.lower() == 'true'
reconf_data = WSLConfigurationAdvanced(
interop_enabled=bool_converter(data['interop_enabled']),
interop_appendwindowspath=bool_converter(

View File

@ -13,7 +13,9 @@
# 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/>.
import configparser
import logging
from os import path
import attr
@ -23,7 +25,10 @@ from subiquity.common.apidef import API
from subiquity.common.types import WSLConfigurationBase
from subiquity.server.controller import SubiquityController
log = logging.getLogger('subiquity.server.controllers.wsl_configuration_base')
from system_setup.common.wsl_utils import config_ref
log = logging.getLogger('system_setup.server' +
'.controllers.wsl_configuration_base')
class WSLConfigurationBaseController(SubiquityController):
@ -43,6 +48,33 @@ class WSLConfigurationBaseController(SubiquityController):
'additionalProperties': False,
}
def __init__(self, app):
super().__init__(app)
# load the config file
data = {}
if path.exists('/etc/wsl.conf'):
wslconfig = configparser.ConfigParser()
wslconfig.read('/etc/wsl.conf')
for conf_sec in wslconfig:
if conf_sec in config_ref['wsl']:
conf_sec_list = wslconfig[conf_sec]
for conf_item in conf_sec_list:
if conf_item in config_ref['wsl'][conf_sec]:
data[config_ref['wsl'][conf_sec][conf_item]] = \
conf_sec_list[conf_item]
if data:
def bool_converter(x):
return x.lower() == 'true'
conf_data = WSLConfigurationBase(
custom_path=data['custom_path'],
custom_mount_opt=data['custom_mount_opt'],
gen_host=bool_converter(data['gen_host']),
gen_resolvconf=bool_converter(data['gen_resolvconf']),
)
self.model.apply_settings(conf_data, self.opts.dry_run)
def load_autoinstall_data(self, data):
if data is not None:
identity_data = WSLConfigurationBase(

View File

@ -13,7 +13,7 @@
# 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/>.
from system_setup.common.helpers import is_reconfigure
from system_setup.common.wsl_utils import is_reconfigure
from subiquity.server.server import SubiquityServer
from system_setup.models.system_setup import SystemSetupModel
from subiquity.models.subiquity import ModelNames
@ -41,7 +41,7 @@ class SystemSetupServer(SubiquityServer):
controllers = [
"Reporting",
"Error",
"Locale",
"WSLLocale",
"Identity",
"WSLConfigurationBase",
"WSLConfigurationAdvanced",