-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99eddd0
commit 1d96455
Showing
15 changed files
with
301 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
ubuntu-advantage-tools (1:1+devel) UNRELEASED; urgency=medium | ||
ubuntu-advantage-tools (35~lxd-auto-attach-test) noble; urgency=medium | ||
|
||
* wip | ||
* testing pro lxd auto attach | ||
|
||
-- Grant Orndorff <[email protected]> Fri, 16 Aug 2024 12:10:33 -0500 | ||
-- Grant Orndorff <[email protected]> Tue, 13 Aug 2024 16:28:22 -0500 | ||
|
||
ubuntu-advantage-tools (33.2) oracular; urgency=medium | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import time | ||
|
||
from behave import step | ||
|
||
from features.steps.shell import when_i_run_command | ||
|
||
|
||
@step("I start `{task}` command `{command}` in the background") | ||
def start_task(context, task, command): | ||
when_i_run_command( | ||
context, | ||
f"systemd-run --no-block --unit={task} --property=Type=oneshot --property=RemainAfterExit=yes {command}", # noqa: E501 | ||
"with sudo", | ||
) | ||
|
||
|
||
@step("I wait for the `{task}` command to complete") | ||
def wait_for_task(context, task): | ||
while True: | ||
try: | ||
when_i_run_command( | ||
context, f"systemctl is-active {task}", "with sudo" | ||
) | ||
break | ||
except AssertionError: | ||
time.sleep(2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import logging | ||
|
||
from uaclient import config, exceptions, http, log, secret_manager, util | ||
from uaclient.clouds import AutoAttachInstance | ||
|
||
LOG = logging.getLogger(util.replace_top_level_logger_name(__name__)) | ||
|
||
LXD_INSTANCE_API_SOCKET_PATH = "/dev/lxd/sock" | ||
LXD_INSTANCE_API_ENDPOINT_UBUNTU_PRO = "/1.0/ubuntu-pro" | ||
LXD_INSTANCE_API_ENDPOINT_UBUNTU_PRO_GUEST_TOKEN = "/1.0/ubuntu-pro/token" | ||
|
||
|
||
class LXDAutoAttachInstance(AutoAttachInstance): | ||
@property | ||
def is_viable(self) -> bool: | ||
return True | ||
|
||
def should_poll_for_pro_license(self) -> bool: | ||
"""Yes, but only once - is_pro_license_present doesn't | ||
support wait_for_change""" | ||
return True | ||
|
||
def is_pro_license_present(self, *, wait_for_change: bool) -> bool: | ||
if wait_for_change: | ||
# Unsupported | ||
raise exceptions.CancelProLicensePolling() | ||
|
||
resp = http.unix_socket_request( | ||
LXD_INSTANCE_API_SOCKET_PATH, | ||
"GET", | ||
LXD_INSTANCE_API_ENDPOINT_UBUNTU_PRO, | ||
) | ||
if resp.code != 200: | ||
LOG.warning( | ||
"LXD instance API returned error for ubuntu-pro query", | ||
extra=log.extra(code=resp.code, body=resp.body), | ||
) | ||
return False | ||
return resp.json_dict.get("guest_attach", "off") in {"on", "available"} | ||
|
||
def acquire_pro_token(self, cfg: config.UAConfig) -> str: | ||
""" | ||
Cloud-specific implementation of acquiring the pro token using whatever | ||
method suits the platform | ||
""" | ||
resp = http.unix_socket_request( | ||
LXD_INSTANCE_API_SOCKET_PATH, | ||
"POST", | ||
LXD_INSTANCE_API_ENDPOINT_UBUNTU_PRO_GUEST_TOKEN, | ||
) | ||
if resp.code == 404: | ||
raise exceptions.LXDAutoAttachNotAvailable() | ||
elif resp.code == 403: | ||
raise exceptions.LXDAutoAttachNotAllowed() | ||
guest_token = resp.json_dict.get("guest_token", "") | ||
secret_manager.secrets.add_secret(guest_token) | ||
return guest_token |
Oops, something went wrong.