-
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.
When a lxd container is launched, pro-client will attempt to auto-attach via the lxd apis that talk to the host's pro-client. It will only try once and will not continue to poll.
- Loading branch information
1 parent
df250f6
commit 85ce40f
Showing
13 changed files
with
442 additions
and
53 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
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,58 @@ | ||
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 | ||
# returning True will cause auto-attach on launch, so only "on" counts | ||
return resp.json_dict.get("guest_attach", "off") == "on" | ||
|
||
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 |
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
Oops, something went wrong.