Skip to content

Commit 486d37c

Browse files
authored
Remove all jinja conditionals from commands (bugfix) (#2422)
* Add ubuntucore (os) detecting function * Propagate the 2 new ubuntucore variables to all jobs Now a job can know if: - it is running from a strict snap (it is namespaced) - it is running on the UC os * Use the new UC envvar Minor: remove random unused jinja2 templates from non-templated units * Make shellcheck happy * Test new function * Fix typo in docstring Minor: remove l as it yields a linting warning
1 parent 96de726 commit 486d37c

8 files changed

Lines changed: 107 additions & 58 deletions

File tree

checkbox-ng/plainbox/impl/execution.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,52 +21,51 @@
2121
"""
2222

2323
import contextlib
24+
import enum
25+
import functools
2426
import getpass
2527
import gzip
2628
import io
2729
import logging
30+
import operator
2831
import os
2932
import select
33+
import shlex
34+
import shutil
35+
import signal
3036
import subprocess
3137
import sys
3238
import tempfile
3339
import threading
3440
import time
35-
import shlex
36-
import signal
37-
import shutil
38-
import functools
39-
import operator
40-
import enum
41-
4241
from contextlib import suppress
43-
from subprocess import check_output, check_call, run
42+
from subprocess import check_call, check_output, run
4443

4544
from plainbox.abc import IJobResult, IJobRunner
4645
from plainbox.i18n import gettext as _
4746
from plainbox.impl.color import Colorizer
48-
from plainbox.impl.unit.job import supported_plugins
49-
from plainbox.impl.unit.unit import (
50-
on_ubuntucore,
51-
get_snap_base,
52-
get_checkbox_runtime_path,
53-
)
47+
from plainbox.impl.jobcache import ResourceJobCache
5448
from plainbox.impl.result import (
49+
IOLogRecord,
5550
IOLogRecordWriter,
5651
JobResultBuilder,
57-
IOLogRecord,
5852
)
5953
from plainbox.impl.runner import (
6054
CommandOutputWriter,
6155
IOLogRecordGenerator,
6256
JobRunnerUIDelegate,
6357
slugify,
6458
)
65-
from plainbox.impl.jobcache import ResourceJobCache
6659
from plainbox.impl.secure.sudo_broker import sudo_password_provider
6760
from plainbox.impl.session.storage import WellKnownDirsHelper
61+
from plainbox.impl.unit.job import InvalidJob, supported_plugins
62+
from plainbox.impl.unit.unit import (
63+
get_checkbox_runtime_path,
64+
get_snap_base,
65+
on_os_ubuntucore,
66+
on_ubuntucore,
67+
)
6868
from plainbox.vendor import extcmd
69-
from plainbox.impl.unit.job import InvalidJob
7069

7170
logger = logging.getLogger("plainbox.unified")
7271

@@ -628,6 +627,13 @@ def set_if_not_none(envvar, source):
628627
set_if_not_none("CHECKBOX_SHARE", job.provider.CHECKBOX_SHARE)
629628
if os.getenv("SNAP"):
630629
set_if_not_none("CHECKBOX_RUNTIME", str(get_checkbox_runtime_path()))
630+
631+
if on_ubuntucore():
632+
set_if_not_none("CHECKBOX_RUNNING_STRICT_SNAP", "1")
633+
634+
if on_os_ubuntucore():
635+
set_if_not_none("CHECKBOX_OS_IS_UBUNTUCORE", "1")
636+
631637
# Inject additional variables that are requested in the config
632638
if environ is not None:
633639
for env_var in environ:

checkbox-ng/plainbox/impl/unit/test_unit.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@
2727
from unittest import TestCase, mock
2828

2929
from plainbox.abc import IProvider1
30-
from plainbox.impl.unit.unit import Unit, MissingParam, get_snap_base
30+
from plainbox.impl.unit.unit import (
31+
Unit,
32+
MissingParam,
33+
get_snap_base,
34+
on_os_ubuntucore,
35+
)
3136
from plainbox.impl.validation import Problem, Severity
3237

3338

@@ -470,3 +475,35 @@ def test_no_snap_envvar(self):
470475
get_snap_base.cache_clear()
471476
with self.assertRaises(ValueError):
472477
get_snap_base()
478+
479+
480+
class OnOSUbuntuCore(TestCase):
481+
@mock.patch(
482+
"plainbox.impl.unit.unit.Path.read_text",
483+
return_value='NAME="Ubuntu Core"\nVERSION="20"',
484+
)
485+
def test_on_uc_strict_snap(self, _):
486+
on_os_ubuntucore.cache_clear()
487+
488+
self.assertTrue(on_os_ubuntucore())
489+
490+
@mock.patch(
491+
"plainbox.impl.unit.unit.Path.read_text",
492+
side_effect=[
493+
FileNotFoundError(),
494+
'NAME="Ubuntu Core"',
495+
],
496+
)
497+
def test_on_uc_source(self, _):
498+
on_os_ubuntucore.cache_clear()
499+
500+
self.assertTrue(on_os_ubuntucore())
501+
502+
@mock.patch(
503+
"plainbox.impl.unit.unit.Path.read_text",
504+
side_effect=[FileNotFoundError(), 'NAME="Ubuntu"'],
505+
)
506+
def test_not_on_uc(self, _):
507+
on_os_ubuntucore.cache_clear()
508+
509+
self.assertFalse(on_os_ubuntucore())

checkbox-ng/plainbox/impl/unit/unit.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,24 @@
2929
import logging
3030
import os
3131
import string
32-
from pathlib import Path
32+
from contextlib import suppress
3333
from functools import lru_cache
34+
from pathlib import Path
3435

3536
from jinja2 import Template
3637

3738
from plainbox.i18n import gettext as _
38-
from plainbox.impl.decorators import cached_property
39-
from plainbox.impl.decorators import instance_method_lru_cache
39+
from plainbox.impl.decorators import cached_property, instance_method_lru_cache
4040
from plainbox.impl.secure.origin import Origin
4141
from plainbox.impl.secure.rfc822 import normalize_rfc822_value
42-
from plainbox.impl.symbol import Symbol
43-
from plainbox.impl.symbol import SymbolDef
44-
from plainbox.impl.symbol import SymbolDefMeta
45-
from plainbox.impl.symbol import SymbolDefNs
46-
from plainbox.impl.unit import concrete_validators
47-
from plainbox.impl.unit import get_accessed_parameters
48-
from plainbox.impl.unit.validators import IFieldValidator
49-
from plainbox.impl.unit.validators import MultiUnitFieldIssue
50-
from plainbox.impl.unit.validators import PresentFieldValidator
51-
from plainbox.impl.unit.validators import UnitFieldIssue
52-
from plainbox.impl.validation import Problem
53-
from plainbox.impl.validation import Severity
42+
from plainbox.impl.symbol import Symbol, SymbolDef, SymbolDefMeta, SymbolDefNs
43+
from plainbox.impl.unit import concrete_validators, get_accessed_parameters
44+
from plainbox.impl.unit.validators import (
45+
MultiUnitFieldIssue,
46+
PresentFieldValidator,
47+
UnitFieldIssue,
48+
)
49+
from plainbox.impl.validation import Problem, Severity
5450

5551
__all__ = ["Unit", "UnitValidator"]
5652

@@ -61,18 +57,32 @@
6157
@lru_cache(maxsize=None)
6258
def on_ubuntucore():
6359
"""
64-
Check if running from on ubuntu core
60+
Returns `True` when running in a strict snap
6561
"""
6662
snap = os.getenv("SNAP")
6763
if snap:
6864
with open(os.path.join(snap, "meta/snap.yaml")) as f:
69-
for l in f.readlines():
70-
if l == "confinement: classic\n":
65+
for line in f.readlines():
66+
if line == "confinement: classic\n":
7167
return False
7268
return True
7369
return False
7470

7571

72+
@lru_cache(maxsize=None)
73+
def on_os_ubuntucore() -> bool:
74+
"""
75+
Returns `True` if the host OS is Ubuntu Core
76+
"""
77+
with suppress(FileNotFoundError):
78+
# if this path exists, we are in a strict snap, but we may not be on UC
79+
return (
80+
'NAME="Ubuntu Core"'
81+
in Path("/var/lib/snapd/hostfs/etc/os-release").read_text()
82+
)
83+
return 'NAME="Ubuntu Core"' in Path("/etc/os-release").read_text()
84+
85+
7686
@lru_cache(maxsize=None)
7787
def get_snap_base():
7888
"""

providers/base/units/disk/encryption.pxu

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
id: disk/encryption/detect
22
category_id: com.canonical.plainbox::disk
33
plugin: shell
4-
template-engine: jinja2
54
user: root
65
imports: from com.canonical.plainbox import manifest
76
requires:
@@ -14,11 +13,11 @@ _purpose:
1413
Examine the system to detect if one of the standard full disk encryption
1514
implementations is in use
1615
command:
17-
{%- if __on_ubuntucore__ %}
16+
if [ "$CHECKBOX_RUNNING_STRICT_SNAP" = "1" ]; then
1817
fde_tests.py
19-
{%- else %}
18+
else
2019
fde_tests.py desktop
21-
{% endif -%}
20+
fi
2221
estimated_duration: 2.0
2322

2423
id: disk/encryption/check-fde-tpm

providers/base/units/info/jobs.pxu

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,12 +376,11 @@ _summary: Attaches info about disk partitions
376376
plugin: attachment
377377
category_id: com.canonical.plainbox::info
378378
id: info/buildstamp
379-
template-engine: jinja2
380379
estimated_duration: 0.1
381380
_description: Attaches the buildstamp identifier for the OS
382381
_summary: Attaches the buildstamp identifier for the OS
383382
command:
384-
{%- if __on_ubuntucore__ %}
383+
if [ "$CHECKBOX_RUNNING_STRICT_SNAP" = "1" ]; then
385384
if [ -s /run/mnt/ubuntu-seed/.disk/info ]; then
386385
cat /run/mnt/ubuntu-seed/.disk/info
387386
elif [ -s /writable/system-data/etc/buildstamp ]; then
@@ -391,7 +390,7 @@ command:
391390
else
392391
exit 1
393392
fi
394-
{% else -%}
393+
else
395394
if [ -s /var/lib/ubuntu_dist_channel ]; then # PC projects
396395
cat /var/lib/ubuntu_dist_channel
397396
elif [ -s /var/log/installer/media-info ]; then # Stock installer info
@@ -403,7 +402,7 @@ command:
403402
else
404403
exit 1
405404
fi
406-
{% endif -%}
405+
fi
407406

408407
plugin: shell
409408
category_id: com.canonical.plainbox::info

providers/base/units/oob-management/jobs.pxu

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,12 @@ requires:
112112
package.name == 'lms'
113113
{% endif -%}
114114
command:
115-
{%- if __on_ubuntucore__ %}
116-
{# TODO: name is a guess until snap provided #}
115+
if [ "$CHECKBOX_RUNNING_STRICT_SNAP" = "1" ]; then
116+
# TODO: name is a guess until snap provided
117117
SERVICE_NAME="snap.lms.lms.service"
118-
{%- else %}
118+
else
119119
SERVICE_NAME="lms.service"
120-
{% endif -%}
120+
fi
121121
systemctl is-active --quiet "$SERVICE_NAME"
122122
RETVAL=$?
123123
if [ $RETVAL -ne 0 ]; then

providers/base/units/submission/jobs.pxu

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
id: dkms_info_json
2-
template-engine: jinja2
32
plugin: attachment
43
category_id: com.canonical.plainbox::info
54
command:
6-
{%- if __on_ubuntucore__ %}
5+
if [ "$CHECKBOX_RUNNING_STRICT_SNAP" = "1" ]; then
76
echo "{}"
8-
{%- else %}
7+
else
98
dkms_info.py --format json | checkbox-support-parse dkms-info | \
109
jq '.dkms_info'
11-
{% endif -%}
10+
fi
1211
_description: Attaches json dumps of installed dkms package information.
1312
_summary: Attaches json dumps of installed dkms package information.
1413

providers/resource/jobs/resource.pxu

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ command: block_device_resource.py
223223
_summary: Create resource info for removable block devices
224224

225225
id: usb
226-
template-engine: jinja2
227226
estimated_duration: 0.33
228227
plugin: resource
229228
category_id: information_gathering
@@ -232,11 +231,11 @@ _summary: Collect information about supported types of USB
232231
command:
233232
for version in 2 3; do
234233
echo -n "usb$version: "
235-
{%- if __on_ubuntucore__ %}
236-
checkbox-support-lsusb -f $SNAP/checkbox-runtime/var/lib/usbutils/usb.ids | grep -Pq "Linux Foundation ${version}.\d+ root hub" && echo "supported" || echo "unsupported"
237-
{% else %}
238-
lsusb | grep -q "Linux Foundation ${version}.0 root hub" && echo "supported" || echo "unsupported"
239-
{% endif -%}
234+
if [ "$CHECKBOX_RUNNING_STRICT_SNAP" = "1" ]; then
235+
checkbox-support-lsusb -f "$SNAP"/checkbox-runtime/var/lib/usbutils/usb.ids | grep -Pq "Linux Foundation ${version}.\d+ root hub" && echo "supported" || echo "unsupported"
236+
else
237+
lsusb | grep -q "Linux Foundation ${version}.0 root hub" && echo "supported" || echo "unsupported"
238+
fi
240239
done
241240

242241
id: xinput

0 commit comments

Comments
 (0)