Merge ~hloeung/content-cache-charm:save-config into content-cache-charm:master

Proposed by Haw Loeung
Status: Merged
Approved by: 🤖 Canonical IS Review Bot
Approved revision: 03ebc45b509f558053251acc38814b5a37da712e
Merged at revision: 289f6fff6634b35408d34b6aa063c084eba3000a
Proposed branch: ~hloeung/content-cache-charm:save-config
Merge into: content-cache-charm:master
Diff against target: 89 lines (+48/-0)
2 files modified
reactive/content_cache.py (+21/-0)
tests/unit/test_content_cache.py (+27/-0)
Reviewer Review Type Date Requested Status
🤖 Canonical IS Review Bot Approve
Canonical IS Reviewers Pending
Review via email: mp+463340@code.launchpad.net

Commit message

Add saving sites config as well as HAProxy & Nginx on changes

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
🤖 Canonical IS Review Bot (canonical-is-reviewbot) wrote :

Proxy approval for alejdg

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 289f6fff6634b35408d34b6aa063c084eba3000a

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/reactive/content_cache.py b/reactive/content_cache.py
index 5e2dcb7..ebbb3ab 100644
--- a/reactive/content_cache.py
+++ b/reactive/content_cache.py
@@ -119,6 +119,25 @@ def service_start_or_reload():
119 reactive.clear_flag('content_cache.{}.reload-required'.format(name))119 reactive.clear_flag('content_cache.{}.reload-required'.format(name))
120120
121121
122@reactive.when(
123 'content_cache.haproxy.configured',
124 'content_cache.nginx.configured',
125 'content_cache.sysctl.configured',
126 'content_cache.save-config',
127)
128def save_config(conf_path='/etc/content-cache', etckeeper_path='/etc/etckeeper', dot_etckeeper_path='/etc/.etckeeper'):
129 config = hookenv.config()
130
131 if not os.path.exists(conf_path):
132 os.mkdir(conf_path)
133 write_file(config.get('sites'), os.path.join(conf_path, 'sites.yaml'))
134
135 if os.path.exists(etckeeper_path) or os.path.exists(dot_etckeeper_path):
136 subprocess.call(['etckeeper', 'commit', 'Saved updated content-cache charm configs'])
137
138 reactive.clear_flag('content_cache.save-config')
139
140
122def configure_nginx_metrics(ngx_conf, enable_prometheus_metrics, listen_address):141def configure_nginx_metrics(ngx_conf, enable_prometheus_metrics, listen_address):
123 """Configure nginx to expose metrics.142 """Configure nginx to expose metrics.
124143
@@ -236,6 +255,7 @@ def configure_nginx(conf_path=None): # NOQA: C901
236255
237 if changed:256 if changed:
238 reactive.set_flag('content_cache.nginx.reload-required')257 reactive.set_flag('content_cache.nginx.reload-required')
258 reactive.set_flag('content_cache.save-config')
239259
240 update_logrotate('nginx', retention=config.get('log_retention'))260 update_logrotate('nginx', retention=config.get('log_retention'))
241 reactive.set_flag('content_cache.nginx.configured')261 reactive.set_flag('content_cache.nginx.configured')
@@ -413,6 +433,7 @@ def configure_haproxy(): # NOQA: C901 LP#1825084
413 haproxy.save_server_state()433 haproxy.save_server_state()
414 reactive.set_flag('content_cache.haproxy.reload-required')434 reactive.set_flag('content_cache.haproxy.reload-required')
415 reactive.clear_flag('content_cache.sysctl.configured')435 reactive.clear_flag('content_cache.sysctl.configured')
436 reactive.set_flag('content_cache.save-config')
416437
417 # Save HAProxy calculated max. fds for use with Nginx438 # Save HAProxy calculated max. fds for use with Nginx
418 unitdata.kv().set('haproxy_max_conns', haproxy.global_max_connections)439 unitdata.kv().set('haproxy_max_conns', haproxy.global_max_connections)
diff --git a/tests/unit/test_content_cache.py b/tests/unit/test_content_cache.py
index fe1ae18..3a39adf 100644
--- a/tests/unit/test_content_cache.py
+++ b/tests/unit/test_content_cache.py
@@ -1,4 +1,6 @@
1import grp
1import os2import os
3import pwd
2import shutil4import shutil
3import sys5import sys
4import tempfile6import tempfile
@@ -199,6 +201,31 @@ class TestCharm(unittest.TestCase):
199 service_start.assert_not_called()201 service_start.assert_not_called()
200 service_reload.assert_called_with('nginx')202 service_reload.assert_called_with('nginx')
201203
204 @mock.patch('charms.reactive.clear_flag')
205 @mock.patch('charmhelpers.core.host.write_file')
206 @mock.patch('subprocess.call')
207 def test_save_config(self, call, write_file, clear_flag):
208 '''Test saving config'''
209 self.mock_config.return_value = {'sites': 'site1:'}
210 conf_path = os.path.join(self.tmpdir, 'content-cache')
211 etckeeper_path = os.path.join(self.tmpdir, 'etckeeper')
212 dot_etckeeper_path = os.path.join(self.tmpdir, '.etckeeper')
213 content_cache.save_config(conf_path, etckeeper_path, dot_etckeeper_path)
214 self.assertTrue(os.path.exists(conf_path))
215 clear_flag.assert_called_once_with('content_cache.save-config')
216 write_file.assert_called_once_with(
217 path=os.path.join(conf_path, 'sites.yaml'),
218 content='site1:',
219 owner=pwd.getpwuid(os.getuid()).pw_name,
220 group=grp.getgrgid(os.getgid()).gr_name,
221 perms=0o644,
222 )
223
224 call.reset_mock()
225 os.mkdir(etckeeper_path)
226 content_cache.save_config(conf_path, etckeeper_path, dot_etckeeper_path)
227 call.assert_called_once_with(['etckeeper', 'commit', 'Saved updated content-cache charm configs'])
228
202 @mock.patch('charmhelpers.core.host.service_running')229 @mock.patch('charmhelpers.core.host.service_running')
203 @mock.patch('charmhelpers.core.host.service_reload')230 @mock.patch('charmhelpers.core.host.service_reload')
204 @mock.patch('charmhelpers.core.host.service_start')231 @mock.patch('charmhelpers.core.host.service_start')

Subscribers

People subscribed via source and target branches