Merge lp:~mvo/ubuntu-system-image/machine-id-fallback into lp:~ubuntu-system-image/ubuntu-system-image/client

Proposed by Michael Vogt
Status: Merged
Approved by: Barry Warsaw
Approved revision: 290
Merge reported by: Barry Warsaw
Merged at revision: not available
Proposed branch: lp:~mvo/ubuntu-system-image/machine-id-fallback
Merge into: lp:~ubuntu-system-image/ubuntu-system-image/client
Diff against target: 106 lines (+30/-16)
2 files modified
systemimage/helpers.py (+7/-3)
systemimage/tests/test_helpers.py (+23/-13)
To merge this branch: bzr merge lp:~mvo/ubuntu-system-image/machine-id-fallback
Reviewer Review Type Date Requested Status
Barry Warsaw (community) Approve
Review via email: mp+240616@code.launchpad.net

Description of the change

This branch adds support for both /etc/machine-id and /var/lib/dbus/machine-id as the later is taking over (see also freedesktop bug https://bugs.freedesktop.org/show_bug.cgi?id=85572 - waiting for final confirmation from upstream though).

To post a comment you must log in.
Revision history for this message
Barry Warsaw (barry) wrote :

Thanks Michael. I had to port this branch to s-i 3.0, which has changed quite a few things as to make this a non-trivial merge. But I copied your implementation in spirit so you get credit. :)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'systemimage/helpers.py'
2--- systemimage/helpers.py 2014-09-16 20:06:56 +0000
3+++ systemimage/helpers.py 2014-11-04 17:13:40 +0000
4@@ -47,7 +47,8 @@
5
6
7 LAST_UPDATE_FILE = '/userdata/.last_update'
8-UNIQUE_MACHINE_ID_FILE = '/var/lib/dbus/machine-id'
9+# systemd is taking over
10+UNIQUE_MACHINE_ID_FILES = ['/var/lib/dbus/machine-id', '/etc/machine-id']
11 DEFAULT_DIRMODE = 0o02700
12 MiB = 1 << 20
13 EMPTYSTRING = ''
14@@ -275,8 +276,11 @@
15 def phased_percentage(*, reset=False):
16 global _pp_cache
17 if _pp_cache is None:
18- with open(UNIQUE_MACHINE_ID_FILE, 'rb') as fp:
19- data = fp.read()
20+ for f in UNIQUE_MACHINE_ID_FILES:
21+ if os.path.exists(f):
22+ with open(f, 'rb') as fp:
23+ data = fp.read()
24+ break
25 now = str(time.time()).encode('us-ascii')
26 r = random.Random()
27 r.seed(data + now)
28
29=== modified file 'systemimage/tests/test_helpers.py'
30--- systemimage/tests/test_helpers.py 2014-09-16 20:06:56 +0000
31+++ systemimage/tests/test_helpers.py 2014-11-04 17:13:40 +0000
32@@ -276,25 +276,26 @@
33 class TestPhasedPercentage(unittest.TestCase):
34 def setUp(self):
35 phased_percentage(reset=True)
36+ self.tmpdir = tempfile.mkdtemp()
37+ self.machine_id = os.path.join(self.tmpdir, 'machine-id')
38+ with open(self.machine_id, 'wb') as fp:
39+ fp.write(b'0123456789abcdef\n')
40
41 def tearDown(self):
42 phased_percentage(reset=True)
43+ shutil.rmtree(self.tmpdir)
44
45 def test_phased_percentage(self):
46 # This function returns a percentage between 0 and 100. If this value
47 # is greater than a similar value in the index.json's 'image' section,
48 # that image is completely ignored.
49 with ExitStack() as stack:
50- tmpdir = stack.enter_context(temporary_directory())
51- path = os.path.join(tmpdir, 'machine-id')
52 stack.enter_context(patch(
53- 'systemimage.helpers.UNIQUE_MACHINE_ID_FILE',
54- path))
55+ 'systemimage.helpers.UNIQUE_MACHINE_ID_FILES',
56+ [self.machine_id]))
57 stack.enter_context(patch(
58 'systemimage.helpers.time.time',
59 return_value=1380659512.983512))
60- with open(path, 'wb') as fp:
61- fp.write(b'0123456789abcdef\n')
62 self.assertEqual(phased_percentage(), 81)
63 # The value is cached, so it's always the same for the life of the
64 # process, at least until we reset it.
65@@ -303,25 +304,34 @@
66 def test_phased_percentage_reset(self):
67 # Test the reset API.
68 with ExitStack() as stack:
69- tmpdir = stack.enter_context(temporary_directory())
70- path = os.path.join(tmpdir, 'machine-id')
71 stack.enter_context(patch(
72- 'systemimage.helpers.UNIQUE_MACHINE_ID_FILE',
73- path))
74+ 'systemimage.helpers.UNIQUE_MACHINE_ID_FILES',
75+ [self.machine_id]))
76 stack.enter_context(patch(
77 'systemimage.helpers.time.time',
78 return_value=1380659512.983512))
79- with open(path, 'wb') as fp:
80- fp.write(b'0123456789abcdef\n')
81 self.assertEqual(phased_percentage(), 81)
82 # The value is cached, so it's always the same for the life of the
83 # process, at least until we reset it.
84- with open(path, 'wb') as fp:
85+ with open(self.machine_id, 'wb') as fp:
86 fp.write(b'x0123456789abcde\n')
87 self.assertEqual(phased_percentage(reset=True), 81)
88 # The next one will have a different value.
89 self.assertEqual(phased_percentage(), 17)
90
91+ def test_phased_percentage_unique_fallback(self):
92+ # ensure that the fallback for
93+ # systemimage.helpers.UNIQUE_MACHINE_ID_FILES
94+ # works
95+ with ExitStack() as stack:
96+ stack.enter_context(patch(
97+ 'systemimage.helpers.UNIQUE_MACHINE_ID_FILES',
98+ ['/xxx/does-not-exists', self.machine_id]))
99+ stack.enter_context(patch(
100+ 'systemimage.helpers.time.time',
101+ return_value=1380659512.983512))
102+ self.assertEqual(phased_percentage(), 81)
103+
104
105 class TestSignature(unittest.TestCase):
106 def test_calculate_signature(self):

Subscribers

People subscribed via source and target branches