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
1diff --git a/reactive/content_cache.py b/reactive/content_cache.py
2index 5e2dcb7..ebbb3ab 100644
3--- a/reactive/content_cache.py
4+++ b/reactive/content_cache.py
5@@ -119,6 +119,25 @@ def service_start_or_reload():
6 reactive.clear_flag('content_cache.{}.reload-required'.format(name))
7
8
9+@reactive.when(
10+ 'content_cache.haproxy.configured',
11+ 'content_cache.nginx.configured',
12+ 'content_cache.sysctl.configured',
13+ 'content_cache.save-config',
14+)
15+def save_config(conf_path='/etc/content-cache', etckeeper_path='/etc/etckeeper', dot_etckeeper_path='/etc/.etckeeper'):
16+ config = hookenv.config()
17+
18+ if not os.path.exists(conf_path):
19+ os.mkdir(conf_path)
20+ write_file(config.get('sites'), os.path.join(conf_path, 'sites.yaml'))
21+
22+ if os.path.exists(etckeeper_path) or os.path.exists(dot_etckeeper_path):
23+ subprocess.call(['etckeeper', 'commit', 'Saved updated content-cache charm configs'])
24+
25+ reactive.clear_flag('content_cache.save-config')
26+
27+
28 def configure_nginx_metrics(ngx_conf, enable_prometheus_metrics, listen_address):
29 """Configure nginx to expose metrics.
30
31@@ -236,6 +255,7 @@ def configure_nginx(conf_path=None): # NOQA: C901
32
33 if changed:
34 reactive.set_flag('content_cache.nginx.reload-required')
35+ reactive.set_flag('content_cache.save-config')
36
37 update_logrotate('nginx', retention=config.get('log_retention'))
38 reactive.set_flag('content_cache.nginx.configured')
39@@ -413,6 +433,7 @@ def configure_haproxy(): # NOQA: C901 LP#1825084
40 haproxy.save_server_state()
41 reactive.set_flag('content_cache.haproxy.reload-required')
42 reactive.clear_flag('content_cache.sysctl.configured')
43+ reactive.set_flag('content_cache.save-config')
44
45 # Save HAProxy calculated max. fds for use with Nginx
46 unitdata.kv().set('haproxy_max_conns', haproxy.global_max_connections)
47diff --git a/tests/unit/test_content_cache.py b/tests/unit/test_content_cache.py
48index fe1ae18..3a39adf 100644
49--- a/tests/unit/test_content_cache.py
50+++ b/tests/unit/test_content_cache.py
51@@ -1,4 +1,6 @@
52+import grp
53 import os
54+import pwd
55 import shutil
56 import sys
57 import tempfile
58@@ -199,6 +201,31 @@ class TestCharm(unittest.TestCase):
59 service_start.assert_not_called()
60 service_reload.assert_called_with('nginx')
61
62+ @mock.patch('charms.reactive.clear_flag')
63+ @mock.patch('charmhelpers.core.host.write_file')
64+ @mock.patch('subprocess.call')
65+ def test_save_config(self, call, write_file, clear_flag):
66+ '''Test saving config'''
67+ self.mock_config.return_value = {'sites': 'site1:'}
68+ conf_path = os.path.join(self.tmpdir, 'content-cache')
69+ etckeeper_path = os.path.join(self.tmpdir, 'etckeeper')
70+ dot_etckeeper_path = os.path.join(self.tmpdir, '.etckeeper')
71+ content_cache.save_config(conf_path, etckeeper_path, dot_etckeeper_path)
72+ self.assertTrue(os.path.exists(conf_path))
73+ clear_flag.assert_called_once_with('content_cache.save-config')
74+ write_file.assert_called_once_with(
75+ path=os.path.join(conf_path, 'sites.yaml'),
76+ content='site1:',
77+ owner=pwd.getpwuid(os.getuid()).pw_name,
78+ group=grp.getgrgid(os.getgid()).gr_name,
79+ perms=0o644,
80+ )
81+
82+ call.reset_mock()
83+ os.mkdir(etckeeper_path)
84+ content_cache.save_config(conf_path, etckeeper_path, dot_etckeeper_path)
85+ call.assert_called_once_with(['etckeeper', 'commit', 'Saved updated content-cache charm configs'])
86+
87 @mock.patch('charmhelpers.core.host.service_running')
88 @mock.patch('charmhelpers.core.host.service_reload')
89 @mock.patch('charmhelpers.core.host.service_start')

Subscribers

People subscribed via source and target branches