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

Proposed by LaMont Jones on 2016-04-15
Status: Merged
Approved by: LaMont Jones on 2016-04-15
Approved revision: 4925
Merged at revision: 4927
Proposed branch: lp:~lamont/maas/bug-1568847b
Merge into: lp: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 2016-04-15 Approve on 2016-04-15
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.
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
=== modified file 'src/maasserver/proxyconfig.py'
--- src/maasserver/proxyconfig.py 2016-03-31 23:43:06 +0000
+++ src/maasserver/proxyconfig.py 2016-04-15 20:49:22 +0000
@@ -5,6 +5,8 @@
55
6__all__ = [6__all__ = [
7 'proxy_update_config',7 'proxy_update_config',
8 'get_proxy_config_path',
9 'is_config_present',
8 ]10 ]
911
10import datetime12import datetime
@@ -39,14 +41,19 @@
39 """Raised if there is a problem with the proxy configuration."""41 """Raised if there is a problem with the proxy configuration."""
4042
4143
42def get_proxy_config_dir():44def get_proxy_config_path():
43 """Location of bind configuration files."""45 """Location of bind configuration files."""
44 setting = os.getenv("MAAS_PROXY_CONFIG_DIR", "/var/lib/maas")46 setting = os.getenv("MAAS_PROXY_CONFIG_DIR", "/var/lib/maas")
45 if isinstance(setting, bytes):47 if isinstance(setting, bytes):
46 fsenc = sys.getfilesystemencoding()48 fsenc = sys.getfilesystemencoding()
47 return setting.decode(fsenc)49 setting = setting.decode(fsenc)
48 else:50 setting = os.sep.join([setting, MAAS_PROXY_CONF_NAME])
49 return setting51 return setting
52
53
54def is_config_present():
55 """Check if there is a configuration file for the proxy."""
56 return os.access(get_proxy_config_path(), os.R_OK)
5057
5158
52@asynchronous59@asynchronous
@@ -72,8 +79,7 @@
72 raise ProxyConfigFail(*error.args)79 raise ProxyConfigFail(*error.args)
73 # Squid prefers ascii.80 # Squid prefers ascii.
74 content = content.encode("ascii")81 content = content.encode("ascii")
75 target_path = os.sep.join(82 target_path = get_proxy_config_path()
76 [get_proxy_config_dir(), MAAS_PROXY_CONF_NAME])
77 atomic_write(content, target_path, overwrite=True, mode=0o644)83 atomic_write(content, target_path, overwrite=True, mode=0o644)
7884
79 if is_proxy_enabled():85 if is_proxy_enabled():
8086
=== modified file 'src/maasserver/service_monitor.py'
--- src/maasserver/service_monitor.py 2016-03-31 22:30:51 +0000
+++ src/maasserver/service_monitor.py 2016-04-15 20:49:22 +0000
@@ -47,10 +47,14 @@
4747
48 @transactional48 @transactional
49 def db_get_expected_state():49 def db_get_expected_state():
50 # Avoid recursive import.
51 from maasserver import proxyconfig
50 if (Config.objects.get_config("enable_http_proxy") and52 if (Config.objects.get_config("enable_http_proxy") and
51 Config.objects.get_config("http_proxy")):53 Config.objects.get_config("http_proxy")):
52 return (SERVICE_STATUS.OFF,54 return (SERVICE_STATUS.OFF,
53 "disabled, alternate proxy is configured in settings.")55 "disabled, alternate proxy is configured in settings.")
56 elif proxyconfig.is_config_present() is False:
57 return (SERVICE_STATUS.OFF, "No configuration file present.")
54 else:58 else:
55 return (SERVICE_STATUS.ON, None)59 return (SERVICE_STATUS.ON, None)
5660
5761
=== modified file 'src/maasserver/tests/test_proxyconfig.py'
--- src/maasserver/tests/test_proxyconfig.py 2016-03-31 20:41:32 +0000
+++ src/maasserver/tests/test_proxyconfig.py 2016-04-15 20:49:22 +0000
@@ -34,17 +34,20 @@
3434
3535
36class TestGetConfigDir(MAASServerTestCase):36class TestGetConfigDir(MAASServerTestCase):
37 """Tests for `maasserver.proxyconfig.get_proxy_config_dir`."""37 """Tests for `maasserver.proxyconfig.get_proxy_config_path`."""
3838
39 def test_returns_default(self):39 def test_returns_default(self):
40 self.assertEquals(40 self.assertEquals(
41 "/var/lib/maas", proxyconfig.get_proxy_config_dir())41 "/var/lib/maas/maas-proxy.conf",
42 proxyconfig.get_proxy_config_path())
4243
43 def test_env_overrides_default(self):44 def test_env_overrides_default(self):
44 os.environ['MAAS_PROXY_CONFIG_DIR'] = factory.make_name('env')45 os.environ['MAAS_PROXY_CONFIG_DIR'] = factory.make_name('env')
45 self.assertEquals(46 self.assertEquals(
46 os.environ['MAAS_PROXY_CONFIG_DIR'],47 os.sep.join([
47 proxyconfig.get_proxy_config_dir())48 os.environ['MAAS_PROXY_CONFIG_DIR'],
49 proxyconfig.MAAS_PROXY_CONF_NAME]),
50 proxyconfig.get_proxy_config_path())
48 del(os.environ['MAAS_PROXY_CONFIG_DIR'])51 del(os.environ['MAAS_PROXY_CONFIG_DIR'])
4952
5053
5154
=== modified file 'src/maasserver/tests/test_service_monitor.py'
--- src/maasserver/tests/test_service_monitor.py 2016-04-01 18:19:53 +0000
+++ src/maasserver/tests/test_service_monitor.py 2016-04-15 20:49:22 +0000
@@ -5,10 +5,14 @@
55
6__all__ = []6__all__ = []
77
8import os
8import random9import random
910
10from crochet import wait_for11from crochet import wait_for
11from maasserver import service_monitor as service_monitor_module12from maasserver import (
13 proxyconfig,
14 service_monitor as service_monitor_module,
15)
12from maasserver.enum import SERVICE_STATUS16from maasserver.enum import SERVICE_STATUS
13from maasserver.models.config import Config17from maasserver.models.config import Config
14from maasserver.models.service import Service18from maasserver.models.service import Service
@@ -118,6 +122,7 @@
118 # Pretend we're in a production environment.122 # Pretend we're in a production environment.
119 self.patch(123 self.patch(
120 service_monitor_module, "is_dev_environment").return_value = False124 service_monitor_module, "is_dev_environment").return_value = False
125 self.patch(proxyconfig, "is_config_present").return_value = True
121126
122 service = self.pick_service()127 service = self.pick_service()
123 state = ServiceState(SERVICE_STATE.ON, "running")128 state = ServiceState(SERVICE_STATE.ON, "running")
@@ -150,6 +155,7 @@
150 @wait_for_reactor155 @wait_for_reactor
151 @inlineCallbacks156 @inlineCallbacks
152 def test__buildServices_builds_services_list(self):157 def test__buildServices_builds_services_list(self):
158 self.patch(proxyconfig, "is_config_present").return_value = True
153 monitor_service = ServiceMonitorService(159 monitor_service = ServiceMonitorService(
154 sentinel.advertisingService, Clock())160 sentinel.advertisingService, Clock())
155 service = self.pick_service()161 service = self.pick_service()
@@ -183,11 +189,23 @@
183 yield deferToDatabase(189 yield deferToDatabase(
184 transactional(Config.objects.set_config),190 transactional(Config.objects.set_config),
185 "http_proxy", "")191 "http_proxy", "")
192 self.patch(proxyconfig, "is_config_present").return_value = True
186 expected_state = yield maybeDeferred(service.get_expected_state)193 expected_state = yield maybeDeferred(service.get_expected_state)
187 self.assertEqual((SERVICE_STATUS.ON, None), expected_state)194 self.assertEqual((SERVICE_STATUS.ON, None), expected_state)
188195
189 @wait_for_reactor196 @wait_for_reactor
190 @inlineCallbacks197 @inlineCallbacks
198 def test_get_expected_state_returns_off_for_no_config(self):
199 service = self.make_proxy_service()
200 os.environ['MAAS_PROXY_CONFIG_DIR'] = "/tmp/%s" % factory.make_name()
201 expected_state = yield maybeDeferred(service.get_expected_state)
202 self.assertEqual(
203 (SERVICE_STATUS.OFF, "No configuration file present."),
204 expected_state)
205 del os.environ['MAAS_PROXY_CONFIG_DIR']
206
207 @wait_for_reactor
208 @inlineCallbacks
191 def test_get_expected_state_returns_on_for_proxy_off_and_set(self):209 def test_get_expected_state_returns_on_for_proxy_off_and_set(self):
192 service = self.make_proxy_service()210 service = self.make_proxy_service()
193 yield deferToDatabase(211 yield deferToDatabase(
@@ -196,6 +214,7 @@
196 yield deferToDatabase(214 yield deferToDatabase(
197 transactional(Config.objects.set_config),215 transactional(Config.objects.set_config),
198 "http_proxy", factory.make_url())216 "http_proxy", factory.make_url())
217 self.patch(proxyconfig, "is_config_present").return_value = True
199 expected_state = yield maybeDeferred(service.get_expected_state)218 expected_state = yield maybeDeferred(service.get_expected_state)
200 self.assertEqual((SERVICE_STATUS.ON, None), expected_state)219 self.assertEqual((SERVICE_STATUS.ON, None), expected_state)
201220
@@ -209,6 +228,7 @@
209 yield deferToDatabase(228 yield deferToDatabase(
210 transactional(Config.objects.set_config),229 transactional(Config.objects.set_config),
211 "http_proxy", "")230 "http_proxy", "")
231 self.patch(proxyconfig, "is_config_present").return_value = True
212 expected_state = yield maybeDeferred(service.get_expected_state)232 expected_state = yield maybeDeferred(service.get_expected_state)
213 self.assertEqual((SERVICE_STATUS.ON, None), expected_state)233 self.assertEqual((SERVICE_STATUS.ON, None), expected_state)
214234
@@ -222,6 +242,7 @@
222 yield deferToDatabase(242 yield deferToDatabase(
223 transactional(Config.objects.set_config),243 transactional(Config.objects.set_config),
224 "http_proxy", factory.make_url())244 "http_proxy", factory.make_url())
245 self.patch(proxyconfig, "is_config_present").return_value = True
225 expected_state = yield maybeDeferred(service.get_expected_state)246 expected_state = yield maybeDeferred(service.get_expected_state)
226 self.assertEqual(247 self.assertEqual(
227 (SERVICE_STATUS.OFF,248 (SERVICE_STATUS.OFF,
228249
=== modified file 'src/provisioningserver/templates/proxy/maas-proxy.conf.template'
--- src/provisioningserver/templates/proxy/maas-proxy.conf.template 2016-03-25 20:14:12 +0000
+++ src/provisioningserver/templates/proxy/maas-proxy.conf.template 2016-04-15 20:49:22 +0000
@@ -6,6 +6,8 @@
6acl maas_proxy_manager proto cache_object6acl maas_proxy_manager proto cache_object
7acl localhost src 127.0.0.1/32 ::17acl localhost src 127.0.0.1/32 ::1
8acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::18acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1
9# Make sure that localnet has at least one entry in it, to avoid errors.
10acl localnet src 127.0.0.0/8
9{{for cidr in cidrs}}11{{for cidr in cidrs}}
10acl localnet src {{cidr}}12acl localnet src {{cidr}}
11{{endfor}}13{{endfor}}