Run ubuntu-advantage from the snap rather from the base ISO

The UAClientUAInterfaceStrategy can now optionally be initialized with
the path to the ubuntu-advantage entry point. Additionally, one can also
specify which Python interpreter should run it.

We leverage on this to force the use of:
 * the Python installation from the Subiquity snap
 * the ubuntu-advantage-tools integrated to the Subiquity snap

Signed-off-by: Olivier Gayot <olivier.gayot@canonical.com>
This commit is contained in:
Olivier Gayot 2022-01-12 13:31:34 +01:00
parent 1995dd002a
commit b03034887c
3 changed files with 39 additions and 4 deletions

View File

@ -16,6 +16,7 @@
"""
import logging
import os
from subiquitycore.async_helpers import schedule_task
@ -49,7 +50,12 @@ class UbuntuAdvantageController(SubiquityTuiController):
if app.opts.dry_run:
strategy = MockedUAInterfaceStrategy(scale_factor=app.scale_factor)
else:
strategy = UAClientUAInterfaceStrategy()
# Make sure we execute `$PYTHON "$SNAP/usr/bin/ubuntu-advantage"`.
executable = (
os.environ["PYTHON"],
os.path.join(os.environ["SNAP"], "usr/bin/ubuntu-advantage"),
)
strategy = UAClientUAInterfaceStrategy(executable=executable)
self.ua_interface = UAInterface(strategy)
super().__init__(app)

View File

@ -58,6 +58,22 @@ class TestMockedUAInterfaceStrategy(unittest.TestCase):
class TestUAClientUAInterfaceStrategy(unittest.TestCase):
arun_command = "subiquity.common.ubuntu_advantage.utils.arun_command"
def test_init(self):
# Default initializer.
strategy = UAClientUAInterfaceStrategy()
self.assertEqual(strategy.executable, ["ubuntu-advantage"])
# Initialize with a mere path.
strategy = UAClientUAInterfaceStrategy("/usr/bin/ubuntu-advantage")
self.assertEqual(strategy.executable, ["/usr/bin/ubuntu-advantage"])
# Initialize with a path + interpreter.
strategy = UAClientUAInterfaceStrategy(
("python3", "/usr/bin/ubuntu-advantage")
)
self.assertEqual(strategy.executable,
["python3", "/usr/bin/ubuntu-advantage"])
def test_query_info_succeeded(self):
strategy = UAClientUAInterfaceStrategy()
command = (

View File

@ -20,6 +20,7 @@ from datetime import datetime as dt
import json
import logging
from subprocess import CalledProcessError, CompletedProcess
from typing import List, Sequence, Union
import asyncio
from subiquitycore import utils
@ -96,12 +97,24 @@ class MockedUAInterfaceStrategy(UAInterfaceStrategy):
class UAClientUAInterfaceStrategy(UAInterfaceStrategy):
""" Strategy that relies on UA client script to retrieve the information.
"""
Executable = Union[str, Sequence[str]]
def __init__(self, executable: Executable = "ubuntu-advantage") -> None:
""" Initialize the strategy using the path to the ubuntu-advantage
executable we want to use. The executable can be specified as a
sequence of strings so that we can specify the interpret to use as
well.
"""
self.executable: List[str] = \
[executable] if isinstance(executable, str) else list(executable)
super().__init__()
async def query_info(self, token: str) -> dict:
""" Return the subscription info associated with the supplied
UA token. The information will be queried using UA client.
UA token. The information will be queried using the UA client
executable passed to the initializer.
"""
command = (
"ubuntu-advantage",
command = tuple(self.executable) + (
"status",
"--format", "json",
"--simulate-with-token", token,