move to a unified model instance

and stop having the controllers construct each model instance for itself
This commit is contained in:
Michael Hudson-Doyle 2017-11-15 10:30:09 +13:00
parent a13ef47bab
commit 8086d5df81
14 changed files with 56 additions and 29 deletions

View File

@ -24,7 +24,6 @@ from subiquity.curtin import (
CURTIN_CONFIGS,
curtin_write_storage_actions,
)
from subiquity.models import (FilesystemModel, RaidModel)
from subiquity.models.filesystem import humanize_size
from subiquity.ui.views import (
BcacheView,
@ -50,10 +49,9 @@ class FilesystemController(BaseController):
def __init__(self, common):
super().__init__(common)
self.model = FilesystemModel(self.prober, self.opts)
self.model = self.base_model.filesystem
# self.iscsi_model = IscsiDiskModel()
# self.ceph_model = CephDiskModel()
self.raid_model = RaidModel()
self.model.probe() # probe before we complete
def default(self):

View File

@ -17,7 +17,6 @@
import logging
from subiquitycore.controller import BaseController
from subiquitycore.models import IdentityModel
from subiquitycore.user import create_user
from subiquity.curtin import curtin_write_postinst_config
@ -30,7 +29,7 @@ class IdentityController(BaseController):
def __init__(self, common):
super().__init__(common)
self.model = IdentityModel(self.opts)
self.model = self.base_model.identity
def default(self):
title = _("Profile setup")

View File

@ -20,7 +20,7 @@ import lsb_release
from subiquitycore.controller import BaseController
from subiquitycore.ui.dummy import DummyView
from subiquity.models import InstallpathModel
from subiquity.models.installpath import InstallpathModel
from subiquity.ui.views import InstallpathView
log = logging.getLogger('subiquity.controller.installpath')

View File

@ -16,7 +16,6 @@
from subiquitycore.controller import BaseController
from subiquity.models import LocaleModel
from subiquity.ui.views import WelcomeView
@ -24,7 +23,7 @@ class WelcomeController(BaseController):
def __init__(self, common):
super().__init__(common)
self.model = LocaleModel()
self.model = self.base_model.locale
def default(self):
title = "Willkommen! Bienvenue! Welcome! Добро пожаловать! Welkom!"

View File

@ -17,6 +17,8 @@ import logging
from subiquitycore.core import Application
from subiquity.models.subiquity import SubiquityModel
log = logging.getLogger('console_conf.core')
@ -25,6 +27,9 @@ class Subiquity(Application):
from subiquity.palette import PALETTE, STYLES, STYLES_MONO
project = "subiquity"
model_class = SubiquityModel
controllers = [
"Welcome",
"Network",

View File

@ -12,8 +12,3 @@
#
# 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 .installpath import InstallpathModel # NOQA
from .filesystem import FilesystemModel # NOQA
from .raid import RaidModel # NOQA
from .locale import LocaleModel

View File

@ -272,9 +272,8 @@ class FilesystemModel(object):
longest_fs_name = len(fs.label)
fs_by_name[fs.label] = fs
def __init__(self, prober, opts):
def __init__(self, prober):
self.prober = prober
self.opts = opts
self._available_disks = {} # keyed by path, eg /dev/sda
self.reset()

View File

@ -17,13 +17,12 @@ import gettext
import logging
from subiquitycore import i18n
log = logging.getLogger('subiquity.models.welcome')
log = logging.getLogger('subiquity.models.locale')
class LocaleModel(object):
""" Model representing locale selection
Only supports language selection for now.
XXX Only represents *language* selection for now.
"""
supported_languages = [
@ -33,6 +32,7 @@ class LocaleModel(object):
('lv_LV', 'Latvian'),
('ru_RU', 'Russian'),
]
selected_language = None
def get_languages(self):

View File

@ -0,0 +1,30 @@
# Copyright 2015 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/>.
from subiquitycore.models.identity import IdentityModel
from subiquitycore.models.network import NetworkModel
from .filesystem import FilesystemModel
from .locale import LocaleModel
class SubiquityModel:
"""The overall model for subiquity."""
def __init__(self, common):
self.locale = LocaleModel()
self.network = NetworkModel()
self.filesystem = FilesystemModel(common['prober'])
self.identity = IdentityModel()

View File

@ -34,6 +34,7 @@ class BaseController(ABC):
self.prober = common['prober']
self.controllers = common['controllers']
self.pool = common['pool']
self.base_model = common['base_model']
def register_signals(self):
"""Defines signals associated with controller from model."""

View File

@ -225,6 +225,7 @@ def sanitize_config(config):
ap_config['password'] = '<REDACTED>'
return config
class SubiquityNetworkEventReceiver(NetworkEventReceiver):
def __init__(self, model):
self.model = model
@ -299,6 +300,7 @@ class NetworkController(BaseController):
def __init__(self, common):
super().__init__(common)
self.model = self.base_model.network
if self.opts.dry_run:
self.root = os.path.abspath(".subiquity")
self.tried_once = False
@ -310,7 +312,7 @@ class NetworkController(BaseController):
os.makedirs(netplan_dir)
with open(netplan_path, 'w') as fp:
fp.write(default_netplan)
self.model = NetworkModel(self.root)
self.model.parse_netplan_configs(self.root)
self.network_event_receiver = SubiquityNetworkEventReceiver(self.model)
self.observer, fds = self.prober.probe_network(self.network_event_receiver)
@ -369,7 +371,7 @@ class NetworkController(BaseController):
w.write("# This is the network config written by '{}'\n".format(self.opts.project))
w.write(yaml.dump(config))
os.rename(tmppath, netplan_path)
self.model.parse_netplan_configs()
self.model.parse_netplan_configs(self.root)
if self.opts.dry_run:
tasks = [
('one', BackgroundProcess(['sleep', '0.1'])),

View File

@ -179,6 +179,8 @@ class Application:
log.debug("Running event loop: {}".format(
self.common['loop'].event_loop))
self.common['base_model'] = self.model_class(self.common)
try:
self.common['loop'].set_alarm_in(0.05, self.next_screen)
controllers_mod = __import__('%s.controllers' % self.project, None, None, [''])

View File

@ -58,8 +58,7 @@ class IdentityModel(object):
""" Model representing user identity
"""
def __init__(self, opts):
self.opts = opts
def __init__(self):
self._user = None
def add_user(self, result):

View File

@ -363,7 +363,7 @@ class NetworkModel(object):
6: 'balance-alb',
}
def __init__(self, netplan_root):
def __init__(self):
self.devices = {} # Maps ifindex to Networkdev
self.devices_by_name = {} # Maps interface names to Networkdev
self.default_v4_gateway = None
@ -371,15 +371,13 @@ class NetworkModel(object):
self.v4_gateway_dev = None
self.v6_gateway_dev = None
self.network_routes = {}
self.netplan_root = netplan_root
self.parse_netplan_configs()
def parse_netplan_configs(self):
def parse_netplan_configs(self, netplan_root):
self.config = NetplanConfig()
configs_by_basename = {}
paths = glob.glob(os.path.join(self.netplan_root, 'lib/netplan', "*.yaml")) + \
glob.glob(os.path.join(self.netplan_root, 'etc/netplan', "*.yaml")) + \
glob.glob(os.path.join(self.netplan_root, 'run/netplan', "*.yaml"))
paths = glob.glob(os.path.join(netplan_root, 'lib/netplan', "*.yaml")) + \
glob.glob(os.path.join(netplan_root, 'etc/netplan', "*.yaml")) + \
glob.glob(os.path.join(netplan_root, 'run/netplan', "*.yaml"))
for path in paths:
configs_by_basename[os.path.basename(path)] = path
for _, path in sorted(configs_by_basename.items()):