Merge lp:~le-charmers/charms/trusty/heat/leadership-election into lp:~openstack-charmers-archive/charms/trusty/heat/next
- Trusty Tahr (14.04)
- leadership-election
- Merge into next
Proposed by
Edward Hope-Morley
Status: | Merged |
---|---|
Merged at revision: | 42 |
Proposed branch: | lp:~le-charmers/charms/trusty/heat/leadership-election |
Merge into: | lp:~openstack-charmers-archive/charms/trusty/heat/next |
Diff against target: |
729 lines (+328/-58) 9 files modified
hooks/charmhelpers/contrib/hahelpers/cluster.py (+37/-2) hooks/charmhelpers/contrib/openstack/neutron.py (+10/-5) hooks/charmhelpers/contrib/openstack/utils.py (+65/-18) hooks/charmhelpers/contrib/python/packages.py (+28/-5) hooks/charmhelpers/core/hookenv.py (+147/-10) hooks/charmhelpers/core/host.py (+1/-1) hooks/charmhelpers/core/services/base.py (+32/-11) hooks/charmhelpers/fetch/__init__.py (+1/-1) hooks/charmhelpers/fetch/giturl.py (+7/-5) |
To merge this branch: | bzr merge lp:~le-charmers/charms/trusty/heat/leadership-election |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
OpenStack Charmers | Pending | ||
Review via email: mp+255009@code.launchpad.net |
Commit message
Description of the change
To post a comment you must log in.
- 39. By Liam Young
-
Merged trunk in + LE charmhelper sync
- 40. By Liam Young
-
Resync le charm helpers
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'hooks/charmhelpers/contrib/hahelpers/cluster.py' | |||
2 | --- hooks/charmhelpers/contrib/hahelpers/cluster.py 2015-02-24 11:04:31 +0000 | |||
3 | +++ hooks/charmhelpers/contrib/hahelpers/cluster.py 2015-06-04 08:45:30 +0000 | |||
4 | @@ -44,6 +44,7 @@ | |||
5 | 44 | ERROR, | 44 | ERROR, |
6 | 45 | WARNING, | 45 | WARNING, |
7 | 46 | unit_get, | 46 | unit_get, |
8 | 47 | is_leader as juju_is_leader | ||
9 | 47 | ) | 48 | ) |
10 | 48 | from charmhelpers.core.decorators import ( | 49 | from charmhelpers.core.decorators import ( |
11 | 49 | retry_on_exception, | 50 | retry_on_exception, |
12 | @@ -52,6 +53,8 @@ | |||
13 | 52 | bool_from_string, | 53 | bool_from_string, |
14 | 53 | ) | 54 | ) |
15 | 54 | 55 | ||
16 | 56 | DC_RESOURCE_NAME = 'DC' | ||
17 | 57 | |||
18 | 55 | 58 | ||
19 | 56 | class HAIncompleteConfig(Exception): | 59 | class HAIncompleteConfig(Exception): |
20 | 57 | pass | 60 | pass |
21 | @@ -66,12 +69,21 @@ | |||
22 | 66 | Returns True if the charm executing this is the elected cluster leader. | 69 | Returns True if the charm executing this is the elected cluster leader. |
23 | 67 | 70 | ||
24 | 68 | It relies on two mechanisms to determine leadership: | 71 | It relies on two mechanisms to determine leadership: |
26 | 69 | 1. If the charm is part of a corosync cluster, call corosync to | 72 | 1. If juju is sufficiently new and leadership election is supported, |
27 | 73 | the is_leader command will be used. | ||
28 | 74 | 2. If the charm is part of a corosync cluster, call corosync to | ||
29 | 70 | determine leadership. | 75 | determine leadership. |
31 | 71 | 2. If the charm is not part of a corosync cluster, the leader is | 76 | 3. If the charm is not part of a corosync cluster, the leader is |
32 | 72 | determined as being "the alive unit with the lowest unit numer". In | 77 | determined as being "the alive unit with the lowest unit numer". In |
33 | 73 | other words, the oldest surviving unit. | 78 | other words, the oldest surviving unit. |
34 | 74 | """ | 79 | """ |
35 | 80 | try: | ||
36 | 81 | return juju_is_leader() | ||
37 | 82 | except NotImplementedError: | ||
38 | 83 | log('Juju leadership election feature not enabled' | ||
39 | 84 | ', using fallback support', | ||
40 | 85 | level=WARNING) | ||
41 | 86 | |||
42 | 75 | if is_clustered(): | 87 | if is_clustered(): |
43 | 76 | if not is_crm_leader(resource): | 88 | if not is_crm_leader(resource): |
44 | 77 | log('Deferring action to CRM leader.', level=INFO) | 89 | log('Deferring action to CRM leader.', level=INFO) |
45 | @@ -95,6 +107,27 @@ | |||
46 | 95 | return False | 107 | return False |
47 | 96 | 108 | ||
48 | 97 | 109 | ||
49 | 110 | def is_crm_dc(): | ||
50 | 111 | """ | ||
51 | 112 | Determine leadership by querying the pacemaker Designated Controller | ||
52 | 113 | """ | ||
53 | 114 | cmd = ['crm', 'status'] | ||
54 | 115 | try: | ||
55 | 116 | status = subprocess.check_output(cmd, stderr=subprocess.STDOUT) | ||
56 | 117 | if not isinstance(status, six.text_type): | ||
57 | 118 | status = six.text_type(status, "utf-8") | ||
58 | 119 | except subprocess.CalledProcessError: | ||
59 | 120 | return False | ||
60 | 121 | current_dc = '' | ||
61 | 122 | for line in status.split('\n'): | ||
62 | 123 | if line.startswith('Current DC'): | ||
63 | 124 | # Current DC: juju-lytrusty-machine-2 (168108163) - partition with quorum | ||
64 | 125 | current_dc = line.split(':')[1].split()[0] | ||
65 | 126 | if current_dc == get_unit_hostname(): | ||
66 | 127 | return True | ||
67 | 128 | return False | ||
68 | 129 | |||
69 | 130 | |||
70 | 98 | @retry_on_exception(5, base_delay=2, exc_type=CRMResourceNotFound) | 131 | @retry_on_exception(5, base_delay=2, exc_type=CRMResourceNotFound) |
71 | 99 | def is_crm_leader(resource, retry=False): | 132 | def is_crm_leader(resource, retry=False): |
72 | 100 | """ | 133 | """ |
73 | @@ -104,6 +137,8 @@ | |||
74 | 104 | We allow this operation to be retried to avoid the possibility of getting a | 137 | We allow this operation to be retried to avoid the possibility of getting a |
75 | 105 | false negative. See LP #1396246 for more info. | 138 | false negative. See LP #1396246 for more info. |
76 | 106 | """ | 139 | """ |
77 | 140 | if resource == DC_RESOURCE_NAME: | ||
78 | 141 | return is_crm_dc() | ||
79 | 107 | cmd = ['crm', 'resource', 'show', resource] | 142 | cmd = ['crm', 'resource', 'show', resource] |
80 | 108 | try: | 143 | try: |
81 | 109 | status = subprocess.check_output(cmd, stderr=subprocess.STDOUT) | 144 | status = subprocess.check_output(cmd, stderr=subprocess.STDOUT) |
82 | 110 | 145 | ||
83 | === modified file 'hooks/charmhelpers/contrib/openstack/neutron.py' | |||
84 | --- hooks/charmhelpers/contrib/openstack/neutron.py 2015-04-16 10:29:48 +0000 | |||
85 | +++ hooks/charmhelpers/contrib/openstack/neutron.py 2015-06-04 08:45:30 +0000 | |||
86 | @@ -256,11 +256,14 @@ | |||
87 | 256 | def parse_mappings(mappings): | 256 | def parse_mappings(mappings): |
88 | 257 | parsed = {} | 257 | parsed = {} |
89 | 258 | if mappings: | 258 | if mappings: |
91 | 259 | mappings = mappings.split(' ') | 259 | mappings = mappings.split() |
92 | 260 | for m in mappings: | 260 | for m in mappings: |
93 | 261 | p = m.partition(':') | 261 | p = m.partition(':') |
96 | 262 | if p[1] == ':': | 262 | key = p[0].strip() |
97 | 263 | parsed[p[0].strip()] = p[2].strip() | 263 | if p[1]: |
98 | 264 | parsed[key] = p[2].strip() | ||
99 | 265 | else: | ||
100 | 266 | parsed[key] = '' | ||
101 | 264 | 267 | ||
102 | 265 | return parsed | 268 | return parsed |
103 | 266 | 269 | ||
104 | @@ -283,13 +286,13 @@ | |||
105 | 283 | Returns dict of the form {bridge:port}. | 286 | Returns dict of the form {bridge:port}. |
106 | 284 | """ | 287 | """ |
107 | 285 | _mappings = parse_mappings(mappings) | 288 | _mappings = parse_mappings(mappings) |
109 | 286 | if not _mappings: | 289 | if not _mappings or list(_mappings.values()) == ['']: |
110 | 287 | if not mappings: | 290 | if not mappings: |
111 | 288 | return {} | 291 | return {} |
112 | 289 | 292 | ||
113 | 290 | # For backwards-compatibility we need to support port-only provided in | 293 | # For backwards-compatibility we need to support port-only provided in |
114 | 291 | # config. | 294 | # config. |
116 | 292 | _mappings = {default_bridge: mappings.split(' ')[0]} | 295 | _mappings = {default_bridge: mappings.split()[0]} |
117 | 293 | 296 | ||
118 | 294 | bridges = _mappings.keys() | 297 | bridges = _mappings.keys() |
119 | 295 | ports = _mappings.values() | 298 | ports = _mappings.values() |
120 | @@ -309,6 +312,8 @@ | |||
121 | 309 | 312 | ||
122 | 310 | Mappings must be a space-delimited list of provider:start:end mappings. | 313 | Mappings must be a space-delimited list of provider:start:end mappings. |
123 | 311 | 314 | ||
124 | 315 | The start:end range is optional and may be omitted. | ||
125 | 316 | |||
126 | 312 | Returns dict of the form {provider: (start, end)}. | 317 | Returns dict of the form {provider: (start, end)}. |
127 | 313 | """ | 318 | """ |
128 | 314 | _mappings = parse_mappings(mappings) | 319 | _mappings = parse_mappings(mappings) |
129 | 315 | 320 | ||
130 | === modified file 'hooks/charmhelpers/contrib/openstack/utils.py' | |||
131 | --- hooks/charmhelpers/contrib/openstack/utils.py 2015-04-16 21:33:43 +0000 | |||
132 | +++ hooks/charmhelpers/contrib/openstack/utils.py 2015-06-04 08:45:30 +0000 | |||
133 | @@ -53,9 +53,13 @@ | |||
134 | 53 | get_ipv6_addr | 53 | get_ipv6_addr |
135 | 54 | ) | 54 | ) |
136 | 55 | 55 | ||
137 | 56 | from charmhelpers.contrib.python.packages import ( | ||
138 | 57 | pip_create_virtualenv, | ||
139 | 58 | pip_install, | ||
140 | 59 | ) | ||
141 | 60 | |||
142 | 56 | from charmhelpers.core.host import lsb_release, mounts, umount | 61 | from charmhelpers.core.host import lsb_release, mounts, umount |
143 | 57 | from charmhelpers.fetch import apt_install, apt_cache, install_remote | 62 | from charmhelpers.fetch import apt_install, apt_cache, install_remote |
144 | 58 | from charmhelpers.contrib.python.packages import pip_install | ||
145 | 59 | from charmhelpers.contrib.storage.linux.utils import is_block_device, zap_disk | 63 | from charmhelpers.contrib.storage.linux.utils import is_block_device, zap_disk |
146 | 60 | from charmhelpers.contrib.storage.linux.loopback import ensure_loopback_device | 64 | from charmhelpers.contrib.storage.linux.loopback import ensure_loopback_device |
147 | 61 | 65 | ||
148 | @@ -497,7 +501,17 @@ | |||
149 | 497 | requirements_dir = None | 501 | requirements_dir = None |
150 | 498 | 502 | ||
151 | 499 | 503 | ||
153 | 500 | def git_clone_and_install(projects_yaml, core_project): | 504 | def _git_yaml_load(projects_yaml): |
154 | 505 | """ | ||
155 | 506 | Load the specified yaml into a dictionary. | ||
156 | 507 | """ | ||
157 | 508 | if not projects_yaml: | ||
158 | 509 | return None | ||
159 | 510 | |||
160 | 511 | return yaml.load(projects_yaml) | ||
161 | 512 | |||
162 | 513 | |||
163 | 514 | def git_clone_and_install(projects_yaml, core_project, depth=1): | ||
164 | 501 | """ | 515 | """ |
165 | 502 | Clone/install all specified OpenStack repositories. | 516 | Clone/install all specified OpenStack repositories. |
166 | 503 | 517 | ||
167 | @@ -510,23 +524,22 @@ | |||
168 | 510 | repository: 'git://git.openstack.org/openstack/requirements.git', | 524 | repository: 'git://git.openstack.org/openstack/requirements.git', |
169 | 511 | branch: 'stable/icehouse'} | 525 | branch: 'stable/icehouse'} |
170 | 512 | directory: /mnt/openstack-git | 526 | directory: /mnt/openstack-git |
173 | 513 | http_proxy: http://squid.internal:3128 | 527 | http_proxy: squid-proxy-url |
174 | 514 | https_proxy: https://squid.internal:3128 | 528 | https_proxy: squid-proxy-url |
175 | 515 | 529 | ||
176 | 516 | The directory, http_proxy, and https_proxy keys are optional. | 530 | The directory, http_proxy, and https_proxy keys are optional. |
177 | 517 | """ | 531 | """ |
178 | 518 | global requirements_dir | 532 | global requirements_dir |
179 | 519 | parent_dir = '/mnt/openstack-git' | 533 | parent_dir = '/mnt/openstack-git' |
185 | 520 | 534 | http_proxy = None | |
186 | 521 | if not projects_yaml: | 535 | |
187 | 522 | return | 536 | projects = _git_yaml_load(projects_yaml) |
183 | 523 | |||
184 | 524 | projects = yaml.load(projects_yaml) | ||
188 | 525 | _git_validate_projects_yaml(projects, core_project) | 537 | _git_validate_projects_yaml(projects, core_project) |
189 | 526 | 538 | ||
190 | 527 | old_environ = dict(os.environ) | 539 | old_environ = dict(os.environ) |
191 | 528 | 540 | ||
192 | 529 | if 'http_proxy' in projects.keys(): | 541 | if 'http_proxy' in projects.keys(): |
193 | 542 | http_proxy = projects['http_proxy'] | ||
194 | 530 | os.environ['http_proxy'] = projects['http_proxy'] | 543 | os.environ['http_proxy'] = projects['http_proxy'] |
195 | 531 | if 'https_proxy' in projects.keys(): | 544 | if 'https_proxy' in projects.keys(): |
196 | 532 | os.environ['https_proxy'] = projects['https_proxy'] | 545 | os.environ['https_proxy'] = projects['https_proxy'] |
197 | @@ -534,15 +547,19 @@ | |||
198 | 534 | if 'directory' in projects.keys(): | 547 | if 'directory' in projects.keys(): |
199 | 535 | parent_dir = projects['directory'] | 548 | parent_dir = projects['directory'] |
200 | 536 | 549 | ||
201 | 550 | pip_create_virtualenv(os.path.join(parent_dir, 'venv')) | ||
202 | 551 | |||
203 | 537 | for p in projects['repositories']: | 552 | for p in projects['repositories']: |
204 | 538 | repo = p['repository'] | 553 | repo = p['repository'] |
205 | 539 | branch = p['branch'] | 554 | branch = p['branch'] |
206 | 540 | if p['name'] == 'requirements': | 555 | if p['name'] == 'requirements': |
208 | 541 | repo_dir = _git_clone_and_install_single(repo, branch, parent_dir, | 556 | repo_dir = _git_clone_and_install_single(repo, branch, depth, |
209 | 557 | parent_dir, http_proxy, | ||
210 | 542 | update_requirements=False) | 558 | update_requirements=False) |
211 | 543 | requirements_dir = repo_dir | 559 | requirements_dir = repo_dir |
212 | 544 | else: | 560 | else: |
214 | 545 | repo_dir = _git_clone_and_install_single(repo, branch, parent_dir, | 561 | repo_dir = _git_clone_and_install_single(repo, branch, depth, |
215 | 562 | parent_dir, http_proxy, | ||
216 | 546 | update_requirements=True) | 563 | update_requirements=True) |
217 | 547 | 564 | ||
218 | 548 | os.environ = old_environ | 565 | os.environ = old_environ |
219 | @@ -574,7 +591,8 @@ | |||
220 | 574 | error_out('openstack-origin-git key \'{}\' is missing'.format(key)) | 591 | error_out('openstack-origin-git key \'{}\' is missing'.format(key)) |
221 | 575 | 592 | ||
222 | 576 | 593 | ||
224 | 577 | def _git_clone_and_install_single(repo, branch, parent_dir, update_requirements): | 594 | def _git_clone_and_install_single(repo, branch, depth, parent_dir, http_proxy, |
225 | 595 | update_requirements): | ||
226 | 578 | """ | 596 | """ |
227 | 579 | Clone and install a single git repository. | 597 | Clone and install a single git repository. |
228 | 580 | """ | 598 | """ |
229 | @@ -587,7 +605,8 @@ | |||
230 | 587 | 605 | ||
231 | 588 | if not os.path.exists(dest_dir): | 606 | if not os.path.exists(dest_dir): |
232 | 589 | juju_log('Cloning git repo: {}, branch: {}'.format(repo, branch)) | 607 | juju_log('Cloning git repo: {}, branch: {}'.format(repo, branch)) |
234 | 590 | repo_dir = install_remote(repo, dest=parent_dir, branch=branch) | 608 | repo_dir = install_remote(repo, dest=parent_dir, branch=branch, |
235 | 609 | depth=depth) | ||
236 | 591 | else: | 610 | else: |
237 | 592 | repo_dir = dest_dir | 611 | repo_dir = dest_dir |
238 | 593 | 612 | ||
239 | @@ -598,7 +617,12 @@ | |||
240 | 598 | _git_update_requirements(repo_dir, requirements_dir) | 617 | _git_update_requirements(repo_dir, requirements_dir) |
241 | 599 | 618 | ||
242 | 600 | juju_log('Installing git repo from dir: {}'.format(repo_dir)) | 619 | juju_log('Installing git repo from dir: {}'.format(repo_dir)) |
244 | 601 | pip_install(repo_dir) | 620 | if http_proxy: |
245 | 621 | pip_install(repo_dir, proxy=http_proxy, | ||
246 | 622 | venv=os.path.join(parent_dir, 'venv')) | ||
247 | 623 | else: | ||
248 | 624 | pip_install(repo_dir, | ||
249 | 625 | venv=os.path.join(parent_dir, 'venv')) | ||
250 | 602 | 626 | ||
251 | 603 | return repo_dir | 627 | return repo_dir |
252 | 604 | 628 | ||
253 | @@ -621,16 +645,27 @@ | |||
254 | 621 | os.chdir(orig_dir) | 645 | os.chdir(orig_dir) |
255 | 622 | 646 | ||
256 | 623 | 647 | ||
257 | 648 | def git_pip_venv_dir(projects_yaml): | ||
258 | 649 | """ | ||
259 | 650 | Return the pip virtualenv path. | ||
260 | 651 | """ | ||
261 | 652 | parent_dir = '/mnt/openstack-git' | ||
262 | 653 | |||
263 | 654 | projects = _git_yaml_load(projects_yaml) | ||
264 | 655 | |||
265 | 656 | if 'directory' in projects.keys(): | ||
266 | 657 | parent_dir = projects['directory'] | ||
267 | 658 | |||
268 | 659 | return os.path.join(parent_dir, 'venv') | ||
269 | 660 | |||
270 | 661 | |||
271 | 624 | def git_src_dir(projects_yaml, project): | 662 | def git_src_dir(projects_yaml, project): |
272 | 625 | """ | 663 | """ |
273 | 626 | Return the directory where the specified project's source is located. | 664 | Return the directory where the specified project's source is located. |
274 | 627 | """ | 665 | """ |
275 | 628 | parent_dir = '/mnt/openstack-git' | 666 | parent_dir = '/mnt/openstack-git' |
276 | 629 | 667 | ||
281 | 630 | if not projects_yaml: | 668 | projects = _git_yaml_load(projects_yaml) |
278 | 631 | return | ||
279 | 632 | |||
280 | 633 | projects = yaml.load(projects_yaml) | ||
282 | 634 | 669 | ||
283 | 635 | if 'directory' in projects.keys(): | 670 | if 'directory' in projects.keys(): |
284 | 636 | parent_dir = projects['directory'] | 671 | parent_dir = projects['directory'] |
285 | @@ -640,3 +675,15 @@ | |||
286 | 640 | return os.path.join(parent_dir, os.path.basename(p['repository'])) | 675 | return os.path.join(parent_dir, os.path.basename(p['repository'])) |
287 | 641 | 676 | ||
288 | 642 | return None | 677 | return None |
289 | 678 | |||
290 | 679 | |||
291 | 680 | def git_yaml_value(projects_yaml, key): | ||
292 | 681 | """ | ||
293 | 682 | Return the value in projects_yaml for the specified key. | ||
294 | 683 | """ | ||
295 | 684 | projects = _git_yaml_load(projects_yaml) | ||
296 | 685 | |||
297 | 686 | if key in projects.keys(): | ||
298 | 687 | return projects[key] | ||
299 | 688 | |||
300 | 689 | return None | ||
301 | 643 | 690 | ||
302 | === modified file 'hooks/charmhelpers/contrib/python/packages.py' | |||
303 | --- hooks/charmhelpers/contrib/python/packages.py 2015-02-24 11:04:31 +0000 | |||
304 | +++ hooks/charmhelpers/contrib/python/packages.py 2015-06-04 08:45:30 +0000 | |||
305 | @@ -17,8 +17,11 @@ | |||
306 | 17 | # You should have received a copy of the GNU Lesser General Public License | 17 | # You should have received a copy of the GNU Lesser General Public License |
307 | 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/>. |
308 | 19 | 19 | ||
309 | 20 | import os | ||
310 | 21 | import subprocess | ||
311 | 22 | |||
312 | 20 | from charmhelpers.fetch import apt_install, apt_update | 23 | from charmhelpers.fetch import apt_install, apt_update |
314 | 21 | from charmhelpers.core.hookenv import log | 24 | from charmhelpers.core.hookenv import charm_dir, log |
315 | 22 | 25 | ||
316 | 23 | try: | 26 | try: |
317 | 24 | from pip import main as pip_execute | 27 | from pip import main as pip_execute |
318 | @@ -51,11 +54,15 @@ | |||
319 | 51 | pip_execute(command) | 54 | pip_execute(command) |
320 | 52 | 55 | ||
321 | 53 | 56 | ||
323 | 54 | def pip_install(package, fatal=False, upgrade=False, **options): | 57 | def pip_install(package, fatal=False, upgrade=False, venv=None, **options): |
324 | 55 | """Install a python package""" | 58 | """Install a python package""" |
326 | 56 | command = ["install"] | 59 | if venv: |
327 | 60 | venv_python = os.path.join(venv, 'bin/pip') | ||
328 | 61 | command = [venv_python, "install"] | ||
329 | 62 | else: | ||
330 | 63 | command = ["install"] | ||
331 | 57 | 64 | ||
333 | 58 | available_options = ('proxy', 'src', 'log', "index-url", ) | 65 | available_options = ('proxy', 'src', 'log', 'index-url', ) |
334 | 59 | for option in parse_options(options, available_options): | 66 | for option in parse_options(options, available_options): |
335 | 60 | command.append(option) | 67 | command.append(option) |
336 | 61 | 68 | ||
337 | @@ -69,7 +76,10 @@ | |||
338 | 69 | 76 | ||
339 | 70 | log("Installing {} package with options: {}".format(package, | 77 | log("Installing {} package with options: {}".format(package, |
340 | 71 | command)) | 78 | command)) |
342 | 72 | pip_execute(command) | 79 | if venv: |
343 | 80 | subprocess.check_call(command) | ||
344 | 81 | else: | ||
345 | 82 | pip_execute(command) | ||
346 | 73 | 83 | ||
347 | 74 | 84 | ||
348 | 75 | def pip_uninstall(package, **options): | 85 | def pip_uninstall(package, **options): |
349 | @@ -94,3 +104,16 @@ | |||
350 | 94 | """Returns the list of current python installed packages | 104 | """Returns the list of current python installed packages |
351 | 95 | """ | 105 | """ |
352 | 96 | return pip_execute(["list"]) | 106 | return pip_execute(["list"]) |
353 | 107 | |||
354 | 108 | |||
355 | 109 | def pip_create_virtualenv(path=None): | ||
356 | 110 | """Create an isolated Python environment.""" | ||
357 | 111 | apt_install('python-virtualenv') | ||
358 | 112 | |||
359 | 113 | if path: | ||
360 | 114 | venv_path = path | ||
361 | 115 | else: | ||
362 | 116 | venv_path = os.path.join(charm_dir(), 'venv') | ||
363 | 117 | |||
364 | 118 | if not os.path.exists(venv_path): | ||
365 | 119 | subprocess.check_call(['virtualenv', venv_path]) | ||
366 | 97 | 120 | ||
367 | === modified file 'hooks/charmhelpers/core/hookenv.py' | |||
368 | --- hooks/charmhelpers/core/hookenv.py 2015-04-16 10:29:48 +0000 | |||
369 | +++ hooks/charmhelpers/core/hookenv.py 2015-06-04 08:45:30 +0000 | |||
370 | @@ -21,12 +21,14 @@ | |||
371 | 21 | # Charm Helpers Developers <juju@lists.ubuntu.com> | 21 | # Charm Helpers Developers <juju@lists.ubuntu.com> |
372 | 22 | 22 | ||
373 | 23 | from __future__ import print_function | 23 | from __future__ import print_function |
374 | 24 | from functools import wraps | ||
375 | 24 | import os | 25 | import os |
376 | 25 | import json | 26 | import json |
377 | 26 | import yaml | 27 | import yaml |
378 | 27 | import subprocess | 28 | import subprocess |
379 | 28 | import sys | 29 | import sys |
380 | 29 | import errno | 30 | import errno |
381 | 31 | import tempfile | ||
382 | 30 | from subprocess import CalledProcessError | 32 | from subprocess import CalledProcessError |
383 | 31 | 33 | ||
384 | 32 | import six | 34 | import six |
385 | @@ -58,15 +60,17 @@ | |||
386 | 58 | 60 | ||
387 | 59 | will cache the result of unit_get + 'test' for future calls. | 61 | will cache the result of unit_get + 'test' for future calls. |
388 | 60 | """ | 62 | """ |
389 | 63 | @wraps(func) | ||
390 | 61 | def wrapper(*args, **kwargs): | 64 | def wrapper(*args, **kwargs): |
391 | 62 | global cache | 65 | global cache |
392 | 63 | key = str((func, args, kwargs)) | 66 | key = str((func, args, kwargs)) |
393 | 64 | try: | 67 | try: |
394 | 65 | return cache[key] | 68 | return cache[key] |
395 | 66 | except KeyError: | 69 | except KeyError: |
399 | 67 | res = func(*args, **kwargs) | 70 | pass # Drop out of the exception handler scope. |
400 | 68 | cache[key] = res | 71 | res = func(*args, **kwargs) |
401 | 69 | return res | 72 | cache[key] = res |
402 | 73 | return res | ||
403 | 70 | return wrapper | 74 | return wrapper |
404 | 71 | 75 | ||
405 | 72 | 76 | ||
406 | @@ -178,7 +182,7 @@ | |||
407 | 178 | 182 | ||
408 | 179 | def remote_unit(): | 183 | def remote_unit(): |
409 | 180 | """The remote unit for the current relation hook""" | 184 | """The remote unit for the current relation hook""" |
411 | 181 | return os.environ['JUJU_REMOTE_UNIT'] | 185 | return os.environ.get('JUJU_REMOTE_UNIT', None) |
412 | 182 | 186 | ||
413 | 183 | 187 | ||
414 | 184 | def service_name(): | 188 | def service_name(): |
415 | @@ -250,6 +254,12 @@ | |||
416 | 250 | except KeyError: | 254 | except KeyError: |
417 | 251 | return (self._prev_dict or {})[key] | 255 | return (self._prev_dict or {})[key] |
418 | 252 | 256 | ||
419 | 257 | def get(self, key, default=None): | ||
420 | 258 | try: | ||
421 | 259 | return self[key] | ||
422 | 260 | except KeyError: | ||
423 | 261 | return default | ||
424 | 262 | |||
425 | 253 | def keys(self): | 263 | def keys(self): |
426 | 254 | prev_keys = [] | 264 | prev_keys = [] |
427 | 255 | if self._prev_dict is not None: | 265 | if self._prev_dict is not None: |
428 | @@ -353,18 +363,49 @@ | |||
429 | 353 | """Set relation information for the current unit""" | 363 | """Set relation information for the current unit""" |
430 | 354 | relation_settings = relation_settings if relation_settings else {} | 364 | relation_settings = relation_settings if relation_settings else {} |
431 | 355 | relation_cmd_line = ['relation-set'] | 365 | relation_cmd_line = ['relation-set'] |
432 | 366 | accepts_file = "--file" in subprocess.check_output( | ||
433 | 367 | relation_cmd_line + ["--help"], universal_newlines=True) | ||
434 | 356 | if relation_id is not None: | 368 | if relation_id is not None: |
435 | 357 | relation_cmd_line.extend(('-r', relation_id)) | 369 | relation_cmd_line.extend(('-r', relation_id)) |
442 | 358 | for k, v in (list(relation_settings.items()) + list(kwargs.items())): | 370 | settings = relation_settings.copy() |
443 | 359 | if v is None: | 371 | settings.update(kwargs) |
444 | 360 | relation_cmd_line.append('{}='.format(k)) | 372 | for key, value in settings.items(): |
445 | 361 | else: | 373 | # Force value to be a string: it always should, but some call |
446 | 362 | relation_cmd_line.append('{}={}'.format(k, v)) | 374 | # sites pass in things like dicts or numbers. |
447 | 363 | subprocess.check_call(relation_cmd_line) | 375 | if value is not None: |
448 | 376 | settings[key] = "{}".format(value) | ||
449 | 377 | if accepts_file: | ||
450 | 378 | # --file was introduced in Juju 1.23.2. Use it by default if | ||
451 | 379 | # available, since otherwise we'll break if the relation data is | ||
452 | 380 | # too big. Ideally we should tell relation-set to read the data from | ||
453 | 381 | # stdin, but that feature is broken in 1.23.2: Bug #1454678. | ||
454 | 382 | with tempfile.NamedTemporaryFile(delete=False) as settings_file: | ||
455 | 383 | settings_file.write(yaml.safe_dump(settings).encode("utf-8")) | ||
456 | 384 | subprocess.check_call( | ||
457 | 385 | relation_cmd_line + ["--file", settings_file.name]) | ||
458 | 386 | os.remove(settings_file.name) | ||
459 | 387 | else: | ||
460 | 388 | for key, value in settings.items(): | ||
461 | 389 | if value is None: | ||
462 | 390 | relation_cmd_line.append('{}='.format(key)) | ||
463 | 391 | else: | ||
464 | 392 | relation_cmd_line.append('{}={}'.format(key, value)) | ||
465 | 393 | subprocess.check_call(relation_cmd_line) | ||
466 | 364 | # Flush cache of any relation-gets for local unit | 394 | # Flush cache of any relation-gets for local unit |
467 | 365 | flush(local_unit()) | 395 | flush(local_unit()) |
468 | 366 | 396 | ||
469 | 367 | 397 | ||
470 | 398 | def relation_clear(r_id=None): | ||
471 | 399 | ''' Clears any relation data already set on relation r_id ''' | ||
472 | 400 | settings = relation_get(rid=r_id, | ||
473 | 401 | unit=local_unit()) | ||
474 | 402 | for setting in settings: | ||
475 | 403 | if setting not in ['public-address', 'private-address']: | ||
476 | 404 | settings[setting] = None | ||
477 | 405 | relation_set(relation_id=r_id, | ||
478 | 406 | **settings) | ||
479 | 407 | |||
480 | 408 | |||
481 | 368 | @cached | 409 | @cached |
482 | 369 | def relation_ids(reltype=None): | 410 | def relation_ids(reltype=None): |
483 | 370 | """A list of relation_ids""" | 411 | """A list of relation_ids""" |
484 | @@ -509,6 +550,11 @@ | |||
485 | 509 | return None | 550 | return None |
486 | 510 | 551 | ||
487 | 511 | 552 | ||
488 | 553 | def unit_public_ip(): | ||
489 | 554 | """Get this unit's public IP address""" | ||
490 | 555 | return unit_get('public-address') | ||
491 | 556 | |||
492 | 557 | |||
493 | 512 | def unit_private_ip(): | 558 | def unit_private_ip(): |
494 | 513 | """Get this unit's private IP address""" | 559 | """Get this unit's private IP address""" |
495 | 514 | return unit_get('private-address') | 560 | return unit_get('private-address') |
496 | @@ -605,3 +651,94 @@ | |||
497 | 605 | 651 | ||
498 | 606 | The results set by action_set are preserved.""" | 652 | The results set by action_set are preserved.""" |
499 | 607 | subprocess.check_call(['action-fail', message]) | 653 | subprocess.check_call(['action-fail', message]) |
500 | 654 | |||
501 | 655 | |||
502 | 656 | def status_set(workload_state, message): | ||
503 | 657 | """Set the workload state with a message | ||
504 | 658 | |||
505 | 659 | Use status-set to set the workload state with a message which is visible | ||
506 | 660 | to the user via juju status. If the status-set command is not found then | ||
507 | 661 | assume this is juju < 1.23 and juju-log the message unstead. | ||
508 | 662 | |||
509 | 663 | workload_state -- valid juju workload state. | ||
510 | 664 | message -- status update message | ||
511 | 665 | """ | ||
512 | 666 | valid_states = ['maintenance', 'blocked', 'waiting', 'active'] | ||
513 | 667 | if workload_state not in valid_states: | ||
514 | 668 | raise ValueError( | ||
515 | 669 | '{!r} is not a valid workload state'.format(workload_state) | ||
516 | 670 | ) | ||
517 | 671 | cmd = ['status-set', workload_state, message] | ||
518 | 672 | try: | ||
519 | 673 | ret = subprocess.call(cmd) | ||
520 | 674 | if ret == 0: | ||
521 | 675 | return | ||
522 | 676 | except OSError as e: | ||
523 | 677 | if e.errno != errno.ENOENT: | ||
524 | 678 | raise | ||
525 | 679 | log_message = 'status-set failed: {} {}'.format(workload_state, | ||
526 | 680 | message) | ||
527 | 681 | log(log_message, level='INFO') | ||
528 | 682 | |||
529 | 683 | |||
530 | 684 | def status_get(): | ||
531 | 685 | """Retrieve the previously set juju workload state | ||
532 | 686 | |||
533 | 687 | If the status-set command is not found then assume this is juju < 1.23 and | ||
534 | 688 | return 'unknown' | ||
535 | 689 | """ | ||
536 | 690 | cmd = ['status-get'] | ||
537 | 691 | try: | ||
538 | 692 | raw_status = subprocess.check_output(cmd, universal_newlines=True) | ||
539 | 693 | status = raw_status.rstrip() | ||
540 | 694 | return status | ||
541 | 695 | except OSError as e: | ||
542 | 696 | if e.errno == errno.ENOENT: | ||
543 | 697 | return 'unknown' | ||
544 | 698 | else: | ||
545 | 699 | raise | ||
546 | 700 | |||
547 | 701 | |||
548 | 702 | def translate_exc(from_exc, to_exc): | ||
549 | 703 | def inner_translate_exc1(f): | ||
550 | 704 | def inner_translate_exc2(*args, **kwargs): | ||
551 | 705 | try: | ||
552 | 706 | return f(*args, **kwargs) | ||
553 | 707 | except from_exc: | ||
554 | 708 | raise to_exc | ||
555 | 709 | |||
556 | 710 | return inner_translate_exc2 | ||
557 | 711 | |||
558 | 712 | return inner_translate_exc1 | ||
559 | 713 | |||
560 | 714 | |||
561 | 715 | @translate_exc(from_exc=OSError, to_exc=NotImplementedError) | ||
562 | 716 | def is_leader(): | ||
563 | 717 | """Does the current unit hold the juju leadership | ||
564 | 718 | |||
565 | 719 | Uses juju to determine whether the current unit is the leader of its peers | ||
566 | 720 | """ | ||
567 | 721 | cmd = ['is-leader', '--format=json'] | ||
568 | 722 | return json.loads(subprocess.check_output(cmd).decode('UTF-8')) | ||
569 | 723 | |||
570 | 724 | |||
571 | 725 | @translate_exc(from_exc=OSError, to_exc=NotImplementedError) | ||
572 | 726 | def leader_get(attribute=None): | ||
573 | 727 | """Juju leader get value(s)""" | ||
574 | 728 | cmd = ['leader-get', '--format=json'] + [attribute or '-'] | ||
575 | 729 | return json.loads(subprocess.check_output(cmd).decode('UTF-8')) | ||
576 | 730 | |||
577 | 731 | |||
578 | 732 | @translate_exc(from_exc=OSError, to_exc=NotImplementedError) | ||
579 | 733 | def leader_set(settings=None, **kwargs): | ||
580 | 734 | """Juju leader set value(s)""" | ||
581 | 735 | log("Juju leader-set '%s'" % (settings), level=DEBUG) | ||
582 | 736 | cmd = ['leader-set'] | ||
583 | 737 | settings = settings or {} | ||
584 | 738 | settings.update(kwargs) | ||
585 | 739 | for k, v in settings.iteritems(): | ||
586 | 740 | if v is None: | ||
587 | 741 | cmd.append('{}='.format(k)) | ||
588 | 742 | else: | ||
589 | 743 | cmd.append('{}={}'.format(k, v)) | ||
590 | 744 | subprocess.check_call(cmd) | ||
591 | 608 | 745 | ||
592 | === modified file 'hooks/charmhelpers/core/host.py' | |||
593 | --- hooks/charmhelpers/core/host.py 2015-04-01 12:18:48 +0000 | |||
594 | +++ hooks/charmhelpers/core/host.py 2015-06-04 08:45:30 +0000 | |||
595 | @@ -90,7 +90,7 @@ | |||
596 | 90 | ['service', service_name, 'status'], | 90 | ['service', service_name, 'status'], |
597 | 91 | stderr=subprocess.STDOUT).decode('UTF-8') | 91 | stderr=subprocess.STDOUT).decode('UTF-8') |
598 | 92 | except subprocess.CalledProcessError as e: | 92 | except subprocess.CalledProcessError as e: |
600 | 93 | return 'unrecognized service' not in e.output | 93 | return b'unrecognized service' not in e.output |
601 | 94 | else: | 94 | else: |
602 | 95 | return True | 95 | return True |
603 | 96 | 96 | ||
604 | 97 | 97 | ||
605 | === modified file 'hooks/charmhelpers/core/services/base.py' | |||
606 | --- hooks/charmhelpers/core/services/base.py 2015-01-26 09:36:58 +0000 | |||
607 | +++ hooks/charmhelpers/core/services/base.py 2015-06-04 08:45:30 +0000 | |||
608 | @@ -15,9 +15,9 @@ | |||
609 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. |
610 | 16 | 16 | ||
611 | 17 | import os | 17 | import os |
612 | 18 | import re | ||
613 | 19 | import json | 18 | import json |
615 | 20 | from collections import Iterable | 19 | from inspect import getargspec |
616 | 20 | from collections import Iterable, OrderedDict | ||
617 | 21 | 21 | ||
618 | 22 | from charmhelpers.core import host | 22 | from charmhelpers.core import host |
619 | 23 | from charmhelpers.core import hookenv | 23 | from charmhelpers.core import hookenv |
620 | @@ -119,7 +119,7 @@ | |||
621 | 119 | """ | 119 | """ |
622 | 120 | self._ready_file = os.path.join(hookenv.charm_dir(), 'READY-SERVICES.json') | 120 | self._ready_file = os.path.join(hookenv.charm_dir(), 'READY-SERVICES.json') |
623 | 121 | self._ready = None | 121 | self._ready = None |
625 | 122 | self.services = {} | 122 | self.services = OrderedDict() |
626 | 123 | for service in services or []: | 123 | for service in services or []: |
627 | 124 | service_name = service['service'] | 124 | service_name = service['service'] |
628 | 125 | self.services[service_name] = service | 125 | self.services[service_name] = service |
629 | @@ -132,8 +132,8 @@ | |||
630 | 132 | if hook_name == 'stop': | 132 | if hook_name == 'stop': |
631 | 133 | self.stop_services() | 133 | self.stop_services() |
632 | 134 | else: | 134 | else: |
633 | 135 | self.reconfigure_services() | ||
634 | 135 | self.provide_data() | 136 | self.provide_data() |
635 | 136 | self.reconfigure_services() | ||
636 | 137 | cfg = hookenv.config() | 137 | cfg = hookenv.config() |
637 | 138 | if cfg.implicit_save: | 138 | if cfg.implicit_save: |
638 | 139 | cfg.save() | 139 | cfg.save() |
639 | @@ -145,15 +145,36 @@ | |||
640 | 145 | A provider must have a `name` attribute, which indicates which relation | 145 | A provider must have a `name` attribute, which indicates which relation |
641 | 146 | to set data on, and a `provide_data()` method, which returns a dict of | 146 | to set data on, and a `provide_data()` method, which returns a dict of |
642 | 147 | data to set. | 147 | data to set. |
643 | 148 | |||
644 | 149 | The `provide_data()` method can optionally accept two parameters: | ||
645 | 150 | |||
646 | 151 | * ``remote_service`` The name of the remote service that the data will | ||
647 | 152 | be provided to. The `provide_data()` method will be called once | ||
648 | 153 | for each connected service (not unit). This allows the method to | ||
649 | 154 | tailor its data to the given service. | ||
650 | 155 | * ``service_ready`` Whether or not the service definition had all of | ||
651 | 156 | its requirements met, and thus the ``data_ready`` callbacks run. | ||
652 | 157 | |||
653 | 158 | Note that the ``provided_data`` methods are now called **after** the | ||
654 | 159 | ``data_ready`` callbacks are run. This gives the ``data_ready`` callbacks | ||
655 | 160 | a chance to generate any data necessary for the providing to the remote | ||
656 | 161 | services. | ||
657 | 148 | """ | 162 | """ |
660 | 149 | hook_name = hookenv.hook_name() | 163 | for service_name, service in self.services.items(): |
661 | 150 | for service in self.services.values(): | 164 | service_ready = self.is_ready(service_name) |
662 | 151 | for provider in service.get('provided_data', []): | 165 | for provider in service.get('provided_data', []): |
668 | 152 | if re.match(r'{}-relation-(joined|changed)'.format(provider.name), hook_name): | 166 | for relid in hookenv.relation_ids(provider.name): |
669 | 153 | data = provider.provide_data() | 167 | units = hookenv.related_units(relid) |
670 | 154 | _ready = provider._is_ready(data) if hasattr(provider, '_is_ready') else data | 168 | if not units: |
671 | 155 | if _ready: | 169 | continue |
672 | 156 | hookenv.relation_set(None, data) | 170 | remote_service = units[0].split('/')[0] |
673 | 171 | argspec = getargspec(provider.provide_data) | ||
674 | 172 | if len(argspec.args) > 1: | ||
675 | 173 | data = provider.provide_data(remote_service, service_ready) | ||
676 | 174 | else: | ||
677 | 175 | data = provider.provide_data() | ||
678 | 176 | if data: | ||
679 | 177 | hookenv.relation_set(relid, data) | ||
680 | 157 | 178 | ||
681 | 158 | def reconfigure_services(self, *service_names): | 179 | def reconfigure_services(self, *service_names): |
682 | 159 | """ | 180 | """ |
683 | 160 | 181 | ||
684 | === modified file 'hooks/charmhelpers/fetch/__init__.py' | |||
685 | --- hooks/charmhelpers/fetch/__init__.py 2015-01-26 09:36:58 +0000 | |||
686 | +++ hooks/charmhelpers/fetch/__init__.py 2015-06-04 08:45:30 +0000 | |||
687 | @@ -158,7 +158,7 @@ | |||
688 | 158 | 158 | ||
689 | 159 | def apt_cache(in_memory=True): | 159 | def apt_cache(in_memory=True): |
690 | 160 | """Build and return an apt cache""" | 160 | """Build and return an apt cache""" |
692 | 161 | import apt_pkg | 161 | from apt import apt_pkg |
693 | 162 | apt_pkg.init() | 162 | apt_pkg.init() |
694 | 163 | if in_memory: | 163 | if in_memory: |
695 | 164 | apt_pkg.config.set("Dir::Cache::pkgcache", "") | 164 | apt_pkg.config.set("Dir::Cache::pkgcache", "") |
696 | 165 | 165 | ||
697 | === modified file 'hooks/charmhelpers/fetch/giturl.py' | |||
698 | --- hooks/charmhelpers/fetch/giturl.py 2015-02-24 11:04:31 +0000 | |||
699 | +++ hooks/charmhelpers/fetch/giturl.py 2015-06-04 08:45:30 +0000 | |||
700 | @@ -45,14 +45,16 @@ | |||
701 | 45 | else: | 45 | else: |
702 | 46 | return True | 46 | return True |
703 | 47 | 47 | ||
705 | 48 | def clone(self, source, dest, branch): | 48 | def clone(self, source, dest, branch, depth=None): |
706 | 49 | if not self.can_handle(source): | 49 | if not self.can_handle(source): |
707 | 50 | raise UnhandledSource("Cannot handle {}".format(source)) | 50 | raise UnhandledSource("Cannot handle {}".format(source)) |
708 | 51 | 51 | ||
711 | 52 | repo = Repo.clone_from(source, dest) | 52 | if depth: |
712 | 53 | repo.git.checkout(branch) | 53 | Repo.clone_from(source, dest, branch=branch, depth=depth) |
713 | 54 | else: | ||
714 | 55 | Repo.clone_from(source, dest, branch=branch) | ||
715 | 54 | 56 | ||
717 | 55 | def install(self, source, branch="master", dest=None): | 57 | def install(self, source, branch="master", dest=None, depth=None): |
718 | 56 | url_parts = self.parse_url(source) | 58 | url_parts = self.parse_url(source) |
719 | 57 | branch_name = url_parts.path.strip("/").split("/")[-1] | 59 | branch_name = url_parts.path.strip("/").split("/")[-1] |
720 | 58 | if dest: | 60 | if dest: |
721 | @@ -63,7 +65,7 @@ | |||
722 | 63 | if not os.path.exists(dest_dir): | 65 | if not os.path.exists(dest_dir): |
723 | 64 | mkdir(dest_dir, perms=0o755) | 66 | mkdir(dest_dir, perms=0o755) |
724 | 65 | try: | 67 | try: |
726 | 66 | self.clone(source, dest_dir, branch) | 68 | self.clone(source, dest_dir, branch, depth) |
727 | 67 | except GitCommandError as e: | 69 | except GitCommandError as e: |
728 | 68 | raise UnhandledSource(e.message) | 70 | raise UnhandledSource(e.message) |
729 | 69 | except OSError as e: | 71 | except OSError as e: |