Skip to content

Commit 01b48e7

Browse files
committed
feat: support this role in container builds
Feature: Support running the timesync role during container builds. Reason: This is particularly useful for building bootc derivative OSes. Result: These flags enable running the bootc container scenarios in CI, which ensures that the role works in buildah build environment. This allows us to officially support this role for image mode builds. Signed-off-by: Rich Megginson <rmeggins@redhat.com>
1 parent 7db4750 commit 01b48e7

18 files changed

+155
-61
lines changed

handlers/main.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,30 @@
33
service:
44
name: chronyd
55
state: restarted
6+
when: __timesync_is_booted | d(false)
67

78
- name: Restart ntpd
89
service:
910
name: ntpd
1011
state: restarted
12+
when: __timesync_is_booted | d(false)
1113

1214
- name: Restart ptp4l
1315
service:
1416
name: ptp4l
1517
state: restarted
18+
when: __timesync_is_booted | d(false)
1619

1720
- name: Restart phc2sys
1821
service:
1922
name: phc2sys
2023
state: restarted
24+
when: __timesync_is_booted | d(false)
2125

2226
# wokeignore:rule=master
2327
- name: Restart timemaster
2428
service:
2529
# wokeignore:rule=master
2630
name: timemaster
2731
state: restarted
32+
when: __timesync_is_booted | d(false)

meta/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ galaxy_info:
1818
galaxy_tags:
1919
- centos
2020
- chrony
21+
- containerbuild
2122
- el6
2223
- el7
2324
- el8

tasks/main.yml

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- name: Populate service facts
66
service_facts:
7+
when: __timesync_is_booted | d(false)
78

89
- name: Set variable `timesync_services` with filtered uniq service names
910
set_fact:
@@ -18,6 +19,24 @@
1819
map('regex_replace', '@$', '') |
1920
unique |
2021
list }}"
22+
when: __timesync_is_booted | d(false)
23+
24+
- name: Populate service facts when in bootc mode
25+
command: systemctl list-unit-files --type=service -l # noqa command-instead-of-module
26+
register: timesync_services_output
27+
changed_when: false
28+
when: not __timesync_is_booted | d(false)
29+
30+
- name: Set variable `timesync_services` with filtered uniq service names when in bootc mode
31+
set_fact:
32+
timesync_services: "{{ timesync_services_output.stdout_lines |
33+
select('search', ' enabled$') |
34+
map('regex_replace', ' +enabled$', '') |
35+
map('regex_replace', '[.]service.*$', '') |
36+
map('regex_replace', '@$', '') |
37+
unique |
38+
list }}"
39+
when: not __timesync_is_booted | d(false)
2140

2241
- name: Check that variable 'timesync_services' is defined
2342
assert:
@@ -250,7 +269,7 @@
250269
- name: Disable chronyd
251270
service:
252271
name: chronyd
253-
state: stopped
272+
state: "{{ 'stopped' if __timesync_is_booted else omit }}"
254273
enabled: false
255274
when:
256275
- timesync_mode != 1 or timesync_ntp_provider != 'chrony'
@@ -264,7 +283,7 @@
264283
- name: Disable ntpd
265284
service:
266285
name: ntpd
267-
state: stopped
286+
state: "{{ 'stopped' if __timesync_is_booted else omit }}"
268287
enabled: false
269288
when:
270289
- timesync_mode != 1 or timesync_ntp_provider != 'ntp'
@@ -278,7 +297,7 @@
278297
- name: Disable ntpdate
279298
service:
280299
name: ntpdate
281-
state: stopped
300+
state: "{{ 'stopped' if __timesync_is_booted else omit }}"
282301
enabled: false
283302
when: "'ntpdate' in timesync_services"
284303
register: __disable_result
@@ -290,7 +309,7 @@
290309
- name: Disable sntp
291310
service:
292311
name: sntp
293-
state: stopped
312+
state: "{{ 'stopped' if __timesync_is_booted else omit }}"
294313
enabled: false
295314
when: "'sntp' in timesync_services"
296315
register: __disable_result
@@ -302,7 +321,7 @@
302321
- name: Disable ptp4l
303322
service:
304323
name: ptp4l
305-
state: stopped
324+
state: "{{ 'stopped' if __timesync_is_booted else omit }}"
306325
enabled: false
307326
when:
308327
- timesync_mode != 2
@@ -316,7 +335,7 @@
316335
- name: Disable phc2sys
317336
service:
318337
name: phc2sys
319-
state: stopped
338+
state: "{{ 'stopped' if __timesync_is_booted else omit }}"
320339
enabled: false
321340
when:
322341
- timesync_mode != 2 or not timesync_mode2_hwts
@@ -334,7 +353,7 @@
334353
__timemstr: timemaster
335354
service:
336355
name: "{{ __timemstr }}"
337-
state: stopped
356+
state: "{{ 'stopped' if __timesync_is_booted else omit }}"
338357
enabled: false
339358
when:
340359
- timesync_mode != 3
@@ -348,7 +367,7 @@
348367
- name: Enable chronyd
349368
service:
350369
name: chronyd
351-
state: started
370+
state: "{{ 'started' if __timesync_is_booted else omit }}"
352371
enabled: true
353372
when:
354373
- timesync_mode == 1
@@ -357,7 +376,7 @@
357376
- name: Enable ntpd
358377
service:
359378
name: ntpd
360-
state: started
379+
state: "{{ 'started' if __timesync_is_booted else omit }}"
361380
enabled: true
362381
when:
363382
- timesync_mode == 1
@@ -366,14 +385,14 @@
366385
- name: Enable ptp4l
367386
service:
368387
name: ptp4l
369-
state: started
388+
state: "{{ 'started' if __timesync_is_booted else omit }}"
370389
enabled: true
371390
when: timesync_mode == 2
372391

