-
Notifications
You must be signed in to change notification settings - Fork 147
/
drivers.py
114 lines (96 loc) · 4.04 KB
/
drivers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Copyright 2022 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 asyncio
import logging
from typing import List, Optional
from subiquitycore.context import with_context
from subiquity.common.apidef import API
from subiquity.common.types import DriversPayload, DriversResponse
from subiquity.server.controller import SubiquityController
from subiquity.server.types import InstallerChannels
from subiquity.server.ubuntu_drivers import (
CommandNotFoundError,
UbuntuDriversInterface,
get_ubuntu_drivers_interface,
)
log = logging.getLogger('subiquity.server.controllers.drivers')
class DriversController(SubiquityController):
endpoint = API.drivers
autoinstall_key = model_name = "drivers"
autoinstall_schema = {
'type': 'object',
'properties': {
'install': {
'type': 'boolean',
},
},
}
autoinstall_default = {"install": False}
drivers: Optional[List[str]] = None
def __init__(self, app) -> None:
super().__init__(app)
self.ubuntu_drivers: UbuntuDriversInterface = \
get_ubuntu_drivers_interface(app)
def make_autoinstall(self):
return {
"install": self.model.do_install,
}
def load_autoinstall_data(self, data):
if data is not None and "install" in data:
self.model.do_install = data["install"]
def start(self):
self._wait_apt = asyncio.Event()
self.app.hub.subscribe(
InstallerChannels.APT_CONFIGURED,
self._wait_apt.set)
self._drivers_task = asyncio.create_task(self._list_drivers())
@with_context()
async def _list_drivers(self, context):
with context.child("wait_apt"):
await self._wait_apt.wait()
# The APT_CONFIGURED event (which unblocks _wait_apt.wait) is sent
# after the user confirms the destruction changes. At this point, the
# source is already mounted so the user can't go back all the way to
# the source screen to enable/disable the "search drivers" checkbox.
if not self.app.controllers.Source.model.search_drivers:
self.drivers = []
await self.configured()
return
apt = self.app.controllers.Mirror.apt_configurer
async with apt.overlay() as d:
try:
# Make sure ubuntu-drivers is available.
await self.ubuntu_drivers.ensure_cmd_exists(d.mountpoint)
except CommandNotFoundError:
self.drivers = []
else:
self.drivers = await self.ubuntu_drivers.list_drivers(
root_dir=d.mountpoint,
context=context)
log.debug("Available drivers to install: %s", self.drivers)
if not self.drivers:
await self.configured()
async def GET(self, wait: bool = False) -> DriversResponse:
local_only = not self.app.base_model.network.has_network
if wait:
await asyncio.shield(self._drivers_task)
search_drivers = self.app.controllers.Source.model.search_drivers
return DriversResponse(install=self.model.do_install,
drivers=self.drivers,
local_only=local_only,
search_drivers=search_drivers)
async def POST(self, data: DriversPayload) -> None:
self.model.do_install = data.install
await self.configured()