Merge lp:~mvo/ubuntu-system-image/machine-id-fallback into lp:~registry/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:~registry/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
=== modified file 'systemimage/helpers.py'
--- systemimage/helpers.py 2014-09-16 20:06:56 +0000
+++ systemimage/helpers.py 2014-11-04 17:13:40 +0000
@@ -47,7 +47,8 @@
4747
4848
49LAST_UPDATE_FILE = '/userdata/.last_update'49LAST_UPDATE_FILE = '/userdata/.last_update'
50UNIQUE_MACHINE_ID_FILE = '/var/lib/dbus/machine-id'50# systemd is taking over
51UNIQUE_MACHINE_ID_FILES = ['/var/lib/dbus/machine-id', '/etc/machine-id']
51DEFAULT_DIRMODE = 0o0270052DEFAULT_DIRMODE = 0o02700
52MiB = 1 << 2053MiB = 1 << 20
53EMPTYSTRING = ''54EMPTYSTRING = ''
@@ -275,8 +276,11 @@
275def phased_percentage(*, reset=False):276def phased_percentage(*, reset=False):
276 global _pp_cache277 global _pp_cache
277 if _pp_cache is None:278 if _pp_cache is None:
278 with open(UNIQUE_MACHINE_ID_FILE, 'rb') as fp:279 for f in UNIQUE_MACHINE_ID_FILES:
279 data = fp.read()280 if os.path.exists(f):
281 with open(f, 'rb') as fp:
282 data = fp.read()
283 break
280 now = str(time.time()).encode('us-ascii')284 now = str(time.time()).encode('us-ascii')
281 r = random.Random()285 r = random.Random()
282 r.seed(data + now)286 r.seed(data + now)
283287
=== modified file 'systemimage/tests/test_helpers.py'
--- systemimage/tests/test_helpers.py 2014-09-16 20:06:56 +0000
+++ systemimage/tests/test_helpers.py 2014-11-04 17:13:40 +0000
@@ -276,25 +276,26 @@
276class TestPhasedPercentage(unittest.TestCase):276class TestPhasedPercentage(unittest.TestCase):
277 def setUp(self):277 def setUp(self):
278 phased_percentage(reset=True)278 phased_percentage(reset=True)
279 self.tmpdir = tempfile.mkdtemp()
280 self.machine_id = os.path.join(self.tmpdir, 'machine-id')
281 with open(self.machine_id, 'wb') as fp:
282 fp.write(b'0123456789abcdef\n')
279283
280 def tearDown(self):284 def tearDown(self):
281 phased_percentage(reset=True)285 phased_percentage(reset=True)
286 shutil.rmtree(self.tmpdir)
282287
283 def test_phased_percentage(self):288 def test_phased_percentage(self):
284 # This function returns a percentage between 0 and 100. If this value289 # This function returns a percentage between 0 and 100. If this value
285 # is greater than a similar value in the index.json's 'image' section,290 # is greater than a similar value in the index.json's 'image' section,
286 # that image is completely ignored.291 # that image is completely ignored.
287 with ExitStack() as stack:292 with ExitStack() as stack:
288 tmpdir = stack.enter_context(temporary_directory())
289 path = os.path.join(tmpdir, 'machine-id')
290 stack.enter_context(patch(293 stack.enter_context(patch(
291 'systemimage.helpers.UNIQUE_MACHINE_ID_FILE',294 'systemimage.helpers.UNIQUE_MACHINE_ID_FILES',
292 path))295 [self.machine_id]))
293 stack.enter_context(patch(296 stack.enter_context(patch(
294 'systemimage.helpers.time.time',297 'systemimage.helpers.time.time',
295 return_value=1380659512.983512))298 return_value=1380659512.983512))
296 with open(path, 'wb') as fp:
297 fp.write(b'0123456789abcdef\n')
298 self.assertEqual(phased_percentage(), 81)299 self.assertEqual(phased_percentage(), 81)
299 # The value is cached, so it's always the same for the life of the300 # The value is cached, so it's always the same for the life of the
300 # process, at least until we reset it.301 # process, at least until we reset it.
@@ -303,25 +304,34 @@
303 def test_phased_percentage_reset(self):304 def test_phased_percentage_reset(self):
304 # Test the reset API.305 # Test the reset API.
305 with ExitStack() as stack:306 with ExitStack() as stack:
306 tmpdir = stack.enter_context(temporary_directory())
307 path = os.path.join(tmpdir, 'machine-id')
308 stack.enter_context(patch(307 stack.enter_context(patch(
309 'systemimage.helpers.UNIQUE_MACHINE_ID_FILE',308 'systemimage.helpers.UNIQUE_MACHINE_ID_FILES',
310 path))309 [self.machine_id]))
311 stack.enter_context(patch(310 stack.enter_context(patch(
312 'systemimage.helpers.time.time',311 'systemimage.helpers.time.time',
313 return_value=1380659512.983512))312 return_value=1380659512.983512))
314 with open(path, 'wb') as fp:
315 fp.write(b'0123456789abcdef\n')
316 self.assertEqual(phased_percentage(), 81)313 self.assertEqual(phased_percentage(), 81)
317 # The value is cached, so it's always the same for the life of the314 # The value is cached, so it's always the same for the life of the
318 # process, at least until we reset it.315 # process, at least until we reset it.
319 with open(path, 'wb') as fp:316 with open(self.machine_id, 'wb') as fp:
320 fp.write(b'x0123456789abcde\n')317 fp.write(b'x0123456789abcde\n')
321 self.assertEqual(phased_percentage(reset=True), 81)318 self.assertEqual(phased_percentage(reset=True), 81)
322 # The next one will have a different value.319 # The next one will have a different value.
323 self.assertEqual(phased_percentage(), 17)320 self.assertEqual(phased_percentage(), 17)
324321
322 def test_phased_percentage_unique_fallback(self):
323 # ensure that the fallback for
324 # systemimage.helpers.UNIQUE_MACHINE_ID_FILES
325 # works
326 with ExitStack() as stack:
327 stack.enter_context(patch(
328 'systemimage.helpers.UNIQUE_MACHINE_ID_FILES',
329 ['/xxx/does-not-exists', self.machine_id]))
330 stack.enter_context(patch(
331 'systemimage.helpers.time.time',
332 return_value=1380659512.983512))
333 self.assertEqual(phased_percentage(), 81)
334
325335
326class TestSignature(unittest.TestCase):336class TestSignature(unittest.TestCase):
327 def test_calculate_signature(self):337 def test_calculate_signature(self):

Subscribers

People subscribed via source and target branches