Merge lp:~ltrager/maas/lp1630394 into lp:~maas-committers/maas/trunk

Proposed by Lee Trager
Status: Merged
Approved by: Lee Trager
Approved revision: no longer in the source branch.
Merged at revision: 5453
Proposed branch: lp:~ltrager/maas/lp1630394
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 104 lines (+32/-7)
3 files modified
src/maasserver/bootresources.py (+4/-3)
src/maasserver/models/signals/bootsources.py (+16/-3)
src/maasserver/models/signals/tests/test_bootsources.py (+12/-1)
To merge this branch: bzr merge lp:~ltrager/maas/lp1630394
Reviewer Review Type Date Requested Status
Blake Rouse (community) Approve
Review via email: mp+307767@code.launchpad.net

Commit message

Always wait to cache_boot_sources before importing resources.

On first run MAAS sets the default boot source in the ImportResourcesService. This causes a signal which refreshes the boot source cache. To prevent the cache from being updated twice MAAS just let the signal handle updating the cache and wasn't waiting for it to complete. This worked as MAAS starts downloading the actual files later on in the process. The way bootloaders are picked are by looking at what bootloaders are available in BootSourceCache. As this happened earlier in the process they were missed. This modifies the ImportResourcesService to always wait on refreshing the cache but modifying the signal to only refresh the cache if a boot source already exists.

To post a comment you must log in.
Revision history for this message
Blake Rouse (blake-rouse) wrote :

Looks good.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/bootresources.py'
2--- src/maasserver/bootresources.py 2016-10-04 13:59:47 +0000
3+++ src/maasserver/bootresources.py 2016-10-05 21:35:18 +0000
4@@ -1295,9 +1295,10 @@
5 create_gnupg_home()
6
7 # Ensure that boot sources exist.
8- if not ensure_boot_source_definition():
9- # Nothing was created so cache the boot sources before import.
10- cache_boot_sources()
11+ ensure_boot_source_definition()
12+
13+ # Cache the boot sources before import.
14+ cache_boot_sources()
15
16 # FIXME: This modifies the environment of the entire process, which is Not
17 # Cool. We should integrate with simplestreams in a more Pythonic manner.
18
19=== modified file 'src/maasserver/models/signals/bootsources.py'
20--- src/maasserver/models/signals/bootsources.py 2016-06-22 17:03:02 +0000
21+++ src/maasserver/models/signals/bootsources.py 2016-10-05 21:35:18 +0000
22@@ -9,6 +9,7 @@
23
24 from django.db.models.signals import (
25 post_delete,
26+ post_init,
27 post_save,
28 )
29 from maasserver.bootsources import cache_boot_sources
30@@ -21,15 +22,27 @@
31 signals = SignalsManager()
32
33
34+def is_first_run(sender, instance, *args, **kwargs):
35+ """Store whether or not this is the first BootSource added to MAAS.
36+
37+ On first run the ImportResourceService sets the default BootSource then
38+ caches the stream's contents as normal. Setting the default BootSource
39+ triggers this signal. This prevents updating the cache twice.
40+ """
41+ instance.__is_first_run = BootSource.objects.count() == 0
42+
43+
44 def update_boot_source_cache(sender, instance, *args, **kwargs):
45 """Update the `BootSourceCache` using the updated source.
46
47 This only begins after a successful commit to the database, and is then
48 run in a thread. Nothing waits for its completion.
49 """
50- post_commit_do(reactor.callLater, 0, cache_boot_sources)
51-
52-
53+ if not isinstance(instance, BootSource) or not instance.__is_first_run:
54+ post_commit_do(reactor.callLater, 0, cache_boot_sources)
55+
56+
57+signals.watch(post_init, is_first_run, BootSource)
58 signals.watch(post_save, update_boot_source_cache, BootSource)
59 signals.watch(post_delete, update_boot_source_cache, BootSource)
60 signals.watch_config(update_boot_source_cache, "enable_http_proxy")
61
62=== modified file 'src/maasserver/models/signals/tests/test_bootsources.py'
63--- src/maasserver/models/signals/tests/test_bootsources.py 2016-05-25 20:32:57 +0000
64+++ src/maasserver/models/signals/tests/test_bootsources.py 2016-10-05 21:35:18 +0000
65@@ -10,20 +10,30 @@
66 from maasserver.models.config import Config
67 from maasserver.testing.factory import factory
68 from maasserver.testing.testcase import MAASServerTestCase
69-from maastesting.matchers import MockCalledOnceWith
70+from maastesting.matchers import (
71+ MockCalledOnceWith,
72+ MockNotCalled,
73+)
74 from twisted.internet import reactor
75
76
77 class TestBootSourceSignals(MAASServerTestCase):
78 """Tests for the `BootSource` model's signals."""
79
80+ def test_doesnt_update_on_initial_BootSource_create(self):
81+ post_commit_do = self.patch(signals.bootsources, "post_commit_do")
82+ factory.make_BootSource(keyring_data=factory.make_bytes())
83+ self.assertThat(post_commit_do, MockNotCalled())
84+
85 def test_arranges_for_update_on_BootSource_create(self):
86+ factory.make_BootSource(keyring_data=factory.make_bytes())
87 post_commit_do = self.patch(signals.bootsources, "post_commit_do")
88 factory.make_BootSource(keyring_data=factory.make_bytes())
89 self.assertThat(post_commit_do, MockCalledOnceWith(
90 reactor.callLater, 0, cache_boot_sources))
91
92 def test_arranges_for_update_on_BootSource_update(self):
93+ factory.make_BootSource(keyring_data=factory.make_bytes())
94 self.patch(signals.bootsources, "post_commit_do")
95 boot_source = factory.make_BootSource(
96 keyring_data=factory.make_bytes())
97@@ -34,6 +44,7 @@
98 reactor.callLater, 0, cache_boot_sources))
99
100 def test_arranges_for_update_on_BootSource_delete(self):
101+ factory.make_BootSource(keyring_data=factory.make_bytes())
102 self.patch(signals.bootsources, "post_commit_do")
103 boot_source = factory.make_BootSource(
104 keyring_data=factory.make_bytes())