Merge lp:~hopem/charms/trusty/keystone/lp1499643 into lp:~openstack-charmers-archive/charms/trusty/keystone/next
- Trusty Tahr (14.04)
- lp1499643
- Merge into next
Proposed by
Edward Hope-Morley
Status: | Merged |
---|---|
Merged at revision: | 179 |
Proposed branch: | lp:~hopem/charms/trusty/keystone/lp1499643 |
Merge into: | lp:~openstack-charmers-archive/charms/trusty/keystone/next |
Diff against target: |
2301 lines (+1618/-93) 16 files modified
charmhelpers/contrib/network/ip.py (+5/-3) charmhelpers/contrib/openstack/amulet/deployment.py (+23/-9) charmhelpers/contrib/openstack/amulet/utils.py (+359/-0) charmhelpers/contrib/openstack/context.py (+60/-16) charmhelpers/contrib/openstack/templating.py (+30/-2) charmhelpers/contrib/openstack/utils.py (+232/-2) charmhelpers/contrib/storage/linux/ceph.py (+226/-13) charmhelpers/core/hookenv.py (+32/-0) charmhelpers/core/host.py (+32/-16) charmhelpers/core/hugepage.py (+8/-1) charmhelpers/core/strutils.py (+30/-0) tests/charmhelpers/contrib/amulet/deployment.py (+4/-2) tests/charmhelpers/contrib/amulet/utils.py (+193/-19) tests/charmhelpers/contrib/openstack/amulet/deployment.py (+23/-9) tests/charmhelpers/contrib/openstack/amulet/utils.py (+359/-0) unit_tests/test_keystone_hooks.py (+2/-1) |
To merge this branch: | bzr merge lp:~hopem/charms/trusty/keystone/lp1499643 |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Liam Young (community) | Approve | ||
Review via email: mp+272411@code.launchpad.net |
Commit message
Description of the change
To post a comment you must log in.
Revision history for this message
uosci-testing-bot (uosci-testing-bot) wrote : | # |
Revision history for this message
uosci-testing-bot (uosci-testing-bot) wrote : | # |
charm_unit_test #9944 keystone-next for hopem mp272411
UNIT OK: passed
Revision history for this message
uosci-testing-bot (uosci-testing-bot) wrote : | # |
charm_amulet_test #6772 keystone-next for hopem mp272411
AMULET FAIL: amulet-test failed
AMULET Results (max last 2 lines):
make: *** [functional_test] Error 1
ERROR:root:Make target returned non-zero.
Full amulet test output: http://
Build: http://
Revision history for this message
Liam Young (gnuoy) wrote : | # |
Amulet fail is unrelated. Approved
review:
Approve
Revision history for this message
Ryan Beisner (1chb1n) wrote : | # |
(!) All keystone/next amulet tests with, and after, this commit, are failing in the same place as the test here.
Last known good was keystone/test rev178
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'charmhelpers/contrib/network/ip.py' | |||
2 | --- charmhelpers/contrib/network/ip.py 2015-09-03 09:40:28 +0000 | |||
3 | +++ charmhelpers/contrib/network/ip.py 2015-09-25 14:42:33 +0000 | |||
4 | @@ -23,7 +23,7 @@ | |||
5 | 23 | from functools import partial | 23 | from functools import partial |
6 | 24 | 24 | ||
7 | 25 | from charmhelpers.core.hookenv import unit_get | 25 | from charmhelpers.core.hookenv import unit_get |
9 | 26 | from charmhelpers.fetch import apt_install | 26 | from charmhelpers.fetch import apt_install, apt_update |
10 | 27 | from charmhelpers.core.hookenv import ( | 27 | from charmhelpers.core.hookenv import ( |
11 | 28 | log, | 28 | log, |
12 | 29 | WARNING, | 29 | WARNING, |
13 | @@ -32,13 +32,15 @@ | |||
14 | 32 | try: | 32 | try: |
15 | 33 | import netifaces | 33 | import netifaces |
16 | 34 | except ImportError: | 34 | except ImportError: |
18 | 35 | apt_install('python-netifaces') | 35 | apt_update(fatal=True) |
19 | 36 | apt_install('python-netifaces', fatal=True) | ||
20 | 36 | import netifaces | 37 | import netifaces |
21 | 37 | 38 | ||
22 | 38 | try: | 39 | try: |
23 | 39 | import netaddr | 40 | import netaddr |
24 | 40 | except ImportError: | 41 | except ImportError: |
26 | 41 | apt_install('python-netaddr') | 42 | apt_update(fatal=True) |
27 | 43 | apt_install('python-netaddr', fatal=True) | ||
28 | 42 | import netaddr | 44 | import netaddr |
29 | 43 | 45 | ||
30 | 44 | 46 | ||
31 | 45 | 47 | ||
32 | === modified file 'charmhelpers/contrib/openstack/amulet/deployment.py' | |||
33 | --- charmhelpers/contrib/openstack/amulet/deployment.py 2015-08-19 13:53:50 +0000 | |||
34 | +++ charmhelpers/contrib/openstack/amulet/deployment.py 2015-09-25 14:42:33 +0000 | |||
35 | @@ -44,20 +44,31 @@ | |||
36 | 44 | Determine if the local branch being tested is derived from its | 44 | Determine if the local branch being tested is derived from its |
37 | 45 | stable or next (dev) branch, and based on this, use the corresonding | 45 | stable or next (dev) branch, and based on this, use the corresonding |
38 | 46 | stable or next branches for the other_services.""" | 46 | stable or next branches for the other_services.""" |
39 | 47 | |||
40 | 48 | # Charms outside the lp:~openstack-charmers namespace | ||
41 | 47 | base_charms = ['mysql', 'mongodb', 'nrpe'] | 49 | base_charms = ['mysql', 'mongodb', 'nrpe'] |
42 | 48 | 50 | ||
43 | 51 | # Force these charms to current series even when using an older series. | ||
44 | 52 | # ie. Use trusty/nrpe even when series is precise, as the P charm | ||
45 | 53 | # does not possess the necessary external master config and hooks. | ||
46 | 54 | force_series_current = ['nrpe'] | ||
47 | 55 | |||
48 | 49 | if self.series in ['precise', 'trusty']: | 56 | if self.series in ['precise', 'trusty']: |
49 | 50 | base_series = self.series | 57 | base_series = self.series |
50 | 51 | else: | 58 | else: |
51 | 52 | base_series = self.current_next | 59 | base_series = self.current_next |
52 | 53 | 60 | ||
55 | 54 | if self.stable: | 61 | for svc in other_services: |
56 | 55 | for svc in other_services: | 62 | if svc['name'] in force_series_current: |
57 | 63 | base_series = self.current_next | ||
58 | 64 | # If a location has been explicitly set, use it | ||
59 | 65 | if svc.get('location'): | ||
60 | 66 | continue | ||
61 | 67 | if self.stable: | ||
62 | 56 | temp = 'lp:charms/{}/{}' | 68 | temp = 'lp:charms/{}/{}' |
63 | 57 | svc['location'] = temp.format(base_series, | 69 | svc['location'] = temp.format(base_series, |
64 | 58 | svc['name']) | 70 | svc['name']) |
67 | 59 | else: | 71 | else: |
66 | 60 | for svc in other_services: | ||
68 | 61 | if svc['name'] in base_charms: | 72 | if svc['name'] in base_charms: |
69 | 62 | temp = 'lp:charms/{}/{}' | 73 | temp = 'lp:charms/{}/{}' |
70 | 63 | svc['location'] = temp.format(base_series, | 74 | svc['location'] = temp.format(base_series, |
71 | @@ -66,6 +77,7 @@ | |||
72 | 66 | temp = 'lp:~openstack-charmers/charms/{}/{}/next' | 77 | temp = 'lp:~openstack-charmers/charms/{}/{}/next' |
73 | 67 | svc['location'] = temp.format(self.current_next, | 78 | svc['location'] = temp.format(self.current_next, |
74 | 68 | svc['name']) | 79 | svc['name']) |
75 | 80 | |||
76 | 69 | return other_services | 81 | return other_services |
77 | 70 | 82 | ||
78 | 71 | def _add_services(self, this_service, other_services): | 83 | def _add_services(self, this_service, other_services): |
79 | @@ -77,21 +89,23 @@ | |||
80 | 77 | 89 | ||
81 | 78 | services = other_services | 90 | services = other_services |
82 | 79 | services.append(this_service) | 91 | services.append(this_service) |
83 | 92 | |||
84 | 93 | # Charms which should use the source config option | ||
85 | 80 | use_source = ['mysql', 'mongodb', 'rabbitmq-server', 'ceph', | 94 | use_source = ['mysql', 'mongodb', 'rabbitmq-server', 'ceph', |
86 | 81 | 'ceph-osd', 'ceph-radosgw'] | 95 | 'ceph-osd', 'ceph-radosgw'] |
90 | 82 | # Most OpenStack subordinate charms do not expose an origin option | 96 | |
91 | 83 | # as that is controlled by the principle. | 97 | # Charms which can not use openstack-origin, ie. many subordinates |
92 | 84 | ignore = ['cinder-ceph', 'hacluster', 'neutron-openvswitch', 'nrpe'] | 98 | no_origin = ['cinder-ceph', 'hacluster', 'neutron-openvswitch', 'nrpe'] |
93 | 85 | 99 | ||
94 | 86 | if self.openstack: | 100 | if self.openstack: |
95 | 87 | for svc in services: | 101 | for svc in services: |
97 | 88 | if svc['name'] not in use_source + ignore: | 102 | if svc['name'] not in use_source + no_origin: |
98 | 89 | config = {'openstack-origin': self.openstack} | 103 | config = {'openstack-origin': self.openstack} |
99 | 90 | self.d.configure(svc['name'], config) | 104 | self.d.configure(svc['name'], config) |
100 | 91 | 105 | ||
101 | 92 | if self.source: | 106 | if self.source: |
102 | 93 | for svc in services: | 107 | for svc in services: |
104 | 94 | if svc['name'] in use_source and svc['name'] not in ignore: | 108 | if svc['name'] in use_source and svc['name'] not in no_origin: |
105 | 95 | config = {'source': self.source} | 109 | config = {'source': self.source} |
106 | 96 | self.d.configure(svc['name'], config) | 110 | self.d.configure(svc['name'], config) |
107 | 97 | 111 | ||
108 | 98 | 112 | ||
109 | === modified file 'charmhelpers/contrib/openstack/amulet/utils.py' | |||
110 | --- charmhelpers/contrib/openstack/amulet/utils.py 2015-07-16 20:17:38 +0000 | |||
111 | +++ charmhelpers/contrib/openstack/amulet/utils.py 2015-09-25 14:42:33 +0000 | |||
112 | @@ -27,6 +27,7 @@ | |||
113 | 27 | import heatclient.v1.client as heat_client | 27 | import heatclient.v1.client as heat_client |
114 | 28 | import keystoneclient.v2_0 as keystone_client | 28 | import keystoneclient.v2_0 as keystone_client |
115 | 29 | import novaclient.v1_1.client as nova_client | 29 | import novaclient.v1_1.client as nova_client |
116 | 30 | import pika | ||
117 | 30 | import swiftclient | 31 | import swiftclient |
118 | 31 | 32 | ||
119 | 32 | from charmhelpers.contrib.amulet.utils import ( | 33 | from charmhelpers.contrib.amulet.utils import ( |
120 | @@ -602,3 +603,361 @@ | |||
121 | 602 | self.log.debug('Ceph {} samples (OK): ' | 603 | self.log.debug('Ceph {} samples (OK): ' |
122 | 603 | '{}'.format(sample_type, samples)) | 604 | '{}'.format(sample_type, samples)) |
123 | 604 | return None | 605 | return None |
124 | 606 | |||
125 | 607 | # rabbitmq/amqp specific helpers: | ||
126 | 608 | def add_rmq_test_user(self, sentry_units, | ||
127 | 609 | username="testuser1", password="changeme"): | ||
128 | 610 | """Add a test user via the first rmq juju unit, check connection as | ||
129 | 611 | the new user against all sentry units. | ||
130 | 612 | |||
131 | 613 | :param sentry_units: list of sentry unit pointers | ||
132 | 614 | :param username: amqp user name, default to testuser1 | ||
133 | 615 | :param password: amqp user password | ||
134 | 616 | :returns: None if successful. Raise on error. | ||
135 | 617 | """ | ||
136 | 618 | self.log.debug('Adding rmq user ({})...'.format(username)) | ||
137 | 619 | |||
138 | 620 | # Check that user does not already exist | ||
139 | 621 | cmd_user_list = 'rabbitmqctl list_users' | ||
140 | 622 | output, _ = self.run_cmd_unit(sentry_units[0], cmd_user_list) | ||
141 | 623 | if username in output: | ||
142 | 624 | self.log.warning('User ({}) already exists, returning ' | ||
143 | 625 | 'gracefully.'.format(username)) | ||
144 | 626 | return | ||
145 | 627 | |||
146 | 628 | perms = '".*" ".*" ".*"' | ||
147 | 629 | cmds = ['rabbitmqctl add_user {} {}'.format(username, password), | ||
148 | 630 | 'rabbitmqctl set_permissions {} {}'.format(username, perms)] | ||
149 | 631 | |||
150 | 632 | # Add user via first unit | ||
151 | 633 | for cmd in cmds: | ||
152 | 634 | output, _ = self.run_cmd_unit(sentry_units[0], cmd) | ||
153 | 635 | |||
154 | 636 | # Check connection against the other sentry_units | ||
155 | 637 | self.log.debug('Checking user connect against units...') | ||
156 | 638 | for sentry_unit in sentry_units: | ||
157 | 639 | connection = self.connect_amqp_by_unit(sentry_unit, ssl=False, | ||
158 | 640 | username=username, | ||
159 | 641 | password=password) | ||
160 | 642 | connection.close() | ||
161 | 643 | |||
162 | 644 | def delete_rmq_test_user(self, sentry_units, username="testuser1"): | ||
163 | 645 | """Delete a rabbitmq user via the first rmq juju unit. | ||
164 | 646 | |||
165 | 647 | :param sentry_units: list of sentry unit pointers | ||
166 | 648 | :param username: amqp user name, default to testuser1 | ||
167 | 649 | :param password: amqp user password | ||
168 | 650 | :returns: None if successful or no such user. | ||
169 | 651 | """ | ||
170 | 652 | self.log.debug('Deleting rmq user ({})...'.format(username)) | ||
171 | 653 | |||
172 | 654 | # Check that the user exists | ||
173 | 655 | cmd_user_list = 'rabbitmqctl list_users' | ||
174 | 656 | output, _ = self.run_cmd_unit(sentry_units[0], cmd_user_list) | ||
175 | 657 | |||
176 | 658 | if username not in output: | ||
177 | 659 | self.log.warning('User ({}) does not exist, returning ' | ||
178 | 660 | 'gracefully.'.format(username)) | ||
179 | 661 | return | ||
180 | 662 | |||
181 | 663 | # Delete the user | ||
182 | 664 | cmd_user_del = 'rabbitmqctl delete_user {}'.format(username) | ||
183 | 665 | output, _ = self.run_cmd_unit(sentry_units[0], cmd_user_del) | ||
184 | 666 | |||
185 | 667 | def get_rmq_cluster_status(self, sentry_unit): | ||
186 | 668 | """Execute rabbitmq cluster status command on a unit and return | ||
187 | 669 | the full output. | ||
188 | 670 | |||
189 | 671 | :param unit: sentry unit | ||
190 | 672 | :returns: String containing console output of cluster status command | ||
191 | 673 | """ | ||
192 | 674 | cmd = 'rabbitmqctl cluster_status' | ||
193 | 675 | output, _ = self.run_cmd_unit(sentry_unit, cmd) | ||
194 | 676 | self.log.debug('{} cluster_status:\n{}'.format( | ||
195 | 677 | sentry_unit.info['unit_name'], output)) | ||
196 | 678 | return str(output) | ||
197 | 679 | |||
198 | 680 | def get_rmq_cluster_running_nodes(self, sentry_unit): | ||
199 | 681 | """Parse rabbitmqctl cluster_status output string, return list of | ||
200 | 682 | running rabbitmq cluster nodes. | ||
201 | 683 | |||
202 | 684 | :param unit: sentry unit | ||
203 | 685 | :returns: List containing node names of running nodes | ||
204 | 686 | """ | ||
205 | 687 | # NOTE(beisner): rabbitmqctl cluster_status output is not | ||
206 | 688 | # json-parsable, do string chop foo, then json.loads that. | ||
207 | 689 | str_stat = self.get_rmq_cluster_status(sentry_unit) | ||
208 | 690 | if 'running_nodes' in str_stat: | ||
209 | 691 | pos_start = str_stat.find("{running_nodes,") + 15 | ||
210 | 692 | pos_end = str_stat.find("]},", pos_start) + 1 | ||
211 | 693 | str_run_nodes = str_stat[pos_start:pos_end].replace("'", '"') | ||
212 | 694 | run_nodes = json.loads(str_run_nodes) | ||
213 | 695 | return run_nodes | ||
214 | 696 | else: | ||
215 | 697 | return [] | ||
216 | 698 | |||
217 | 699 | def validate_rmq_cluster_running_nodes(self, sentry_units): | ||
218 | 700 | """Check that all rmq unit hostnames are represented in the | ||
219 | 701 | cluster_status output of all units. | ||
220 | 702 | |||
221 | 703 | :param host_names: dict of juju unit names to host names | ||
222 | 704 | :param units: list of sentry unit pointers (all rmq units) | ||
223 | 705 | :returns: None if successful, otherwise return error message | ||
224 | 706 | """ | ||
225 | 707 | host_names = self.get_unit_hostnames(sentry_units) | ||
226 | 708 | errors = [] | ||
227 | 709 | |||
228 | 710 | # Query every unit for cluster_status running nodes | ||
229 | 711 | for query_unit in sentry_units: | ||
230 | 712 | query_unit_name = query_unit.info['unit_name'] | ||
231 | 713 | running_nodes = self.get_rmq_cluster_running_nodes(query_unit) | ||
232 | 714 | |||
233 | 715 | # Confirm that every unit is represented in the queried unit's | ||
234 | 716 | # cluster_status running nodes output. | ||
235 | 717 | for validate_unit in sentry_units: | ||
236 | 718 | val_host_name = host_names[validate_unit.info['unit_name']] | ||
237 | 719 | val_node_name = 'rabbit@{}'.format(val_host_name) | ||
238 | 720 | |||
239 | 721 | if val_node_name not in running_nodes: | ||
240 | 722 | errors.append('Cluster member check failed on {}: {} not ' | ||
241 | 723 | 'in {}\n'.format(query_unit_name, | ||
242 | 724 | val_node_name, | ||
243 | 725 | running_nodes)) | ||
244 | 726 | if errors: | ||
245 | 727 | return ''.join(errors) | ||
246 | 728 | |||
247 | 729 | def rmq_ssl_is_enabled_on_unit(self, sentry_unit, port=None): | ||
248 | 730 | """Check a single juju rmq unit for ssl and port in the config file.""" | ||
249 | 731 | host = sentry_unit.info['public-address'] | ||
250 | 732 | unit_name = sentry_unit.info['unit_name'] | ||
251 | 733 | |||
252 | 734 | conf_file = '/etc/rabbitmq/rabbitmq.config' | ||
253 | 735 | conf_contents = str(self.file_contents_safe(sentry_unit, | ||
254 | 736 | conf_file, max_wait=16)) | ||
255 | 737 | # Checks | ||
256 | 738 | conf_ssl = 'ssl' in conf_contents | ||
257 | 739 | conf_port = str(port) in conf_contents | ||
258 | 740 | |||
259 | 741 | # Port explicitly checked in config | ||
260 | 742 | if port and conf_port and conf_ssl: | ||
261 | 743 | self.log.debug('SSL is enabled @{}:{} ' | ||
262 | 744 | '({})'.format(host, port, unit_name)) | ||
263 | 745 | return True | ||
264 | 746 | elif port and not conf_port and conf_ssl: | ||
265 | 747 | self.log.debug('SSL is enabled @{} but not on port {} ' | ||
266 | 748 | '({})'.format(host, port, unit_name)) | ||
267 | 749 | return False | ||
268 | 750 | # Port not checked (useful when checking that ssl is disabled) | ||
269 | 751 | elif not port and conf_ssl: | ||
270 | 752 | self.log.debug('SSL is enabled @{}:{} ' | ||
271 | 753 | '({})'.format(host, port, unit_name)) | ||
272 | 754 | return True | ||
273 | 755 | elif not port and not conf_ssl: | ||
274 | 756 | self.log.debug('SSL not enabled @{}:{} ' | ||
275 | 757 | '({})'.format(host, port, unit_name)) | ||
276 | 758 | return False | ||
277 | 759 | else: | ||
278 | 760 | msg = ('Unknown condition when checking SSL status @{}:{} ' | ||
279 | 761 | '({})'.format(host, port, unit_name)) | ||
280 | 762 | amulet.raise_status(amulet.FAIL, msg) | ||
281 | 763 | |||
282 | 764 | def validate_rmq_ssl_enabled_units(self, sentry_units, port=None): | ||
283 | 765 | """Check that ssl is enabled on rmq juju sentry units. | ||
284 | 766 | |||
285 | 767 | :param sentry_units: list of all rmq sentry units | ||
286 | 768 | :param port: optional ssl port override to validate | ||
287 | 769 | :returns: None if successful, otherwise return error message | ||
288 | 770 | """ | ||
289 | 771 | for sentry_unit in sentry_units: | ||
290 | 772 | if not self.rmq_ssl_is_enabled_on_unit(sentry_unit, port=port): | ||
291 | 773 | return ('Unexpected condition: ssl is disabled on unit ' | ||
292 | 774 | '({})'.format(sentry_unit.info['unit_name'])) | ||
293 | 775 | return None | ||
294 | 776 | |||
295 | 777 | def validate_rmq_ssl_disabled_units(self, sentry_units): | ||
296 | 778 | """Check that ssl is enabled on listed rmq juju sentry units. | ||
297 | 779 | |||
298 | 780 | :param sentry_units: list of all rmq sentry units | ||
299 | 781 | :returns: True if successful. Raise on error. | ||
300 | 782 | """ | ||
301 | 783 | for sentry_unit in sentry_units: | ||
302 | 784 | if self.rmq_ssl_is_enabled_on_unit(sentry_unit): | ||
303 | 785 | return ('Unexpected condition: ssl is enabled on unit ' | ||
304 | 786 | '({})'.format(sentry_unit.info['unit_name'])) | ||
305 | 787 | return None | ||
306 | 788 | |||
307 | 789 | def configure_rmq_ssl_on(self, sentry_units, deployment, | ||
308 | 790 | port=None, max_wait=60): | ||
309 | 791 | """Turn ssl charm config option on, with optional non-default | ||
310 | 792 | ssl port specification. Confirm that it is enabled on every | ||
311 | 793 | unit. | ||
312 | 794 | |||
313 | 795 | :param sentry_units: list of sentry units | ||
314 | 796 | :param deployment: amulet deployment object pointer | ||
315 | 797 | :param port: amqp port, use defaults if None | ||
316 | 798 | :param max_wait: maximum time to wait in seconds to confirm | ||
317 | 799 | :returns: None if successful. Raise on error. | ||
318 | 800 | """ | ||
319 | 801 | self.log.debug('Setting ssl charm config option: on') | ||
320 | 802 | |||
321 | 803 | # Enable RMQ SSL | ||
322 | 804 | config = {'ssl': 'on'} | ||
323 | 805 | if port: | ||
324 | 806 | config['ssl_port'] = port | ||
325 | 807 | |||
326 | 808 | deployment.configure('rabbitmq-server', config) | ||
327 | 809 | |||
328 | 810 | # Confirm | ||
329 | 811 | tries = 0 | ||
330 | 812 | ret = self.validate_rmq_ssl_enabled_units(sentry_units, port=port) | ||
331 | 813 | while ret and tries < (max_wait / 4): | ||
332 | 814 | time.sleep(4) | ||
333 | 815 | self.log.debug('Attempt {}: {}'.format(tries, ret)) | ||
334 | 816 | ret = self.validate_rmq_ssl_enabled_units(sentry_units, port=port) | ||
335 | 817 | tries += 1 | ||
336 | 818 | |||
337 | 819 | if ret: | ||
338 | 820 | amulet.raise_status(amulet.FAIL, ret) | ||
339 | 821 | |||
340 | 822 | def configure_rmq_ssl_off(self, sentry_units, deployment, max_wait=60): | ||
341 | 823 | """Turn ssl charm config option off, confirm that it is disabled | ||
342 | 824 | on every unit. | ||
343 | 825 | |||
344 | 826 | :param sentry_units: list of sentry units | ||
345 | 827 | :param deployment: amulet deployment object pointer | ||
346 | 828 | :param max_wait: maximum time to wait in seconds to confirm | ||
347 | 829 | :returns: None if successful. Raise on error. | ||
348 | 830 | """ | ||
349 | 831 | self.log.debug('Setting ssl charm config option: off') | ||
350 | 832 | |||
351 | 833 | # Disable RMQ SSL | ||
352 | 834 | config = {'ssl': 'off'} | ||
353 | 835 | deployment.configure('rabbitmq-server', config) | ||
354 | 836 | |||
355 | 837 | # Confirm | ||
356 | 838 | tries = 0 | ||
357 | 839 | ret = self.validate_rmq_ssl_disabled_units(sentry_units) | ||
358 | 840 | while ret and tries < (max_wait / 4): | ||
359 | 841 | time.sleep(4) | ||
360 | 842 | self.log.debug('Attempt {}: {}'.format(tries, ret)) | ||
361 | 843 | ret = self.validate_rmq_ssl_disabled_units(sentry_units) | ||
362 | 844 | tries += 1 | ||
363 | 845 | |||
364 | 846 | if ret: | ||
365 | 847 | amulet.raise_status(amulet.FAIL, ret) | ||
366 | 848 | |||
367 | 849 | def connect_amqp_by_unit(self, sentry_unit, ssl=False, | ||
368 | 850 | port=None, fatal=True, | ||
369 | 851 | username="testuser1", password="changeme"): | ||
370 | 852 | """Establish and return a pika amqp connection to the rabbitmq service | ||
371 | 853 | running on a rmq juju unit. | ||
372 | 854 | |||
373 | 855 | :param sentry_unit: sentry unit pointer | ||
374 | 856 | :param ssl: boolean, default to False | ||
375 | 857 | :param port: amqp port, use defaults if None | ||
376 | 858 | :param fatal: boolean, default to True (raises on connect error) | ||
377 | 859 | :param username: amqp user name, default to testuser1 | ||
378 | 860 | :param password: amqp user password | ||
379 | 861 | :returns: pika amqp connection pointer or None if failed and non-fatal | ||
380 | 862 | """ | ||
381 | 863 | host = sentry_unit.info['public-address'] | ||
382 | 864 | unit_name = sentry_unit.info['unit_name'] | ||
383 | 865 | |||
384 | 866 | # Default port logic if port is not specified | ||
385 | 867 | if ssl and not port: | ||
386 | 868 | port = 5671 | ||
387 | 869 | elif not ssl and not port: | ||
388 | 870 | port = 5672 | ||
389 | 871 | |||
390 | 872 | self.log.debug('Connecting to amqp on {}:{} ({}) as ' | ||
391 | 873 | '{}...'.format(host, port, unit_name, username)) | ||
392 | 874 | |||
393 | 875 | try: | ||
394 | 876 | credentials = pika.PlainCredentials(username, password) | ||
395 | 877 | parameters = pika.ConnectionParameters(host=host, port=port, | ||
396 | 878 | credentials=credentials, | ||
397 | 879 | ssl=ssl, | ||
398 | 880 | connection_attempts=3, | ||
399 | 881 | retry_delay=5, | ||
400 | 882 | socket_timeout=1) | ||
401 | 883 | connection = pika.BlockingConnection(parameters) | ||
402 | 884 | assert connection.server_properties['product'] == 'RabbitMQ' | ||
403 | 885 | self.log.debug('Connect OK') | ||
404 | 886 | return connection | ||
405 | 887 | except Exception as e: | ||
406 | 888 | msg = ('amqp connection failed to {}:{} as ' | ||
407 | 889 | '{} ({})'.format(host, port, username, str(e))) | ||
408 | 890 | if fatal: | ||
409 | 891 | amulet.raise_status(amulet.FAIL, msg) | ||
410 | 892 | else: | ||
411 | 893 | self.log.warn(msg) | ||
412 | 894 | return None | ||
413 | 895 | |||
414 | 896 | def publish_amqp_message_by_unit(self, sentry_unit, message, | ||
415 | 897 | queue="test", ssl=False, | ||
416 | 898 | username="testuser1", | ||
417 | 899 | password="changeme", | ||
418 | 900 | port=None): | ||
419 | 901 | """Publish an amqp message to a rmq juju unit. | ||
420 | 902 | |||
421 | 903 | :param sentry_unit: sentry unit pointer | ||
422 | 904 | :param message: amqp message string | ||
423 | 905 | :param queue: message queue, default to test | ||
424 | 906 | :param username: amqp user name, default to testuser1 | ||
425 | 907 | :param password: amqp user password | ||
426 | 908 | :param ssl: boolean, default to False | ||
427 | 909 | :param port: amqp port, use defaults if None | ||
428 | 910 | :returns: None. Raises exception if publish failed. | ||
429 | 911 | """ | ||
430 | 912 | self.log.debug('Publishing message to {} queue:\n{}'.format(queue, | ||
431 | 913 | message)) | ||
432 | 914 | connection = self.connect_amqp_by_unit(sentry_unit, ssl=ssl, | ||
433 | 915 | port=port, | ||
434 | 916 | username=username, | ||
435 | 917 | password=password) | ||
436 | 918 | |||
437 | 919 | # NOTE(beisner): extra debug here re: pika hang potential: | ||
438 | 920 | # https://github.com/pika/pika/issues/297 | ||
439 | 921 | # https://groups.google.com/forum/#!topic/rabbitmq-users/Ja0iyfF0Szw | ||
440 | 922 | self.log.debug('Defining channel...') | ||
441 | 923 | channel = connection.channel() | ||
442 | 924 | self.log.debug('Declaring queue...') | ||
443 | 925 | channel.queue_declare(queue=queue, auto_delete=False, durable=True) | ||
444 | 926 | self.log.debug('Publishing message...') | ||
445 | 927 | channel.basic_publish(exchange='', routing_key=queue, body=message) | ||
446 | 928 | self.log.debug('Closing channel...') | ||
447 | 929 | channel.close() | ||
448 | 930 | self.log.debug('Closing connection...') | ||
449 | 931 | connection.close() | ||
450 | 932 | |||
451 | 933 | def get_amqp_message_by_unit(self, sentry_unit, queue="test", | ||
452 | 934 | username="testuser1", | ||
453 | 935 | password="changeme", | ||
454 | 936 | ssl=False, port=None): | ||
455 | 937 | """Get an amqp message from a rmq juju unit. | ||
456 | 938 | |||
457 | 939 | :param sentry_unit: sentry unit pointer | ||
458 | 940 | :param queue: message queue, default to test | ||
459 | 941 | :param username: amqp user name, default to testuser1 | ||
460 | 942 | :param password: amqp user password | ||
461 | 943 | :param ssl: boolean, default to False | ||
462 | 944 | :param port: amqp port, use defaults if None | ||
463 | 945 | :returns: amqp message body as string. Raise if get fails. | ||
464 | 946 | """ | ||
465 | 947 | connection = self.connect_amqp_by_unit(sentry_unit, ssl=ssl, | ||
466 | 948 | port=port, | ||
467 | 949 | username=username, | ||
468 | 950 | password=password) | ||
469 | 951 | channel = connection.channel() | ||
470 | 952 | method_frame, _, body = channel.basic_get(queue) | ||
471 | 953 | |||
472 | 954 | if method_frame: | ||
473 | 955 | self.log.debug('Retreived message from {} queue:\n{}'.format(queue, | ||
474 | 956 | body)) | ||
475 | 957 | channel.basic_ack(method_frame.delivery_tag) | ||
476 | 958 | channel.close() | ||
477 | 959 | connection.close() | ||
478 | 960 | return body | ||
479 | 961 | else: | ||
480 | 962 | msg = 'No message retrieved.' | ||
481 | 963 | amulet.raise_status(amulet.FAIL, msg) | ||
482 | 605 | 964 | ||
483 | === modified file 'charmhelpers/contrib/openstack/context.py' | |||
484 | --- charmhelpers/contrib/openstack/context.py 2015-09-03 09:40:28 +0000 | |||
485 | +++ charmhelpers/contrib/openstack/context.py 2015-09-25 14:42:33 +0000 | |||
486 | @@ -194,10 +194,50 @@ | |||
487 | 194 | class OSContextGenerator(object): | 194 | class OSContextGenerator(object): |
488 | 195 | """Base class for all context generators.""" | 195 | """Base class for all context generators.""" |
489 | 196 | interfaces = [] | 196 | interfaces = [] |
490 | 197 | related = False | ||
491 | 198 | complete = False | ||
492 | 199 | missing_data = [] | ||
493 | 197 | 200 | ||
494 | 198 | def __call__(self): | 201 | def __call__(self): |
495 | 199 | raise NotImplementedError | 202 | raise NotImplementedError |
496 | 200 | 203 | ||
497 | 204 | def context_complete(self, ctxt): | ||
498 | 205 | """Check for missing data for the required context data. | ||
499 | 206 | Set self.missing_data if it exists and return False. | ||
500 | 207 | Set self.complete if no missing data and return True. | ||
501 | 208 | """ | ||
502 | 209 | # Fresh start | ||
503 | 210 | self.complete = False | ||
504 | 211 | self.missing_data = [] | ||
505 | 212 | for k, v in six.iteritems(ctxt): | ||
506 | 213 | if v is None or v == '': | ||
507 | 214 | if k not in self.missing_data: | ||
508 | 215 | self.missing_data.append(k) | ||
509 | 216 | |||
510 | 217 | if self.missing_data: | ||
511 | 218 | self.complete = False | ||
512 | 219 | log('Missing required data: %s' % ' '.join(self.missing_data), level=INFO) | ||
513 | 220 | else: | ||
514 | 221 | self.complete = True | ||
515 | 222 | return self.complete | ||
516 | 223 | |||
517 | 224 | def get_related(self): | ||
518 | 225 | """Check if any of the context interfaces have relation ids. | ||
519 | 226 | Set self.related and return True if one of the interfaces | ||
520 | 227 | has relation ids. | ||
521 | 228 | """ | ||
522 | 229 | # Fresh start | ||
523 | 230 | self.related = False | ||
524 | 231 | try: | ||
525 | 232 | for interface in self.interfaces: | ||
526 | 233 | if relation_ids(interface): | ||
527 | 234 | self.related = True | ||
528 | 235 | return self.related | ||
529 | 236 | except AttributeError as e: | ||
530 | 237 | log("{} {}" | ||
531 | 238 | "".format(self, e), 'INFO') | ||
532 | 239 | return self.related | ||
533 | 240 | |||
534 | 201 | 241 | ||
535 | 202 | class SharedDBContext(OSContextGenerator): | 242 | class SharedDBContext(OSContextGenerator): |
536 | 203 | interfaces = ['shared-db'] | 243 | interfaces = ['shared-db'] |
537 | @@ -213,6 +253,7 @@ | |||
538 | 213 | self.database = database | 253 | self.database = database |
539 | 214 | self.user = user | 254 | self.user = user |
540 | 215 | self.ssl_dir = ssl_dir | 255 | self.ssl_dir = ssl_dir |
541 | 256 | self.rel_name = self.interfaces[0] | ||
542 | 216 | 257 | ||
543 | 217 | def __call__(self): | 258 | def __call__(self): |
544 | 218 | self.database = self.database or config('database') | 259 | self.database = self.database or config('database') |
545 | @@ -246,6 +287,7 @@ | |||
546 | 246 | password_setting = self.relation_prefix + '_password' | 287 | password_setting = self.relation_prefix + '_password' |
547 | 247 | 288 | ||
548 | 248 | for rid in relation_ids(self.interfaces[0]): | 289 | for rid in relation_ids(self.interfaces[0]): |
549 | 290 | self.related = True | ||
550 | 249 | for unit in related_units(rid): | 291 | for unit in related_units(rid): |
551 | 250 | rdata = relation_get(rid=rid, unit=unit) | 292 | rdata = relation_get(rid=rid, unit=unit) |
552 | 251 | host = rdata.get('db_host') | 293 | host = rdata.get('db_host') |
553 | @@ -257,7 +299,7 @@ | |||
554 | 257 | 'database_password': rdata.get(password_setting), | 299 | 'database_password': rdata.get(password_setting), |
555 | 258 | 'database_type': 'mysql' | 300 | 'database_type': 'mysql' |
556 | 259 | } | 301 | } |
558 | 260 | if context_complete(ctxt): | 302 | if self.context_complete(ctxt): |
559 | 261 | db_ssl(rdata, ctxt, self.ssl_dir) | 303 | db_ssl(rdata, ctxt, self.ssl_dir) |
560 | 262 | return ctxt | 304 | return ctxt |
561 | 263 | return {} | 305 | return {} |
562 | @@ -278,6 +320,7 @@ | |||
563 | 278 | 320 | ||
564 | 279 | ctxt = {} | 321 | ctxt = {} |
565 | 280 | for rid in relation_ids(self.interfaces[0]): | 322 | for rid in relation_ids(self.interfaces[0]): |
566 | 323 | self.related = True | ||
567 | 281 | for unit in related_units(rid): | 324 | for unit in related_units(rid): |
568 | 282 | rel_host = relation_get('host', rid=rid, unit=unit) | 325 | rel_host = relation_get('host', rid=rid, unit=unit) |
569 | 283 | rel_user = relation_get('user', rid=rid, unit=unit) | 326 | rel_user = relation_get('user', rid=rid, unit=unit) |
570 | @@ -287,7 +330,7 @@ | |||
571 | 287 | 'database_user': rel_user, | 330 | 'database_user': rel_user, |
572 | 288 | 'database_password': rel_passwd, | 331 | 'database_password': rel_passwd, |
573 | 289 | 'database_type': 'postgresql'} | 332 | 'database_type': 'postgresql'} |
575 | 290 | if context_complete(ctxt): | 333 | if self.context_complete(ctxt): |
576 | 291 | return ctxt | 334 | return ctxt |
577 | 292 | 335 | ||
578 | 293 | return {} | 336 | return {} |
579 | @@ -348,6 +391,7 @@ | |||
580 | 348 | ctxt['signing_dir'] = cachedir | 391 | ctxt['signing_dir'] = cachedir |
581 | 349 | 392 | ||
582 | 350 | for rid in relation_ids(self.rel_name): | 393 | for rid in relation_ids(self.rel_name): |
583 | 394 | self.related = True | ||
584 | 351 | for unit in related_units(rid): | 395 | for unit in related_units(rid): |
585 | 352 | rdata = relation_get(rid=rid, unit=unit) | 396 | rdata = relation_get(rid=rid, unit=unit) |
586 | 353 | serv_host = rdata.get('service_host') | 397 | serv_host = rdata.get('service_host') |
587 | @@ -366,7 +410,7 @@ | |||
588 | 366 | 'service_protocol': svc_protocol, | 410 | 'service_protocol': svc_protocol, |
589 | 367 | 'auth_protocol': auth_protocol}) | 411 | 'auth_protocol': auth_protocol}) |
590 | 368 | 412 | ||
592 | 369 | if context_complete(ctxt): | 413 | if self.context_complete(ctxt): |
593 | 370 | # NOTE(jamespage) this is required for >= icehouse | 414 | # NOTE(jamespage) this is required for >= icehouse |
594 | 371 | # so a missing value just indicates keystone needs | 415 | # so a missing value just indicates keystone needs |
595 | 372 | # upgrading | 416 | # upgrading |
596 | @@ -405,6 +449,7 @@ | |||
597 | 405 | ctxt = {} | 449 | ctxt = {} |
598 | 406 | for rid in relation_ids(self.rel_name): | 450 | for rid in relation_ids(self.rel_name): |
599 | 407 | ha_vip_only = False | 451 | ha_vip_only = False |
600 | 452 | self.related = True | ||
601 | 408 | for unit in related_units(rid): | 453 | for unit in related_units(rid): |
602 | 409 | if relation_get('clustered', rid=rid, unit=unit): | 454 | if relation_get('clustered', rid=rid, unit=unit): |
603 | 410 | ctxt['clustered'] = True | 455 | ctxt['clustered'] = True |
604 | @@ -437,7 +482,7 @@ | |||
605 | 437 | ha_vip_only = relation_get('ha-vip-only', | 482 | ha_vip_only = relation_get('ha-vip-only', |
606 | 438 | rid=rid, unit=unit) is not None | 483 | rid=rid, unit=unit) is not None |
607 | 439 | 484 | ||
609 | 440 | if context_complete(ctxt): | 485 | if self.context_complete(ctxt): |
610 | 441 | if 'rabbit_ssl_ca' in ctxt: | 486 | if 'rabbit_ssl_ca' in ctxt: |
611 | 442 | if not self.ssl_dir: | 487 | if not self.ssl_dir: |
612 | 443 | log("Charm not setup for ssl support but ssl ca " | 488 | log("Charm not setup for ssl support but ssl ca " |
613 | @@ -469,7 +514,7 @@ | |||
614 | 469 | ctxt['oslo_messaging_flags'] = config_flags_parser( | 514 | ctxt['oslo_messaging_flags'] = config_flags_parser( |
615 | 470 | oslo_messaging_flags) | 515 | oslo_messaging_flags) |
616 | 471 | 516 | ||
618 | 472 | if not context_complete(ctxt): | 517 | if not self.complete: |
619 | 473 | return {} | 518 | return {} |
620 | 474 | 519 | ||
621 | 475 | return ctxt | 520 | return ctxt |
622 | @@ -485,13 +530,15 @@ | |||
623 | 485 | 530 | ||
624 | 486 | log('Generating template context for ceph', level=DEBUG) | 531 | log('Generating template context for ceph', level=DEBUG) |
625 | 487 | mon_hosts = [] | 532 | mon_hosts = [] |
629 | 488 | auth = None | 533 | ctxt = { |
630 | 489 | key = None | 534 | 'use_syslog': str(config('use-syslog')).lower() |
631 | 490 | use_syslog = str(config('use-syslog')).lower() | 535 | } |
632 | 491 | for rid in relation_ids('ceph'): | 536 | for rid in relation_ids('ceph'): |
633 | 492 | for unit in related_units(rid): | 537 | for unit in related_units(rid): |
636 | 493 | auth = relation_get('auth', rid=rid, unit=unit) | 538 | if not ctxt.get('auth'): |
637 | 494 | key = relation_get('key', rid=rid, unit=unit) | 539 | ctxt['auth'] = relation_get('auth', rid=rid, unit=unit) |
638 | 540 | if not ctxt.get('key'): | ||
639 | 541 | ctxt['key'] = relation_get('key', rid=rid, unit=unit) | ||
640 | 495 | ceph_pub_addr = relation_get('ceph-public-address', rid=rid, | 542 | ceph_pub_addr = relation_get('ceph-public-address', rid=rid, |
641 | 496 | unit=unit) | 543 | unit=unit) |
642 | 497 | unit_priv_addr = relation_get('private-address', rid=rid, | 544 | unit_priv_addr = relation_get('private-address', rid=rid, |
643 | @@ -500,15 +547,12 @@ | |||
644 | 500 | ceph_addr = format_ipv6_addr(ceph_addr) or ceph_addr | 547 | ceph_addr = format_ipv6_addr(ceph_addr) or ceph_addr |
645 | 501 | mon_hosts.append(ceph_addr) | 548 | mon_hosts.append(ceph_addr) |
646 | 502 | 549 | ||
651 | 503 | ctxt = {'mon_hosts': ' '.join(sorted(mon_hosts)), | 550 | ctxt['mon_hosts'] = ' '.join(sorted(mon_hosts)) |
648 | 504 | 'auth': auth, | ||
649 | 505 | 'key': key, | ||
650 | 506 | 'use_syslog': use_syslog} | ||
652 | 507 | 551 | ||
653 | 508 | if not os.path.isdir('/etc/ceph'): | 552 | if not os.path.isdir('/etc/ceph'): |
654 | 509 | os.mkdir('/etc/ceph') | 553 | os.mkdir('/etc/ceph') |
655 | 510 | 554 | ||
657 | 511 | if not context_complete(ctxt): | 555 | if not self.context_complete(ctxt): |
658 | 512 | return {} | 556 | return {} |
659 | 513 | 557 | ||
660 | 514 | ensure_packages(['ceph-common']) | 558 | ensure_packages(['ceph-common']) |
661 | @@ -1367,6 +1411,6 @@ | |||
662 | 1367 | 'auth_protocol': | 1411 | 'auth_protocol': |
663 | 1368 | rdata.get('auth_protocol') or 'http', | 1412 | rdata.get('auth_protocol') or 'http', |
664 | 1369 | } | 1413 | } |
666 | 1370 | if context_complete(ctxt): | 1414 | if self.context_complete(ctxt): |
667 | 1371 | return ctxt | 1415 | return ctxt |
668 | 1372 | return {} | 1416 | return {} |
669 | 1373 | 1417 | ||
670 | === modified file 'charmhelpers/contrib/openstack/templating.py' | |||
671 | --- charmhelpers/contrib/openstack/templating.py 2015-07-29 10:47:17 +0000 | |||
672 | +++ charmhelpers/contrib/openstack/templating.py 2015-09-25 14:42:33 +0000 | |||
673 | @@ -18,7 +18,7 @@ | |||
674 | 18 | 18 | ||
675 | 19 | import six | 19 | import six |
676 | 20 | 20 | ||
678 | 21 | from charmhelpers.fetch import apt_install | 21 | from charmhelpers.fetch import apt_install, apt_update |
679 | 22 | from charmhelpers.core.hookenv import ( | 22 | from charmhelpers.core.hookenv import ( |
680 | 23 | log, | 23 | log, |
681 | 24 | ERROR, | 24 | ERROR, |
682 | @@ -29,6 +29,7 @@ | |||
683 | 29 | try: | 29 | try: |
684 | 30 | from jinja2 import FileSystemLoader, ChoiceLoader, Environment, exceptions | 30 | from jinja2 import FileSystemLoader, ChoiceLoader, Environment, exceptions |
685 | 31 | except ImportError: | 31 | except ImportError: |
686 | 32 | apt_update(fatal=True) | ||
687 | 32 | apt_install('python-jinja2', fatal=True) | 33 | apt_install('python-jinja2', fatal=True) |
688 | 33 | from jinja2 import FileSystemLoader, ChoiceLoader, Environment, exceptions | 34 | from jinja2 import FileSystemLoader, ChoiceLoader, Environment, exceptions |
689 | 34 | 35 | ||
690 | @@ -112,7 +113,7 @@ | |||
691 | 112 | 113 | ||
692 | 113 | def complete_contexts(self): | 114 | def complete_contexts(self): |
693 | 114 | ''' | 115 | ''' |
695 | 115 | Return a list of interfaces that have atisfied contexts. | 116 | Return a list of interfaces that have satisfied contexts. |
696 | 116 | ''' | 117 | ''' |
697 | 117 | if self._complete_contexts: | 118 | if self._complete_contexts: |
698 | 118 | return self._complete_contexts | 119 | return self._complete_contexts |
699 | @@ -293,3 +294,30 @@ | |||
700 | 293 | [interfaces.extend(i.complete_contexts()) | 294 | [interfaces.extend(i.complete_contexts()) |
701 | 294 | for i in six.itervalues(self.templates)] | 295 | for i in six.itervalues(self.templates)] |
702 | 295 | return interfaces | 296 | return interfaces |
703 | 297 | |||
704 | 298 | def get_incomplete_context_data(self, interfaces): | ||
705 | 299 | ''' | ||
706 | 300 | Return dictionary of relation status of interfaces and any missing | ||
707 | 301 | required context data. Example: | ||
708 | 302 | {'amqp': {'missing_data': ['rabbitmq_password'], 'related': True}, | ||
709 | 303 | 'zeromq-configuration': {'related': False}} | ||
710 | 304 | ''' | ||
711 | 305 | incomplete_context_data = {} | ||
712 | 306 | |||
713 | 307 | for i in six.itervalues(self.templates): | ||
714 | 308 | for context in i.contexts: | ||
715 | 309 | for interface in interfaces: | ||
716 | 310 | related = False | ||
717 | 311 | if interface in context.interfaces: | ||
718 | 312 | related = context.get_related() | ||
719 | 313 | missing_data = context.missing_data | ||
720 | 314 | if missing_data: | ||
721 | 315 | incomplete_context_data[interface] = {'missing_data': missing_data} | ||
722 | 316 | if related: | ||
723 | 317 | if incomplete_context_data.get(interface): | ||
724 | 318 | incomplete_context_data[interface].update({'related': True}) | ||
725 | 319 | else: | ||
726 | 320 | incomplete_context_data[interface] = {'related': True} | ||
727 | 321 | else: | ||
728 | 322 | incomplete_context_data[interface] = {'related': False} | ||
729 | 323 | return incomplete_context_data | ||
730 | 296 | 324 | ||
731 | === modified file 'charmhelpers/contrib/openstack/utils.py' | |||
732 | --- charmhelpers/contrib/openstack/utils.py 2015-09-03 09:40:28 +0000 | |||
733 | +++ charmhelpers/contrib/openstack/utils.py 2015-09-25 14:42:33 +0000 | |||
734 | @@ -25,6 +25,7 @@ | |||
735 | 25 | import re | 25 | import re |
736 | 26 | 26 | ||
737 | 27 | import six | 27 | import six |
738 | 28 | import traceback | ||
739 | 28 | import yaml | 29 | import yaml |
740 | 29 | 30 | ||
741 | 30 | from charmhelpers.contrib.network import ip | 31 | from charmhelpers.contrib.network import ip |
742 | @@ -34,12 +35,16 @@ | |||
743 | 34 | ) | 35 | ) |
744 | 35 | 36 | ||
745 | 36 | from charmhelpers.core.hookenv import ( | 37 | from charmhelpers.core.hookenv import ( |
746 | 38 | action_fail, | ||
747 | 39 | action_set, | ||
748 | 37 | config, | 40 | config, |
749 | 38 | log as juju_log, | 41 | log as juju_log, |
750 | 39 | charm_dir, | 42 | charm_dir, |
751 | 40 | INFO, | 43 | INFO, |
752 | 41 | relation_ids, | 44 | relation_ids, |
754 | 42 | relation_set | 45 | relation_set, |
755 | 46 | status_set, | ||
756 | 47 | hook_name | ||
757 | 43 | ) | 48 | ) |
758 | 44 | 49 | ||
759 | 45 | from charmhelpers.contrib.storage.linux.lvm import ( | 50 | from charmhelpers.contrib.storage.linux.lvm import ( |
760 | @@ -49,7 +54,8 @@ | |||
761 | 49 | ) | 54 | ) |
762 | 50 | 55 | ||
763 | 51 | from charmhelpers.contrib.network.ip import ( | 56 | from charmhelpers.contrib.network.ip import ( |
765 | 52 | get_ipv6_addr | 57 | get_ipv6_addr, |
766 | 58 | is_ipv6, | ||
767 | 53 | ) | 59 | ) |
768 | 54 | 60 | ||
769 | 55 | from charmhelpers.contrib.python.packages import ( | 61 | from charmhelpers.contrib.python.packages import ( |
770 | @@ -114,6 +120,7 @@ | |||
771 | 114 | ('2.2.1', 'kilo'), | 120 | ('2.2.1', 'kilo'), |
772 | 115 | ('2.2.2', 'kilo'), | 121 | ('2.2.2', 'kilo'), |
773 | 116 | ('2.3.0', 'liberty'), | 122 | ('2.3.0', 'liberty'), |
774 | 123 | ('2.4.0', 'liberty'), | ||
775 | 117 | ]) | 124 | ]) |
776 | 118 | 125 | ||
777 | 119 | # >= Liberty version->codename mapping | 126 | # >= Liberty version->codename mapping |
778 | @@ -142,6 +149,9 @@ | |||
779 | 142 | 'glance-common': OrderedDict([ | 149 | 'glance-common': OrderedDict([ |
780 | 143 | ('11.0.0', 'liberty'), | 150 | ('11.0.0', 'liberty'), |
781 | 144 | ]), | 151 | ]), |
782 | 152 | 'openstack-dashboard': OrderedDict([ | ||
783 | 153 | ('8.0.0', 'liberty'), | ||
784 | 154 | ]), | ||
785 | 145 | } | 155 | } |
786 | 146 | 156 | ||
787 | 147 | DEFAULT_LOOPBACK_SIZE = '5G' | 157 | DEFAULT_LOOPBACK_SIZE = '5G' |
788 | @@ -510,6 +520,12 @@ | |||
789 | 510 | relation_prefix=None): | 520 | relation_prefix=None): |
790 | 511 | hosts = get_ipv6_addr(dynamic_only=False) | 521 | hosts = get_ipv6_addr(dynamic_only=False) |
791 | 512 | 522 | ||
792 | 523 | if config('vip'): | ||
793 | 524 | vips = config('vip').split() | ||
794 | 525 | for vip in vips: | ||
795 | 526 | if vip and is_ipv6(vip): | ||
796 | 527 | hosts.append(vip) | ||
797 | 528 | |||
798 | 513 | kwargs = {'database': database, | 529 | kwargs = {'database': database, |
799 | 514 | 'username': database_user, | 530 | 'username': database_user, |
800 | 515 | 'hostname': json.dumps(hosts)} | 531 | 'hostname': json.dumps(hosts)} |
801 | @@ -745,3 +761,217 @@ | |||
802 | 745 | return projects[key] | 761 | return projects[key] |
803 | 746 | 762 | ||
804 | 747 | return None | 763 | return None |
805 | 764 | |||
806 | 765 | |||
807 | 766 | def os_workload_status(configs, required_interfaces, charm_func=None): | ||
808 | 767 | """ | ||
809 | 768 | Decorator to set workload status based on complete contexts | ||
810 | 769 | """ | ||
811 | 770 | def wrap(f): | ||
812 | 771 | @wraps(f) | ||
813 | 772 | def wrapped_f(*args, **kwargs): | ||
814 | 773 | # Run the original function first | ||
815 | 774 | f(*args, **kwargs) | ||
816 | 775 | # Set workload status now that contexts have been | ||
817 | 776 | # acted on | ||
818 | 777 | set_os_workload_status(configs, required_interfaces, charm_func) | ||
819 | 778 | return wrapped_f | ||
820 | 779 | return wrap | ||
821 | 780 | |||
822 | 781 | |||
823 | 782 | def set_os_workload_status(configs, required_interfaces, charm_func=None): | ||
824 | 783 | """ | ||
825 | 784 | Set workload status based on complete contexts. | ||
826 | 785 | status-set missing or incomplete contexts | ||
827 | 786 | and juju-log details of missing required data. | ||
828 | 787 | charm_func is a charm specific function to run checking | ||
829 | 788 | for charm specific requirements such as a VIP setting. | ||
830 | 789 | """ | ||
831 | 790 | incomplete_rel_data = incomplete_relation_data(configs, required_interfaces) | ||
832 | 791 | state = 'active' | ||
833 | 792 | missing_relations = [] | ||
834 | 793 | incomplete_relations = [] | ||
835 | 794 | message = None | ||
836 | 795 | charm_state = None | ||
837 | 796 | charm_message = None | ||
838 | 797 | |||
839 | 798 | for generic_interface in incomplete_rel_data.keys(): | ||
840 | 799 | related_interface = None | ||
841 | 800 | missing_data = {} | ||
842 | 801 | # Related or not? | ||
843 | 802 | for interface in incomplete_rel_data[generic_interface]: | ||
844 | 803 | if incomplete_rel_data[generic_interface][interface].get('related'): | ||
845 | 804 | related_interface = interface | ||
846 | 805 | missing_data = incomplete_rel_data[generic_interface][interface].get('missing_data') | ||
847 | 806 | # No relation ID for the generic_interface | ||
848 | 807 | if not related_interface: | ||
849 | 808 | juju_log("{} relation is missing and must be related for " | ||
850 | 809 | "functionality. ".format(generic_interface), 'WARN') | ||
851 | 810 | state = 'blocked' | ||
852 | 811 | if generic_interface not in missing_relations: | ||
853 | 812 | missing_relations.append(generic_interface) | ||
854 | 813 | else: | ||
855 | 814 | # Relation ID exists but no related unit | ||
856 | 815 | if not missing_data: | ||
857 | 816 | # Edge case relation ID exists but departing | ||
858 | 817 | if ('departed' in hook_name() or 'broken' in hook_name()) \ | ||
859 | 818 | and related_interface in hook_name(): | ||
860 | 819 | state = 'blocked' | ||
861 | 820 | if generic_interface not in missing_relations: | ||
862 | 821 | missing_relations.append(generic_interface) | ||
863 | 822 | juju_log("{} relation's interface, {}, " | ||
864 | 823 | "relationship is departed or broken " | ||
865 | 824 | "and is required for functionality." | ||
866 | 825 | "".format(generic_interface, related_interface), "WARN") | ||
867 | 826 | # Normal case relation ID exists but no related unit | ||
868 | 827 | # (joining) | ||
869 | 828 | else: | ||
870 | 829 | juju_log("{} relations's interface, {}, is related but has " | ||
871 | 830 | "no units in the relation." | ||
872 | 831 | "".format(generic_interface, related_interface), "INFO") | ||
873 | 832 | # Related unit exists and data missing on the relation | ||
874 | 833 | else: | ||
875 | 834 | juju_log("{} relation's interface, {}, is related awaiting " | ||
876 | 835 | "the following data from the relationship: {}. " | ||
877 | 836 | "".format(generic_interface, related_interface, | ||
878 | 837 | ", ".join(missing_data)), "INFO") | ||
879 | 838 | if state != 'blocked': | ||
880 | 839 | state = 'waiting' | ||
881 | 840 | if generic_interface not in incomplete_relations \ | ||
882 | 841 | and generic_interface not in missing_relations: | ||
883 | 842 | incomplete_relations.append(generic_interface) | ||
884 | 843 | |||
885 | 844 | if missing_relations: | ||
886 | 845 | message = "Missing relations: {}".format(", ".join(missing_relations)) | ||
887 | 846 | if incomplete_relations: | ||
888 | 847 | message += "; incomplete relations: {}" \ | ||
889 | 848 | "".format(", ".join(incomplete_relations)) | ||
890 | 849 | state = 'blocked' | ||
891 | 850 | elif incomplete_relations: | ||
892 | 851 | message = "Incomplete relations: {}" \ | ||
893 | 852 | "".format(", ".join(incomplete_relations)) | ||
894 | 853 | state = 'waiting' | ||
895 | 854 | |||
896 | 855 | # Run charm specific checks | ||
897 | 856 | if charm_func: | ||
898 | 857 | charm_state, charm_message = charm_func(configs) | ||
899 | 858 | if charm_state != 'active' and charm_state != 'unknown': | ||
900 | 859 | state = workload_state_compare(state, charm_state) | ||
901 | 860 | if message: | ||
902 | 861 | message = "{} {}".format(message, charm_message) | ||
903 | 862 | else: | ||
904 | 863 | message = charm_message | ||
905 | 864 | |||
906 | 865 | # Set to active if all requirements have been met | ||
907 | 866 | if state == 'active': | ||
908 | 867 | message = "Unit is ready" | ||
909 | 868 | juju_log(message, "INFO") | ||
910 | 869 | |||
911 | 870 | status_set(state, message) | ||
912 | 871 | |||
913 | 872 | |||
914 | 873 | def workload_state_compare(current_workload_state, workload_state): | ||
915 | 874 | """ Return highest priority of two states""" | ||
916 | 875 | hierarchy = {'unknown': -1, | ||
917 | 876 | 'active': 0, | ||
918 | 877 | 'maintenance': 1, | ||
919 | 878 | 'waiting': 2, | ||
920 | 879 | 'blocked': 3, | ||
921 | 880 | } | ||
922 | 881 | |||
923 | 882 | if hierarchy.get(workload_state) is None: | ||
924 | 883 | workload_state = 'unknown' | ||
925 | 884 | if hierarchy.get(current_workload_state) is None: | ||
926 | 885 | current_workload_state = 'unknown' | ||
927 | 886 | |||
928 | 887 | # Set workload_state based on hierarchy of statuses | ||
929 | 888 | if hierarchy.get(current_workload_state) > hierarchy.get(workload_state): | ||
930 | 889 | return current_workload_state | ||
931 | 890 | else: | ||
932 | 891 | return workload_state | ||
933 | 892 | |||
934 | 893 | |||
935 | 894 | def incomplete_relation_data(configs, required_interfaces): | ||
936 | 895 | """ | ||
937 | 896 | Check complete contexts against required_interfaces | ||
938 | 897 | Return dictionary of incomplete relation data. | ||
939 | 898 | |||
940 | 899 | configs is an OSConfigRenderer object with configs registered | ||
941 | 900 | |||
942 | 901 | required_interfaces is a dictionary of required general interfaces | ||
943 | 902 | with dictionary values of possible specific interfaces. | ||
944 | 903 | Example: | ||
945 | 904 | required_interfaces = {'database': ['shared-db', 'pgsql-db']} | ||
946 | 905 | |||
947 | 906 | The interface is said to be satisfied if anyone of the interfaces in the | ||
948 | 907 | list has a complete context. | ||
949 | 908 | |||
950 | 909 | Return dictionary of incomplete or missing required contexts with relation | ||
951 | 910 | status of interfaces and any missing data points. Example: | ||
952 | 911 | {'message': | ||
953 | 912 | {'amqp': {'missing_data': ['rabbitmq_password'], 'related': True}, | ||
954 | 913 | 'zeromq-configuration': {'related': False}}, | ||
955 | 914 | 'identity': | ||
956 | 915 | {'identity-service': {'related': False}}, | ||
957 | 916 | 'database': | ||
958 | 917 | {'pgsql-db': {'related': False}, | ||
959 | 918 | 'shared-db': {'related': True}}} | ||
960 | 919 | """ | ||
961 | 920 | complete_ctxts = configs.complete_contexts() | ||
962 | 921 | incomplete_relations = [] | ||
963 | 922 | for svc_type in required_interfaces.keys(): | ||
964 | 923 | # Avoid duplicates | ||
965 | 924 | found_ctxt = False | ||
966 | 925 | for interface in required_interfaces[svc_type]: | ||
967 | 926 | if interface in complete_ctxts: | ||
968 | 927 | found_ctxt = True | ||
969 | 928 | if not found_ctxt: | ||
970 | 929 | incomplete_relations.append(svc_type) | ||
971 | 930 | incomplete_context_data = {} | ||
972 | 931 | for i in incomplete_relations: | ||
973 | 932 | incomplete_context_data[i] = configs.get_incomplete_context_data(required_interfaces[i]) | ||
974 | 933 | return incomplete_context_data | ||
975 | 934 | |||
976 | 935 | |||
977 | 936 | def do_action_openstack_upgrade(package, upgrade_callback, configs): | ||
978 | 937 | """Perform action-managed OpenStack upgrade. | ||
979 | 938 | |||
980 | 939 | Upgrades packages to the configured openstack-origin version and sets | ||
981 | 940 | the corresponding action status as a result. | ||
982 | 941 | |||
983 | 942 | If the charm was installed from source we cannot upgrade it. | ||
984 | 943 | For backwards compatibility a config flag (action-managed-upgrade) must | ||
985 | 944 | be set for this code to run, otherwise a full service level upgrade will | ||
986 | 945 | fire on config-changed. | ||
987 | 946 | |||
988 | 947 | @param package: package name for determining if upgrade available | ||
989 | 948 | @param upgrade_callback: function callback to charm's upgrade function | ||
990 | 949 | @param configs: templating object derived from OSConfigRenderer class | ||
991 | 950 | |||
992 | 951 | @return: True if upgrade successful; False if upgrade failed or skipped | ||
993 | 952 | """ | ||
994 | 953 | ret = False | ||
995 | 954 | |||
996 | 955 | if git_install_requested(): | ||
997 | 956 | action_set({'outcome': 'installed from source, skipped upgrade.'}) | ||
998 | 957 | else: | ||
999 | 958 | if openstack_upgrade_available(package): | ||
1000 | 959 | if config('action-managed-upgrade'): | ||
1001 | 960 | juju_log('Upgrading OpenStack release') | ||
1002 | 961 | |||
1003 | 962 | try: | ||
1004 | 963 | upgrade_callback(configs=configs) | ||
1005 | 964 | action_set({'outcome': 'success, upgrade completed.'}) | ||
1006 | 965 | ret = True | ||
1007 | 966 | except: | ||
1008 | 967 | action_set({'outcome': 'upgrade failed, see traceback.'}) | ||
1009 | 968 | action_set({'traceback': traceback.format_exc()}) | ||
1010 | 969 | action_fail('do_openstack_upgrade resulted in an ' | ||
1011 | 970 | 'unexpected error') | ||
1012 | 971 | else: | ||
1013 | 972 | action_set({'outcome': 'action-managed-upgrade config is ' | ||
1014 | 973 | 'False, skipped upgrade.'}) | ||
1015 | 974 | else: | ||
1016 | 975 | action_set({'outcome': 'no upgrade available.'}) | ||
1017 | 976 | |||
1018 | 977 | return ret | ||
1019 | 748 | 978 | ||
1020 | === modified file 'charmhelpers/contrib/storage/linux/ceph.py' | |||
1021 | --- charmhelpers/contrib/storage/linux/ceph.py 2015-07-16 20:17:38 +0000 | |||
1022 | +++ charmhelpers/contrib/storage/linux/ceph.py 2015-09-25 14:42:33 +0000 | |||
1023 | @@ -28,6 +28,7 @@ | |||
1024 | 28 | import shutil | 28 | import shutil |
1025 | 29 | import json | 29 | import json |
1026 | 30 | import time | 30 | import time |
1027 | 31 | import uuid | ||
1028 | 31 | 32 | ||
1029 | 32 | from subprocess import ( | 33 | from subprocess import ( |
1030 | 33 | check_call, | 34 | check_call, |
1031 | @@ -35,8 +36,10 @@ | |||
1032 | 35 | CalledProcessError, | 36 | CalledProcessError, |
1033 | 36 | ) | 37 | ) |
1034 | 37 | from charmhelpers.core.hookenv import ( | 38 | from charmhelpers.core.hookenv import ( |
1035 | 39 | local_unit, | ||
1036 | 38 | relation_get, | 40 | relation_get, |
1037 | 39 | relation_ids, | 41 | relation_ids, |
1038 | 42 | relation_set, | ||
1039 | 40 | related_units, | 43 | related_units, |
1040 | 41 | log, | 44 | log, |
1041 | 42 | DEBUG, | 45 | DEBUG, |
1042 | @@ -56,6 +59,8 @@ | |||
1043 | 56 | apt_install, | 59 | apt_install, |
1044 | 57 | ) | 60 | ) |
1045 | 58 | 61 | ||
1046 | 62 | from charmhelpers.core.kernel import modprobe | ||
1047 | 63 | |||
1048 | 59 | KEYRING = '/etc/ceph/ceph.client.{}.keyring' | 64 | KEYRING = '/etc/ceph/ceph.client.{}.keyring' |
1049 | 60 | KEYFILE = '/etc/ceph/ceph.client.{}.key' | 65 | KEYFILE = '/etc/ceph/ceph.client.{}.key' |
1050 | 61 | 66 | ||
1051 | @@ -288,17 +293,6 @@ | |||
1052 | 288 | os.chown(data_src_dst, uid, gid) | 293 | os.chown(data_src_dst, uid, gid) |
1053 | 289 | 294 | ||
1054 | 290 | 295 | ||
1055 | 291 | # TODO: re-use | ||
1056 | 292 | def modprobe(module): | ||
1057 | 293 | """Load a kernel module and configure for auto-load on reboot.""" | ||
1058 | 294 | log('Loading kernel module', level=INFO) | ||
1059 | 295 | cmd = ['modprobe', module] | ||
1060 | 296 | check_call(cmd) | ||
1061 | 297 | with open('/etc/modules', 'r+') as modules: | ||
1062 | 298 | if module not in modules.read(): | ||
1063 | 299 | modules.write(module) | ||
1064 | 300 | |||
1065 | 301 | |||
1066 | 302 | def copy_files(src, dst, symlinks=False, ignore=None): | 296 | def copy_files(src, dst, symlinks=False, ignore=None): |
1067 | 303 | """Copy files from src to dst.""" | 297 | """Copy files from src to dst.""" |
1068 | 304 | for item in os.listdir(src): | 298 | for item in os.listdir(src): |
1069 | @@ -411,17 +405,52 @@ | |||
1070 | 411 | 405 | ||
1071 | 412 | The API is versioned and defaults to version 1. | 406 | The API is versioned and defaults to version 1. |
1072 | 413 | """ | 407 | """ |
1074 | 414 | def __init__(self, api_version=1): | 408 | def __init__(self, api_version=1, request_id=None): |
1075 | 415 | self.api_version = api_version | 409 | self.api_version = api_version |
1076 | 410 | if request_id: | ||
1077 | 411 | self.request_id = request_id | ||
1078 | 412 | else: | ||
1079 | 413 | self.request_id = str(uuid.uuid1()) | ||
1080 | 416 | self.ops = [] | 414 | self.ops = [] |
1081 | 417 | 415 | ||
1082 | 418 | def add_op_create_pool(self, name, replica_count=3): | 416 | def add_op_create_pool(self, name, replica_count=3): |
1083 | 419 | self.ops.append({'op': 'create-pool', 'name': name, | 417 | self.ops.append({'op': 'create-pool', 'name': name, |
1084 | 420 | 'replicas': replica_count}) | 418 | 'replicas': replica_count}) |
1085 | 421 | 419 | ||
1086 | 420 | def set_ops(self, ops): | ||
1087 | 421 | """Set request ops to provided value. | ||
1088 | 422 | |||
1089 | 423 | Useful for injecting ops that come from a previous request | ||
1090 | 424 | to allow comparisons to ensure validity. | ||
1091 | 425 | """ | ||
1092 | 426 | self.ops = ops | ||
1093 | 427 | |||
1094 | 422 | @property | 428 | @property |
1095 | 423 | def request(self): | 429 | def request(self): |
1097 | 424 | return json.dumps({'api-version': self.api_version, 'ops': self.ops}) | 430 | return json.dumps({'api-version': self.api_version, 'ops': self.ops, |
1098 | 431 | 'request-id': self.request_id}) | ||
1099 | 432 | |||
1100 | 433 | def _ops_equal(self, other): | ||
1101 | 434 | if len(self.ops) == len(other.ops): | ||
1102 | 435 | for req_no in range(0, len(self.ops)): | ||
1103 | 436 | for key in ['replicas', 'name', 'op']: | ||
1104 | 437 | if self.ops[req_no][key] != other.ops[req_no][key]: | ||
1105 | 438 | return False | ||
1106 | 439 | else: | ||
1107 | 440 | return False | ||
1108 | 441 | return True | ||
1109 | 442 | |||
1110 | 443 | def __eq__(self, other): | ||
1111 | 444 | if not isinstance(other, self.__class__): | ||
1112 | 445 | return False | ||
1113 | 446 | if self.api_version == other.api_version and \ | ||
1114 | 447 | self._ops_equal(other): | ||
1115 | 448 | return True | ||
1116 | 449 | else: | ||
1117 | 450 | return False | ||
1118 | 451 | |||
1119 | 452 | def __ne__(self, other): | ||
1120 | 453 | return not self.__eq__(other) | ||
1121 | 425 | 454 | ||
1122 | 426 | 455 | ||
1123 | 427 | class CephBrokerRsp(object): | 456 | class CephBrokerRsp(object): |
1124 | @@ -431,14 +460,198 @@ | |||
1125 | 431 | 460 | ||
1126 | 432 | The API is versioned and defaults to version 1. | 461 | The API is versioned and defaults to version 1. |
1127 | 433 | """ | 462 | """ |
1128 | 463 | |||
1129 | 434 | def __init__(self, encoded_rsp): | 464 | def __init__(self, encoded_rsp): |
1130 | 435 | self.api_version = None | 465 | self.api_version = None |
1131 | 436 | self.rsp = json.loads(encoded_rsp) | 466 | self.rsp = json.loads(encoded_rsp) |
1132 | 437 | 467 | ||
1133 | 438 | @property | 468 | @property |
1134 | 469 | def request_id(self): | ||
1135 | 470 | return self.rsp.get('request-id') | ||
1136 | 471 | |||
1137 | 472 | @property | ||
1138 | 439 | def exit_code(self): | 473 | def exit_code(self): |
1139 | 440 | return self.rsp.get('exit-code') | 474 | return self.rsp.get('exit-code') |
1140 | 441 | 475 | ||
1141 | 442 | @property | 476 | @property |
1142 | 443 | def exit_msg(self): | 477 | def exit_msg(self): |
1143 | 444 | return self.rsp.get('stderr') | 478 | return self.rsp.get('stderr') |
1144 | 479 | |||
1145 | 480 | |||
1146 | 481 | # Ceph Broker Conversation: | ||
1147 | 482 | # If a charm needs an action to be taken by ceph it can create a CephBrokerRq | ||
1148 | 483 | # and send that request to ceph via the ceph relation. The CephBrokerRq has a | ||
1149 | 484 | # unique id so that the client can identity which CephBrokerRsp is associated | ||
1150 | 485 | # with the request. Ceph will also respond to each client unit individually | ||
1151 | 486 | # creating a response key per client unit eg glance/0 will get a CephBrokerRsp | ||
1152 | 487 | # via key broker-rsp-glance-0 | ||
1153 | 488 | # | ||
1154 | 489 | # To use this the charm can just do something like: | ||
1155 | 490 | # | ||
1156 | 491 | # from charmhelpers.contrib.storage.linux.ceph import ( | ||
1157 | 492 | # send_request_if_needed, | ||
1158 | 493 | # is_request_complete, | ||
1159 | 494 | # CephBrokerRq, | ||
1160 | 495 | # ) | ||
1161 | 496 | # | ||
1162 | 497 | # @hooks.hook('ceph-relation-changed') | ||
1163 | 498 | # def ceph_changed(): | ||
1164 | 499 | # rq = CephBrokerRq() | ||
1165 | 500 | # rq.add_op_create_pool(name='poolname', replica_count=3) | ||
1166 | 501 | # | ||
1167 | 502 | # if is_request_complete(rq): | ||
1168 | 503 | # <Request complete actions> | ||
1169 | 504 | # else: | ||
1170 | 505 | # send_request_if_needed(get_ceph_request()) | ||
1171 | 506 | # | ||
1172 | 507 | # CephBrokerRq and CephBrokerRsp are serialized into JSON. Below is an example | ||
1173 | 508 | # of glance having sent a request to ceph which ceph has successfully processed | ||
1174 | 509 | # 'ceph:8': { | ||
1175 | 510 | # 'ceph/0': { | ||
1176 | 511 | # 'auth': 'cephx', | ||
1177 | 512 | # 'broker-rsp-glance-0': '{"request-id": "0bc7dc54", "exit-code": 0}', | ||
1178 | 513 | # 'broker_rsp': '{"request-id": "0da543b8", "exit-code": 0}', | ||
1179 | 514 | # 'ceph-public-address': '10.5.44.103', | ||
1180 | 515 | # 'key': 'AQCLDttVuHXINhAAvI144CB09dYchhHyTUY9BQ==', | ||
1181 | 516 | # 'private-address': '10.5.44.103', | ||
1182 | 517 | # }, | ||
1183 | 518 | # 'glance/0': { | ||
1184 | 519 | # 'broker_req': ('{"api-version": 1, "request-id": "0bc7dc54", ' | ||
1185 | 520 | # '"ops": [{"replicas": 3, "name": "glance", ' | ||
1186 | 521 | # '"op": "create-pool"}]}'), | ||
1187 | 522 | # 'private-address': '10.5.44.109', | ||
1188 | 523 | # }, | ||
1189 | 524 | # } | ||
1190 | 525 | |||
1191 | 526 | def get_previous_request(rid): | ||
1192 | 527 | """Return the last ceph broker request sent on a given relation | ||
1193 | 528 | |||
1194 | 529 | @param rid: Relation id to query for request | ||
1195 | 530 | """ | ||
1196 | 531 | request = None | ||
1197 | 532 | broker_req = relation_get(attribute='broker_req', rid=rid, | ||
1198 | 533 | unit=local_unit()) | ||
1199 | 534 | if broker_req: | ||
1200 | 535 | request_data = json.loads(broker_req) | ||
1201 | 536 | request = CephBrokerRq(api_version=request_data['api-version'], | ||
1202 | 537 | request_id=request_data['request-id']) | ||
1203 | 538 | request.set_ops(request_data['ops']) | ||
1204 | 539 | |||
1205 | 540 | return request | ||
1206 | 541 | |||
1207 | 542 | |||
1208 | 543 | def get_request_states(request): | ||
1209 | 544 | """Return a dict of requests per relation id with their corresponding | ||
1210 | 545 | completion state. | ||
1211 | 546 | |||
1212 | 547 | This allows a charm, which has a request for ceph, to see whether there is | ||
1213 | 548 | an equivalent request already being processed and if so what state that | ||
1214 | 549 | request is in. | ||
1215 | 550 | |||
1216 | 551 | @param request: A CephBrokerRq object | ||
1217 | 552 | """ | ||
1218 | 553 | complete = [] | ||
1219 | 554 | requests = {} | ||
1220 | 555 | for rid in relation_ids('ceph'): | ||
1221 | 556 | complete = False | ||
1222 | 557 | previous_request = get_previous_request(rid) | ||
1223 | 558 | if request == previous_request: | ||
1224 | 559 | sent = True | ||
1225 | 560 | complete = is_request_complete_for_rid(previous_request, rid) | ||
1226 | 561 | else: | ||
1227 | 562 | sent = False | ||
1228 | 563 | complete = False | ||
1229 | 564 | |||
1230 | 565 | requests[rid] = { | ||
1231 | 566 | 'sent': sent, | ||
1232 | 567 | 'complete': complete, | ||
1233 | 568 | } | ||
1234 | 569 | |||
1235 | 570 | return requests | ||
1236 | 571 | |||
1237 | 572 | |||
1238 | 573 | def is_request_sent(request): | ||
1239 | 574 | """Check to see if a functionally equivalent request has already been sent | ||
1240 | 575 | |||
1241 | 576 | Returns True if a similair request has been sent | ||
1242 | 577 | |||
1243 | 578 | @param request: A CephBrokerRq object | ||
1244 | 579 | """ | ||
1245 | 580 | states = get_request_states(request) | ||
1246 | 581 | for rid in states.keys(): | ||
1247 | 582 | if not states[rid]['sent']: | ||
1248 | 583 | return False | ||
1249 | 584 | |||
1250 | 585 | return True | ||
1251 | 586 | |||
1252 | 587 | |||
1253 | 588 | def is_request_complete(request): | ||
1254 | 589 | """Check to see if a functionally equivalent request has already been | ||
1255 | 590 | completed | ||
1256 | 591 | |||
1257 | 592 | Returns True if a similair request has been completed | ||
1258 | 593 | |||
1259 | 594 | @param request: A CephBrokerRq object | ||
1260 | 595 | """ | ||
1261 | 596 | states = get_request_states(request) | ||
1262 | 597 | for rid in states.keys(): | ||
1263 | 598 | if not states[rid]['complete']: | ||
1264 | 599 | return False | ||
1265 | 600 | |||
1266 | 601 | return True | ||
1267 | 602 | |||
1268 | 603 | |||
1269 | 604 | def is_request_complete_for_rid(request, rid): | ||
1270 | 605 | """Check if a given request has been completed on the given relation | ||
1271 | 606 | |||
1272 | 607 | @param request: A CephBrokerRq object | ||
1273 | 608 | @param rid: Relation ID | ||
1274 | 609 | """ | ||
1275 | 610 | broker_key = get_broker_rsp_key() | ||
1276 | 611 | for unit in related_units(rid): | ||
1277 | 612 | rdata = relation_get(rid=rid, unit=unit) | ||
1278 | 613 | if rdata.get(broker_key): | ||
1279 | 614 | rsp = CephBrokerRsp(rdata.get(broker_key)) | ||
1280 | 615 | if rsp.request_id == request.request_id: | ||
1281 | 616 | if not rsp.exit_code: | ||
1282 | 617 | return True | ||
1283 | 618 | else: | ||
1284 | 619 | # The remote unit sent no reply targeted at this unit so either the | ||
1285 | 620 | # remote ceph cluster does not support unit targeted replies or it | ||
1286 | 621 | # has not processed our request yet. | ||
1287 | 622 | if rdata.get('broker_rsp'): | ||
1288 | 623 | request_data = json.loads(rdata['broker_rsp']) | ||
1289 | 624 | if request_data.get('request-id'): | ||
1290 | 625 | log('Ignoring legacy broker_rsp without unit key as remote ' | ||
1291 | 626 | 'service supports unit specific replies', level=DEBUG) | ||
1292 | 627 | else: | ||
1293 | 628 | log('Using legacy broker_rsp as remote service does not ' | ||
1294 | 629 | 'supports unit specific replies', level=DEBUG) | ||
1295 | 630 | rsp = CephBrokerRsp(rdata['broker_rsp']) | ||
1296 | 631 | if not rsp.exit_code: | ||
1297 | 632 | return True | ||
1298 | 633 | |||
1299 | 634 | return False | ||
1300 | 635 | |||
1301 | 636 | |||
1302 | 637 | def get_broker_rsp_key(): | ||
1303 | 638 | """Return broker response key for this unit | ||
1304 | 639 | |||
1305 | 640 | This is the key that ceph is going to use to pass request status | ||
1306 | 641 | information back to this unit | ||
1307 | 642 | """ | ||
1308 | 643 | return 'broker-rsp-' + local_unit().replace('/', '-') | ||
1309 | 644 | |||
1310 | 645 | |||
1311 | 646 | def send_request_if_needed(request): | ||
1312 | 647 | """Send broker request if an equivalent request has not already been sent | ||
1313 | 648 | |||
1314 | 649 | @param request: A CephBrokerRq object | ||
1315 | 650 | """ | ||
1316 | 651 | if is_request_sent(request): | ||
1317 | 652 | log('Request already sent but not complete, not sending new request', | ||
1318 | 653 | level=DEBUG) | ||
1319 | 654 | else: | ||
1320 | 655 | for rid in relation_ids('ceph'): | ||
1321 | 656 | log('Sending request {}'.format(request.request_id), level=DEBUG) | ||
1322 | 657 | relation_set(relation_id=rid, broker_req=request.request) | ||
1323 | 445 | 658 | ||
1324 | === modified file 'charmhelpers/core/hookenv.py' | |||
1325 | --- charmhelpers/core/hookenv.py 2015-09-03 09:40:28 +0000 | |||
1326 | +++ charmhelpers/core/hookenv.py 2015-09-25 14:42:33 +0000 | |||
1327 | @@ -623,6 +623,38 @@ | |||
1328 | 623 | return unit_get('private-address') | 623 | return unit_get('private-address') |
1329 | 624 | 624 | ||
1330 | 625 | 625 | ||
1331 | 626 | @cached | ||
1332 | 627 | def storage_get(attribute="", storage_id=""): | ||
1333 | 628 | """Get storage attributes""" | ||
1334 | 629 | _args = ['storage-get', '--format=json'] | ||
1335 | 630 | if storage_id: | ||
1336 | 631 | _args.extend(('-s', storage_id)) | ||
1337 | 632 | if attribute: | ||
1338 | 633 | _args.append(attribute) | ||
1339 | 634 | try: | ||
1340 | 635 | return json.loads(subprocess.check_output(_args).decode('UTF-8')) | ||
1341 | 636 | except ValueError: | ||
1342 | 637 | return None | ||
1343 | 638 | |||
1344 | 639 | |||
1345 | 640 | @cached | ||
1346 | 641 | def storage_list(storage_name=""): | ||
1347 | 642 | """List the storage IDs for the unit""" | ||
1348 | 643 | _args = ['storage-list', '--format=json'] | ||
1349 | 644 | if storage_name: | ||
1350 | 645 | _args.append(storage_name) | ||
1351 | 646 | try: | ||
1352 | 647 | return json.loads(subprocess.check_output(_args).decode('UTF-8')) | ||
1353 | 648 | except ValueError: | ||
1354 | 649 | return None | ||
1355 | 650 | except OSError as e: | ||
1356 | 651 | import errno | ||
1357 | 652 | if e.errno == errno.ENOENT: | ||
1358 | 653 | # storage-list does not exist | ||
1359 | 654 | return [] | ||
1360 | 655 | raise | ||
1361 | 656 | |||
1362 | 657 | |||
1363 | 626 | class UnregisteredHookError(Exception): | 658 | class UnregisteredHookError(Exception): |
1364 | 627 | """Raised when an undefined hook is called""" | 659 | """Raised when an undefined hook is called""" |
1365 | 628 | pass | 660 | pass |
1366 | 629 | 661 | ||
1367 | === modified file 'charmhelpers/core/host.py' | |||
1368 | --- charmhelpers/core/host.py 2015-08-24 08:56:12 +0000 | |||
1369 | +++ charmhelpers/core/host.py 2015-09-25 14:42:33 +0000 | |||
1370 | @@ -63,32 +63,48 @@ | |||
1371 | 63 | return service_result | 63 | return service_result |
1372 | 64 | 64 | ||
1373 | 65 | 65 | ||
1375 | 66 | def service_pause(service_name, init_dir=None): | 66 | def service_pause(service_name, init_dir="/etc/init", initd_dir="/etc/init.d"): |
1376 | 67 | """Pause a system service. | 67 | """Pause a system service. |
1377 | 68 | 68 | ||
1378 | 69 | Stop it, and prevent it from starting again at boot.""" | 69 | Stop it, and prevent it from starting again at boot.""" |
1379 | 70 | if init_dir is None: | ||
1380 | 71 | init_dir = "/etc/init" | ||
1381 | 72 | stopped = service_stop(service_name) | 70 | stopped = service_stop(service_name) |
1387 | 73 | # XXX: Support systemd too | 71 | upstart_file = os.path.join(init_dir, "{}.conf".format(service_name)) |
1388 | 74 | override_path = os.path.join( | 72 | sysv_file = os.path.join(initd_dir, service_name) |
1389 | 75 | init_dir, '{}.override'.format(service_name)) | 73 | if os.path.exists(upstart_file): |
1390 | 76 | with open(override_path, 'w') as fh: | 74 | override_path = os.path.join( |
1391 | 77 | fh.write("manual\n") | 75 | init_dir, '{}.override'.format(service_name)) |
1392 | 76 | with open(override_path, 'w') as fh: | ||
1393 | 77 | fh.write("manual\n") | ||
1394 | 78 | elif os.path.exists(sysv_file): | ||
1395 | 79 | subprocess.check_call(["update-rc.d", service_name, "disable"]) | ||
1396 | 80 | else: | ||
1397 | 81 | # XXX: Support SystemD too | ||
1398 | 82 | raise ValueError( | ||
1399 | 83 | "Unable to detect {0} as either Upstart {1} or SysV {2}".format( | ||
1400 | 84 | service_name, upstart_file, sysv_file)) | ||
1401 | 78 | return stopped | 85 | return stopped |
1402 | 79 | 86 | ||
1403 | 80 | 87 | ||
1405 | 81 | def service_resume(service_name, init_dir=None): | 88 | def service_resume(service_name, init_dir="/etc/init", |
1406 | 89 | initd_dir="/etc/init.d"): | ||
1407 | 82 | """Resume a system service. | 90 | """Resume a system service. |
1408 | 83 | 91 | ||
1409 | 84 | Reenable starting again at boot. Start the service""" | 92 | Reenable starting again at boot. Start the service""" |
1417 | 85 | # XXX: Support systemd too | 93 | upstart_file = os.path.join(init_dir, "{}.conf".format(service_name)) |
1418 | 86 | if init_dir is None: | 94 | sysv_file = os.path.join(initd_dir, service_name) |
1419 | 87 | init_dir = "/etc/init" | 95 | if os.path.exists(upstart_file): |
1420 | 88 | override_path = os.path.join( | 96 | override_path = os.path.join( |
1421 | 89 | init_dir, '{}.override'.format(service_name)) | 97 | init_dir, '{}.override'.format(service_name)) |
1422 | 90 | if os.path.exists(override_path): | 98 | if os.path.exists(override_path): |
1423 | 91 | os.unlink(override_path) | 99 | os.unlink(override_path) |
1424 | 100 | elif os.path.exists(sysv_file): | ||
1425 | 101 | subprocess.check_call(["update-rc.d", service_name, "enable"]) | ||
1426 | 102 | else: | ||
1427 | 103 | # XXX: Support SystemD too | ||
1428 | 104 | raise ValueError( | ||
1429 | 105 | "Unable to detect {0} as either Upstart {1} or SysV {2}".format( | ||
1430 | 106 | service_name, upstart_file, sysv_file)) | ||
1431 | 107 | |||
1432 | 92 | started = service_start(service_name) | 108 | started = service_start(service_name) |
1433 | 93 | return started | 109 | return started |
1434 | 94 | 110 | ||
1435 | 95 | 111 | ||
1436 | === modified file 'charmhelpers/core/hugepage.py' | |||
1437 | --- charmhelpers/core/hugepage.py 2015-08-19 13:49:05 +0000 | |||
1438 | +++ charmhelpers/core/hugepage.py 2015-09-25 14:42:33 +0000 | |||
1439 | @@ -25,11 +25,13 @@ | |||
1440 | 25 | fstab_mount, | 25 | fstab_mount, |
1441 | 26 | mkdir, | 26 | mkdir, |
1442 | 27 | ) | 27 | ) |
1443 | 28 | from charmhelpers.core.strutils import bytes_from_string | ||
1444 | 29 | from subprocess import check_output | ||
1445 | 28 | 30 | ||
1446 | 29 | 31 | ||
1447 | 30 | def hugepage_support(user, group='hugetlb', nr_hugepages=256, | 32 | def hugepage_support(user, group='hugetlb', nr_hugepages=256, |
1448 | 31 | max_map_count=65536, mnt_point='/run/hugepages/kvm', | 33 | max_map_count=65536, mnt_point='/run/hugepages/kvm', |
1450 | 32 | pagesize='2MB', mount=True): | 34 | pagesize='2MB', mount=True, set_shmmax=False): |
1451 | 33 | """Enable hugepages on system. | 35 | """Enable hugepages on system. |
1452 | 34 | 36 | ||
1453 | 35 | Args: | 37 | Args: |
1454 | @@ -49,6 +51,11 @@ | |||
1455 | 49 | 'vm.max_map_count': max_map_count, | 51 | 'vm.max_map_count': max_map_count, |
1456 | 50 | 'vm.hugetlb_shm_group': gid, | 52 | 'vm.hugetlb_shm_group': gid, |
1457 | 51 | } | 53 | } |
1458 | 54 | if set_shmmax: | ||
1459 | 55 | shmmax_current = int(check_output(['sysctl', '-n', 'kernel.shmmax'])) | ||
1460 | 56 | shmmax_minsize = bytes_from_string(pagesize) * nr_hugepages | ||
1461 | 57 | if shmmax_minsize > shmmax_current: | ||
1462 | 58 | sysctl_settings['kernel.shmmax'] = shmmax_minsize | ||
1463 | 52 | sysctl.create(yaml.dump(sysctl_settings), '/etc/sysctl.d/10-hugepage.conf') | 59 | sysctl.create(yaml.dump(sysctl_settings), '/etc/sysctl.d/10-hugepage.conf') |
1464 | 53 | mkdir(mnt_point, owner='root', group='root', perms=0o755, force=False) | 60 | mkdir(mnt_point, owner='root', group='root', perms=0o755, force=False) |
1465 | 54 | lfstab = fstab.Fstab() | 61 | lfstab = fstab.Fstab() |
1466 | 55 | 62 | ||
1467 | === modified file 'charmhelpers/core/strutils.py' | |||
1468 | --- charmhelpers/core/strutils.py 2015-04-16 19:55:16 +0000 | |||
1469 | +++ charmhelpers/core/strutils.py 2015-09-25 14:42:33 +0000 | |||
1470 | @@ -18,6 +18,7 @@ | |||
1471 | 18 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | 18 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. |
1472 | 19 | 19 | ||
1473 | 20 | import six | 20 | import six |
1474 | 21 | import re | ||
1475 | 21 | 22 | ||
1476 | 22 | 23 | ||
1477 | 23 | def bool_from_string(value): | 24 | def bool_from_string(value): |
1478 | @@ -40,3 +41,32 @@ | |||
1479 | 40 | 41 | ||
1480 | 41 | msg = "Unable to interpret string value '%s' as boolean" % (value) | 42 | msg = "Unable to interpret string value '%s' as boolean" % (value) |
1481 | 42 | raise ValueError(msg) | 43 | raise ValueError(msg) |
1482 | 44 | |||
1483 | 45 | |||
1484 | 46 | def bytes_from_string(value): | ||
1485 | 47 | """Interpret human readable string value as bytes. | ||
1486 | 48 | |||
1487 | 49 | Returns int | ||
1488 | 50 | """ | ||
1489 | 51 | BYTE_POWER = { | ||
1490 | 52 | 'K': 1, | ||
1491 | 53 | 'KB': 1, | ||
1492 | 54 | 'M': 2, | ||
1493 | 55 | 'MB': 2, | ||
1494 | 56 | 'G': 3, | ||
1495 | 57 | 'GB': 3, | ||
1496 | 58 | 'T': 4, | ||
1497 | 59 | 'TB': 4, | ||
1498 | 60 | 'P': 5, | ||
1499 | 61 | 'PB': 5, | ||
1500 | 62 | } | ||
1501 | 63 | if isinstance(value, six.string_types): | ||
1502 | 64 | value = six.text_type(value) | ||
1503 | 65 | else: | ||
1504 | 66 | msg = "Unable to interpret non-string value '%s' as boolean" % (value) | ||
1505 | 67 | raise ValueError(msg) | ||
1506 | 68 | matches = re.match("([0-9]+)([a-zA-Z]+)", value) | ||
1507 | 69 | if not matches: | ||
1508 | 70 | msg = "Unable to interpret string value '%s' as bytes" % (value) | ||
1509 | 71 | raise ValueError(msg) | ||
1510 | 72 | return int(matches.group(1)) * (1024 ** BYTE_POWER[matches.group(2)]) | ||
1511 | 43 | 73 | ||
1512 | === modified file 'tests/charmhelpers/contrib/amulet/deployment.py' | |||
1513 | --- tests/charmhelpers/contrib/amulet/deployment.py 2015-03-11 11:45:09 +0000 | |||
1514 | +++ tests/charmhelpers/contrib/amulet/deployment.py 2015-09-25 14:42:33 +0000 | |||
1515 | @@ -51,7 +51,8 @@ | |||
1516 | 51 | if 'units' not in this_service: | 51 | if 'units' not in this_service: |
1517 | 52 | this_service['units'] = 1 | 52 | this_service['units'] = 1 |
1518 | 53 | 53 | ||
1520 | 54 | self.d.add(this_service['name'], units=this_service['units']) | 54 | self.d.add(this_service['name'], units=this_service['units'], |
1521 | 55 | constraints=this_service.get('constraints')) | ||
1522 | 55 | 56 | ||
1523 | 56 | for svc in other_services: | 57 | for svc in other_services: |
1524 | 57 | if 'location' in svc: | 58 | if 'location' in svc: |
1525 | @@ -64,7 +65,8 @@ | |||
1526 | 64 | if 'units' not in svc: | 65 | if 'units' not in svc: |
1527 | 65 | svc['units'] = 1 | 66 | svc['units'] = 1 |
1528 | 66 | 67 | ||
1530 | 67 | self.d.add(svc['name'], charm=branch_location, units=svc['units']) | 68 | self.d.add(svc['name'], charm=branch_location, units=svc['units'], |
1531 | 69 | constraints=svc.get('constraints')) | ||
1532 | 68 | 70 | ||
1533 | 69 | def _add_relations(self, relations): | 71 | def _add_relations(self, relations): |
1534 | 70 | """Add all of the relations for the services.""" | 72 | """Add all of the relations for the services.""" |
1535 | 71 | 73 | ||
1536 | === modified file 'tests/charmhelpers/contrib/amulet/utils.py' | |||
1537 | --- tests/charmhelpers/contrib/amulet/utils.py 2015-09-02 02:20:28 +0000 | |||
1538 | +++ tests/charmhelpers/contrib/amulet/utils.py 2015-09-25 14:42:33 +0000 | |||
1539 | @@ -19,9 +19,11 @@ | |||
1540 | 19 | import logging | 19 | import logging |
1541 | 20 | import os | 20 | import os |
1542 | 21 | import re | 21 | import re |
1543 | 22 | import socket | ||
1544 | 22 | import subprocess | 23 | import subprocess |
1545 | 23 | import sys | 24 | import sys |
1546 | 24 | import time | 25 | import time |
1547 | 26 | import uuid | ||
1548 | 25 | 27 | ||
1549 | 26 | import amulet | 28 | import amulet |
1550 | 27 | import distro_info | 29 | import distro_info |
1551 | @@ -324,7 +326,7 @@ | |||
1552 | 324 | 326 | ||
1553 | 325 | def service_restarted_since(self, sentry_unit, mtime, service, | 327 | def service_restarted_since(self, sentry_unit, mtime, service, |
1554 | 326 | pgrep_full=None, sleep_time=20, | 328 | pgrep_full=None, sleep_time=20, |
1556 | 327 | retry_count=2, retry_sleep_time=30): | 329 | retry_count=30, retry_sleep_time=10): |
1557 | 328 | """Check if service was been started after a given time. | 330 | """Check if service was been started after a given time. |
1558 | 329 | 331 | ||
1559 | 330 | Args: | 332 | Args: |
1560 | @@ -332,8 +334,9 @@ | |||
1561 | 332 | mtime (float): The epoch time to check against | 334 | mtime (float): The epoch time to check against |
1562 | 333 | service (string): service name to look for in process table | 335 | service (string): service name to look for in process table |
1563 | 334 | pgrep_full: [Deprecated] Use full command line search mode with pgrep | 336 | pgrep_full: [Deprecated] Use full command line search mode with pgrep |
1566 | 335 | sleep_time (int): Seconds to sleep before looking for process | 337 | sleep_time (int): Initial sleep time (s) before looking for file |
1567 | 336 | retry_count (int): If service is not found, how many times to retry | 338 | retry_sleep_time (int): Time (s) to sleep between retries |
1568 | 339 | retry_count (int): If file is not found, how many times to retry | ||
1569 | 337 | 340 | ||
1570 | 338 | Returns: | 341 | Returns: |
1571 | 339 | bool: True if service found and its start time it newer than mtime, | 342 | bool: True if service found and its start time it newer than mtime, |
1572 | @@ -357,11 +360,12 @@ | |||
1573 | 357 | pgrep_full) | 360 | pgrep_full) |
1574 | 358 | self.log.debug('Attempt {} to get {} proc start time on {} ' | 361 | self.log.debug('Attempt {} to get {} proc start time on {} ' |
1575 | 359 | 'OK'.format(tries, service, unit_name)) | 362 | 'OK'.format(tries, service, unit_name)) |
1577 | 360 | except IOError: | 363 | except IOError as e: |
1578 | 361 | # NOTE(beisner) - race avoidance, proc may not exist yet. | 364 | # NOTE(beisner) - race avoidance, proc may not exist yet. |
1579 | 362 | # https://bugs.launchpad.net/charm-helpers/+bug/1474030 | 365 | # https://bugs.launchpad.net/charm-helpers/+bug/1474030 |
1580 | 363 | self.log.debug('Attempt {} to get {} proc start time on {} ' | 366 | self.log.debug('Attempt {} to get {} proc start time on {} ' |
1582 | 364 | 'failed'.format(tries, service, unit_name)) | 367 | 'failed\n{}'.format(tries, service, |
1583 | 368 | unit_name, e)) | ||
1584 | 365 | time.sleep(retry_sleep_time) | 369 | time.sleep(retry_sleep_time) |
1585 | 366 | tries += 1 | 370 | tries += 1 |
1586 | 367 | 371 | ||
1587 | @@ -381,38 +385,62 @@ | |||
1588 | 381 | return False | 385 | return False |
1589 | 382 | 386 | ||
1590 | 383 | def config_updated_since(self, sentry_unit, filename, mtime, | 387 | def config_updated_since(self, sentry_unit, filename, mtime, |
1592 | 384 | sleep_time=20): | 388 | sleep_time=20, retry_count=30, |
1593 | 389 | retry_sleep_time=10): | ||
1594 | 385 | """Check if file was modified after a given time. | 390 | """Check if file was modified after a given time. |
1595 | 386 | 391 | ||
1596 | 387 | Args: | 392 | Args: |
1597 | 388 | sentry_unit (sentry): The sentry unit to check the file mtime on | 393 | sentry_unit (sentry): The sentry unit to check the file mtime on |
1598 | 389 | filename (string): The file to check mtime of | 394 | filename (string): The file to check mtime of |
1599 | 390 | mtime (float): The epoch time to check against | 395 | mtime (float): The epoch time to check against |
1601 | 391 | sleep_time (int): Seconds to sleep before looking for process | 396 | sleep_time (int): Initial sleep time (s) before looking for file |
1602 | 397 | retry_sleep_time (int): Time (s) to sleep between retries | ||
1603 | 398 | retry_count (int): If file is not found, how many times to retry | ||
1604 | 392 | 399 | ||
1605 | 393 | Returns: | 400 | Returns: |
1606 | 394 | bool: True if file was modified more recently than mtime, False if | 401 | bool: True if file was modified more recently than mtime, False if |
1608 | 395 | file was modified before mtime, | 402 | file was modified before mtime, or if file not found. |
1609 | 396 | """ | 403 | """ |
1610 | 397 | self.log.debug('Checking that %s file updated since ' | ||
1611 | 398 | '%s' % (filename, mtime)) | ||
1612 | 399 | unit_name = sentry_unit.info['unit_name'] | 404 | unit_name = sentry_unit.info['unit_name'] |
1613 | 405 | self.log.debug('Checking that %s updated since %s on ' | ||
1614 | 406 | '%s' % (filename, mtime, unit_name)) | ||
1615 | 400 | time.sleep(sleep_time) | 407 | time.sleep(sleep_time) |
1617 | 401 | file_mtime = self._get_file_mtime(sentry_unit, filename) | 408 | file_mtime = None |
1618 | 409 | tries = 0 | ||
1619 | 410 | while tries <= retry_count and not file_mtime: | ||
1620 | 411 | try: | ||
1621 | 412 | file_mtime = self._get_file_mtime(sentry_unit, filename) | ||
1622 | 413 | self.log.debug('Attempt {} to get {} file mtime on {} ' | ||
1623 | 414 | 'OK'.format(tries, filename, unit_name)) | ||
1624 | 415 | except IOError as e: | ||
1625 | 416 | # NOTE(beisner) - race avoidance, file may not exist yet. | ||
1626 | 417 | # https://bugs.launchpad.net/charm-helpers/+bug/1474030 | ||
1627 | 418 | self.log.debug('Attempt {} to get {} file mtime on {} ' | ||
1628 | 419 | 'failed\n{}'.format(tries, filename, | ||
1629 | 420 | unit_name, e)) | ||
1630 | 421 | time.sleep(retry_sleep_time) | ||
1631 | 422 | tries += 1 | ||
1632 | 423 | |||
1633 | 424 | if not file_mtime: | ||
1634 | 425 | self.log.warn('Could not determine file mtime, assuming ' | ||
1635 | 426 | 'file does not exist') | ||
1636 | 427 | return False | ||
1637 | 428 | |||
1638 | 402 | if file_mtime >= mtime: | 429 | if file_mtime >= mtime: |
1639 | 403 | self.log.debug('File mtime is newer than provided mtime ' | 430 | self.log.debug('File mtime is newer than provided mtime ' |
1642 | 404 | '(%s >= %s) on %s (OK)' % (file_mtime, mtime, | 431 | '(%s >= %s) on %s (OK)' % (file_mtime, |
1643 | 405 | unit_name)) | 432 | mtime, unit_name)) |
1644 | 406 | return True | 433 | return True |
1645 | 407 | else: | 434 | else: |
1648 | 408 | self.log.warn('File mtime %s is older than provided mtime %s' | 435 | self.log.warn('File mtime is older than provided mtime' |
1649 | 409 | % (file_mtime, mtime)) | 436 | '(%s < on %s) on %s' % (file_mtime, |
1650 | 437 | mtime, unit_name)) | ||
1651 | 410 | return False | 438 | return False |
1652 | 411 | 439 | ||
1653 | 412 | def validate_service_config_changed(self, sentry_unit, mtime, service, | 440 | def validate_service_config_changed(self, sentry_unit, mtime, service, |
1654 | 413 | filename, pgrep_full=None, | 441 | filename, pgrep_full=None, |
1657 | 414 | sleep_time=20, retry_count=2, | 442 | sleep_time=20, retry_count=30, |
1658 | 415 | retry_sleep_time=30): | 443 | retry_sleep_time=10): |
1659 | 416 | """Check service and file were updated after mtime | 444 | """Check service and file were updated after mtime |
1660 | 417 | 445 | ||
1661 | 418 | Args: | 446 | Args: |
1662 | @@ -457,7 +485,9 @@ | |||
1663 | 457 | sentry_unit, | 485 | sentry_unit, |
1664 | 458 | filename, | 486 | filename, |
1665 | 459 | mtime, | 487 | mtime, |
1667 | 460 | sleep_time=0) | 488 | sleep_time=sleep_time, |
1668 | 489 | retry_count=retry_count, | ||
1669 | 490 | retry_sleep_time=retry_sleep_time) | ||
1670 | 461 | 491 | ||
1671 | 462 | return service_restart and config_update | 492 | return service_restart and config_update |
1672 | 463 | 493 | ||
1673 | @@ -476,7 +506,6 @@ | |||
1674 | 476 | """Return a list of all Ubuntu releases in order of release.""" | 506 | """Return a list of all Ubuntu releases in order of release.""" |
1675 | 477 | _d = distro_info.UbuntuDistroInfo() | 507 | _d = distro_info.UbuntuDistroInfo() |
1676 | 478 | _release_list = _d.all | 508 | _release_list = _d.all |
1677 | 479 | self.log.debug('Ubuntu release list: {}'.format(_release_list)) | ||
1678 | 480 | return _release_list | 509 | return _release_list |
1679 | 481 | 510 | ||
1680 | 482 | def file_to_url(self, file_rel_path): | 511 | def file_to_url(self, file_rel_path): |
1681 | @@ -616,6 +645,142 @@ | |||
1682 | 616 | 645 | ||
1683 | 617 | return None | 646 | return None |
1684 | 618 | 647 | ||
1685 | 648 | def validate_sectionless_conf(self, file_contents, expected): | ||
1686 | 649 | """A crude conf parser. Useful to inspect configuration files which | ||
1687 | 650 | do not have section headers (as would be necessary in order to use | ||
1688 | 651 | the configparser). Such as openstack-dashboard or rabbitmq confs.""" | ||
1689 | 652 | for line in file_contents.split('\n'): | ||
1690 | 653 | if '=' in line: | ||
1691 | 654 | args = line.split('=') | ||
1692 | 655 | if len(args) <= 1: | ||
1693 | 656 | continue | ||
1694 | 657 | key = args[0].strip() | ||
1695 | 658 | value = args[1].strip() | ||
1696 | 659 | if key in expected.keys(): | ||
1697 | 660 | if expected[key] != value: | ||
1698 | 661 | msg = ('Config mismatch. Expected, actual: {}, ' | ||
1699 | 662 | '{}'.format(expected[key], value)) | ||
1700 | 663 | amulet.raise_status(amulet.FAIL, msg=msg) | ||
1701 | 664 | |||
1702 | 665 | def get_unit_hostnames(self, units): | ||
1703 | 666 | """Return a dict of juju unit names to hostnames.""" | ||
1704 | 667 | host_names = {} | ||
1705 | 668 | for unit in units: | ||
1706 | 669 | host_names[unit.info['unit_name']] = \ | ||
1707 | 670 | str(unit.file_contents('/etc/hostname').strip()) | ||
1708 | 671 | self.log.debug('Unit host names: {}'.format(host_names)) | ||
1709 | 672 | return host_names | ||
1710 | 673 | |||
1711 | 674 | def run_cmd_unit(self, sentry_unit, cmd): | ||
1712 | 675 | """Run a command on a unit, return the output and exit code.""" | ||
1713 | 676 | output, code = sentry_unit.run(cmd) | ||
1714 | 677 | if code == 0: | ||
1715 | 678 | self.log.debug('{} `{}` command returned {} ' | ||
1716 | 679 | '(OK)'.format(sentry_unit.info['unit_name'], | ||
1717 | 680 | cmd, code)) | ||
1718 | 681 | else: | ||
1719 | 682 | msg = ('{} `{}` command returned {} ' | ||
1720 | 683 | '{}'.format(sentry_unit.info['unit_name'], | ||
1721 | 684 | cmd, code, output)) | ||
1722 | 685 | amulet.raise_status(amulet.FAIL, msg=msg) | ||
1723 | 686 | return str(output), code | ||
1724 | 687 | |||
1725 | 688 | def file_exists_on_unit(self, sentry_unit, file_name): | ||
1726 | 689 | """Check if a file exists on a unit.""" | ||
1727 | 690 | try: | ||
1728 | 691 | sentry_unit.file_stat(file_name) | ||
1729 | 692 | return True | ||
1730 | 693 | except IOError: | ||
1731 | 694 | return False | ||
1732 | 695 | except Exception as e: | ||
1733 | 696 | msg = 'Error checking file {}: {}'.format(file_name, e) | ||
1734 | 697 | amulet.raise_status(amulet.FAIL, msg=msg) | ||
1735 | 698 | |||
1736 | 699 | def file_contents_safe(self, sentry_unit, file_name, | ||
1737 | 700 | max_wait=60, fatal=False): | ||
1738 | 701 | """Get file contents from a sentry unit. Wrap amulet file_contents | ||
1739 | 702 | with retry logic to address races where a file checks as existing, | ||
1740 | 703 | but no longer exists by the time file_contents is called. | ||
1741 | 704 | Return None if file not found. Optionally raise if fatal is True.""" | ||
1742 | 705 | unit_name = sentry_unit.info['unit_name'] | ||
1743 | 706 | file_contents = False | ||
1744 | 707 | tries = 0 | ||
1745 | 708 | while not file_contents and tries < (max_wait / 4): | ||
1746 | 709 | try: | ||
1747 | 710 | file_contents = sentry_unit.file_contents(file_name) | ||
1748 | 711 | except IOError: | ||
1749 | 712 | self.log.debug('Attempt {} to open file {} from {} ' | ||
1750 | 713 | 'failed'.format(tries, file_name, | ||
1751 | 714 | unit_name)) | ||
1752 | 715 | time.sleep(4) | ||
1753 | 716 | tries += 1 | ||
1754 | 717 | |||
1755 | 718 | if file_contents: | ||
1756 | 719 | return file_contents | ||
1757 | 720 | elif not fatal: | ||
1758 | 721 | return None | ||
1759 | 722 | elif fatal: | ||
1760 | 723 | msg = 'Failed to get file contents from unit.' | ||
1761 | 724 | amulet.raise_status(amulet.FAIL, msg) | ||
1762 | 725 | |||
1763 | 726 | def port_knock_tcp(self, host="localhost", port=22, timeout=15): | ||
1764 | 727 | """Open a TCP socket to check for a listening sevice on a host. | ||
1765 | 728 | |||
1766 | 729 | :param host: host name or IP address, default to localhost | ||
1767 | 730 | :param port: TCP port number, default to 22 | ||
1768 | 731 | :param timeout: Connect timeout, default to 15 seconds | ||
1769 | 732 | :returns: True if successful, False if connect failed | ||
1770 | 733 | """ | ||
1771 | 734 | |||
1772 | 735 | # Resolve host name if possible | ||
1773 | 736 | try: | ||
1774 | 737 | connect_host = socket.gethostbyname(host) | ||
1775 | 738 | host_human = "{} ({})".format(connect_host, host) | ||
1776 | 739 | except socket.error as e: | ||
1777 | 740 | self.log.warn('Unable to resolve address: ' | ||
1778 | 741 | '{} ({}) Trying anyway!'.format(host, e)) | ||
1779 | 742 | connect_host = host | ||
1780 | 743 | host_human = connect_host | ||
1781 | 744 | |||
1782 | 745 | # Attempt socket connection | ||
1783 | 746 | try: | ||
1784 | 747 | knock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
1785 | 748 | knock.settimeout(timeout) | ||
1786 | 749 | knock.connect((connect_host, port)) | ||
1787 | 750 | knock.close() | ||
1788 | 751 | self.log.debug('Socket connect OK for host ' | ||
1789 | 752 | '{} on port {}.'.format(host_human, port)) | ||
1790 | 753 | return True | ||
1791 | 754 | except socket.error as e: | ||
1792 | 755 | self.log.debug('Socket connect FAIL for' | ||
1793 | 756 | ' {} port {} ({})'.format(host_human, port, e)) | ||
1794 | 757 | return False | ||
1795 | 758 | |||
1796 | 759 | def port_knock_units(self, sentry_units, port=22, | ||
1797 | 760 | timeout=15, expect_success=True): | ||
1798 | 761 | """Open a TCP socket to check for a listening sevice on each | ||
1799 | 762 | listed juju unit. | ||
1800 | 763 | |||
1801 | 764 | :param sentry_units: list of sentry unit pointers | ||
1802 | 765 | :param port: TCP port number, default to 22 | ||
1803 | 766 | :param timeout: Connect timeout, default to 15 seconds | ||
1804 | 767 | :expect_success: True by default, set False to invert logic | ||
1805 | 768 | :returns: None if successful, Failure message otherwise | ||
1806 | 769 | """ | ||
1807 | 770 | for unit in sentry_units: | ||
1808 | 771 | host = unit.info['public-address'] | ||
1809 | 772 | connected = self.port_knock_tcp(host, port, timeout) | ||
1810 | 773 | if not connected and expect_success: | ||
1811 | 774 | return 'Socket connect failed.' | ||
1812 | 775 | elif connected and not expect_success: | ||
1813 | 776 | return 'Socket connected unexpectedly.' | ||
1814 | 777 | |||
1815 | 778 | def get_uuid_epoch_stamp(self): | ||
1816 | 779 | """Returns a stamp string based on uuid4 and epoch time. Useful in | ||
1817 | 780 | generating test messages which need to be unique-ish.""" | ||
1818 | 781 | return '[{}-{}]'.format(uuid.uuid4(), time.time()) | ||
1819 | 782 | |||
1820 | 783 | # amulet juju action helpers: | ||
1821 | 619 | def run_action(self, unit_sentry, action, | 784 | def run_action(self, unit_sentry, action, |
1822 | 620 | _check_output=subprocess.check_output): | 785 | _check_output=subprocess.check_output): |
1823 | 621 | """Run the named action on a given unit sentry. | 786 | """Run the named action on a given unit sentry. |
1824 | @@ -642,3 +807,12 @@ | |||
1825 | 642 | output = _check_output(command, universal_newlines=True) | 807 | output = _check_output(command, universal_newlines=True) |
1826 | 643 | data = json.loads(output) | 808 | data = json.loads(output) |
1827 | 644 | return data.get(u"status") == "completed" | 809 | return data.get(u"status") == "completed" |
1828 | 810 | |||
1829 | 811 | def status_get(self, unit): | ||
1830 | 812 | """Return the current service status of this unit.""" | ||
1831 | 813 | raw_status, return_code = unit.run( | ||
1832 | 814 | "status-get --format=json --include-data") | ||
1833 | 815 | if return_code != 0: | ||
1834 | 816 | return ("unknown", "") | ||
1835 | 817 | status = json.loads(raw_status) | ||
1836 | 818 | return (status["status"], status["message"]) | ||
1837 | 645 | 819 | ||
1838 | === modified file 'tests/charmhelpers/contrib/openstack/amulet/deployment.py' | |||
1839 | --- tests/charmhelpers/contrib/openstack/amulet/deployment.py 2015-08-19 13:53:50 +0000 | |||
1840 | +++ tests/charmhelpers/contrib/openstack/amulet/deployment.py 2015-09-25 14:42:33 +0000 | |||
1841 | @@ -44,20 +44,31 @@ | |||
1842 | 44 | Determine if the local branch being tested is derived from its | 44 | Determine if the local branch being tested is derived from its |
1843 | 45 | stable or next (dev) branch, and based on this, use the corresonding | 45 | stable or next (dev) branch, and based on this, use the corresonding |
1844 | 46 | stable or next branches for the other_services.""" | 46 | stable or next branches for the other_services.""" |
1845 | 47 | |||
1846 | 48 | # Charms outside the lp:~openstack-charmers namespace | ||
1847 | 47 | base_charms = ['mysql', 'mongodb', 'nrpe'] | 49 | base_charms = ['mysql', 'mongodb', 'nrpe'] |
1848 | 48 | 50 | ||
1849 | 51 | # Force these charms to current series even when using an older series. | ||
1850 | 52 | # ie. Use trusty/nrpe even when series is precise, as the P charm | ||
1851 | 53 | # does not possess the necessary external master config and hooks. | ||
1852 | 54 | force_series_current = ['nrpe'] | ||
1853 | 55 | |||
1854 | 49 | if self.series in ['precise', 'trusty']: | 56 | if self.series in ['precise', 'trusty']: |
1855 | 50 | base_series = self.series | 57 | base_series = self.series |
1856 | 51 | else: | 58 | else: |
1857 | 52 | base_series = self.current_next | 59 | base_series = self.current_next |
1858 | 53 | 60 | ||
1861 | 54 | if self.stable: | 61 | for svc in other_services: |
1862 | 55 | for svc in other_services: | 62 | if svc['name'] in force_series_current: |
1863 | 63 | base_series = self.current_next | ||
1864 | 64 | # If a location has been explicitly set, use it | ||
1865 | 65 | if svc.get('location'): | ||
1866 | 66 | continue | ||
1867 | 67 | if self.stable: | ||
1868 | 56 | temp = 'lp:charms/{}/{}' | 68 | temp = 'lp:charms/{}/{}' |
1869 | 57 | svc['location'] = temp.format(base_series, | 69 | svc['location'] = temp.format(base_series, |
1870 | 58 | svc['name']) | 70 | svc['name']) |
1873 | 59 | else: | 71 | else: |
1872 | 60 | for svc in other_services: | ||
1874 | 61 | if svc['name'] in base_charms: | 72 | if svc['name'] in base_charms: |
1875 | 62 | temp = 'lp:charms/{}/{}' | 73 | temp = 'lp:charms/{}/{}' |
1876 | 63 | svc['location'] = temp.format(base_series, | 74 | svc['location'] = temp.format(base_series, |
1877 | @@ -66,6 +77,7 @@ | |||
1878 | 66 | temp = 'lp:~openstack-charmers/charms/{}/{}/next' | 77 | temp = 'lp:~openstack-charmers/charms/{}/{}/next' |
1879 | 67 | svc['location'] = temp.format(self.current_next, | 78 | svc['location'] = temp.format(self.current_next, |
1880 | 68 | svc['name']) | 79 | svc['name']) |
1881 | 80 | |||
1882 | 69 | return other_services | 81 | return other_services |
1883 | 70 | 82 | ||
1884 | 71 | def _add_services(self, this_service, other_services): | 83 | def _add_services(self, this_service, other_services): |
1885 | @@ -77,21 +89,23 @@ | |||
1886 | 77 | 89 | ||
1887 | 78 | services = other_services | 90 | services = other_services |
1888 | 79 | services.append(this_service) | 91 | services.append(this_service) |
1889 | 92 | |||
1890 | 93 | # Charms which should use the source config option | ||
1891 | 80 | use_source = ['mysql', 'mongodb', 'rabbitmq-server', 'ceph', | 94 | use_source = ['mysql', 'mongodb', 'rabbitmq-server', 'ceph', |
1892 | 81 | 'ceph-osd', 'ceph-radosgw'] | 95 | 'ceph-osd', 'ceph-radosgw'] |
1896 | 82 | # Most OpenStack subordinate charms do not expose an origin option | 96 | |
1897 | 83 | # as that is controlled by the principle. | 97 | # Charms which can not use openstack-origin, ie. many subordinates |
1898 | 84 | ignore = ['cinder-ceph', 'hacluster', 'neutron-openvswitch', 'nrpe'] | 98 | no_origin = ['cinder-ceph', 'hacluster', 'neutron-openvswitch', 'nrpe'] |
1899 | 85 | 99 | ||
1900 | 86 | if self.openstack: | 100 | if self.openstack: |
1901 | 87 | for svc in services: | 101 | for svc in services: |
1903 | 88 | if svc['name'] not in use_source + ignore: | 102 | if svc['name'] not in use_source + no_origin: |
1904 | 89 | config = {'openstack-origin': self.openstack} | 103 | config = {'openstack-origin': self.openstack} |
1905 | 90 | self.d.configure(svc['name'], config) | 104 | self.d.configure(svc['name'], config) |
1906 | 91 | 105 | ||
1907 | 92 | if self.source: | 106 | if self.source: |
1908 | 93 | for svc in services: | 107 | for svc in services: |
1910 | 94 | if svc['name'] in use_source and svc['name'] not in ignore: | 108 | if svc['name'] in use_source and svc['name'] not in no_origin: |
1911 | 95 | config = {'source': self.source} | 109 | config = {'source': self.source} |
1912 | 96 | self.d.configure(svc['name'], config) | 110 | self.d.configure(svc['name'], config) |
1913 | 97 | 111 | ||
1914 | 98 | 112 | ||
1915 | === modified file 'tests/charmhelpers/contrib/openstack/amulet/utils.py' | |||
1916 | --- tests/charmhelpers/contrib/openstack/amulet/utils.py 2015-07-01 04:03:20 +0000 | |||
1917 | +++ tests/charmhelpers/contrib/openstack/amulet/utils.py 2015-09-25 14:42:33 +0000 | |||
1918 | @@ -27,6 +27,7 @@ | |||
1919 | 27 | import heatclient.v1.client as heat_client | 27 | import heatclient.v1.client as heat_client |
1920 | 28 | import keystoneclient.v2_0 as keystone_client | 28 | import keystoneclient.v2_0 as keystone_client |
1921 | 29 | import novaclient.v1_1.client as nova_client | 29 | import novaclient.v1_1.client as nova_client |
1922 | 30 | import pika | ||
1923 | 30 | import swiftclient | 31 | import swiftclient |
1924 | 31 | 32 | ||
1925 | 32 | from charmhelpers.contrib.amulet.utils import ( | 33 | from charmhelpers.contrib.amulet.utils import ( |
1926 | @@ -602,3 +603,361 @@ | |||
1927 | 602 | self.log.debug('Ceph {} samples (OK): ' | 603 | self.log.debug('Ceph {} samples (OK): ' |
1928 | 603 | '{}'.format(sample_type, samples)) | 604 | '{}'.format(sample_type, samples)) |
1929 | 604 | return None | 605 | return None |
1930 | 606 | |||
1931 | 607 | # rabbitmq/amqp specific helpers: | ||
1932 | 608 | def add_rmq_test_user(self, sentry_units, | ||
1933 | 609 | username="testuser1", password="changeme"): | ||
1934 | 610 | """Add a test user via the first rmq juju unit, check connection as | ||
1935 | 611 | the new user against all sentry units. | ||
1936 | 612 | |||
1937 | 613 | :param sentry_units: list of sentry unit pointers | ||
1938 | 614 | :param username: amqp user name, default to testuser1 | ||
1939 | 615 | :param password: amqp user password | ||
1940 | 616 | :returns: None if successful. Raise on error. | ||
1941 | 617 | """ | ||
1942 | 618 | self.log.debug('Adding rmq user ({})...'.format(username)) | ||
1943 | 619 | |||
1944 | 620 | # Check that user does not already exist | ||
1945 | 621 | cmd_user_list = 'rabbitmqctl list_users' | ||
1946 | 622 | output, _ = self.run_cmd_unit(sentry_units[0], cmd_user_list) | ||
1947 | 623 | if username in output: | ||
1948 | 624 | self.log.warning('User ({}) already exists, returning ' | ||
1949 | 625 | 'gracefully.'.format(username)) | ||
1950 | 626 | return | ||
1951 | 627 | |||
1952 | 628 | perms = '".*" ".*" ".*"' | ||
1953 | 629 | cmds = ['rabbitmqctl add_user {} {}'.format(username, password), | ||
1954 | 630 | 'rabbitmqctl set_permissions {} {}'.format(username, perms)] | ||
1955 | 631 | |||
1956 | 632 | # Add user via first unit | ||
1957 | 633 | for cmd in cmds: | ||
1958 | 634 | output, _ = self.run_cmd_unit(sentry_units[0], cmd) | ||
1959 | 635 | |||
1960 | 636 | # Check connection against the other sentry_units | ||
1961 | 637 | self.log.debug('Checking user connect against units...') | ||
1962 | 638 | for sentry_unit in sentry_units: | ||
1963 | 639 | connection = self.connect_amqp_by_unit(sentry_unit, ssl=False, | ||
1964 | 640 | username=username, | ||
1965 | 641 | password=password) | ||
1966 | 642 | connection.close() | ||
1967 | 643 | |||
1968 | 644 | def delete_rmq_test_user(self, sentry_units, username="testuser1"): | ||
1969 | 645 | """Delete a rabbitmq user via the first rmq juju unit. | ||
1970 | 646 | |||
1971 | 647 | :param sentry_units: list of sentry unit pointers | ||
1972 | 648 | :param username: amqp user name, default to testuser1 | ||
1973 | 649 | :param password: amqp user password | ||
1974 | 650 | :returns: None if successful or no such user. | ||
1975 | 651 | """ | ||
1976 | 652 | self.log.debug('Deleting rmq user ({})...'.format(username)) | ||
1977 | 653 | |||
1978 | 654 | # Check that the user exists | ||
1979 | 655 | cmd_user_list = 'rabbitmqctl list_users' | ||
1980 | 656 | output, _ = self.run_cmd_unit(sentry_units[0], cmd_user_list) | ||
1981 | 657 | |||
1982 | 658 | if username not in output: | ||
1983 | 659 | self.log.warning('User ({}) does not exist, returning ' | ||
1984 | 660 | 'gracefully.'.format(username)) | ||
1985 | 661 | return | ||
1986 | 662 | |||
1987 | 663 | # Delete the user | ||
1988 | 664 | cmd_user_del = 'rabbitmqctl delete_user {}'.format(username) | ||
1989 | 665 | output, _ = self.run_cmd_unit(sentry_units[0], cmd_user_del) | ||
1990 | 666 | |||
1991 | 667 | def get_rmq_cluster_status(self, sentry_unit): | ||
1992 | 668 | """Execute rabbitmq cluster status command on a unit and return | ||
1993 | 669 | the full output. | ||
1994 | 670 | |||
1995 | 671 | :param unit: sentry unit | ||
1996 | 672 | :returns: String containing console output of cluster status command | ||
1997 | 673 | """ | ||
1998 | 674 | cmd = 'rabbitmqctl cluster_status' | ||
1999 | 675 | output, _ = self.run_cmd_unit(sentry_unit, cmd) | ||
2000 | 676 | self.log.debug('{} cluster_status:\n{}'.format( | ||
2001 | 677 | sentry_unit.info['unit_name'], output)) | ||
2002 | 678 | return str(output) | ||
2003 | 679 | |||
2004 | 680 | def get_rmq_cluster_running_nodes(self, sentry_unit): | ||
2005 | 681 | """Parse rabbitmqctl cluster_status output string, return list of | ||
2006 | 682 | running rabbitmq cluster nodes. | ||
2007 | 683 | |||
2008 | 684 | :param unit: sentry unit | ||
2009 | 685 | :returns: List containing node names of running nodes | ||
2010 | 686 | """ | ||
2011 | 687 | # NOTE(beisner): rabbitmqctl cluster_status output is not | ||
2012 | 688 | # json-parsable, do string chop foo, then json.loads that. | ||
2013 | 689 | str_stat = self.get_rmq_cluster_status(sentry_unit) | ||
2014 | 690 | if 'running_nodes' in str_stat: | ||
2015 | 691 | pos_start = str_stat.find("{running_nodes,") + 15 | ||
2016 | 692 | pos_end = str_stat.find("]},", pos_start) + 1 | ||
2017 | 693 | str_run_nodes = str_stat[pos_start:pos_end].replace("'", '"') | ||
2018 | 694 | run_nodes = json.loads(str_run_nodes) | ||
2019 | 695 | return run_nodes | ||
2020 | 696 | else: | ||
2021 | 697 | return [] | ||
2022 | 698 | |||
2023 | 699 | def validate_rmq_cluster_running_nodes(self, sentry_units): | ||
2024 | 700 | """Check that all rmq unit hostnames are represented in the | ||
2025 | 701 | cluster_status output of all units. | ||
2026 | 702 | |||
2027 | 703 | :param host_names: dict of juju unit names to host names | ||
2028 | 704 | :param units: list of sentry unit pointers (all rmq units) | ||
2029 | 705 | :returns: None if successful, otherwise return error message | ||
2030 | 706 | """ | ||
2031 | 707 | host_names = self.get_unit_hostnames(sentry_units) | ||
2032 | 708 | errors = [] | ||
2033 | 709 | |||
2034 | 710 | # Query every unit for cluster_status running nodes | ||
2035 | 711 | for query_unit in sentry_units: | ||
2036 | 712 | query_unit_name = query_unit.info['unit_name'] | ||
2037 | 713 | running_nodes = self.get_rmq_cluster_running_nodes(query_unit) | ||
2038 | 714 | |||
2039 | 715 | # Confirm that every unit is represented in the queried unit's | ||
2040 | 716 | # cluster_status running nodes output. | ||
2041 | 717 | for validate_unit in sentry_units: | ||
2042 | 718 | val_host_name = host_names[validate_unit.info['unit_name']] | ||
2043 | 719 | val_node_name = 'rabbit@{}'.format(val_host_name) | ||
2044 | 720 | |||
2045 | 721 | if val_node_name not in running_nodes: | ||
2046 | 722 | errors.append('Cluster member check failed on {}: {} not ' | ||
2047 | 723 | 'in {}\n'.format(query_unit_name, | ||
2048 | 724 | val_node_name, | ||
2049 | 725 | running_nodes)) | ||
2050 | 726 | if errors: | ||
2051 | 727 | return ''.join(errors) | ||
2052 | 728 | |||
2053 | 729 | def rmq_ssl_is_enabled_on_unit(self, sentry_unit, port=None): | ||
2054 | 730 | """Check a single juju rmq unit for ssl and port in the config file.""" | ||
2055 | 731 | host = sentry_unit.info['public-address'] | ||
2056 | 732 | unit_name = sentry_unit.info['unit_name'] | ||
2057 | 733 | |||
2058 | 734 | conf_file = '/etc/rabbitmq/rabbitmq.config' | ||
2059 | 735 | conf_contents = str(self.file_contents_safe(sentry_unit, | ||
2060 | 736 | conf_file, max_wait=16)) | ||
2061 | 737 | # Checks | ||
2062 | 738 | conf_ssl = 'ssl' in conf_contents | ||
2063 | 739 | conf_port = str(port) in conf_contents | ||
2064 | 740 | |||
2065 | 741 | # Port explicitly checked in config | ||
2066 | 742 | if port and conf_port and conf_ssl: | ||
2067 | 743 | self.log.debug('SSL is enabled @{}:{} ' | ||
2068 | 744 | '({})'.format(host, port, unit_name)) | ||
2069 | 745 | return True | ||
2070 | 746 | elif port and not conf_port and conf_ssl: | ||
2071 | 747 | self.log.debug('SSL is enabled @{} but not on port {} ' | ||
2072 | 748 | '({})'.format(host, port, unit_name)) | ||
2073 | 749 | return False | ||
2074 | 750 | # Port not checked (useful when checking that ssl is disabled) | ||
2075 | 751 | elif not port and conf_ssl: | ||
2076 | 752 | self.log.debug('SSL is enabled @{}:{} ' | ||
2077 | 753 | '({})'.format(host, port, unit_name)) | ||
2078 | 754 | return True | ||
2079 | 755 | elif not port and not conf_ssl: | ||
2080 | 756 | self.log.debug('SSL not enabled @{}:{} ' | ||
2081 | 757 | '({})'.format(host, port, unit_name)) | ||
2082 | 758 | return False | ||
2083 | 759 | else: | ||
2084 | 760 | msg = ('Unknown condition when checking SSL status @{}:{} ' | ||
2085 | 761 | '({})'.format(host, port, unit_name)) | ||
2086 | 762 | amulet.raise_status(amulet.FAIL, msg) | ||
2087 | 763 | |||
2088 | 764 | def validate_rmq_ssl_enabled_units(self, sentry_units, port=None): | ||
2089 | 765 | """Check that ssl is enabled on rmq juju sentry units. | ||
2090 | 766 | |||
2091 | 767 | :param sentry_units: list of all rmq sentry units | ||
2092 | 768 | :param port: optional ssl port override to validate | ||
2093 | 769 | :returns: None if successful, otherwise return error message | ||
2094 | 770 | """ | ||
2095 | 771 | for sentry_unit in sentry_units: | ||
2096 | 772 | if not self.rmq_ssl_is_enabled_on_unit(sentry_unit, port=port): | ||
2097 | 773 | return ('Unexpected condition: ssl is disabled on unit ' | ||
2098 | 774 | '({})'.format(sentry_unit.info['unit_name'])) | ||
2099 | 775 | return None | ||
2100 | 776 | |||
2101 | 777 | def validate_rmq_ssl_disabled_units(self, sentry_units): | ||
2102 | 778 | """Check that ssl is enabled on listed rmq juju sentry units. | ||
2103 | 779 | |||
2104 | 780 | :param sentry_units: list of all rmq sentry units | ||
2105 | 781 | :returns: True if successful. Raise on error. | ||
2106 | 782 | """ | ||
2107 | 783 | for sentry_unit in sentry_units: | ||
2108 | 784 | if self.rmq_ssl_is_enabled_on_unit(sentry_unit): | ||
2109 | 785 | return ('Unexpected condition: ssl is enabled on unit ' | ||
2110 | 786 | '({})'.format(sentry_unit.info['unit_name'])) | ||
2111 | 787 | return None | ||
2112 | 788 | |||
2113 | 789 | def configure_rmq_ssl_on(self, sentry_units, deployment, | ||
2114 | 790 | port=None, max_wait=60): | ||
2115 | 791 | """Turn ssl charm config option on, with optional non-default | ||
2116 | 792 | ssl port specification. Confirm that it is enabled on every | ||
2117 | 793 | unit. | ||
2118 | 794 | |||
2119 | 795 | :param sentry_units: list of sentry units | ||
2120 | 796 | :param deployment: amulet deployment object pointer | ||
2121 | 797 | :param port: amqp port, use defaults if None | ||
2122 | 798 | :param max_wait: maximum time to wait in seconds to confirm | ||
2123 | 799 | :returns: None if successful. Raise on error. | ||
2124 | 800 | """ | ||
2125 | 801 | self.log.debug('Setting ssl charm config option: on') | ||
2126 | 802 | |||
2127 | 803 | # Enable RMQ SSL | ||
2128 | 804 | config = {'ssl': 'on'} | ||
2129 | 805 | if port: | ||
2130 | 806 | config['ssl_port'] = port | ||
2131 | 807 | |||
2132 | 808 | deployment.configure('rabbitmq-server', config) | ||
2133 | 809 | |||
2134 | 810 | # Confirm | ||
2135 | 811 | tries = 0 | ||
2136 | 812 | ret = self.validate_rmq_ssl_enabled_units(sentry_units, port=port) | ||
2137 | 813 | while ret and tries < (max_wait / 4): | ||
2138 | 814 | time.sleep(4) | ||
2139 | 815 | self.log.debug('Attempt {}: {}'.format(tries, ret)) | ||
2140 | 816 | ret = self.validate_rmq_ssl_enabled_units(sentry_units, port=port) | ||
2141 | 817 | tries += 1 | ||
2142 | 818 | |||
2143 | 819 | if ret: | ||
2144 | 820 | amulet.raise_status(amulet.FAIL, ret) | ||
2145 | 821 | |||
2146 | 822 | def configure_rmq_ssl_off(self, sentry_units, deployment, max_wait=60): | ||
2147 | 823 | """Turn ssl charm config option off, confirm that it is disabled | ||
2148 | 824 | on every unit. | ||
2149 | 825 | |||
2150 | 826 | :param sentry_units: list of sentry units | ||
2151 | 827 | :param deployment: amulet deployment object pointer | ||
2152 | 828 | :param max_wait: maximum time to wait in seconds to confirm | ||
2153 | 829 | :returns: None if successful. Raise on error. | ||
2154 | 830 | """ | ||
2155 | 831 | self.log.debug('Setting ssl charm config option: off') | ||
2156 | 832 | |||
2157 | 833 | # Disable RMQ SSL | ||
2158 | 834 | config = {'ssl': 'off'} | ||
2159 | 835 | deployment.configure('rabbitmq-server', config) | ||
2160 | 836 | |||
2161 | 837 | # Confirm | ||
2162 | 838 | tries = 0 | ||
2163 | 839 | ret = self.validate_rmq_ssl_disabled_units(sentry_units) | ||
2164 | 840 | while ret and tries < (max_wait / 4): | ||
2165 | 841 | time.sleep(4) | ||
2166 | 842 | self.log.debug('Attempt {}: {}'.format(tries, ret)) | ||
2167 | 843 | ret = self.validate_rmq_ssl_disabled_units(sentry_units) | ||
2168 | 844 | tries += 1 | ||
2169 | 845 | |||
2170 | 846 | if ret: | ||
2171 | 847 | amulet.raise_status(amulet.FAIL, ret) | ||
2172 | 848 | |||
2173 | 849 | def connect_amqp_by_unit(self, sentry_unit, ssl=False, | ||
2174 | 850 | port=None, fatal=True, | ||
2175 | 851 | username="testuser1", password="changeme"): | ||
2176 | 852 | """Establish and return a pika amqp connection to the rabbitmq service | ||
2177 | 853 | running on a rmq juju unit. | ||
2178 | 854 | |||
2179 | 855 | :param sentry_unit: sentry unit pointer | ||
2180 | 856 | :param ssl: boolean, default to False | ||
2181 | 857 | :param port: amqp port, use defaults if None | ||
2182 | 858 | :param fatal: boolean, default to True (raises on connect error) | ||
2183 | 859 | :param username: amqp user name, default to testuser1 | ||
2184 | 860 | :param password: amqp user password | ||
2185 | 861 | :returns: pika amqp connection pointer or None if failed and non-fatal | ||
2186 | 862 | """ | ||
2187 | 863 | host = sentry_unit.info['public-address'] | ||
2188 | 864 | unit_name = sentry_unit.info['unit_name'] | ||
2189 | 865 | |||
2190 | 866 | # Default port logic if port is not specified | ||
2191 | 867 | if ssl and not port: | ||
2192 | 868 | port = 5671 | ||
2193 | 869 | elif not ssl and not port: | ||
2194 | 870 | port = 5672 | ||
2195 | 871 | |||
2196 | 872 | self.log.debug('Connecting to amqp on {}:{} ({}) as ' | ||
2197 | 873 | '{}...'.format(host, port, unit_name, username)) | ||
2198 | 874 | |||
2199 | 875 | try: | ||
2200 | 876 | credentials = pika.PlainCredentials(username, password) | ||
2201 | 877 | parameters = pika.ConnectionParameters(host=host, port=port, | ||
2202 | 878 | credentials=credentials, | ||
2203 | 879 | ssl=ssl, | ||
2204 | 880 | connection_attempts=3, | ||
2205 | 881 | retry_delay=5, | ||
2206 | 882 | socket_timeout=1) | ||
2207 | 883 | connection = pika.BlockingConnection(parameters) | ||
2208 | 884 | assert connection.server_properties['product'] == 'RabbitMQ' | ||
2209 | 885 | self.log.debug('Connect OK') | ||
2210 | 886 | return connection | ||
2211 | 887 | except Exception as e: | ||
2212 | 888 | msg = ('amqp connection failed to {}:{} as ' | ||
2213 | 889 | '{} ({})'.format(host, port, username, str(e))) | ||
2214 | 890 | if fatal: | ||
2215 | 891 | amulet.raise_status(amulet.FAIL, msg) | ||
2216 | 892 | else: | ||
2217 | 893 | self.log.warn(msg) | ||
2218 | 894 | return None | ||
2219 | 895 | |||
2220 | 896 | def publish_amqp_message_by_unit(self, sentry_unit, message, | ||
2221 | 897 | queue="test", ssl=False, | ||
2222 | 898 | username="testuser1", | ||
2223 | 899 | password="changeme", | ||
2224 | 900 | port=None): | ||
2225 | 901 | """Publish an amqp message to a rmq juju unit. | ||
2226 | 902 | |||
2227 | 903 | :param sentry_unit: sentry unit pointer | ||
2228 | 904 | :param message: amqp message string | ||
2229 | 905 | :param queue: message queue, default to test | ||
2230 | 906 | :param username: amqp user name, default to testuser1 | ||
2231 | 907 | :param password: amqp user password | ||
2232 | 908 | :param ssl: boolean, default to False | ||
2233 | 909 | :param port: amqp port, use defaults if None | ||
2234 | 910 | :returns: None. Raises exception if publish failed. | ||
2235 | 911 | """ | ||
2236 | 912 | self.log.debug('Publishing message to {} queue:\n{}'.format(queue, | ||
2237 | 913 | message)) | ||
2238 | 914 | connection = self.connect_amqp_by_unit(sentry_unit, ssl=ssl, | ||
2239 | 915 | port=port, | ||
2240 | 916 | username=username, | ||
2241 | 917 | password=password) | ||
2242 | 918 | |||
2243 | 919 | # NOTE(beisner): extra debug here re: pika hang potential: | ||
2244 | 920 | # https://github.com/pika/pika/issues/297 | ||
2245 | 921 | # https://groups.google.com/forum/#!topic/rabbitmq-users/Ja0iyfF0Szw | ||
2246 | 922 | self.log.debug('Defining channel...') | ||
2247 | 923 | channel = connection.channel() | ||
2248 | 924 | self.log.debug('Declaring queue...') | ||
2249 | 925 | channel.queue_declare(queue=queue, auto_delete=False, durable=True) | ||
2250 | 926 | self.log.debug('Publishing message...') | ||
2251 | 927 | channel.basic_publish(exchange='', routing_key=queue, body=message) | ||
2252 | 928 | self.log.debug('Closing channel...') | ||
2253 | 929 | channel.close() | ||
2254 | 930 | self.log.debug('Closing connection...') | ||
2255 | 931 | connection.close() | ||
2256 | 932 | |||
2257 | 933 | def get_amqp_message_by_unit(self, sentry_unit, queue="test", | ||
2258 | 934 | username="testuser1", | ||
2259 | 935 | password="changeme", | ||
2260 | 936 | ssl=False, port=None): | ||
2261 | 937 | """Get an amqp message from a rmq juju unit. | ||
2262 | 938 | |||
2263 | 939 | :param sentry_unit: sentry unit pointer | ||
2264 | 940 | :param queue: message queue, default to test | ||
2265 | 941 | :param username: amqp user name, default to testuser1 | ||
2266 | 942 | :param password: amqp user password | ||
2267 | 943 | :param ssl: boolean, default to False | ||
2268 | 944 | :param port: amqp port, use defaults if None | ||
2269 | 945 | :returns: amqp message body as string. Raise if get fails. | ||
2270 | 946 | """ | ||
2271 | 947 | connection = self.connect_amqp_by_unit(sentry_unit, ssl=ssl, | ||
2272 | 948 | port=port, | ||
2273 | 949 | username=username, | ||
2274 | 950 | password=password) | ||
2275 | 951 | channel = connection.channel() | ||
2276 | 952 | method_frame, _, body = channel.basic_get(queue) | ||
2277 | 953 | |||
2278 | 954 | if method_frame: | ||
2279 | 955 | self.log.debug('Retreived message from {} queue:\n{}'.format(queue, | ||
2280 | 956 | body)) | ||
2281 | 957 | channel.basic_ack(method_frame.delivery_tag) | ||
2282 | 958 | channel.close() | ||
2283 | 959 | connection.close() | ||
2284 | 960 | return body | ||
2285 | 961 | else: | ||
2286 | 962 | msg = 'No message retrieved.' | ||
2287 | 963 | amulet.raise_status(amulet.FAIL, msg) | ||
2288 | 605 | 964 | ||
2289 | === modified file 'unit_tests/test_keystone_hooks.py' | |||
2290 | --- unit_tests/test_keystone_hooks.py 2015-08-14 16:10:45 +0000 | |||
2291 | +++ unit_tests/test_keystone_hooks.py 2015-09-25 14:42:33 +0000 | |||
2292 | @@ -142,7 +142,8 @@ | |||
2293 | 142 | 142 | ||
2294 | 143 | cfg_dict = {'prefer-ipv6': False, | 143 | cfg_dict = {'prefer-ipv6': False, |
2295 | 144 | 'database': 'keystone', | 144 | 'database': 'keystone', |
2297 | 145 | 'database-user': 'keystone'} | 145 | 'database-user': 'keystone', |
2298 | 146 | 'vip': None} | ||
2299 | 146 | 147 | ||
2300 | 147 | class mock_cls_config(): | 148 | class mock_cls_config(): |
2301 | 148 | def __call__(self, key): | 149 | def __call__(self, key): |
charm_lint_check #10767 keystone-next for hopem mp272411
LINT OK: passed
Build: http:// 10.245. 162.77: 8080/job/ charm_lint_ check/10767/