ubuntu-pro: add ability to run magic-attach locally in dry-run mode

In dry-run mode, the 'magic_attach_run_locally' variable can now be used
to execute uaclient on the host (against the uacontracts environment
specified in /etc/ubuntu-advantage/uaclient.conf) instead of relying on
a mock mechanism.

Signed-off-by: Olivier Gayot <olivier.gayot@canonical.com>
This commit is contained in:
Olivier Gayot 2022-11-28 14:32:29 +01:00
parent e887232ea1
commit d74a506f98
5 changed files with 21 additions and 2 deletions

View File

@ -0,0 +1 @@
pro_magic_attach_run_locally: true

View File

@ -22,6 +22,7 @@ import sys
import jsonschema
from subiquity.cmd.server import make_server_args_parser
from subiquity.server.dryrun import DRConfig
from subiquity.server.server import SubiquityServer
@ -54,6 +55,9 @@ def make_app():
parser = make_server_args_parser()
opts, unknown = parser.parse_known_args(['--dry-run'])
app = SubiquityServer(opts, '')
# This is needed because the ubuntu-pro server controller accesses dr_cfg
# in the initializer.
app.dr_cfg = DRConfig()
app.base_model = app.make_model()
app.controllers.load_all()
return app

View File

@ -18,12 +18,15 @@ import unittest
from subiquity.server.controllers.ubuntu_pro import (
UbuntuProController,
)
from subiquity.server.dryrun import DRConfig
from subiquitycore.tests.mocks import make_app
class TestUbuntuProController(unittest.TestCase):
def setUp(self):
self.controller = UbuntuProController(make_app())
app = make_app()
app.dr_cfg = DRConfig()
self.controller = UbuntuProController(app)
def test_serialize(self):
self.controller.model.token = "1a2b3C"

View File

@ -90,7 +90,12 @@ class UbuntuProController(SubiquityController):
""" Initializer for server-side Ubuntu Pro controller. """
strategy: UAInterfaceStrategy
if app.opts.dry_run:
strategy = MockedUAInterfaceStrategy(scale_factor=app.scale_factor)
if app.dr_cfg.pro_magic_attach_run_locally:
executable = "/usr/bin/ubuntu-advantage"
strategy = UAClientUAInterfaceStrategy(executable=executable)
else:
strategy = MockedUAInterfaceStrategy(
scale_factor=app.scale_factor)
else:
# Make sure we execute `$PYTHON "$SNAP/usr/bin/ubuntu-advantage"`.
executable = (

View File

@ -36,6 +36,12 @@ class DRConfig:
# Tells whether "$source"/var/lib/snapd/seed/systems exists on the source.
systems_dir_exists: bool = False
# Tells whether we should run /usr/bin/ubuntu-advantage instead of using
# Mock objects.
pro_magic_attach_run_locally: bool = False
# When running /usr/bin/ubuntu-advantage locally, do not use the production
# ua-contrats.
pro_ua_contracts_url: str = "https://contracts.staging.canonical.com"
@classmethod
def load(cls, stream):