Merge lp:~lamont/maas/bug-1568847b into lp:~maas-committers/maas/trunk

Proposed by LaMont Jones
Status: Merged
Approved by: LaMont Jones
Approved revision: no longer in the source branch.
Merged at revision: 4927
Proposed branch: lp:~lamont/maas/bug-1568847b
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 191 lines (+47/-11)
5 files modified
src/maasserver/proxyconfig.py (+12/-6)
src/maasserver/service_monitor.py (+4/-0)
src/maasserver/tests/test_proxyconfig.py (+7/-4)
src/maasserver/tests/test_service_monitor.py (+22/-1)
src/provisioningserver/templates/proxy/maas-proxy.conf.template (+2/-0)
To merge this branch: bzr merge lp:~lamont/maas/bug-1568847b
Reviewer Review Type Date Requested Status
Blake Rouse (community) Approve
Review via email: mp+292050@code.launchpad.net

Commit message

Only start maas-proxy if we have a configuration file.

Description of the change

Only start maas-proxy if we have a configuration file.

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

Nicely done.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/proxyconfig.py'
2--- src/maasserver/proxyconfig.py 2016-03-31 23:43:06 +0000
3+++ src/maasserver/proxyconfig.py 2016-04-15 20:49:22 +0000
4@@ -5,6 +5,8 @@
5
6 __all__ = [
7 'proxy_update_config',
8+ 'get_proxy_config_path',
9+ 'is_config_present',
10 ]
11
12 import datetime
13@@ -39,14 +41,19 @@
14 """Raised if there is a problem with the proxy configuration."""
15
16
17-def get_proxy_config_dir():
18+def get_proxy_config_path():
19 """Location of bind configuration files."""
20 setting = os.getenv("MAAS_PROXY_CONFIG_DIR", "/var/lib/maas")
21 if isinstance(setting, bytes):
22 fsenc = sys.getfilesystemencoding()
23- return setting.decode(fsenc)
24- else:
25- return setting
26+ setting = setting.decode(fsenc)
27+ setting = os.sep.join([setting, MAAS_PROXY_CONF_NAME])
28+ return setting
29+
30+
31+def is_config_present():
32+ """Check if there is a configuration file for the proxy."""
33+ return os.access(get_proxy_config_path(), os.R_OK)
34
35
36 @asynchronous
37@@ -72,8 +79,7 @@
38 raise ProxyConfigFail(*error.args)
39 # Squid prefers ascii.
40 content = content.encode("ascii")
41- target_path = os.sep.join(
42- [get_proxy_config_dir(), MAAS_PROXY_CONF_NAME])
43+ target_path = get_proxy_config_path()
44 atomic_write(content, target_path, overwrite=True, mode=0o644)
45
46 if is_proxy_enabled():
47
48=== modified file 'src/maasserver/service_monitor.py'
49--- src/maasserver/service_monitor.py 2016-03-31 22:30:51 +0000
50+++ src/maasserver/service_monitor.py 2016-04-15 20:49:22 +0000
51@@ -47,10 +47,14 @@
52
53 @transactional
54 def db_get_expected_state():
55+ # Avoid recursive import.
56+ from maasserver import proxyconfig
57 if (Config.objects.get_config("enable_http_proxy") and
58 Config.objects.get_config("http_proxy")):
59 return (SERVICE_STATUS.OFF,
60 "disabled, alternate proxy is configured in settings.")
61+ elif proxyconfig.is_config_present() is False:
62+ return (SERVICE_STATUS.OFF, "No configuration file present.")
63 else:
64 return (SERVICE_STATUS.ON, None)
65
66
67=== modified file 'src/maasserver/tests/test_proxyconfig.py'
68--- src/maasserver/tests/test_proxyconfig.py 2016-03-31 20:41:32 +0000
69+++ src/maasserver/tests/test_proxyconfig.py 2016-04-15 20:49:22 +0000
70@@ -34,17 +34,20 @@
71
72
73 class TestGetConfigDir(MAASServerTestCase):
74- """Tests for `maasserver.proxyconfig.get_proxy_config_dir`."""
75+ """Tests for `maasserver.proxyconfig.get_proxy_config_path`."""
76
77 def test_returns_default(self):
78 self.assertEquals(
79- "/var/lib/maas", proxyconfig.get_proxy_config_dir())
80+ "/var/lib/maas/maas-proxy.conf",
81+ proxyconfig.get_proxy_config_path())
82
83 def test_env_overrides_default(self):
84 os.environ['MAAS_PROXY_CONFIG_DIR'] = factory.make_name('env')
85 self.assertEquals(
86- os.environ['MAAS_PROXY_CONFIG_DIR'],
87- proxyconfig.get_proxy_config_dir())
88+ os.sep.join([
89+ os.environ['MAAS_PROXY_CONFIG_DIR'],
90+ proxyconfig.MAAS_PROXY_CONF_NAME]),
91+ proxyconfig.get_proxy_config_path())
92 del(os.environ['MAAS_PROXY_CONFIG_DIR'])
93
94
95
96=== modified file 'src/maasserver/tests/test_service_monitor.py'
97--- src/maasserver/tests/test_service_monitor.py 2016-04-01 18:19:53 +0000
98+++ src/maasserver/tests/test_service_monitor.py 2016-04-15 20:49:22 +0000
99@@ -5,10 +5,14 @@
100
101 __all__ = []
102
103+import os
104 import random
105
106 from crochet import wait_for
107-from maasserver import service_monitor as service_monitor_module
108+from maasserver import (
109+ proxyconfig,
110+ service_monitor as service_monitor_module,
111+)
112 from maasserver.enum import SERVICE_STATUS
113 from maasserver.models.config import Config
114 from maasserver.models.service import Service
115@@ -118,6 +122,7 @@
116 # Pretend we're in a production environment.
117 self.patch(
118 service_monitor_module, "is_dev_environment").return_value = False
119+ self.patch(proxyconfig, "is_config_present").return_value = True
120
121 service = self.pick_service()
122 state = ServiceState(SERVICE_STATE.ON, "running")
123@@ -150,6 +155,7 @@
124 @wait_for_reactor
125 @inlineCallbacks
126 def test__buildServices_builds_services_list(self):
127+ self.patch(proxyconfig, "is_config_present").return_value = True
128 monitor_service = ServiceMonitorService(
129 sentinel.advertisingService, Clock())
130 service = self.pick_service()
131@@ -183,11 +189,23 @@
132 yield deferToDatabase(
133 transactional(Config.objects.set_config),
134 "http_proxy", "")
135+ self.patch(proxyconfig, "is_config_present").return_value = True
136 expected_state = yield maybeDeferred(service.get_expected_state)
137 self.assertEqual((SERVICE_STATUS.ON, None), expected_state)
138
139 @wait_for_reactor
140 @inlineCallbacks
141+ def test_get_expected_state_returns_off_for_no_config(self):
142+ service = self.make_proxy_service()
143+ os.environ['MAAS_PROXY_CONFIG_DIR'] = "/tmp/%s" % factory.make_name()
144+ expected_state = yield maybeDeferred(service.get_expected_state)
145+ self.assertEqual(
146+ (SERVICE_STATUS.OFF, "No configuration file present."),
147+ expected_state)
148+ del os.environ['MAAS_PROXY_CONFIG_DIR']
149+
150+ @wait_for_reactor
151+ @inlineCallbacks
152 def test_get_expected_state_returns_on_for_proxy_off_and_set(self):
153 service = self.make_proxy_service()
154 yield deferToDatabase(
155@@ -196,6 +214,7 @@
156 yield deferToDatabase(
157 transactional(Config.objects.set_config),
158 "http_proxy", factory.make_url())
159+ self.patch(proxyconfig, "is_config_present").return_value = True
160 expected_state = yield maybeDeferred(service.get_expected_state)
161 self.assertEqual((SERVICE_STATUS.ON, None), expected_state)
162
163@@ -209,6 +228,7 @@
164 yield deferToDatabase(
165 transactional(Config.objects.set_config),
166 "http_proxy", "")
167+ self.patch(proxyconfig, "is_config_present").return_value = True
168 expected_state = yield maybeDeferred(service.get_expected_state)
169 self.assertEqual((SERVICE_STATUS.ON, None), expected_state)
170
171@@ -222,6 +242,7 @@
172 yield deferToDatabase(
173 transactional(Config.objects.set_config),
174 "http_proxy", factory.make_url())
175+ self.patch(proxyconfig, "is_config_present").return_value = True
176 expected_state = yield maybeDeferred(service.get_expected_state)
177 self.assertEqual(
178 (SERVICE_STATUS.OFF,
179
180=== modified file 'src/provisioningserver/templates/proxy/maas-proxy.conf.template'
181--- src/provisioningserver/templates/proxy/maas-proxy.conf.template 2016-03-25 20:14:12 +0000
182+++ src/provisioningserver/templates/proxy/maas-proxy.conf.template 2016-04-15 20:49:22 +0000
183@@ -6,6 +6,8 @@
184 acl maas_proxy_manager proto cache_object
185 acl localhost src 127.0.0.1/32 ::1
186 acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1
187+# Make sure that localnet has at least one entry in it, to avoid errors.
188+acl localnet src 127.0.0.0/8
189 {{for cidr in cidrs}}
190 acl localnet src {{cidr}}
191 {{endfor}}