Skip to content

Commit

Permalink
Add service to detect clones on boot
Browse files Browse the repository at this point in the history
Fixes: #3068
  • Loading branch information
dheyay committed May 1, 2024
1 parent 3f15789 commit 54a51ca
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ override_dh_systemd_enable:
dh_systemd_enable -pubuntu-pro-client ua-reboot-cmds.service
dh_systemd_enable -pubuntu-pro-client ua-timer.timer
dh_systemd_enable -pubuntu-pro-client ua-timer.service
dh_systemd_enable -pubuntu-pro-client ua-activity-ping-on-boot.service
dh_systemd_enable -pubuntu-pro-client ubuntu-advantage.service
ifeq (${VERSION_ID},"16.04")
# Only enable cloud-id-shim on Xenial
Expand Down
22 changes: 19 additions & 3 deletions lib/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Timer used to run all jobs that need to be frequently run on the system
"""

import argparse
import logging
from datetime import datetime, timedelta, timezone
from typing import Callable, Optional
Expand All @@ -24,6 +25,17 @@
UPDATE_CONTRACT_INFO_INTERVAL = 86400 # 24 hours


def parse_arguments():
"""Parse command line arguments for the timer file"""
parser = argparse.ArgumentParser(description="Run timer jobs")
parser.add_argument(
"--run-on-boot",
action="store_true",
help="Run the metering job on boot, bypassing the normal schedule to ping the server", # noqa
)
return parser.parse_args()


class TimedJob:
def __init__(
self,
Expand Down Expand Up @@ -125,10 +137,11 @@ def run_job(
job: TimedJob,
current_time: datetime,
job_status: Optional[TimerJobState],
ignore_interval: bool = False,
) -> Optional[TimerJobState]:
if job_status:
next_run = job_status.next_run
if next_run and next_run > current_time:
if next_run and next_run > current_time and not ignore_interval:
return job_status
if job.run(cfg):
# Persist last_run and next_run UTC-based times on job success.
Expand All @@ -137,7 +150,6 @@ def run_job(
seconds=job.run_interval_seconds(cfg)
)
job_status = TimerJobState(next_run=next_run, last_run=last_run)

return job_status


Expand Down Expand Up @@ -180,10 +192,14 @@ def run_jobs(cfg: UAConfig, current_time: datetime):

if __name__ == "__main__":
log.setup_journald_logging()
args = parse_arguments()

cfg = UAConfig()
current_time = datetime.now(timezone.utc)

http.configure_web_proxy(cfg.http_proxy, cfg.https_proxy)

run_jobs(cfg=cfg, current_time=current_time)
if args.run_on_boot:
run_job(cfg, metering_job, current_time, None, ignore_interval=True)
else:
run_jobs(cfg=cfg, current_time=current_time)
16 changes: 16 additions & 0 deletions systemd/ua-activity-ping-on-boot.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# On clones of machines that are currently attached, an activity ping
# is required on boot to detect the clones
# This requires using the ua-timer to update the activity id immediately

[Unit]
Description=Ubuntu Pro clone detection activity ping on boot
After=network.target network-online.target systemd-networkd.service ua-auto-attach.service
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/bin/python3 /usr/lib/ubuntu-advantage/timer.py --run-on-boot
TimeoutSec=0

[Install]
WantedBy=multi-user.target
1 change: 1 addition & 0 deletions uaclient/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
UA_SERVICES = (
"apt-news.service",
"esm-cache.service",
"ua-activity-ping-on-boot.service",
"ua-timer.service",
"ua-timer.timer",
"ua-auto-attach.path",
Expand Down
6 changes: 6 additions & 0 deletions uaclient/cli/tests/test_cli_collect_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ def test_collect_logs(
"-u",
"esm-cache.service",
"-u",
"ua-activity-ping-on-boot.service",
"-u",
"ua-timer.service",
"-u",
"ua-auto-attach.service",
Expand All @@ -160,6 +162,10 @@ def test_collect_logs(
mock.call(
["systemctl", "status", "esm-cache.service"], rcs=[0, 3]
),
mock.call(
["systemctl", "status", "ua-activity-ping-on-boot.service"],
rcs=[0, 3], # noqa
),
mock.call(["systemctl", "status", "ua-timer.service"], rcs=[0, 3]),
mock.call(["systemctl", "status", "ua-timer.timer"], rcs=[0, 3]),
mock.call(
Expand Down

0 comments on commit 54a51ca

Please sign in to comment.