373392
- name: Enable phc2sys
374393
service:
375394
name: phc2sys
376-
state: started
395+
state: "{{ 'started' if __timesync_is_booted else omit }}"
377396
enabled: true
378397
when:
379398
- timesync_mode == 2
@@ -384,6 +403,6 @@
384403
service:
385404
# wokeignore:rule=master
386405
name: timemaster
387-
state: started
406+
state: "{{ 'started' if __timesync_is_booted else omit }}"
388407
enabled: true
389408
when: timesync_mode == 3

tasks/set_vars.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,23 @@
4343
- "default.yml"
4444
paths:
4545
- "{{ role_path }}/vars"
46+
47+
- name: Determine if system is booted with systemd
48+
when: __timesync_is_booted is not defined
49+
block:
50+
- name: Run systemctl
51+
# noqa command-instead-of-module
52+
command: systemctl is-system-running
53+
register: __is_system_running
54+
changed_when: false
55+
failed_when: false
56+
57+
- name: Require installed systemd
58+
fail:
59+
msg: "Error: This role requires systemd to be installed."
60+
when: '"No such file or directory" in __is_system_running.msg | d("")'
61+
62+
- name: Set flag to indicate that systemd runtime operations are available
63+
set_fact:
64+
# see https://www.man7.org/linux/man-pages/man1/systemctl.1.html#:~:text=is-system-running%20output
65+
__timesync_is_booted: "{{ __is_system_running.stdout != 'offline' }}"

