-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
daemon: auto-attach when pro license added on gcp
- Loading branch information
1 parent
dc131c2
commit 8b32e1c
Showing
29 changed files
with
773 additions
and
13 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
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,37 @@ | ||
Feature: UA daemon starts and stops jobs appropriately | ||
|
||
# TODO: Replace this as soon as the daemon does something real | ||
@series.all | ||
@uses.config.machine_type.lxd.container | ||
Scenario Outline: Check daemon starts and restarts | ||
Given a `<release>` machine with ubuntu-advantage-tools installed | ||
Then I verify that running `systemctl status ubuntu-advantage.service` `with sudo` exits `3` | ||
Then stdout matches regexp: | ||
""" | ||
code=exited, status=0/SUCCESS | ||
""" | ||
When I run `cat /var/log/ubuntu-advantage-daemon.log` with sudo | ||
Then stdout matches regexp: | ||
""" | ||
daemon started | ||
""" | ||
When I run `truncate -s 0 /var/log/ubuntu-advantage-daemon.log` with sudo | ||
When I run `systemctl restart ubuntu-advantage.service` with sudo | ||
When I wait `1` seconds | ||
Then I verify that running `systemctl status ubuntu-advantage.service` `with sudo` exits `3` | ||
Then stdout matches regexp: | ||
""" | ||
code=exited, status=0/SUCCESS | ||
""" | ||
When I run `cat /var/log/ubuntu-advantage-daemon.log` with sudo | ||
Then stdout matches regexp: | ||
""" | ||
daemon started | ||
""" | ||
Examples: version | ||
| release | | ||
| xenial | | ||
| bionic | | ||
| focal | | ||
| hirsute | | ||
| impish | |
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,39 @@ | ||
import logging | ||
import sys | ||
|
||
from systemd.daemon import notify # type: ignore | ||
|
||
from uaclient import daemon | ||
from uaclient.cli import setup_logging | ||
from uaclient.config import UAConfig | ||
|
||
LOG = logging.getLogger("ua") | ||
|
||
|
||
def main() -> int: | ||
|
||
cfg = UAConfig() | ||
setup_logging( | ||
logging.INFO, logging.DEBUG, log_file=cfg.daemon_log_file, logger=LOG | ||
) | ||
# The ua-daemon logger should log everything to its file | ||
# Make sure the ua-daemon logger does not generate double logging | ||
# by propagating to the root logger | ||
LOG.propagate = False | ||
# The root logger should only log errors to the daemon log file | ||
setup_logging( | ||
logging.CRITICAL, logging.ERROR, log_file=cfg.daemon_log_file | ||
) | ||
|
||
LOG.debug("daemon starting") | ||
|
||
notify("READY=1") | ||
|
||
daemon.poll_for_pro_license(cfg) | ||
|
||
LOG.debug("daemon ending") | ||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |
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,83 @@ | ||
#!/bin/sh | ||
deb=$1 | ||
ZONE="us-east1-b" | ||
INSTANCE_NAME="test-auto-attach" | ||
INSTANCE_TYPE="n1-standard-1" | ||
DISK_NAME="persistent-disk-0" | ||
|
||
set -e | ||
|
||
GREEN="\e[32m" | ||
RED="\e[31m" | ||
BLUE="\e[36m" | ||
END_COLOR="\e[0m" | ||
|
||
function cleanup { | ||
gcloud compute ssh $INSTANCE_NAME -- "sudo ua detach --assume-yes || true" | ||
gcloud compute instances delete $INSTANCE_NAME | ||
} | ||
|
||
function on_err { | ||
echo -e "${RED}Test Failed${END_COLOR}" | ||
cleanup | ||
exit 1 | ||
} | ||
|
||
trap on_err ERR | ||
|
||
function print_and_run_cmd { | ||
echo -e "${BLUE}Running:${END_COLOR}" "$@" | ||
echo -e "${BLUE}Output:${END_COLOR}" | ||
gcloud compute ssh $INSTANCE_NAME -- "sh -c \"$@\"" | ||
echo | ||
} | ||
|
||
function explanatory_message { | ||
echo -e "${BLUE}$@${END_COLOR}" | ||
} | ||
|
||
explanatory_message "Starting gcloud instance" | ||
gcloud compute instances create $INSTANCE_NAME \ | ||
--image="ubuntu-1604-xenial-v20210429" \ | ||
--image-project="ubuntu-os-cloud" \ | ||
--machine-type=$INSTANCE_TYPE \ | ||
--zone=$ZONE | ||
sleep 30 | ||
|
||
explanatory_message "Installing new version of ubuntu-advantage-tools from local copy" | ||
gcloud compute scp $deb $INSTANCE_NAME:/tmp/ubuntu-advantage-tools.deb | ||
gcloud compute ssh $INSTANCE_NAME -- "sudo apt update" | ||
gcloud compute ssh $INSTANCE_NAME -- "sudo apt install ubuntu-advantage-tools -y" | ||
print_and_run_cmd "sudo dpkg -i /tmp/ubuntu-advantage-tools.deb" | ||
explanatory_message "skip initial license check" | ||
print_and_run_cmd "sudo sed -zi \\\"s/cloud.is_pro_license_present(\n wait_for_change=False\n )/False/\\\" /usr/lib/python3/dist-packages/uaclient/daemon.py" | ||
explanatory_message "turn on polling in config file" | ||
print_and_run_cmd "sudo sh -c \\\"printf \\\\\\\" poll_for_pro_license: true\\\\\\\" >> /etc/ubuntu-advantage/uaclient.conf\\\"" | ||
explanatory_message "change won't happen while daemon is running, so set short timeout to simulate the long poll returning" | ||
print_and_run_cmd "sudo sed -i \\\"s/wait_for_change=true/wait_for_change=true\&timeout_sec=5/\\\" /usr/lib/python3/dist-packages/uaclient/clouds/gcp.py" | ||
|
||
gcloud compute ssh $INSTANCE_NAME -- "sudo touch /run/cloud-init/cloud-id-gce" # feature not in cloud-init yet | ||
gcloud compute ssh $INSTANCE_NAME -- "sudo systemctl restart ubuntu-advantage.service" | ||
explanatory_message "Checking the status and logs beforehand" | ||
print_and_run_cmd "sudo ua status --wait" | ||
print_and_run_cmd "sudo cat /var/log/ubuntu-advantage-daemon.log" | ||
|
||
explanatory_message "Stopping the machine, adding license, restarting..." | ||
gcloud compute instances stop $INSTANCE_NAME | ||
gcloud beta compute disks update $INSTANCE_NAME --zone=$ZONE --update-user-licenses="https://www.googleapis.com/compute/v1/projects/ubuntu-os-pro-cloud/global/licenses/ubuntu-pro-1604-lts" | ||
gcloud compute instances start $INSTANCE_NAME | ||
sleep 30 | ||
|
||
gcloud compute ssh $INSTANCE_NAME -- "sudo truncate -s 0 /var/log/ubuntu-advantage-daemon.log" | ||
gcloud compute ssh $INSTANCE_NAME -- "sudo touch /run/cloud-init/cloud-id-gce" # feature not in cloud-init yet | ||
gcloud compute ssh $INSTANCE_NAME -- "sudo systemctl restart ubuntu-advantage.service" | ||
sleep 10 | ||
|
||
explanatory_message "Now with the license, it will succeed auto_attaching" | ||
print_and_run_cmd "sudo ua status --wait" | ||
print_and_run_cmd "sudo cat /var/log/ubuntu-advantage-daemon.log" | ||
result=$(gcloud compute ssh $INSTANCE_NAME -- "sudo ua status --format json") | ||
echo $result | jq -r ".attached" | grep "true" | ||
|
||
echo -e "${GREEN}Test Passed${END_COLOR}" | ||
cleanup |
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,76 @@ | ||
#!/bin/sh | ||
deb=$1 | ||
ZONE="us-east1-b" | ||
INSTANCE_NAME="test-auto-attach" | ||
INSTANCE_TYPE="n1-standard-1" | ||
DISK_NAME="persistent-disk-0" | ||
|
||
set -e | ||
|
||
GREEN="\e[32m" | ||
RED="\e[31m" | ||
BLUE="\e[36m" | ||
END_COLOR="\e[0m" | ||
|
||
function cleanup { | ||
gcloud compute ssh $INSTANCE_NAME -- "sudo ua detach --assume-yes || true" | ||
gcloud compute instances delete $INSTANCE_NAME | ||
} | ||
|
||
function on_err { | ||
echo -e "${RED}Test Failed${END_COLOR}" | ||
cleanup | ||
exit 1 | ||
} | ||
|
||
trap on_err ERR | ||
|
||
function print_and_run_cmd { | ||
echo -e "${BLUE}Running:${END_COLOR}" "$@" | ||
echo -e "${BLUE}Output:${END_COLOR}" | ||
gcloud compute ssh $INSTANCE_NAME -- "sh -c \"$@\"" | ||
echo | ||
} | ||
|
||
function explanatory_message { | ||
echo -e "${BLUE}$@${END_COLOR}" | ||
} | ||
|
||
explanatory_message "Starting gcloud instance" | ||
gcloud compute instances create $INSTANCE_NAME \ | ||
--image="ubuntu-1604-xenial-v20210429" \ | ||
--image-project="ubuntu-os-cloud" \ | ||
--machine-type=$INSTANCE_TYPE \ | ||
--zone=$ZONE | ||
sleep 30 | ||
|
||
|
||
explanatory_message "Installing new version of ubuntu-advantage-tools from local copy" | ||
gcloud compute scp $deb $INSTANCE_NAME:/tmp/ubuntu-advantage-tools.deb | ||
gcloud compute ssh $INSTANCE_NAME -- "sudo apt update" | ||
gcloud compute ssh $INSTANCE_NAME -- "sudo apt install ubuntu-advantage-tools jq -y" | ||
print_and_run_cmd "sudo dpkg -i /tmp/ubuntu-advantage-tools.deb" | ||
explanatory_message "turn on polling in config file" | ||
gcloud compute ssh $INSTANCE_NAME -- "sudo touch /run/cloud-init/cloud-id-gce" # feature not in cloud-init yet | ||
gcloud compute ssh $INSTANCE_NAME -- "sudo systemctl restart ubuntu-advantage.service" | ||
explanatory_message "Checking the status and logs beforehand" | ||
print_and_run_cmd "sudo ua status --wait" | ||
print_and_run_cmd "sudo cat /var/log/ubuntu-advantage-daemon.log" | ||
|
||
explanatory_message "Stopping the machine, adding license, restarting..." | ||
gcloud compute instances stop $INSTANCE_NAME | ||
gcloud beta compute disks update $INSTANCE_NAME --zone=$ZONE --update-user-licenses="https://www.googleapis.com/compute/v1/projects/ubuntu-os-pro-cloud/global/licenses/ubuntu-pro-1604-lts" | ||
gcloud compute instances start $INSTANCE_NAME | ||
sleep 30 | ||
|
||
print_and_run_cmd "sudo truncate -s 0 /var/log/ubuntu-advantage-daemon.log" | ||
gcloud compute ssh $INSTANCE_NAME -- "sudo touch /run/cloud-init/cloud-id-gce" # feature not in cloud-init yet | ||
gcloud compute ssh $INSTANCE_NAME -- "sudo systemctl restart ubuntu-advantage.service" | ||
explanatory_message "Now with the license, it will succeed auto_attaching on boot" | ||
print_and_run_cmd "sudo ua status --wait" | ||
print_and_run_cmd "sudo cat /var/log/ubuntu-advantage-daemon.log" | ||
result=$(gcloud compute ssh $INSTANCE_NAME -- "sudo ua status --format json") | ||
echo $result | jq -r ".attached" | grep "true" | ||
|
||
echo -e "${GREEN}Test Passed${END_COLOR}" | ||
cleanup |
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,25 @@ | ||
# If you are uninterested in the (free for personal use) Ubuntu Advantage | ||
# services, including security updates after standard EOL and kernel patching | ||
# without rebooting, then you can safely stop and disable this service: | ||
# sudo systemctl stop ubuntu-advantage.service | ||
# sudo systemctl disable ubuntu-advantage.service | ||
|
||
[Unit] | ||
Description=Ubuntu Advantage GCP Auto Attach Daemon | ||
Documentation=man:ubuntu-advantage https://ubuntu.com/advantage | ||
After=network.target network-online.target systemd-networkd.service ua-auto-attach.service cloud-config.target | ||
|
||
# Only run if not already attached | ||
ConditionPathExists=!/var/lib/ubuntu-advantage/private/machine-token.json | ||
# Only run on GCP | ||
ConditionPathExists=/run/cloud-init/cloud-id-gce | ||
|
||
[Service] | ||
Type=notify | ||
NotifyAccess=main | ||
ExecStart=/usr/bin/python3 /usr/lib/ubuntu-advantage/daemon.py | ||
Restart=on-failure | ||
WorkingDirectory=/var/lib/ubuntu-advantage/ | ||
|
||
[Install] | ||
WantedBy=multi-user.target |
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
Oops, something went wrong.