Merge ~sylvain-pineau/plainbox:is_snap_based_helper into plainbox:master

Proposed by Sylvain Pineau
Status: Merged
Approved by: Sylvain Pineau
Approved revision: 675c511894f6060ab1494e8aeefbe023c94e93a7
Merged at revision: 259a49907b0f6d978a5c032d68b00f78a8d7bab8
Proposed branch: ~sylvain-pineau/plainbox:is_snap_based_helper
Merge into: plainbox:master
Diff against target: 66 lines (+20/-0)
1 file modified
plainbox/impl/unit/unit.py (+20/-0)
Reviewer Review Type Date Requested Status
Jonathan Cave (community) Approve
Sylvain Pineau Needs Resubmitting
Review via email: mp+335304@code.launchpad.net

Description of the change

this MR adds helpers functions to detect is we're running from a snap on ubuntu core (not classic).

Tested using https://code.launchpad.net/~sylvain-pineau/plainbox-provider-checkbox/+git/plainbox-provider-checkbox/+merge/335303 on a server image.

To post a comment you must log in.
Revision history for this message
Jonathan Cave (jocave) wrote :

This MR also seems to remove this censoREd module, but isn't mentioned in the description. Is that intended?

The function is_classic_snap() is defined and a variable is_classic_snap referenced, but I don't see it being set anywhere. If the variable is not going to be initialised then maybe it should be removed?

review: Needs Fixing
Revision history for this message
Sylvain Pineau (sylvain-pineau) wrote :

cleanup commits and keep only one function (module internal)

review: Needs Resubmitting
Revision history for this message
Jonathan Cave (jocave) wrote :

Cleaner, thanks

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/plainbox/impl/unit/unit.py b/plainbox/impl/unit/unit.py
2index 1cc2397..b21377d 100644
3--- a/plainbox/impl/unit/unit.py
4+++ b/plainbox/impl/unit/unit.py
5@@ -29,6 +29,7 @@ import json
6 import logging
7 import os
8 import string
9+from functools import lru_cache
10
11 from jinja2 import Template
12
13@@ -56,6 +57,21 @@ __all__ = ['Unit', 'UnitValidator']
14 logger = logging.getLogger("plainbox.unit")
15
16
17+@lru_cache(maxsize=None)
18+def on_ubuntucore():
19+ """
20+ Check if running from on ubuntu core
21+ """
22+ snap = os.getenv("SNAP")
23+ if snap:
24+ with open(os.path.join(snap, 'meta/snap.yaml')) as f:
25+ for l in f.readlines():
26+ if l == "confinement: classic\n":
27+ return False
28+ return True
29+ return False
30+
31+
32 class MissingParam(Exception):
33 """
34 Indicaiton of a missing parameter required for template instantiation.
35@@ -613,6 +629,7 @@ class Unit(metaclass=UnitType):
36 # checkpoints
37 tmp_params = self.parameters.copy()
38 tmp_params.update({'__system_env__': os.environ})
39+ tmp_params.update({'__on_ubuntucore__': on_ubuntucore()})
40 value = Template(value).render(tmp_params)
41 else:
42 try:
43@@ -646,6 +663,7 @@ class Unit(metaclass=UnitType):
44 if self.template_engine == 'jinja2':
45 tmp_params = self.parameters.copy()
46 tmp_params.update({'__system_env__': os.environ})
47+ tmp_params.update({'__on_ubuntucore__': on_ubuntucore()})
48 value = Template(value).render(tmp_params)
49 else:
50 value = string.Formatter().vformat(value, (), self.parameters)
51@@ -691,6 +709,7 @@ class Unit(metaclass=UnitType):
52 if self.template_engine == 'jinja2':
53 tmp_params = self.parameters.copy()
54 tmp_params.update({'__system_env__': os.environ})
55+ tmp_params.update({'__on_ubuntucore__': on_ubuntucore()})
56 msgstr = Template(msgstr).render(tmp_params)
57 else:
58 msgstr = string.Formatter().vformat(
59@@ -706,6 +725,7 @@ class Unit(metaclass=UnitType):
60 if self.template_engine == 'jinja2':
61 tmp_params = self.parameters.copy()
62 tmp_params.update({'__system_env__': os.environ})
63+ tmp_params.update({'__on_ubuntucore__': on_ubuntucore()})
64 msgstr = Template(msgstr).render(tmp_params)
65 else:
66 msgstr = string.Formatter().vformat(

Subscribers

People subscribed via source and target branches