tests/inventory.yaml.j2

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
all:
22
hosts:
33
{{ inventory_hostname }}:
4-
{% for key in ["ansible_all_ipv4_addresses", "ansible_all_ipv6_addresses",
4+
{% if __ansible_connection in ["local", "buildah"] %}
5+
ansible_connection: {{ __ansible_connection }}
6+
ansible_host: localhost
7+
{% else %}
8+
{% for key in ["ansible_all_ipv4_addresses", "ansible_all_ipv6_addresses",
59
"ansible_default_ipv4", "ansible_default_ipv6", "ansible_host",
610
"ansible_port", "ansible_ssh_common_args",
711
"ansible_ssh_private_key_file","ansible_user"] %}
8-
{% if key in hostvars[inventory_hostname] %}
12+
{% if key in hostvars[inventory_hostname] %}
913
{{ key }}: {{ hostvars[inventory_hostname][key] }}
10-
{% endif %}
11-
{% endfor %}
14+
{% endif %}
15+
{% endfor %}
16+
{% endif %}

tests/tasks/setup.yml

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
---
22
# common test setup tasks
3-
- name: Determine if system is ostree and set flag
4-
when: not __timesync_is_ostree is defined
5-
block:
6-
- name: Check if system is ostree
7-
stat:
8-
path: /run/ostree-booted
9-
register: __ostree_booted_stat
3+
- name: Run the role only to get vars needed for validation
4+
include_role:
5+
name: linux-system-roles.timesync
6+
public: true
7+
tasks_from: set_vars.yml
108

11-
- name: Set flag to indicate system is ostree
12-
set_fact:
13-
__timesync_is_ostree: "{{ __ostree_booted_stat.stat.exists }}"
9+
- name: Skip test on ostree systems if unsupported
10+
meta: end_host
11+
when:
12+
- __timesync_ostree_unsupported | d(false)
13+
- __timesync_is_ostree | d(false)
1414

1515
- name: Ensure iproute for gathering default_ipv4 fact
1616
package:
@@ -25,20 +25,9 @@
2525
when: __required_facts |
2626
difference(ansible_facts.keys() | list) | length > 0
2727
vars:
28-
__required_facts:
29-
- default_ipv4
30-
- distribution
31-
- distribution_major_version
32-
- distribution_version
33-
- os_family
28+
__required_facts: "{{ __timesync_required_facts + ['default_ipv4'] }}"
3429
__required_facts_subsets: "{{ ['!all', '!min'] + __required_facts }}"
3530

3631
- name: Debug
3732
debug:
3833
msg: facts {{ ansible_facts | to_nice_json }}
39-
40-
- name: Skip test on ostree systems if unsupported
41-
meta: end_host
42-
when:
43-
- __timesync_ostree_unsupported | d(false)
44-
- __timesync_is_ostree | d(false)

tests/tests_chrony.yml

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,59 @@
2525
include_role:
2626
name: linux-system-roles.timesync
2727
public: true
28+
when: not __bootc_validation | d(false)
29+
30+
- name: Run the role only to get vars needed for validation
31+
include_role:
32+
name: linux-system-roles.timesync
33+
public: true
34+
tasks_from: set_vars.yml
35+
when: __bootc_validation | d(false)
2836

2937
- name: Run chrony tests
3038
tags: tests::verify
3139
block:
3240
- name: Flush handlers
3341
meta: flush_handlers
42+
when: not __bootc_validation | d(false)
3443

3544
- name: Wait for services to start
3645
wait_for:
3746
timeout: 2
47+
when: not __bootc_validation | d(false)
48+
49+
- name: Create QEMU deployment during bootc end-to-end test
50+
delegate_to: localhost
51+
command: "{{ lsr_scriptdir | quote }}/bootc-buildah-qcow.sh {{ ansible_host | quote }}"
52+
changed_when: true
53+
when: ansible_connection == "buildah"
54+
55+
- name: Check headers for ansible_managed, fingerprint
56+
include_tasks: tasks/check_header.yml
57+
loop:
58+
- "{{ timesync_chrony_conf_path }}"
59+
- "{{ timesync_chrony_sysconfig_path }}"
60+
loop_control:
61+
loop_var: __file
62+
vars:
63+
__fingerprint: "system_role:timesync"
64+
65+
# check settings in files - only check we can do in non-booted mode
66+
- name: Check for settings in chrony.conf
67+
command: grep -q {{ item | quote }} {{ timesync_chrony_conf_path }}
68+
loop: "{{ timesync_chrony_custom_settings +
69+
(timesync_ntp_servers | selectattr('hostname') | map(attribute='hostname') | list) +
70+
['makestep ' ~ timesync_step_threshold ~ ' 3'] +
71+
([sourcedirstr] if sourcedirstr | length > 0 else []) }}"
72+
vars:
73+
sourcedirstr: "{{ 'sourcedir ' ~ timesync_chrony_dhcp_sourcedir
74+
if timesync_dhcp_ntp_servers and timesync_chrony_dhcp_sourcedir
75+
else '' }}"
76+
changed_when: false
77+
78+
- name: Skip remaining checks unless booted
79+
meta: end_host
80+
when: not __timesync_is_booted
3881

3982
- name: Get list of currently used time sources
4083
shell: chronyc -n sources || ntpq -pn
@@ -58,16 +101,6 @@
58101
that:
59102
- "'tracking.log' in logfiles.stdout"
60103

61-
- name: Check headers for ansible_managed, fingerprint
62-
include_tasks: tasks/check_header.yml
63-
loop:
64-
- "{{ timesync_chrony_conf_path }}"
65-
- "{{ timesync_chrony_sysconfig_path }}"
66-
loop_control:
67-
loop_var: __file
68-
vars:
69-
__fingerprint: "system_role:timesync"
70-
71104
always:
72105
- name: Cleanup after tests
73106
include_tasks: tasks/cleanup.yml

tests/tests_default_wrapper.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
---
22
- name: Create static inventory from hostvars
33
tags:
4-
- 'tests::slow'
4+
- tests::slow
5+
- tests::booted
56
hosts: all
67
tasks:
8+
- name: Save connection method
9+
set_fact:
10+
__ansible_connection: "{{ ansible_connection }}"
11+
712
- name: Create temporary file
813
tempfile:
914
state: file
@@ -20,12 +25,14 @@
2025

2126
- name: Run tests_default.yml normally
2227
tags:
23-
- 'tests::slow'
28+
- tests::slow
29+
- tests::booted
2430
import_playbook: tests_default.yml
2531

2632
- name: Run tests_default.yml in check_mode
2733
tags:
28-
- 'tests::slow'
34+
- tests::slow
35+
- tests::booted
2936
hosts: all
3037
tasks:
3138
- name: Run ansible-playbook with tests_default.yml in check mode

0 commit comments

Comments
 (0)