Merge lp:~stub/charms/trusty/postgresql/fix-bsb into lp:charms/trusty/postgresql

Proposed by Stuart Bishop
Status: Merged
Merged at revision: 141
Proposed branch: lp:~stub/charms/trusty/postgresql/fix-bsb
Merge into: lp:charms/trusty/postgresql
Diff against target: 113 lines (+49/-5)
2 files modified
hooks/storage.py (+26/-5)
tests/test_integration.py (+23/-0)
To merge this branch: bzr merge lp:~stub/charms/trusty/postgresql/fix-bsb
Reviewer Review Type Date Requested Status
Review Queue (community) automated testing Approve
Björn Tillenius (community) Approve
Chris Glass Pending
Review via email: mp+282422@code.launchpad.net

Description of the change

Fix the regressions in block storage broker external mounts.

Per Bug #1533502 and Bug #1533503, the request for disk was not being sent to the storage subordinate, and if it was, it would not do anything if an existing database was already in place. With this fixed, it again becomes possible to migrate PostgreSQL databases into fresh environments by remounting them on the new unit.

To post a comment you must log in.
142. By Stuart Bishop

Fix tests

Revision history for this message
Björn Tillenius (bjornt) wrote :

I can't give an official +1, but the code looks good to me, and I've confirmed that it works with one postgresql unit having persistent storage. I haven't gotten it to work when two postgresql units are deployed, but it doesn't look like the postgresql charm is the blame for that.

review: Approve
Revision history for this message
Review Queue (review-queue) wrote :

This item has failed automated testing! Results available here http://juju-ci.vapour.ws:8080/job/charm-bundle-test-lxc/2173/

review: Needs Fixing (automated testing)
Revision history for this message
Review Queue (review-queue) wrote :

This item has failed automated testing! Results available here http://juju-ci.vapour.ws:8080/job/charm-bundle-test-aws/2154/

review: Needs Fixing (automated testing)
Revision history for this message
Review Queue (review-queue) wrote :

This item has failed automated testing! Results available here http://juju-ci.vapour.ws:8080/job/charm-bundle-test-lxc/2188/

review: Needs Fixing (automated testing)
Revision history for this message
Review Queue (review-queue) wrote :

This item has failed automated testing! Results available here http://juju-ci.vapour.ws:8080/job/charm-bundle-test-aws/2169/

review: Needs Fixing (automated testing)
Revision history for this message
Review Queue (review-queue) wrote :

The results (PASS) are in and available here: http://juju-ci.vapour.ws:8080/job/charm-bundle-test-lxc/2204/

review: Approve (automated testing)
Revision history for this message
Review Queue (review-queue) wrote :

The results (PASS) are in and available here: http://juju-ci.vapour.ws:8080/job/charm-bundle-test-aws/2186/

review: Approve (automated testing)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hooks/storage.py'
2--- hooks/storage.py 2015-08-10 16:23:33 +0000
3+++ hooks/storage.py 2016-01-13 11:00:29 +0000
4@@ -19,7 +19,7 @@
5 import time
6
7 from charmhelpers import context
8-from charmhelpers.core import hookenv
9+from charmhelpers.core import hookenv, host
10 from charmhelpers.core.hookenv import DEBUG
11
12 from coordinator import coordinator
13@@ -42,7 +42,7 @@
14 raise SystemExit(0)
15 elif data_rels:
16 relid, rel = list(data_rels.items())[0]
17- rel['mountpoint'] = external_volume_mount
18+ rel.local['mountpoint'] = external_volume_mount
19
20 if needs_remount():
21 # Migrate any data when we can restart.
22@@ -55,12 +55,29 @@
23 return mounted and not linked
24
25
26+def fix_perms(data_dir):
27+ # The path to data_dir must be world readable, so the postgres user
28+ # can traverse to it.
29+ p = data_dir
30+ while p != '/':
31+ p = os.path.dirname(p)
32+ subprocess.check_call(['chmod', 'a+rX', p], universal_newlines=True)
33+
34+ # data_dir and all of its contents should be owned by the postgres
35+ # user and group.
36+ host.chownr(data_dir, 'postgres', 'postgres', follow_links=False)
37+
38+ # data_dir should not be world readable.
39+ os.chmod(data_dir, 0o700)
40+
41+
42 @data_ready_action
43 def remount():
44 if not needs_remount():
45 return
46
47 if postgresql.is_running():
48+ # Attempting this while PostgreSQL is live would be really, really bad.
49 postgresql.stop()
50
51 old_data_dir = postgresql.data_dir()
52@@ -69,9 +86,11 @@
53 backup_data_dir = '{}-{}'.format(old_data_dir, int(time.time()))
54
55 if not os.path.isdir(new_data_dir):
56+ # If there is not an existing db on the mount, migrate the current
57+ # db there.
58 hookenv.log('Migrating data from {} to {}'.format(old_data_dir,
59 new_data_dir))
60- helpers.makedirs(new_data_dir, mode=0o770,
61+ helpers.makedirs(new_data_dir, mode=0o700,
62 user='postgres', group='postgres')
63 try:
64 rsync_cmd = ['rsync', '-av',
65@@ -79,10 +98,12 @@
66 new_data_dir + '/']
67 hookenv.log('Running {}'.format(' '.join(rsync_cmd)), DEBUG)
68 subprocess.check_call(rsync_cmd)
69- os.replace(old_data_dir, backup_data_dir)
70- os.symlink(new_data_dir, old_data_dir)
71 except subprocess.CalledProcessError:
72 helpers.status_set('blocked',
73 'Failed to sync data from {} to {}'
74 ''.format(old_data_dir, new_data_dir))
75 raise SystemExit(0)
76+
77+ os.replace(old_data_dir, backup_data_dir)
78+ os.symlink(new_data_dir, old_data_dir)
79+ fix_perms(new_data_dir)
80
81=== modified file 'tests/test_integration.py'
82--- tests/test_integration.py 2015-12-19 08:25:47 +0000
83+++ tests/test_integration.py 2016-01-13 11:00:29 +0000
84@@ -538,6 +538,29 @@
85 test_config = dict(version=(None if SERIES == 'trusty' else '9.3'),
86 pgdg=(False if SERIES == 'trusty' else True))
87
88+ def test_mount(self):
89+ client_unit = self.deployment.sentry['postgresql'][0].info['unit_name']
90+ details = subprocess.check_output(['juju', 'run',
91+ '--unit', client_unit,
92+ 'stat --format "%A %U %G %N" '
93+ '/var/lib/postgresql/9.3/main'],
94+ stderr=subprocess.DEVNULL,
95+ universal_newlines=True).strip()
96+ self.assertEqual(details,
97+ "lrwxrwxrwx root root "
98+ "'/var/lib/postgresql/9.3/main' -> "
99+ "'/srv/data/postgresql/9.3/main'")
100+
101+ details = subprocess.check_output(['juju', 'run',
102+ '--unit', client_unit,
103+ 'stat --format "%A %U %G %N" '
104+ '/srv/data/postgresql/9.3/main'],
105+ stderr=subprocess.DEVNULL,
106+ universal_newlines=True).strip()
107+ self.assertEqual(details,
108+ "drwx------ postgres postgres "
109+ "'/srv/data/postgresql/9.3/main'")
110+
111
112 class PG94Tests(PGBaseTestCase, unittest.TestCase):
113 test_config = dict(version=(None if SERIES == 'wily' else '9.4'),

Subscribers

People subscribed via source and target branches

to all changes: