Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Add service to detect clones on boot #3098

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,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
2 changes: 2 additions & 0 deletions features/cli/collect_logs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Feature: CLI collect-logs command
pro-journal.txt
pro-status.json
systemd-timers.txt
ua-activity-ping-on-boot.service.txt
ua-auto-attach.path.txt(-error)?
ua-auto-attach.service.txt(-error)?
uaclient.conf
Expand Down Expand Up @@ -76,6 +77,7 @@ Feature: CLI collect-logs command
pro-journal.txt
pro-status.json
systemd-timers.txt
ua-activity-ping-on-boot.service.txt
ua-auto-attach.path.txt(-error)?
ua-auto-attach.service.txt(-error)?
uaclient.conf
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 @@ -27,6 +28,17 @@
CHECK_RELEASE_SERIES_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 @@ -124,10 +136,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 @@ -136,7 +149,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 @@ -43,6 +43,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 @@ -107,6 +107,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 @@ -121,6 +123,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
Loading