ubuntu-pro: disable support when network is unavailable

Signed-off-by: Olivier Gayot <olivier.gayot@canonical.com>
This commit is contained in:
Olivier Gayot 2022-08-12 18:22:54 +02:00
parent d07364dc11
commit 5e063ab3ab
5 changed files with 66 additions and 11 deletions

View File

@ -27,6 +27,7 @@ from subiquitycore.async_helpers import schedule_task
from subiquity.client.controller import SubiquityTuiController
from subiquity.common.types import (
UbuntuProInfo,
UbuntuProResponse,
UbuntuProCheckTokenStatus as TokenStatus,
UbuntuProSubscription,
UPCSWaitStatus,
@ -68,8 +69,10 @@ class UbuntuProController(SubiquityTuiController):
await self.endpoint.skip.POST()
raise Skip("Not running LTS version")
ubuntu_pro_info = await self.endpoint.GET()
return UbuntuProView(self, ubuntu_pro_info.token)
ubuntu_pro_info: UbuntuProResponse = await self.endpoint.GET()
return UbuntuProView(self,
token=ubuntu_pro_info.token,
has_network=ubuntu_pro_info.has_network)
async def run_answers(self) -> None:
""" Interact with the UI to go through the pre-attach process if

View File

@ -55,6 +55,7 @@ from subiquity.common.types import (
StorageResponseV2,
TimeZoneInfo,
UbuntuProInfo,
UbuntuProResponse,
UbuntuProCheckTokenAnswer,
UPCSInitiateResponse,
UPCSWaitResponse,
@ -333,7 +334,7 @@ class API:
def POST(data: Payload[List[str]]): ...
class ubuntu_pro:
def GET() -> UbuntuProInfo: ...
def GET() -> UbuntuProResponse: ...
def POST(data: Payload[UbuntuProInfo]) -> None: ...
class skip:

View File

@ -533,6 +533,13 @@ class UbuntuProInfo:
token: str = attr.ib(repr=False)
@attr.s(auto_attribs=True)
class UbuntuProResponse:
""" Response to GET request to /ubuntu_pro """
token: str = attr.ib(repr=False)
has_network: bool
class UbuntuProCheckTokenStatus(enum.Enum):
VALID_TOKEN = enum.auto()
INVALID_TOKEN = enum.auto()

View File

@ -24,6 +24,7 @@ from subiquity.common.types import (
UbuntuProInfo,
UbuntuProCheckTokenAnswer,
UbuntuProCheckTokenStatus,
UbuntuProResponse,
UPCSInitiateResponse,
UPCSWaitStatus,
UPCSWaitResponse,
@ -128,9 +129,11 @@ class UbuntuProController(SubiquityController):
""" Loads the last-known state of the model. """
self.model.token = token
async def GET(self) -> UbuntuProInfo:
async def GET(self) -> UbuntuProResponse:
""" Handle a GET request coming from the client-side controller. """
return UbuntuProInfo(token=self.model.token)
has_network = self.app.base_model.network.has_network
return UbuntuProResponse(token=self.model.token,
has_network=has_network)
async def POST(self, data: UbuntuProInfo) -> None:
""" Handle a POST request coming from the client-side controller and

View File

@ -222,6 +222,39 @@ class UpgradeYesNoForm(Form):
" 'pro attach' command."))
class UpgradeYesNoFormNoNetwork(UpgradeYesNoForm):
""" Represents a "read-only" form that does not let the user enable Ubuntu
Pro because the network is unavailable.
+---------------------------------------------------------+
| ( ) Enable Ubuntu Pro |
| |
| (X) Skip Ubuntu Pro setup for now |
| |
| Once you are connected to the Internet, you can |
| enable Ubuntu Pro via the 'pro attach' command. |
| |
| [ Continue ] |
| [ Back ] |
+---------------------------------------------------------+
"""
group: List[RadioButtonField] = []
upgrade = RadioButtonField(
group, _("Enable Ubuntu Pro"),
help=NO_HELP)
skip = RadioButtonField(
group, _("Skip Ubuntu Pro setup for now"),
help="\n" + _("Once you are connected to the Internet, you can"
" enable Ubuntu Po via the 'pro attach' command."))
def __init__(self):
""" Initializer that disables the relevant fields. """
super().__init__(initial={})
self.upgrade.value = False
self.upgrade.enabled = False
self.skip.value = True
class CheckingContractToken(WidgetWrap):
""" Widget displaying a loading animation while checking ubuntu pro
subscription. """
@ -253,14 +286,20 @@ class UbuntuProView(BaseView):
title = _("Upgrade to Ubuntu Pro")
subscription_done_label = _("Continue")
def __init__(self, controller, token: str):
def __init__(self, controller, token: str, has_network: bool):
""" Initialize the view with the default value for the token. """
self.controller = controller
self.upgrade_yes_no_form = UpgradeYesNoForm(initial={
"skip": not token,
"upgrade": bool(token),
})
self.has_network = has_network
if self.has_network:
self.upgrade_yes_no_form = UpgradeYesNoForm(initial={
"skip": not token,
"upgrade": bool(token),
})
else:
self.upgrade_yes_no_form = UpgradeYesNoFormNoNetwork()
self.upgrade_mode_form = UpgradeModeForm(initial={
"with_contract_token_subform": {"token": token},
"with_contract_token": bool(token),
@ -356,6 +395,8 @@ class UbuntuProView(BaseView):
" on a much wider range of packages, until 2032. Assists"
" with FedRAMP, FIPS, STIG, HIPAA and other compliance or"
" hardening requirements.")
excerpt_no_net = _("An Internet connection is required to enable"
" Ubuntu Pro.")
about_pro_btn = menu_btn(
_("About Ubuntu Pro"),
@ -370,7 +411,7 @@ class UbuntuProView(BaseView):
return screen(
ListBox(rows),
self.upgrade_yes_no_form.buttons,
excerpt=excerpt,
excerpt=excerpt if self.has_network else excerpt_no_net,
focus_buttons=True)
def subscription_screen(self, subscription: UbuntuProSubscription) \