Merge lp:~freyes/charms/trusty/glance-simplestreams-sync/lp1434356 into lp:~openstack-charmers/charms/trusty/glance-simplestreams-sync/next
- Trusty Tahr (14.04)
- lp1434356
- Merge into next
Proposed by
Felipe Reyes
Status: | Merged |
---|---|
Merged at revision: | 56 |
Proposed branch: | lp:~freyes/charms/trusty/glance-simplestreams-sync/lp1434356 |
Merge into: | lp:~openstack-charmers/charms/trusty/glance-simplestreams-sync/next |
Diff against target: |
25126 lines (+11877/-12381) 152 files modified
.bzrignore (+4/-0) .testr.conf (+8/-0) Makefile (+21/-5) charm-helpers-sync.yaml (+4/-1) charmhelpers/__init__.py (+38/-0) charmhelpers/contrib/__init__.py (+15/-0) charmhelpers/contrib/charmsupport/__init__.py (+15/-0) charmhelpers/contrib/charmsupport/nrpe.py (+360/-0) charmhelpers/contrib/charmsupport/volumes.py (+175/-0) charmhelpers/contrib/hahelpers/__init__.py (+15/-0) charmhelpers/contrib/hahelpers/apache.py (+82/-0) charmhelpers/contrib/hahelpers/cluster.py (+316/-0) charmhelpers/contrib/network/__init__.py (+15/-0) charmhelpers/contrib/network/ip.py (+456/-0) charmhelpers/contrib/openstack/__init__.py (+15/-0) charmhelpers/contrib/openstack/alternatives.py (+33/-0) charmhelpers/contrib/openstack/amulet/__init__.py (+15/-0) charmhelpers/contrib/openstack/amulet/deployment.py (+197/-0) charmhelpers/contrib/openstack/amulet/utils.py (+963/-0) charmhelpers/contrib/openstack/context.py (+1427/-0) charmhelpers/contrib/openstack/files/__init__.py (+18/-0) charmhelpers/contrib/openstack/files/check_haproxy.sh (+32/-0) charmhelpers/contrib/openstack/files/check_haproxy_queue_depth.sh (+30/-0) charmhelpers/contrib/openstack/ip.py (+151/-0) charmhelpers/contrib/openstack/neutron.py (+356/-0) charmhelpers/contrib/openstack/templates/__init__.py (+18/-0) charmhelpers/contrib/openstack/templates/ceph.conf (+21/-0) charmhelpers/contrib/openstack/templates/git.upstart (+17/-0) charmhelpers/contrib/openstack/templates/haproxy.cfg (+58/-0) charmhelpers/contrib/openstack/templates/openstack_https_frontend (+24/-0) charmhelpers/contrib/openstack/templates/openstack_https_frontend.conf (+24/-0) charmhelpers/contrib/openstack/templates/section-keystone-authtoken (+9/-0) charmhelpers/contrib/openstack/templates/section-rabbitmq-oslo (+22/-0) charmhelpers/contrib/openstack/templates/section-zeromq (+14/-0) charmhelpers/contrib/openstack/templating.py (+323/-0) charmhelpers/contrib/openstack/utils.py (+977/-0) charmhelpers/contrib/python/__init__.py (+15/-0) charmhelpers/contrib/python/packages.py (+121/-0) charmhelpers/contrib/storage/__init__.py (+15/-0) charmhelpers/contrib/storage/linux/__init__.py (+15/-0) charmhelpers/contrib/storage/linux/ceph.py (+657/-0) charmhelpers/contrib/storage/linux/loopback.py (+78/-0) charmhelpers/contrib/storage/linux/lvm.py (+105/-0) charmhelpers/contrib/storage/linux/utils.py (+71/-0) charmhelpers/core/__init__.py (+15/-0) charmhelpers/core/decorators.py (+57/-0) charmhelpers/core/files.py (+45/-0) charmhelpers/core/fstab.py (+134/-0) charmhelpers/core/hookenv.py (+930/-0) charmhelpers/core/host.py (+586/-0) charmhelpers/core/hugepage.py (+69/-0) charmhelpers/core/kernel.py (+68/-0) charmhelpers/core/services/__init__.py (+18/-0) charmhelpers/core/services/base.py (+353/-0) charmhelpers/core/services/helpers.py (+283/-0) charmhelpers/core/strutils.py (+72/-0) charmhelpers/core/sysctl.py (+56/-0) charmhelpers/core/templating.py (+68/-0) charmhelpers/core/unitdata.py (+521/-0) charmhelpers/fetch/__init__.py (+456/-0) charmhelpers/fetch/archiveurl.py (+167/-0) charmhelpers/fetch/bzrurl.py (+78/-0) charmhelpers/fetch/giturl.py (+73/-0) charmhelpers/payload/__init__.py (+17/-0) charmhelpers/payload/archive.py (+73/-0) charmhelpers/payload/execd.py (+66/-0) hooks/charmhelpers/__init__.py (+0/-38) hooks/charmhelpers/cli/README.rst (+0/-57) hooks/charmhelpers/cli/__init__.py (+0/-147) hooks/charmhelpers/cli/commands.py (+0/-2) hooks/charmhelpers/cli/host.py (+0/-15) hooks/charmhelpers/contrib/__init__.py (+0/-15) hooks/charmhelpers/contrib/ansible/__init__.py (+0/-165) hooks/charmhelpers/contrib/charmhelpers/IMPORT (+0/-4) hooks/charmhelpers/contrib/charmhelpers/__init__.py (+0/-184) hooks/charmhelpers/contrib/charmsupport/__init__.py (+0/-15) hooks/charmhelpers/contrib/charmsupport/nrpe.py (+0/-360) hooks/charmhelpers/contrib/charmsupport/volumes.py (+0/-175) hooks/charmhelpers/contrib/hahelpers/apache.py (+0/-59) hooks/charmhelpers/contrib/hahelpers/cluster.py (+0/-183) hooks/charmhelpers/contrib/jujugui/IMPORT (+0/-4) hooks/charmhelpers/contrib/jujugui/utils.py (+0/-602) hooks/charmhelpers/contrib/network/__init__.py (+0/-15) hooks/charmhelpers/contrib/network/ip.py (+0/-454) hooks/charmhelpers/contrib/network/ovs/__init__.py (+0/-75) hooks/charmhelpers/contrib/openstack/__init__.py (+0/-15) hooks/charmhelpers/contrib/openstack/alternatives.py (+0/-33) hooks/charmhelpers/contrib/openstack/amulet/__init__.py (+0/-15) hooks/charmhelpers/contrib/openstack/amulet/deployment.py (+0/-183) hooks/charmhelpers/contrib/openstack/amulet/utils.py (+0/-604) hooks/charmhelpers/contrib/openstack/context.py (+0/-1372) hooks/charmhelpers/contrib/openstack/files/__init__.py (+0/-18) hooks/charmhelpers/contrib/openstack/files/check_haproxy.sh (+0/-32) hooks/charmhelpers/contrib/openstack/files/check_haproxy_queue_depth.sh (+0/-30) hooks/charmhelpers/contrib/openstack/ip.py (+0/-151) hooks/charmhelpers/contrib/openstack/neutron.py (+0/-356) hooks/charmhelpers/contrib/openstack/templates/__init__.py (+0/-18) hooks/charmhelpers/contrib/openstack/templates/ceph.conf (+0/-15) hooks/charmhelpers/contrib/openstack/templates/git.upstart (+0/-17) hooks/charmhelpers/contrib/openstack/templates/haproxy.cfg (+0/-58) hooks/charmhelpers/contrib/openstack/templates/openstack_https_frontend (+0/-24) hooks/charmhelpers/contrib/openstack/templates/openstack_https_frontend.conf (+0/-24) hooks/charmhelpers/contrib/openstack/templates/section-keystone-authtoken (+0/-9) hooks/charmhelpers/contrib/openstack/templates/section-rabbitmq-oslo (+0/-22) hooks/charmhelpers/contrib/openstack/templates/section-zeromq (+0/-14) hooks/charmhelpers/contrib/openstack/templating.py (+0/-295) hooks/charmhelpers/contrib/openstack/utils.py (+0/-751) hooks/charmhelpers/contrib/peerstorage/__init__.py (+0/-83) hooks/charmhelpers/contrib/python/__init__.py (+0/-15) hooks/charmhelpers/contrib/python/packages.py (+0/-121) hooks/charmhelpers/contrib/python/version.py (+0/-18) hooks/charmhelpers/contrib/saltstack/__init__.py (+0/-102) hooks/charmhelpers/contrib/ssl/__init__.py (+0/-78) hooks/charmhelpers/contrib/ssl/service.py (+0/-267) hooks/charmhelpers/contrib/storage/linux/ceph.py (+0/-387) hooks/charmhelpers/contrib/storage/linux/loopback.py (+0/-62) hooks/charmhelpers/contrib/storage/linux/lvm.py (+0/-88) hooks/charmhelpers/contrib/storage/linux/utils.py (+0/-49) hooks/charmhelpers/contrib/templating/contexts.py (+0/-104) hooks/charmhelpers/contrib/templating/pyformat.py (+0/-13) hooks/charmhelpers/contrib/unison/__init__.py (+0/-257) hooks/charmhelpers/core/__init__.py (+0/-15) hooks/charmhelpers/core/decorators.py (+0/-57) hooks/charmhelpers/core/files.py (+0/-45) hooks/charmhelpers/core/fstab.py (+0/-134) hooks/charmhelpers/core/hookenv.py (+0/-898) hooks/charmhelpers/core/host.py (+0/-586) hooks/charmhelpers/core/hugepage.py (+0/-62) hooks/charmhelpers/core/kernel.py (+0/-68) hooks/charmhelpers/core/services/__init__.py (+0/-18) hooks/charmhelpers/core/services/base.py (+0/-353) hooks/charmhelpers/core/services/helpers.py (+0/-283) hooks/charmhelpers/core/strutils.py (+0/-42) hooks/charmhelpers/core/sysctl.py (+0/-56) hooks/charmhelpers/core/templating.py (+0/-68) hooks/charmhelpers/core/unitdata.py (+0/-521) hooks/charmhelpers/fetch/__init__.py (+0/-456) hooks/charmhelpers/fetch/archiveurl.py (+0/-167) hooks/charmhelpers/fetch/bzrurl.py (+0/-78) hooks/charmhelpers/fetch/giturl.py (+0/-73) hooks/charmhelpers/payload/__init__.py (+0/-1) hooks/charmhelpers/payload/archive.py (+0/-57) hooks/charmhelpers/payload/execd.py (+0/-50) hooks/hooks.py (+23/-17) metadata.yaml (+2/-1) requirements/requirements-precise.txt (+8/-0) requirements/requirements-trusty.txt (+8/-0) requirements/test-requirements.txt (+6/-0) setup.cfg (+6/-0) tox.ini (+36/-0) unit_tests/test_hooks.py (+105/-0) unit_tests/test_utils.py (+133/-0) |
To merge this branch: | bzr merge lp:~freyes/charms/trusty/glance-simplestreams-sync/lp1434356 |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Billy Olsen | Approve | ||
Review via email: mp+271384@code.launchpad.net |
Commit message
Description of the change
Dear OpenStack Charmers,
This MP refactors the config-changed hook handler to fix LP: #1434356 and unit tests.
Best Regards,
To post a comment you must log in.
Revision history for this message
Felipe Reyes (freyes) wrote : | # |
Billy, thanks for taking the time to review it, I'll integrate tox.
Revision history for this message
Felipe Reyes (freyes) wrote : | # |
Billy, I included tox support into this MP and it's ready for review.
Best,
- 67. By Felipe Reyes
-
Use ostestr when running 'make unit_tests'
This change keeps consistency between Makefile and tox
- 68. By Felipe Reyes
-
Adjust mock patches from 'charmhelpers' to 'hooks.
charmhelpers' - 69. By Felipe Reyes
-
Add sitepackages=True to tox.ini
Revision history for this message
Billy Olsen (billy-olsen) wrote : | # |
LGTM. Approved - thanks Felipe!
review:
Approve
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file '.bzrignore' | |||
2 | --- .bzrignore 2015-09-08 16:25:57 +0000 | |||
3 | +++ .bzrignore 2015-10-08 20:44:37 +0000 | |||
4 | @@ -1,1 +1,5 @@ | |||
5 | 1 | bin | 1 | bin |
6 | 2 | .coverage | ||
7 | 3 | .venv | ||
8 | 4 | .testrepository/ | ||
9 | 5 | .tox/ | ||
10 | 2 | 6 | ||
11 | === added file '.testr.conf' | |||
12 | --- .testr.conf 1970-01-01 00:00:00 +0000 | |||
13 | +++ .testr.conf 2015-10-08 20:44:37 +0000 | |||
14 | @@ -0,0 +1,8 @@ | |||
15 | 1 | [DEFAULT] | ||
16 | 2 | test_command=OS_STDOUT_CAPTURE=${OS_STDOUT_CAPTURE:-1} \ | ||
17 | 3 | OS_STDERR_CAPTURE=${OS_STDERR_CAPTURE:-1} \ | ||
18 | 4 | OS_TEST_TIMEOUT=${OS_TEST_TIMEOUT:-60} \ | ||
19 | 5 | ${PYTHON:-python} -m subunit.run discover -t ./ ./unit_tests $LISTOPT $IDOPTION | ||
20 | 6 | |||
21 | 7 | test_id_option=--load-list $IDFILE | ||
22 | 8 | test_list_option=--list | ||
23 | 0 | 9 | ||
24 | === modified file 'Makefile' | |||
25 | --- Makefile 2014-06-18 17:32:48 +0000 | |||
26 | +++ Makefile 2015-10-08 20:44:37 +0000 | |||
27 | @@ -1,13 +1,29 @@ | |||
28 | 1 | #!/usr/bin/make | 1 | #!/usr/bin/make |
29 | 2 | PYTHON := /usr/bin/env python | 2 | PYTHON := /usr/bin/env python |
33 | 3 | 3 | CHARM_DIR := $(PWD) | |
34 | 4 | lint: | 4 | HOOKS_DIR := $(PWD)/hooks |
35 | 5 | @pyflakes hooks/*.py scripts/*.py | 5 | TEST_PREFIX := PYTHONPATH=$(HOOKS_DIR) |
36 | 6 | |||
37 | 7 | clean: | ||
38 | 8 | rm -f .coverage | ||
39 | 9 | find . -name '*.pyc' -delete | ||
40 | 10 | rm -rf .venv | ||
41 | 11 | (which dh_clean && dh_clean) || true | ||
42 | 12 | |||
43 | 13 | .venv: | ||
44 | 14 | dpkg -s gcc python-dev python-virtualenv python-apt > /dev/null || sudo apt-get install -y gcc python-dev python-virtualenv python-apt | ||
45 | 15 | virtualenv .venv --system-site-packages | ||
46 | 16 | .venv/bin/pip install -I \ | ||
47 | 17 | -r requirements/requirements-trusty.txt \ | ||
48 | 18 | -r requirements/test-requirements.txt | ||
49 | 19 | |||
50 | 20 | lint: clean .venv | ||
51 | 21 | .venv/bin/flake8 hooks/*.py scripts/*.py unit_tests/*.py | ||
52 | 6 | @charm proof | 22 | @charm proof |
53 | 7 | 23 | ||
55 | 8 | test: | 24 | unit_tests: clean .venv |
56 | 9 | @echo Starting tests... | 25 | @echo Starting tests... |
58 | 10 | @$(PYTHON) /usr/bin/nosetests --nologcapture --with-coverage -v unit_tests | 26 | env CHARM_DIR=$(CHARM_DIR) $(TEST_PREFIX) .venv/bin/ostestr |
59 | 11 | 27 | ||
60 | 12 | 28 | ||
61 | 13 | bin/charm_helpers_sync.py: | 29 | bin/charm_helpers_sync.py: |
62 | 14 | 30 | ||
63 | === modified file 'charm-helpers-sync.yaml' | |||
64 | --- charm-helpers-sync.yaml 2015-09-08 16:39:40 +0000 | |||
65 | +++ charm-helpers-sync.yaml 2015-10-08 20:44:37 +0000 | |||
66 | @@ -1,9 +1,12 @@ | |||
67 | 1 | branch: lp:charm-helpers | 1 | branch: lp:charm-helpers |
69 | 2 | destination: hooks/charmhelpers | 2 | destination: charmhelpers |
70 | 3 | include: | 3 | include: |
71 | 4 | - core | 4 | - core |
72 | 5 | - fetch | 5 | - fetch |
73 | 6 | - payload | ||
74 | 6 | - contrib.openstack|inc=* | 7 | - contrib.openstack|inc=* |
75 | 7 | - contrib.charmsupport | 8 | - contrib.charmsupport |
76 | 8 | - contrib.network.ip | 9 | - contrib.network.ip |
77 | 9 | - contrib.python.packages | 10 | - contrib.python.packages |
78 | 11 | - contrib.hahelpers | ||
79 | 12 | - contrib.storage | ||
80 | 10 | 13 | ||
81 | === added directory 'charmhelpers' | |||
82 | === added file 'charmhelpers/__init__.py' | |||
83 | --- charmhelpers/__init__.py 1970-01-01 00:00:00 +0000 | |||
84 | +++ charmhelpers/__init__.py 2015-10-08 20:44:37 +0000 | |||
85 | @@ -0,0 +1,38 @@ | |||
86 | 1 | # Copyright 2014-2015 Canonical Limited. | ||
87 | 2 | # | ||
88 | 3 | # This file is part of charm-helpers. | ||
89 | 4 | # | ||
90 | 5 | # charm-helpers is free software: you can redistribute it and/or modify | ||
91 | 6 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
92 | 7 | # published by the Free Software Foundation. | ||
93 | 8 | # | ||
94 | 9 | # charm-helpers is distributed in the hope that it will be useful, | ||
95 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
96 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
97 | 12 | # GNU Lesser General Public License for more details. | ||
98 | 13 | # | ||
99 | 14 | # You should have received a copy of the GNU Lesser General Public License | ||
100 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
101 | 16 | |||
102 | 17 | # Bootstrap charm-helpers, installing its dependencies if necessary using | ||
103 | 18 | # only standard libraries. | ||
104 | 19 | import subprocess | ||
105 | 20 | import sys | ||
106 | 21 | |||
107 | 22 | try: | ||
108 | 23 | import six # flake8: noqa | ||
109 | 24 | except ImportError: | ||
110 | 25 | if sys.version_info.major == 2: | ||
111 | 26 | subprocess.check_call(['apt-get', 'install', '-y', 'python-six']) | ||
112 | 27 | else: | ||
113 | 28 | subprocess.check_call(['apt-get', 'install', '-y', 'python3-six']) | ||
114 | 29 | import six # flake8: noqa | ||
115 | 30 | |||
116 | 31 | try: | ||
117 | 32 | import yaml # flake8: noqa | ||
118 | 33 | except ImportError: | ||
119 | 34 | if sys.version_info.major == 2: | ||
120 | 35 | subprocess.check_call(['apt-get', 'install', '-y', 'python-yaml']) | ||
121 | 36 | else: | ||
122 | 37 | subprocess.check_call(['apt-get', 'install', '-y', 'python3-yaml']) | ||
123 | 38 | import yaml # flake8: noqa | ||
124 | 0 | 39 | ||
125 | === added directory 'charmhelpers/contrib' | |||
126 | === added file 'charmhelpers/contrib/__init__.py' | |||
127 | --- charmhelpers/contrib/__init__.py 1970-01-01 00:00:00 +0000 | |||
128 | +++ charmhelpers/contrib/__init__.py 2015-10-08 20:44:37 +0000 | |||
129 | @@ -0,0 +1,15 @@ | |||
130 | 1 | # Copyright 2014-2015 Canonical Limited. | ||
131 | 2 | # | ||
132 | 3 | # This file is part of charm-helpers. | ||
133 | 4 | # | ||
134 | 5 | # charm-helpers is free software: you can redistribute it and/or modify | ||
135 | 6 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
136 | 7 | # published by the Free Software Foundation. | ||
137 | 8 | # | ||
138 | 9 | # charm-helpers is distributed in the hope that it will be useful, | ||
139 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
140 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
141 | 12 | # GNU Lesser General Public License for more details. | ||
142 | 13 | # | ||
143 | 14 | # You should have received a copy of the GNU Lesser General Public License | ||
144 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
145 | 0 | 16 | ||
146 | === added directory 'charmhelpers/contrib/charmsupport' | |||
147 | === added file 'charmhelpers/contrib/charmsupport/__init__.py' | |||
148 | --- charmhelpers/contrib/charmsupport/__init__.py 1970-01-01 00:00:00 +0000 | |||
149 | +++ charmhelpers/contrib/charmsupport/__init__.py 2015-10-08 20:44:37 +0000 | |||
150 | @@ -0,0 +1,15 @@ | |||
151 | 1 | # Copyright 2014-2015 Canonical Limited. | ||
152 | 2 | # | ||
153 | 3 | # This file is part of charm-helpers. | ||
154 | 4 | # | ||
155 | 5 | # charm-helpers is free software: you can redistribute it and/or modify | ||
156 | 6 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
157 | 7 | # published by the Free Software Foundation. | ||
158 | 8 | # | ||
159 | 9 | # charm-helpers is distributed in the hope that it will be useful, | ||
160 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
161 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
162 | 12 | # GNU Lesser General Public License for more details. | ||
163 | 13 | # | ||
164 | 14 | # You should have received a copy of the GNU Lesser General Public License | ||
165 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
166 | 0 | 16 | ||
167 | === added file 'charmhelpers/contrib/charmsupport/nrpe.py' | |||
168 | --- charmhelpers/contrib/charmsupport/nrpe.py 1970-01-01 00:00:00 +0000 | |||
169 | +++ charmhelpers/contrib/charmsupport/nrpe.py 2015-10-08 20:44:37 +0000 | |||
170 | @@ -0,0 +1,360 @@ | |||
171 | 1 | # Copyright 2014-2015 Canonical Limited. | ||
172 | 2 | # | ||
173 | 3 | # This file is part of charm-helpers. | ||
174 | 4 | # | ||
175 | 5 | # charm-helpers is free software: you can redistribute it and/or modify | ||
176 | 6 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
177 | 7 | # published by the Free Software Foundation. | ||
178 | 8 | # | ||
179 | 9 | # charm-helpers is distributed in the hope that it will be useful, | ||
180 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
181 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
182 | 12 | # GNU Lesser General Public License for more details. | ||
183 | 13 | # | ||
184 | 14 | # You should have received a copy of the GNU Lesser General Public License | ||
185 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
186 | 16 | |||
187 | 17 | """Compatibility with the nrpe-external-master charm""" | ||
188 | 18 | # Copyright 2012 Canonical Ltd. | ||
189 | 19 | # | ||
190 | 20 | # Authors: | ||
191 | 21 | # Matthew Wedgwood <matthew.wedgwood@canonical.com> | ||
192 | 22 | |||
193 | 23 | import subprocess | ||
194 | 24 | import pwd | ||
195 | 25 | import grp | ||
196 | 26 | import os | ||
197 | 27 | import glob | ||
198 | 28 | import shutil | ||
199 | 29 | import re | ||
200 | 30 | import shlex | ||
201 | 31 | import yaml | ||
202 | 32 | |||
203 | 33 | from charmhelpers.core.hookenv import ( | ||
204 | 34 | config, | ||
205 | 35 | local_unit, | ||
206 | 36 | log, | ||
207 | 37 | relation_ids, | ||
208 | 38 | relation_set, | ||
209 | 39 | relations_of_type, | ||
210 | 40 | ) | ||
211 | 41 | |||
212 | 42 | from charmhelpers.core.host import service | ||
213 | 43 | |||
214 | 44 | # This module adds compatibility with the nrpe-external-master and plain nrpe | ||
215 | 45 | # subordinate charms. To use it in your charm: | ||
216 | 46 | # | ||
217 | 47 | # 1. Update metadata.yaml | ||
218 | 48 | # | ||
219 | 49 | # provides: | ||
220 | 50 | # (...) | ||
221 | 51 | # nrpe-external-master: | ||
222 | 52 | # interface: nrpe-external-master | ||
223 | 53 | # scope: container | ||
224 | 54 | # | ||
225 | 55 | # and/or | ||
226 | 56 | # | ||
227 | 57 | # provides: | ||
228 | 58 | # (...) | ||
229 | 59 | # local-monitors: | ||
230 | 60 | # interface: local-monitors | ||
231 | 61 | # scope: container | ||
232 | 62 | |||
233 | 63 | # | ||
234 | 64 | # 2. Add the following to config.yaml | ||
235 | 65 | # | ||
236 | 66 | # nagios_context: | ||
237 | 67 | # default: "juju" | ||
238 | 68 | # type: string | ||
239 | 69 | # description: | | ||
240 | 70 | # Used by the nrpe subordinate charms. | ||
241 | 71 | # A string that will be prepended to instance name to set the host name | ||
242 | 72 | # in nagios. So for instance the hostname would be something like: | ||
243 | 73 | # juju-myservice-0 | ||
244 | 74 | # If you're running multiple environments with the same services in them | ||
245 | 75 | # this allows you to differentiate between them. | ||
246 | 76 | # nagios_servicegroups: | ||
247 | 77 | # default: "" | ||
248 | 78 | # type: string | ||
249 | 79 | # description: | | ||
250 | 80 | # A comma-separated list of nagios servicegroups. | ||
251 | 81 | # If left empty, the nagios_context will be used as the servicegroup | ||
252 | 82 | # | ||
253 | 83 | # 3. Add custom checks (Nagios plugins) to files/nrpe-external-master | ||
254 | 84 | # | ||
255 | 85 | # 4. Update your hooks.py with something like this: | ||
256 | 86 | # | ||
257 | 87 | # from charmsupport.nrpe import NRPE | ||
258 | 88 | # (...) | ||
259 | 89 | # def update_nrpe_config(): | ||
260 | 90 | # nrpe_compat = NRPE() | ||
261 | 91 | # nrpe_compat.add_check( | ||
262 | 92 | # shortname = "myservice", | ||
263 | 93 | # description = "Check MyService", | ||
264 | 94 | # check_cmd = "check_http -w 2 -c 10 http://localhost" | ||
265 | 95 | # ) | ||
266 | 96 | # nrpe_compat.add_check( | ||
267 | 97 | # "myservice_other", | ||
268 | 98 | # "Check for widget failures", | ||
269 | 99 | # check_cmd = "/srv/myapp/scripts/widget_check" | ||
270 | 100 | # ) | ||
271 | 101 | # nrpe_compat.write() | ||
272 | 102 | # | ||
273 | 103 | # def config_changed(): | ||
274 | 104 | # (...) | ||
275 | 105 | # update_nrpe_config() | ||
276 | 106 | # | ||
277 | 107 | # def nrpe_external_master_relation_changed(): | ||
278 | 108 | # update_nrpe_config() | ||
279 | 109 | # | ||
280 | 110 | # def local_monitors_relation_changed(): | ||
281 | 111 | # update_nrpe_config() | ||
282 | 112 | # | ||
283 | 113 | # 5. ln -s hooks.py nrpe-external-master-relation-changed | ||
284 | 114 | # ln -s hooks.py local-monitors-relation-changed | ||
285 | 115 | |||
286 | 116 | |||
287 | 117 | class CheckException(Exception): | ||
288 | 118 | pass | ||
289 | 119 | |||
290 | 120 | |||
291 | 121 | class Check(object): | ||
292 | 122 | shortname_re = '[A-Za-z0-9-_]+$' | ||
293 | 123 | service_template = (""" | ||
294 | 124 | #--------------------------------------------------- | ||
295 | 125 | # This file is Juju managed | ||
296 | 126 | #--------------------------------------------------- | ||
297 | 127 | define service {{ | ||
298 | 128 | use active-service | ||
299 | 129 | host_name {nagios_hostname} | ||
300 | 130 | service_description {nagios_hostname}[{shortname}] """ | ||
301 | 131 | """{description} | ||
302 | 132 | check_command check_nrpe!{command} | ||
303 | 133 | servicegroups {nagios_servicegroup} | ||
304 | 134 | }} | ||
305 | 135 | """) | ||
306 | 136 | |||
307 | 137 | def __init__(self, shortname, description, check_cmd): | ||
308 | 138 | super(Check, self).__init__() | ||
309 | 139 | # XXX: could be better to calculate this from the service name | ||
310 | 140 | if not re.match(self.shortname_re, shortname): | ||
311 | 141 | raise CheckException("shortname must match {}".format( | ||
312 | 142 | Check.shortname_re)) | ||
313 | 143 | self.shortname = shortname | ||
314 | 144 | self.command = "check_{}".format(shortname) | ||
315 | 145 | # Note: a set of invalid characters is defined by the | ||
316 | 146 | # Nagios server config | ||
317 | 147 | # The default is: illegal_object_name_chars=`~!$%^&*"|'<>?,()= | ||
318 | 148 | self.description = description | ||
319 | 149 | self.check_cmd = self._locate_cmd(check_cmd) | ||
320 | 150 | |||
321 | 151 | def _locate_cmd(self, check_cmd): | ||
322 | 152 | search_path = ( | ||
323 | 153 | '/usr/lib/nagios/plugins', | ||
324 | 154 | '/usr/local/lib/nagios/plugins', | ||
325 | 155 | ) | ||
326 | 156 | parts = shlex.split(check_cmd) | ||
327 | 157 | for path in search_path: | ||
328 | 158 | if os.path.exists(os.path.join(path, parts[0])): | ||
329 | 159 | command = os.path.join(path, parts[0]) | ||
330 | 160 | if len(parts) > 1: | ||
331 | 161 | command += " " + " ".join(parts[1:]) | ||
332 | 162 | return command | ||
333 | 163 | log('Check command not found: {}'.format(parts[0])) | ||
334 | 164 | return '' | ||
335 | 165 | |||
336 | 166 | def write(self, nagios_context, hostname, nagios_servicegroups): | ||
337 | 167 | nrpe_check_file = '/etc/nagios/nrpe.d/{}.cfg'.format( | ||
338 | 168 | self.command) | ||
339 | 169 | with open(nrpe_check_file, 'w') as nrpe_check_config: | ||
340 | 170 | nrpe_check_config.write("# check {}\n".format(self.shortname)) | ||
341 | 171 | nrpe_check_config.write("command[{}]={}\n".format( | ||
342 | 172 | self.command, self.check_cmd)) | ||
343 | 173 | |||
344 | 174 | if not os.path.exists(NRPE.nagios_exportdir): | ||
345 | 175 | log('Not writing service config as {} is not accessible'.format( | ||
346 | 176 | NRPE.nagios_exportdir)) | ||
347 | 177 | else: | ||
348 | 178 | self.write_service_config(nagios_context, hostname, | ||
349 | 179 | nagios_servicegroups) | ||
350 | 180 | |||
351 | 181 | def write_service_config(self, nagios_context, hostname, | ||
352 | 182 | nagios_servicegroups): | ||
353 | 183 | for f in os.listdir(NRPE.nagios_exportdir): | ||
354 | 184 | if re.search('.*{}.cfg'.format(self.command), f): | ||
355 | 185 | os.remove(os.path.join(NRPE.nagios_exportdir, f)) | ||
356 | 186 | |||
357 | 187 | templ_vars = { | ||
358 | 188 | 'nagios_hostname': hostname, | ||
359 | 189 | 'nagios_servicegroup': nagios_servicegroups, | ||
360 | 190 | 'description': self.description, | ||
361 | 191 | 'shortname': self.shortname, | ||
362 | 192 | 'command': self.command, | ||
363 | 193 | } | ||
364 | 194 | nrpe_service_text = Check.service_template.format(**templ_vars) | ||
365 | 195 | nrpe_service_file = '{}/service__{}_{}.cfg'.format( | ||
366 | 196 | NRPE.nagios_exportdir, hostname, self.command) | ||
367 | 197 | with open(nrpe_service_file, 'w') as nrpe_service_config: | ||
368 | 198 | nrpe_service_config.write(str(nrpe_service_text)) | ||
369 | 199 | |||
370 | 200 | def run(self): | ||
371 | 201 | subprocess.call(self.check_cmd) | ||
372 | 202 | |||
373 | 203 | |||
374 | 204 | class NRPE(object): | ||
375 | 205 | nagios_logdir = '/var/log/nagios' | ||
376 | 206 | nagios_exportdir = '/var/lib/nagios/export' | ||
377 | 207 | nrpe_confdir = '/etc/nagios/nrpe.d' | ||
378 | 208 | |||
379 | 209 | def __init__(self, hostname=None): | ||
380 | 210 | super(NRPE, self).__init__() | ||
381 | 211 | self.config = config() | ||
382 | 212 | self.nagios_context = self.config['nagios_context'] | ||
383 | 213 | if 'nagios_servicegroups' in self.config and self.config['nagios_servicegroups']: | ||
384 | 214 | self.nagios_servicegroups = self.config['nagios_servicegroups'] | ||
385 | 215 | else: | ||
386 | 216 | self.nagios_servicegroups = self.nagios_context | ||
387 | 217 | self.unit_name = local_unit().replace('/', '-') | ||
388 | 218 | if hostname: | ||
389 | 219 | self.hostname = hostname | ||
390 | 220 | else: | ||
391 | 221 | self.hostname = "{}-{}".format(self.nagios_context, self.unit_name) | ||
392 | 222 | self.checks = [] | ||
393 | 223 | |||
394 | 224 | def add_check(self, *args, **kwargs): | ||
395 | 225 | self.checks.append(Check(*args, **kwargs)) | ||
396 | 226 | |||
397 | 227 | def write(self): | ||
398 | 228 | try: | ||
399 | 229 | nagios_uid = pwd.getpwnam('nagios').pw_uid | ||
400 | 230 | nagios_gid = grp.getgrnam('nagios').gr_gid | ||
401 | 231 | except: | ||
402 | 232 | log("Nagios user not set up, nrpe checks not updated") | ||
403 | 233 | return | ||
404 | 234 | |||
405 | 235 | if not os.path.exists(NRPE.nagios_logdir): | ||
406 | 236 | os.mkdir(NRPE.nagios_logdir) | ||
407 | 237 | os.chown(NRPE.nagios_logdir, nagios_uid, nagios_gid) | ||
408 | 238 | |||
409 | 239 | nrpe_monitors = {} | ||
410 | 240 | monitors = {"monitors": {"remote": {"nrpe": nrpe_monitors}}} | ||
411 | 241 | for nrpecheck in self.checks: | ||
412 | 242 | nrpecheck.write(self.nagios_context, self.hostname, | ||
413 | 243 | self.nagios_servicegroups) | ||
414 | 244 | nrpe_monitors[nrpecheck.shortname] = { | ||
415 | 245 | "command": nrpecheck.command, | ||
416 | 246 | } | ||
417 | 247 | |||
418 | 248 | service('restart', 'nagios-nrpe-server') | ||
419 | 249 | |||
420 | 250 | monitor_ids = relation_ids("local-monitors") + \ | ||
421 | 251 | relation_ids("nrpe-external-master") | ||
422 | 252 | for rid in monitor_ids: | ||
423 | 253 | relation_set(relation_id=rid, monitors=yaml.dump(monitors)) | ||
424 | 254 | |||
425 | 255 | |||
426 | 256 | def get_nagios_hostcontext(relation_name='nrpe-external-master'): | ||
427 | 257 | """ | ||
428 | 258 | Query relation with nrpe subordinate, return the nagios_host_context | ||
429 | 259 | |||
430 | 260 | :param str relation_name: Name of relation nrpe sub joined to | ||
431 | 261 | """ | ||
432 | 262 | for rel in relations_of_type(relation_name): | ||
433 | 263 | if 'nagios_hostname' in rel: | ||
434 | 264 | return rel['nagios_host_context'] | ||
435 | 265 | |||
436 | 266 | |||
437 | 267 | def get_nagios_hostname(relation_name='nrpe-external-master'): | ||
438 | 268 | """ | ||
439 | 269 | Query relation with nrpe subordinate, return the nagios_hostname | ||
440 | 270 | |||
441 | 271 | :param str relation_name: Name of relation nrpe sub joined to | ||
442 | 272 | """ | ||
443 | 273 | for rel in relations_of_type(relation_name): | ||
444 | 274 | if 'nagios_hostname' in rel: | ||
445 | 275 | return rel['nagios_hostname'] | ||
446 | 276 | |||
447 | 277 | |||
448 | 278 | def get_nagios_unit_name(relation_name='nrpe-external-master'): | ||
449 | 279 | """ | ||
450 | 280 | Return the nagios unit name prepended with host_context if needed | ||
451 | 281 | |||
452 | 282 | :param str relation_name: Name of relation nrpe sub joined to | ||
453 | 283 | """ | ||
454 | 284 | host_context = get_nagios_hostcontext(relation_name) | ||
455 | 285 | if host_context: | ||
456 | 286 | unit = "%s:%s" % (host_context, local_unit()) | ||
457 | 287 | else: | ||
458 | 288 | unit = local_unit() | ||
459 | 289 | return unit | ||
460 | 290 | |||
461 | 291 | |||
462 | 292 | def add_init_service_checks(nrpe, services, unit_name): | ||
463 | 293 | """ | ||
464 | 294 | Add checks for each service in list | ||
465 | 295 | |||
466 | 296 | :param NRPE nrpe: NRPE object to add check to | ||
467 | 297 | :param list services: List of services to check | ||
468 | 298 | :param str unit_name: Unit name to use in check description | ||
469 | 299 | """ | ||
470 | 300 | for svc in services: | ||
471 | 301 | upstart_init = '/etc/init/%s.conf' % svc | ||
472 | 302 | sysv_init = '/etc/init.d/%s' % svc | ||
473 | 303 | if os.path.exists(upstart_init): | ||
474 | 304 | nrpe.add_check( | ||
475 | 305 | shortname=svc, | ||
476 | 306 | description='process check {%s}' % unit_name, | ||
477 | 307 | check_cmd='check_upstart_job %s' % svc | ||
478 | 308 | ) | ||
479 | 309 | elif os.path.exists(sysv_init): | ||
480 | 310 | cronpath = '/etc/cron.d/nagios-service-check-%s' % svc | ||
481 | 311 | cron_file = ('*/5 * * * * root ' | ||
482 | 312 | '/usr/local/lib/nagios/plugins/check_exit_status.pl ' | ||
483 | 313 | '-s /etc/init.d/%s status > ' | ||
484 | 314 | '/var/lib/nagios/service-check-%s.txt\n' % (svc, | ||
485 | 315 | svc) | ||
486 | 316 | ) | ||
487 | 317 | f = open(cronpath, 'w') | ||
488 | 318 | f.write(cron_file) | ||
489 | 319 | f.close() | ||
490 | 320 | nrpe.add_check( | ||
491 | 321 | shortname=svc, | ||
492 | 322 | description='process check {%s}' % unit_name, | ||
493 | 323 | check_cmd='check_status_file.py -f ' | ||
494 | 324 | '/var/lib/nagios/service-check-%s.txt' % svc, | ||
495 | 325 | ) | ||
496 | 326 | |||
497 | 327 | |||
498 | 328 | def copy_nrpe_checks(): | ||
499 | 329 | """ | ||
500 | 330 | Copy the nrpe checks into place | ||
501 | 331 | |||
502 | 332 | """ | ||
503 | 333 | NAGIOS_PLUGINS = '/usr/local/lib/nagios/plugins' | ||
504 | 334 | nrpe_files_dir = os.path.join(os.getenv('CHARM_DIR'), 'hooks', | ||
505 | 335 | 'charmhelpers', 'contrib', 'openstack', | ||
506 | 336 | 'files') | ||
507 | 337 | |||
508 | 338 | if not os.path.exists(NAGIOS_PLUGINS): | ||
509 | 339 | os.makedirs(NAGIOS_PLUGINS) | ||
510 | 340 | for fname in glob.glob(os.path.join(nrpe_files_dir, "check_*")): | ||
511 | 341 | if os.path.isfile(fname): | ||
512 | 342 | shutil.copy2(fname, | ||
513 | 343 | os.path.join(NAGIOS_PLUGINS, os.path.basename(fname))) | ||
514 | 344 | |||
515 | 345 | |||
516 | 346 | def add_haproxy_checks(nrpe, unit_name): | ||
517 | 347 | """ | ||
518 | 348 | Add checks for each service in list | ||
519 | 349 | |||
520 | 350 | :param NRPE nrpe: NRPE object to add check to | ||
521 | 351 | :param str unit_name: Unit name to use in check description | ||
522 | 352 | """ | ||
523 | 353 | nrpe.add_check( | ||
524 | 354 | shortname='haproxy_servers', | ||
525 | 355 | description='Check HAProxy {%s}' % unit_name, | ||
526 | 356 | check_cmd='check_haproxy.sh') | ||
527 | 357 | nrpe.add_check( | ||
528 | 358 | shortname='haproxy_queue', | ||
529 | 359 | description='Check HAProxy queue depth {%s}' % unit_name, | ||
530 | 360 | check_cmd='check_haproxy_queue_depth.sh') | ||
531 | 0 | 361 | ||
532 | === added file 'charmhelpers/contrib/charmsupport/volumes.py' | |||
533 | --- charmhelpers/contrib/charmsupport/volumes.py 1970-01-01 00:00:00 +0000 | |||
534 | +++ charmhelpers/contrib/charmsupport/volumes.py 2015-10-08 20:44:37 +0000 | |||
535 | @@ -0,0 +1,175 @@ | |||
536 | 1 | # Copyright 2014-2015 Canonical Limited. | ||
537 | 2 | # | ||
538 | 3 | # This file is part of charm-helpers. | ||
539 | 4 | # | ||
540 | 5 | # charm-helpers is free software: you can redistribute it and/or modify | ||
541 | 6 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
542 | 7 | # published by the Free Software Foundation. | ||
543 | 8 | # | ||
544 | 9 | # charm-helpers is distributed in the hope that it will be useful, | ||
545 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
546 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
547 | 12 | # GNU Lesser General Public License for more details. | ||
548 | 13 | # | ||
549 | 14 | # You should have received a copy of the GNU Lesser General Public License | ||
550 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
551 | 16 | |||
552 | 17 | ''' | ||
553 | 18 | Functions for managing volumes in juju units. One volume is supported per unit. | ||
554 | 19 | Subordinates may have their own storage, provided it is on its own partition. | ||
555 | 20 | |||
556 | 21 | Configuration stanzas:: | ||
557 | 22 | |||
558 | 23 | volume-ephemeral: | ||
559 | 24 | type: boolean | ||
560 | 25 | default: true | ||
561 | 26 | description: > | ||
562 | 27 | If false, a volume is mounted as sepecified in "volume-map" | ||
563 | 28 | If true, ephemeral storage will be used, meaning that log data | ||
564 | 29 | will only exist as long as the machine. YOU HAVE BEEN WARNED. | ||
565 | 30 | volume-map: | ||
566 | 31 | type: string | ||
567 | 32 | default: {} | ||
568 | 33 | description: > | ||
569 | 34 | YAML map of units to device names, e.g: | ||
570 | 35 | "{ rsyslog/0: /dev/vdb, rsyslog/1: /dev/vdb }" | ||
571 | 36 | Service units will raise a configure-error if volume-ephemeral | ||
572 | 37 | is 'true' and no volume-map value is set. Use 'juju set' to set a | ||
573 | 38 | value and 'juju resolved' to complete configuration. | ||
574 | 39 | |||
575 | 40 | Usage:: | ||
576 | 41 | |||
577 | 42 | from charmsupport.volumes import configure_volume, VolumeConfigurationError | ||
578 | 43 | from charmsupport.hookenv import log, ERROR | ||
579 | 44 | def post_mount_hook(): | ||
580 | 45 | stop_service('myservice') | ||
581 | 46 | def post_mount_hook(): | ||
582 | 47 | start_service('myservice') | ||
583 | 48 | |||
584 | 49 | if __name__ == '__main__': | ||
585 | 50 | try: | ||
586 | 51 | configure_volume(before_change=pre_mount_hook, | ||
587 | 52 | after_change=post_mount_hook) | ||
588 | 53 | except VolumeConfigurationError: | ||
589 | 54 | log('Storage could not be configured', ERROR) | ||
590 | 55 | |||
591 | 56 | ''' | ||
592 | 57 | |||
593 | 58 | # XXX: Known limitations | ||
594 | 59 | # - fstab is neither consulted nor updated | ||
595 | 60 | |||
596 | 61 | import os | ||
597 | 62 | from charmhelpers.core import hookenv | ||
598 | 63 | from charmhelpers.core import host | ||
599 | 64 | import yaml | ||
600 | 65 | |||
601 | 66 | |||
602 | 67 | MOUNT_BASE = '/srv/juju/volumes' | ||
603 | 68 | |||
604 | 69 | |||
605 | 70 | class VolumeConfigurationError(Exception): | ||
606 | 71 | '''Volume configuration data is missing or invalid''' | ||
607 | 72 | pass | ||
608 | 73 | |||
609 | 74 | |||
610 | 75 | def get_config(): | ||
611 | 76 | '''Gather and sanity-check volume configuration data''' | ||
612 | 77 | volume_config = {} | ||
613 | 78 | config = hookenv.config() | ||
614 | 79 | |||
615 | 80 | errors = False | ||
616 | 81 | |||
617 | 82 | if config.get('volume-ephemeral') in (True, 'True', 'true', 'Yes', 'yes'): | ||
618 | 83 | volume_config['ephemeral'] = True | ||
619 | 84 | else: | ||
620 | 85 | volume_config['ephemeral'] = False | ||
621 | 86 | |||
622 | 87 | try: | ||
623 | 88 | volume_map = yaml.safe_load(config.get('volume-map', '{}')) | ||
624 | 89 | except yaml.YAMLError as e: | ||
625 | 90 | hookenv.log("Error parsing YAML volume-map: {}".format(e), | ||
626 | 91 | hookenv.ERROR) | ||
627 | 92 | errors = True | ||
628 | 93 | if volume_map is None: | ||
629 | 94 | # probably an empty string | ||
630 | 95 | volume_map = {} | ||
631 | 96 | elif not isinstance(volume_map, dict): | ||
632 | 97 | hookenv.log("Volume-map should be a dictionary, not {}".format( | ||
633 | 98 | type(volume_map))) | ||
634 | 99 | errors = True | ||
635 | 100 | |||
636 | 101 | volume_config['device'] = volume_map.get(os.environ['JUJU_UNIT_NAME']) | ||
637 | 102 | if volume_config['device'] and volume_config['ephemeral']: | ||
638 | 103 | # asked for ephemeral storage but also defined a volume ID | ||
639 | 104 | hookenv.log('A volume is defined for this unit, but ephemeral ' | ||
640 | 105 | 'storage was requested', hookenv.ERROR) | ||
641 | 106 | errors = True | ||
642 | 107 | elif not volume_config['device'] and not volume_config['ephemeral']: | ||
643 | 108 | # asked for permanent storage but did not define volume ID | ||
644 | 109 | hookenv.log('Ephemeral storage was requested, but there is no volume ' | ||
645 | 110 | 'defined for this unit.', hookenv.ERROR) | ||
646 | 111 | errors = True | ||
647 | 112 | |||
648 | 113 | unit_mount_name = hookenv.local_unit().replace('/', '-') | ||
649 | 114 | volume_config['mountpoint'] = os.path.join(MOUNT_BASE, unit_mount_name) | ||
650 | 115 | |||
651 | 116 | if errors: | ||
652 | 117 | return None | ||
653 | 118 | return volume_config | ||
654 | 119 | |||
655 | 120 | |||
656 | 121 | def mount_volume(config): | ||
657 | 122 | if os.path.exists(config['mountpoint']): | ||
658 | 123 | if not os.path.isdir(config['mountpoint']): | ||
659 | 124 | hookenv.log('Not a directory: {}'.format(config['mountpoint'])) | ||
660 | 125 | raise VolumeConfigurationError() | ||
661 | 126 | else: | ||
662 | 127 | host.mkdir(config['mountpoint']) | ||
663 | 128 | if os.path.ismount(config['mountpoint']): | ||
664 | 129 | unmount_volume(config) | ||
665 | 130 | if not host.mount(config['device'], config['mountpoint'], persist=True): | ||
666 | 131 | raise VolumeConfigurationError() | ||
667 | 132 | |||
668 | 133 | |||
669 | 134 | def unmount_volume(config): | ||
670 | 135 | if os.path.ismount(config['mountpoint']): | ||
671 | 136 | if not host.umount(config['mountpoint'], persist=True): | ||
672 | 137 | raise VolumeConfigurationError() | ||
673 | 138 | |||
674 | 139 | |||
675 | 140 | def managed_mounts(): | ||
676 | 141 | '''List of all mounted managed volumes''' | ||
677 | 142 | return filter(lambda mount: mount[0].startswith(MOUNT_BASE), host.mounts()) | ||
678 | 143 | |||
679 | 144 | |||
680 | 145 | def configure_volume(before_change=lambda: None, after_change=lambda: None): | ||
681 | 146 | '''Set up storage (or don't) according to the charm's volume configuration. | ||
682 | 147 | Returns the mount point or "ephemeral". before_change and after_change | ||
683 | 148 | are optional functions to be called if the volume configuration changes. | ||
684 | 149 | ''' | ||
685 | 150 | |||
686 | 151 | config = get_config() | ||
687 | 152 | if not config: | ||
688 | 153 | hookenv.log('Failed to read volume configuration', hookenv.CRITICAL) | ||
689 | 154 | raise VolumeConfigurationError() | ||
690 | 155 | |||
691 | 156 | if config['ephemeral']: | ||
692 | 157 | if os.path.ismount(config['mountpoint']): | ||
693 | 158 | before_change() | ||
694 | 159 | unmount_volume(config) | ||
695 | 160 | after_change() | ||
696 | 161 | return 'ephemeral' | ||
697 | 162 | else: | ||
698 | 163 | # persistent storage | ||
699 | 164 | if os.path.ismount(config['mountpoint']): | ||
700 | 165 | mounts = dict(managed_mounts()) | ||
701 | 166 | if mounts.get(config['mountpoint']) != config['device']: | ||
702 | 167 | before_change() | ||
703 | 168 | unmount_volume(config) | ||
704 | 169 | mount_volume(config) | ||
705 | 170 | after_change() | ||
706 | 171 | else: | ||
707 | 172 | before_change() | ||
708 | 173 | mount_volume(config) | ||
709 | 174 | after_change() | ||
710 | 175 | return config['mountpoint'] | ||
711 | 0 | 176 | ||
712 | === added directory 'charmhelpers/contrib/hahelpers' | |||
713 | === added file 'charmhelpers/contrib/hahelpers/__init__.py' | |||
714 | --- charmhelpers/contrib/hahelpers/__init__.py 1970-01-01 00:00:00 +0000 | |||
715 | +++ charmhelpers/contrib/hahelpers/__init__.py 2015-10-08 20:44:37 +0000 | |||
716 | @@ -0,0 +1,15 @@ | |||
717 | 1 | # Copyright 2014-2015 Canonical Limited. | ||
718 | 2 | # | ||
719 | 3 | # This file is part of charm-helpers. | ||
720 | 4 | # | ||
721 | 5 | # charm-helpers is free software: you can redistribute it and/or modify | ||
722 | 6 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
723 | 7 | # published by the Free Software Foundation. | ||
724 | 8 | # | ||
725 | 9 | # charm-helpers is distributed in the hope that it will be useful, | ||
726 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
727 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
728 | 12 | # GNU Lesser General Public License for more details. | ||
729 | 13 | # | ||
730 | 14 | # You should have received a copy of the GNU Lesser General Public License | ||
731 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
732 | 0 | 16 | ||
733 | === added file 'charmhelpers/contrib/hahelpers/apache.py' | |||
734 | --- charmhelpers/contrib/hahelpers/apache.py 1970-01-01 00:00:00 +0000 | |||
735 | +++ charmhelpers/contrib/hahelpers/apache.py 2015-10-08 20:44:37 +0000 | |||
736 | @@ -0,0 +1,82 @@ | |||
737 | 1 | # Copyright 2014-2015 Canonical Limited. | ||
738 | 2 | # | ||
739 | 3 | # This file is part of charm-helpers. | ||
740 | 4 | # | ||
741 | 5 | # charm-helpers is free software: you can redistribute it and/or modify | ||
742 | 6 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
743 | 7 | # published by the Free Software Foundation. | ||
744 | 8 | # | ||
745 | 9 | # charm-helpers is distributed in the hope that it will be useful, | ||
746 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
747 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
748 | 12 | # GNU Lesser General Public License for more details. | ||
749 | 13 | # | ||
750 | 14 | # You should have received a copy of the GNU Lesser General Public License | ||
751 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
752 | 16 | |||
753 | 17 | # | ||
754 | 18 | # Copyright 2012 Canonical Ltd. | ||
755 | 19 | # | ||
756 | 20 | # This file is sourced from lp:openstack-charm-helpers | ||
757 | 21 | # | ||
758 | 22 | # Authors: | ||
759 | 23 | # James Page <james.page@ubuntu.com> | ||
760 | 24 | # Adam Gandelman <adamg@ubuntu.com> | ||
761 | 25 | # | ||
762 | 26 | |||
763 | 27 | import subprocess | ||
764 | 28 | |||
765 | 29 | from charmhelpers.core.hookenv import ( | ||
766 | 30 | config as config_get, | ||
767 | 31 | relation_get, | ||
768 | 32 | relation_ids, | ||
769 | 33 | related_units as relation_list, | ||
770 | 34 | log, | ||
771 | 35 | INFO, | ||
772 | 36 | ) | ||
773 | 37 | |||
774 | 38 | |||
775 | 39 | def get_cert(cn=None): | ||
776 | 40 | # TODO: deal with multiple https endpoints via charm config | ||
777 | 41 | cert = config_get('ssl_cert') | ||
778 | 42 | key = config_get('ssl_key') | ||
779 | 43 | if not (cert and key): | ||
780 | 44 | log("Inspecting identity-service relations for SSL certificate.", | ||
781 | 45 | level=INFO) | ||
782 | 46 | cert = key = None | ||
783 | 47 | if cn: | ||
784 | 48 | ssl_cert_attr = 'ssl_cert_{}'.format(cn) | ||
785 | 49 | ssl_key_attr = 'ssl_key_{}'.format(cn) | ||
786 | 50 | else: | ||
787 | 51 | ssl_cert_attr = 'ssl_cert' | ||
788 | 52 | ssl_key_attr = 'ssl_key' | ||
789 | 53 | for r_id in relation_ids('identity-service'): | ||
790 | 54 | for unit in relation_list(r_id): | ||
791 | 55 | if not cert: | ||
792 | 56 | cert = relation_get(ssl_cert_attr, | ||
793 | 57 | rid=r_id, unit=unit) | ||
794 | 58 | if not key: | ||
795 | 59 | key = relation_get(ssl_key_attr, | ||
796 | 60 | rid=r_id, unit=unit) | ||
797 | 61 | return (cert, key) | ||
798 | 62 | |||
799 | 63 | |||
800 | 64 | def get_ca_cert(): | ||
801 | 65 | ca_cert = config_get('ssl_ca') | ||
802 | 66 | if ca_cert is None: | ||
803 | 67 | log("Inspecting identity-service relations for CA SSL certificate.", | ||
804 | 68 | level=INFO) | ||
805 | 69 | for r_id in relation_ids('identity-service'): | ||
806 | 70 | for unit in relation_list(r_id): | ||
807 | 71 | if ca_cert is None: | ||
808 | 72 | ca_cert = relation_get('ca_cert', | ||
809 | 73 | rid=r_id, unit=unit) | ||
810 | 74 | return ca_cert | ||
811 | 75 | |||
812 | 76 | |||
813 | 77 | def install_ca_cert(ca_cert): | ||
814 | 78 | if ca_cert: | ||
815 | 79 | with open('/usr/local/share/ca-certificates/keystone_juju_ca_cert.crt', | ||
816 | 80 | 'w') as crt: | ||
817 | 81 | crt.write(ca_cert) | ||
818 | 82 | subprocess.check_call(['update-ca-certificates', '--fresh']) | ||
819 | 0 | 83 | ||
820 | === added file 'charmhelpers/contrib/hahelpers/cluster.py' | |||
821 | --- charmhelpers/contrib/hahelpers/cluster.py 1970-01-01 00:00:00 +0000 | |||
822 | +++ charmhelpers/contrib/hahelpers/cluster.py 2015-10-08 20:44:37 +0000 | |||
823 | @@ -0,0 +1,316 @@ | |||
824 | 1 | # Copyright 2014-2015 Canonical Limited. | ||
825 | 2 | # | ||
826 | 3 | # This file is part of charm-helpers. | ||
827 | 4 | # | ||
828 | 5 | # charm-helpers is free software: you can redistribute it and/or modify | ||
829 | 6 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
830 | 7 | # published by the Free Software Foundation. | ||
831 | 8 | # | ||
832 | 9 | # charm-helpers is distributed in the hope that it will be useful, | ||
833 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
834 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
835 | 12 | # GNU Lesser General Public License for more details. | ||
836 | 13 | # | ||
837 | 14 | # You should have received a copy of the GNU Lesser General Public License | ||
838 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
839 | 16 | |||
840 | 17 | # | ||
841 | 18 | # Copyright 2012 Canonical Ltd. | ||
842 | 19 | # | ||
843 | 20 | # Authors: | ||
844 | 21 | # James Page <james.page@ubuntu.com> | ||
845 | 22 | # Adam Gandelman <adamg@ubuntu.com> | ||
846 | 23 | # | ||
847 | 24 | |||
848 | 25 | """ | ||
849 | 26 | Helpers for clustering and determining "cluster leadership" and other | ||
850 | 27 | clustering-related helpers. | ||
851 | 28 | """ | ||
852 | 29 | |||
853 | 30 | import subprocess | ||
854 | 31 | import os | ||
855 | 32 | |||
856 | 33 | from socket import gethostname as get_unit_hostname | ||
857 | 34 | |||
858 | 35 | import six | ||
859 | 36 | |||
860 | 37 | from charmhelpers.core.hookenv import ( | ||
861 | 38 | log, | ||
862 | 39 | relation_ids, | ||
863 | 40 | related_units as relation_list, | ||
864 | 41 | relation_get, | ||
865 | 42 | config as config_get, | ||
866 | 43 | INFO, | ||
867 | 44 | ERROR, | ||
868 | 45 | WARNING, | ||
869 | 46 | unit_get, | ||
870 | 47 | is_leader as juju_is_leader | ||
871 | 48 | ) | ||
872 | 49 | from charmhelpers.core.decorators import ( | ||
873 | 50 | retry_on_exception, | ||
874 | 51 | ) | ||
875 | 52 | from charmhelpers.core.strutils import ( | ||
876 | 53 | bool_from_string, | ||
877 | 54 | ) | ||
878 | 55 | |||
879 | 56 | DC_RESOURCE_NAME = 'DC' | ||
880 | 57 | |||
881 | 58 | |||
882 | 59 | class HAIncompleteConfig(Exception): | ||
883 | 60 | pass | ||
884 | 61 | |||
885 | 62 | |||
886 | 63 | class CRMResourceNotFound(Exception): | ||
887 | 64 | pass | ||
888 | 65 | |||
889 | 66 | |||
890 | 67 | class CRMDCNotFound(Exception): | ||
891 | 68 | pass | ||
892 | 69 | |||
893 | 70 | |||
894 | 71 | def is_elected_leader(resource): | ||
895 | 72 | """ | ||
896 | 73 | Returns True if the charm executing this is the elected cluster leader. | ||
897 | 74 | |||
898 | 75 | It relies on two mechanisms to determine leadership: | ||
899 | 76 | 1. If juju is sufficiently new and leadership election is supported, | ||
900 | 77 | the is_leader command will be used. | ||
901 | 78 | 2. If the charm is part of a corosync cluster, call corosync to | ||
902 | 79 | determine leadership. | ||
903 | 80 | 3. If the charm is not part of a corosync cluster, the leader is | ||
904 | 81 | determined as being "the alive unit with the lowest unit numer". In | ||
905 | 82 | other words, the oldest surviving unit. | ||
906 | 83 | """ | ||
907 | 84 | try: | ||
908 | 85 | return juju_is_leader() | ||
909 | 86 | except NotImplementedError: | ||
910 | 87 | log('Juju leadership election feature not enabled' | ||
911 | 88 | ', using fallback support', | ||
912 | 89 | level=WARNING) | ||
913 | 90 | |||
914 | 91 | if is_clustered(): | ||
915 | 92 | if not is_crm_leader(resource): | ||
916 | 93 | log('Deferring action to CRM leader.', level=INFO) | ||
917 | 94 | return False | ||
918 | 95 | else: | ||
919 | 96 | peers = peer_units() | ||
920 | 97 | if peers and not oldest_peer(peers): | ||
921 | 98 | log('Deferring action to oldest service unit.', level=INFO) | ||
922 | 99 | return False | ||
923 | 100 | return True | ||
924 | 101 | |||
925 | 102 | |||
926 | 103 | def is_clustered(): | ||
927 | 104 | for r_id in (relation_ids('ha') or []): | ||
928 | 105 | for unit in (relation_list(r_id) or []): | ||
929 | 106 | clustered = relation_get('clustered', | ||
930 | 107 | rid=r_id, | ||
931 | 108 | unit=unit) | ||
932 | 109 | if clustered: | ||
933 | 110 | return True | ||
934 | 111 | return False | ||
935 | 112 | |||
936 | 113 | |||
937 | 114 | def is_crm_dc(): | ||
938 | 115 | """ | ||
939 | 116 | Determine leadership by querying the pacemaker Designated Controller | ||
940 | 117 | """ | ||
941 | 118 | cmd = ['crm', 'status'] | ||
942 | 119 | try: | ||
943 | 120 | status = subprocess.check_output(cmd, stderr=subprocess.STDOUT) | ||
944 | 121 | if not isinstance(status, six.text_type): | ||
945 | 122 | status = six.text_type(status, "utf-8") | ||
946 | 123 | except subprocess.CalledProcessError as ex: | ||
947 | 124 | raise CRMDCNotFound(str(ex)) | ||
948 | 125 | |||
949 | 126 | current_dc = '' | ||
950 | 127 | for line in status.split('\n'): | ||
951 | 128 | if line.startswith('Current DC'): | ||
952 | 129 | # Current DC: juju-lytrusty-machine-2 (168108163) - partition with quorum | ||
953 | 130 | current_dc = line.split(':')[1].split()[0] | ||
954 | 131 | if current_dc == get_unit_hostname(): | ||
955 | 132 | return True | ||
956 | 133 | elif current_dc == 'NONE': | ||
957 | 134 | raise CRMDCNotFound('Current DC: NONE') | ||
958 | 135 | |||
959 | 136 | return False | ||
960 | 137 | |||
961 | 138 | |||
962 | 139 | @retry_on_exception(5, base_delay=2, | ||
963 | 140 | exc_type=(CRMResourceNotFound, CRMDCNotFound)) | ||
964 | 141 | def is_crm_leader(resource, retry=False): | ||
965 | 142 | """ | ||
966 | 143 | Returns True if the charm calling this is the elected corosync leader, | ||
967 | 144 | as returned by calling the external "crm" command. | ||
968 | 145 | |||
969 | 146 | We allow this operation to be retried to avoid the possibility of getting a | ||
970 | 147 | false negative. See LP #1396246 for more info. | ||
971 | 148 | """ | ||
972 | 149 | if resource == DC_RESOURCE_NAME: | ||
973 | 150 | return is_crm_dc() | ||
974 | 151 | cmd = ['crm', 'resource', 'show', resource] | ||
975 | 152 | try: | ||
976 | 153 | status = subprocess.check_output(cmd, stderr=subprocess.STDOUT) | ||
977 | 154 | if not isinstance(status, six.text_type): | ||
978 | 155 | status = six.text_type(status, "utf-8") | ||
979 | 156 | except subprocess.CalledProcessError: | ||
980 | 157 | status = None | ||
981 | 158 | |||
982 | 159 | if status and get_unit_hostname() in status: | ||
983 | 160 | return True | ||
984 | 161 | |||
985 | 162 | if status and "resource %s is NOT running" % (resource) in status: | ||
986 | 163 | raise CRMResourceNotFound("CRM resource %s not found" % (resource)) | ||
987 | 164 | |||
988 | 165 | return False | ||
989 | 166 | |||
990 | 167 | |||
991 | 168 | def is_leader(resource): | ||
992 | 169 | log("is_leader is deprecated. Please consider using is_crm_leader " | ||
993 | 170 | "instead.", level=WARNING) | ||
994 | 171 | return is_crm_leader(resource) | ||
995 | 172 | |||
996 | 173 | |||
997 | 174 | def peer_units(peer_relation="cluster"): | ||
998 | 175 | peers = [] | ||
999 | 176 | for r_id in (relation_ids(peer_relation) or []): | ||
1000 | 177 | for unit in (relation_list(r_id) or []): | ||
1001 | 178 | peers.append(unit) | ||
1002 | 179 | return peers | ||
1003 | 180 | |||
1004 | 181 | |||
1005 | 182 | def peer_ips(peer_relation='cluster', addr_key='private-address'): | ||
1006 | 183 | '''Return a dict of peers and their private-address''' | ||
1007 | 184 | peers = {} | ||
1008 | 185 | for r_id in relation_ids(peer_relation): | ||
1009 | 186 | for unit in relation_list(r_id): | ||
1010 | 187 | peers[unit] = relation_get(addr_key, rid=r_id, unit=unit) | ||
1011 | 188 | return peers | ||
1012 | 189 | |||
1013 | 190 | |||
1014 | 191 | def oldest_peer(peers): | ||
1015 | 192 | """Determines who the oldest peer is by comparing unit numbers.""" | ||
1016 | 193 | local_unit_no = int(os.getenv('JUJU_UNIT_NAME').split('/')[1]) | ||
1017 | 194 | for peer in peers: | ||
1018 | 195 | remote_unit_no = int(peer.split('/')[1]) | ||
1019 | 196 | if remote_unit_no < local_unit_no: | ||
1020 | 197 | return False | ||
1021 | 198 | return True | ||
1022 | 199 | |||
1023 | 200 | |||
1024 | 201 | def eligible_leader(resource): | ||
1025 | 202 | log("eligible_leader is deprecated. Please consider using " | ||
1026 | 203 | "is_elected_leader instead.", level=WARNING) | ||
1027 | 204 | return is_elected_leader(resource) | ||
1028 | 205 | |||
1029 | 206 | |||
1030 | 207 | def https(): | ||
1031 | 208 | ''' | ||
1032 | 209 | Determines whether enough data has been provided in configuration | ||
1033 | 210 | or relation data to configure HTTPS | ||
1034 | 211 | . | ||
1035 | 212 | returns: boolean | ||
1036 | 213 | ''' | ||
1037 | 214 | use_https = config_get('use-https') | ||
1038 | 215 | if use_https and bool_from_string(use_https): | ||
1039 | 216 | return True | ||
1040 | 217 | if config_get('ssl_cert') and config_get('ssl_key'): | ||
1041 | 218 | return True | ||
1042 | 219 | for r_id in relation_ids('identity-service'): | ||
1043 | 220 | for unit in relation_list(r_id): | ||
1044 | 221 | # TODO - needs fixing for new helper as ssl_cert/key suffixes with CN | ||
1045 | 222 | rel_state = [ | ||
1046 | 223 | relation_get('https_keystone', rid=r_id, unit=unit), | ||
1047 | 224 | relation_get('ca_cert', rid=r_id, unit=unit), | ||
1048 | 225 | ] | ||
1049 | 226 | # NOTE: works around (LP: #1203241) | ||
1050 | 227 | if (None not in rel_state) and ('' not in rel_state): | ||
1051 | 228 | return True | ||
1052 | 229 | return False | ||
1053 | 230 | |||
1054 | 231 | |||
1055 | 232 | def determine_api_port(public_port, singlenode_mode=False): | ||
1056 | 233 | ''' | ||
1057 | 234 | Determine correct API server listening port based on | ||
1058 | 235 | existence of HTTPS reverse proxy and/or haproxy. | ||
1059 | 236 | |||
1060 | 237 | public_port: int: standard public port for given service | ||
1061 | 238 | |||
1062 | 239 | singlenode_mode: boolean: Shuffle ports when only a single unit is present | ||
1063 | 240 | |||
1064 | 241 | returns: int: the correct listening port for the API service | ||
1065 | 242 | ''' | ||
1066 | 243 | i = 0 | ||
1067 | 244 | if singlenode_mode: | ||
1068 | 245 | i += 1 | ||
1069 | 246 | elif len(peer_units()) > 0 or is_clustered(): | ||
1070 | 247 | i += 1 | ||
1071 | 248 | if https(): | ||
1072 | 249 | i += 1 | ||
1073 | 250 | return public_port - (i * 10) | ||
1074 | 251 | |||
1075 | 252 | |||
1076 | 253 | def determine_apache_port(public_port, singlenode_mode=False): | ||
1077 | 254 | ''' | ||
1078 | 255 | Description: Determine correct apache listening port based on public IP + | ||
1079 | 256 | state of the cluster. | ||
1080 | 257 | |||
1081 | 258 | public_port: int: standard public port for given service | ||
1082 | 259 | |||
1083 | 260 | singlenode_mode: boolean: Shuffle ports when only a single unit is present | ||
1084 | 261 | |||
1085 | 262 | returns: int: the correct listening port for the HAProxy service | ||
1086 | 263 | ''' | ||
1087 | 264 | i = 0 | ||
1088 | 265 | if singlenode_mode: | ||
1089 | 266 | i += 1 | ||
1090 | 267 | elif len(peer_units()) > 0 or is_clustered(): | ||
1091 | 268 | i += 1 | ||
1092 | 269 | return public_port - (i * 10) | ||
1093 | 270 | |||
1094 | 271 | |||
1095 | 272 | def get_hacluster_config(exclude_keys=None): | ||
1096 | 273 | ''' | ||
1097 | 274 | Obtains all relevant configuration from charm configuration required | ||
1098 | 275 | for initiating a relation to hacluster: | ||
1099 | 276 | |||
1100 | 277 | ha-bindiface, ha-mcastport, vip | ||
1101 | 278 | |||
1102 | 279 | param: exclude_keys: list of setting key(s) to be excluded. | ||
1103 | 280 | returns: dict: A dict containing settings keyed by setting name. | ||
1104 | 281 | raises: HAIncompleteConfig if settings are missing. | ||
1105 | 282 | ''' | ||
1106 | 283 | settings = ['ha-bindiface', 'ha-mcastport', 'vip'] | ||
1107 | 284 | conf = {} | ||
1108 | 285 | for setting in settings: | ||
1109 | 286 | if exclude_keys and setting in exclude_keys: | ||
1110 | 287 | continue | ||
1111 | 288 | |||
1112 | 289 | conf[setting] = config_get(setting) | ||
1113 | 290 | missing = [] | ||
1114 | 291 | [missing.append(s) for s, v in six.iteritems(conf) if v is None] | ||
1115 | 292 | if missing: | ||
1116 | 293 | log('Insufficient config data to configure hacluster.', level=ERROR) | ||
1117 | 294 | raise HAIncompleteConfig | ||
1118 | 295 | return conf | ||
1119 | 296 | |||
1120 | 297 | |||
1121 | 298 | def canonical_url(configs, vip_setting='vip'): | ||
1122 | 299 | ''' | ||
1123 | 300 | Returns the correct HTTP URL to this host given the state of HTTPS | ||
1124 | 301 | configuration and hacluster. | ||
1125 | 302 | |||
1126 | 303 | :configs : OSTemplateRenderer: A config tempating object to inspect for | ||
1127 | 304 | a complete https context. | ||
1128 | 305 | |||
1129 | 306 | :vip_setting: str: Setting in charm config that specifies | ||
1130 | 307 | VIP address. | ||
1131 | 308 | ''' | ||
1132 | 309 | scheme = 'http' | ||
1133 | 310 | if 'https' in configs.complete_contexts(): | ||
1134 | 311 | scheme = 'https' | ||
1135 | 312 | if is_clustered(): | ||
1136 | 313 | addr = config_get(vip_setting) | ||
1137 | 314 | else: | ||
1138 | 315 | addr = unit_get('private-address') | ||
1139 | 316 | return '%s://%s' % (scheme, addr) | ||
1140 | 0 | 317 | ||
1141 | === added directory 'charmhelpers/contrib/network' | |||
1142 | === added file 'charmhelpers/contrib/network/__init__.py' | |||
1143 | --- charmhelpers/contrib/network/__init__.py 1970-01-01 00:00:00 +0000 | |||
1144 | +++ charmhelpers/contrib/network/__init__.py 2015-10-08 20:44:37 +0000 | |||
1145 | @@ -0,0 +1,15 @@ | |||
1146 | 1 | # Copyright 2014-2015 Canonical Limited. | ||
1147 | 2 | # | ||
1148 | 3 | # This file is part of charm-helpers. | ||
1149 | 4 | # | ||
1150 | 5 | # charm-helpers is free software: you can redistribute it and/or modify | ||
1151 | 6 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
1152 | 7 | # published by the Free Software Foundation. | ||
1153 | 8 | # | ||
1154 | 9 | # charm-helpers is distributed in the hope that it will be useful, | ||
1155 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1156 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1157 | 12 | # GNU Lesser General Public License for more details. | ||
1158 | 13 | # | ||
1159 | 14 | # You should have received a copy of the GNU Lesser General Public License | ||
1160 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
1161 | 0 | 16 | ||
1162 | === added file 'charmhelpers/contrib/network/ip.py' | |||
1163 | --- charmhelpers/contrib/network/ip.py 1970-01-01 00:00:00 +0000 | |||
1164 | +++ charmhelpers/contrib/network/ip.py 2015-10-08 20:44:37 +0000 | |||
1165 | @@ -0,0 +1,456 @@ | |||
1166 | 1 | # Copyright 2014-2015 Canonical Limited. | ||
1167 | 2 | # | ||
1168 | 3 | # This file is part of charm-helpers. | ||
1169 | 4 | # | ||
1170 | 5 | # charm-helpers is free software: you can redistribute it and/or modify | ||
1171 | 6 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
1172 | 7 | # published by the Free Software Foundation. | ||
1173 | 8 | # | ||
1174 | 9 | # charm-helpers is distributed in the hope that it will be useful, | ||
1175 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1176 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1177 | 12 | # GNU Lesser General Public License for more details. | ||
1178 | 13 | # | ||
1179 | 14 | # You should have received a copy of the GNU Lesser General Public License | ||
1180 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
1181 | 16 | |||
1182 | 17 | import glob | ||
1183 | 18 | import re | ||
1184 | 19 | import subprocess | ||
1185 | 20 | import six | ||
1186 | 21 | import socket | ||
1187 | 22 | |||
1188 | 23 | from functools import partial | ||
1189 | 24 | |||
1190 | 25 | from charmhelpers.core.hookenv import unit_get | ||
1191 | 26 | from charmhelpers.fetch import apt_install, apt_update | ||
1192 | 27 | from charmhelpers.core.hookenv import ( | ||
1193 | 28 | log, | ||
1194 | 29 | WARNING, | ||
1195 | 30 | ) | ||
1196 | 31 | |||
1197 | 32 | try: | ||
1198 | 33 | import netifaces | ||
1199 | 34 | except ImportError: | ||
1200 | 35 | apt_update(fatal=True) | ||
1201 | 36 | apt_install('python-netifaces', fatal=True) | ||
1202 | 37 | import netifaces | ||
1203 | 38 | |||
1204 | 39 | try: | ||
1205 | 40 | import netaddr | ||
1206 | 41 | except ImportError: | ||
1207 | 42 | apt_update(fatal=True) | ||
1208 | 43 | apt_install('python-netaddr', fatal=True) | ||
1209 | 44 | import netaddr | ||
1210 | 45 | |||
1211 | 46 | |||
1212 | 47 | def _validate_cidr(network): | ||
1213 | 48 | try: | ||
1214 | 49 | netaddr.IPNetwork(network) | ||
1215 | 50 | except (netaddr.core.AddrFormatError, ValueError): | ||
1216 | 51 | raise ValueError("Network (%s) is not in CIDR presentation format" % | ||
1217 | 52 | network) | ||
1218 | 53 | |||
1219 | 54 | |||
1220 | 55 | def no_ip_found_error_out(network): | ||
1221 | 56 | errmsg = ("No IP address found in network: %s" % network) | ||
1222 | 57 | raise ValueError(errmsg) | ||
1223 | 58 | |||
1224 | 59 | |||
1225 | 60 | def get_address_in_network(network, fallback=None, fatal=False): | ||
1226 | 61 | """Get an IPv4 or IPv6 address within the network from the host. | ||
1227 | 62 | |||
1228 | 63 | :param network (str): CIDR presentation format. For example, | ||
1229 | 64 | '192.168.1.0/24'. | ||
1230 | 65 | :param fallback (str): If no address is found, return fallback. | ||
1231 | 66 | :param fatal (boolean): If no address is found, fallback is not | ||
1232 | 67 | set and fatal is True then exit(1). | ||
1233 | 68 | """ | ||
1234 | 69 | if network is None: | ||
1235 | 70 | if fallback is not None: | ||
1236 | 71 | return fallback | ||
1237 | 72 | |||
1238 | 73 | if fatal: | ||
1239 | 74 | no_ip_found_error_out(network) | ||
1240 | 75 | else: | ||
1241 | 76 | return None | ||
1242 | 77 | |||
1243 | 78 | _validate_cidr(network) | ||
1244 | 79 | network = netaddr.IPNetwork(network) | ||
1245 | 80 | for iface in netifaces.interfaces(): | ||
1246 | 81 | addresses = netifaces.ifaddresses(iface) | ||
1247 | 82 | if network.version == 4 and netifaces.AF_INET in addresses: | ||
1248 | 83 | addr = addresses[netifaces.AF_INET][0]['addr'] | ||
1249 | 84 | netmask = addresses[netifaces.AF_INET][0]['netmask'] | ||
1250 | 85 | cidr = netaddr.IPNetwork("%s/%s" % (addr, netmask)) | ||
1251 | 86 | if cidr in network: | ||
1252 | 87 | return str(cidr.ip) | ||
1253 | 88 | |||
1254 | 89 | if network.version == 6 and netifaces.AF_INET6 in addresses: | ||
1255 | 90 | for addr in addresses[netifaces.AF_INET6]: | ||
1256 | 91 | if not addr['addr'].startswith('fe80'): | ||
1257 | 92 | cidr = netaddr.IPNetwork("%s/%s" % (addr['addr'], | ||
1258 | 93 | addr['netmask'])) | ||
1259 | 94 | if cidr in network: | ||
1260 | 95 | return str(cidr.ip) | ||
1261 | 96 | |||
1262 | 97 | if fallback is not None: | ||
1263 | 98 | return fallback | ||
1264 | 99 | |||
1265 | 100 | if fatal: | ||
1266 | 101 | no_ip_found_error_out(network) | ||
1267 | 102 | |||
1268 | 103 | return None | ||
1269 | 104 | |||
1270 | 105 | |||
1271 | 106 | def is_ipv6(address): | ||
1272 | 107 | """Determine whether provided address is IPv6 or not.""" | ||
1273 | 108 | try: | ||
1274 | 109 | address = netaddr.IPAddress(address) | ||
1275 | 110 | except netaddr.AddrFormatError: | ||
1276 | 111 | # probably a hostname - so not an address at all! | ||
1277 | 112 | return False | ||
1278 | 113 | |||
1279 | 114 | return address.version == 6 | ||
1280 | 115 | |||
1281 | 116 | |||
1282 | 117 | def is_address_in_network(network, address): | ||
1283 | 118 | """ | ||
1284 | 119 | Determine whether the provided address is within a network range. | ||
1285 | 120 | |||
1286 | 121 | :param network (str): CIDR presentation format. For example, | ||
1287 | 122 | '192.168.1.0/24'. | ||
1288 | 123 | :param address: An individual IPv4 or IPv6 address without a net | ||
1289 | 124 | mask or subnet prefix. For example, '192.168.1.1'. | ||
1290 | 125 | :returns boolean: Flag indicating whether address is in network. | ||
1291 | 126 | """ | ||
1292 | 127 | try: | ||
1293 | 128 | network = netaddr.IPNetwork(network) | ||
1294 | 129 | except (netaddr.core.AddrFormatError, ValueError): | ||
1295 | 130 | raise ValueError("Network (%s) is not in CIDR presentation format" % | ||
1296 | 131 | network) | ||
1297 | 132 | |||
1298 | 133 | try: | ||
1299 | 134 | address = netaddr.IPAddress(address) | ||
1300 | 135 | except (netaddr.core.AddrFormatError, ValueError): | ||
1301 | 136 | raise ValueError("Address (%s) is not in correct presentation format" % | ||
1302 | 137 | address) | ||
1303 | 138 | |||
1304 | 139 | if address in network: | ||
1305 | 140 | return True | ||
1306 | 141 | else: | ||
1307 | 142 | return False | ||
1308 | 143 | |||
1309 | 144 | |||
1310 | 145 | def _get_for_address(address, key): | ||
1311 | 146 | """Retrieve an attribute of or the physical interface that | ||
1312 | 147 | the IP address provided could be bound to. | ||
1313 | 148 | |||
1314 | 149 | :param address (str): An individual IPv4 or IPv6 address without a net | ||
1315 | 150 | mask or subnet prefix. For example, '192.168.1.1'. | ||
1316 | 151 | :param key: 'iface' for the physical interface name or an attribute | ||
1317 | 152 | of the configured interface, for example 'netmask'. | ||
1318 | 153 | :returns str: Requested attribute or None if address is not bindable. | ||
1319 | 154 | """ | ||
1320 | 155 | address = netaddr.IPAddress(address) | ||
1321 | 156 | for iface in netifaces.interfaces(): | ||
1322 | 157 | addresses = netifaces.ifaddresses(iface) | ||
1323 | 158 | if address.version == 4 and netifaces.AF_INET in addresses: | ||
1324 | 159 | addr = addresses[netifaces.AF_INET][0]['addr'] | ||
1325 | 160 | netmask = addresses[netifaces.AF_INET][0]['netmask'] | ||
1326 | 161 | network = netaddr.IPNetwork("%s/%s" % (addr, netmask)) | ||
1327 | 162 | cidr = network.cidr | ||
1328 | 163 | if address in cidr: | ||
1329 | 164 | if key == 'iface': | ||
1330 | 165 | return iface | ||
1331 | 166 | else: | ||
1332 | 167 | return addresses[netifaces.AF_INET][0][key] | ||
1333 | 168 | |||
1334 | 169 | if address.version == 6 and netifaces.AF_INET6 in addresses: | ||
1335 | 170 | for addr in addresses[netifaces.AF_INET6]: | ||
1336 | 171 | if not addr['addr'].startswith('fe80'): | ||
1337 | 172 | network = netaddr.IPNetwork("%s/%s" % (addr['addr'], | ||
1338 | 173 | addr['netmask'])) | ||
1339 | 174 | cidr = network.cidr | ||
1340 | 175 | if address in cidr: | ||
1341 | 176 | if key == 'iface': | ||
1342 | 177 | return iface | ||
1343 | 178 | elif key == 'netmask' and cidr: | ||
1344 | 179 | return str(cidr).split('/')[1] | ||
1345 | 180 | else: | ||
1346 | 181 | return addr[key] | ||
1347 | 182 | |||
1348 | 183 | return None | ||
1349 | 184 | |||
1350 | 185 | |||
1351 | 186 | get_iface_for_address = partial(_get_for_address, key='iface') | ||
1352 | 187 | |||
1353 | 188 | |||
1354 | 189 | get_netmask_for_address = partial(_get_for_address, key='netmask') | ||
1355 | 190 | |||
1356 | 191 | |||
1357 | 192 | def format_ipv6_addr(address): | ||
1358 | 193 | """If address is IPv6, wrap it in '[]' otherwise return None. | ||
1359 | 194 | |||
1360 | 195 | This is required by most configuration files when specifying IPv6 | ||
1361 | 196 | addresses. | ||
1362 | 197 | """ | ||
1363 | 198 | if is_ipv6(address): | ||
1364 | 199 | return "[%s]" % address | ||
1365 | 200 | |||
1366 | 201 | return None | ||
1367 | 202 | |||
1368 | 203 | |||
1369 | 204 | def get_iface_addr(iface='eth0', inet_type='AF_INET', inc_aliases=False, | ||
1370 | 205 | fatal=True, exc_list=None): | ||
1371 | 206 | """Return the assigned IP address for a given interface, if any.""" | ||
1372 | 207 | # Extract nic if passed /dev/ethX | ||
1373 | 208 | if '/' in iface: | ||
1374 | 209 | iface = iface.split('/')[-1] | ||
1375 | 210 | |||
1376 | 211 | if not exc_list: | ||
1377 | 212 | exc_list = [] | ||
1378 | 213 | |||
1379 | 214 | try: | ||
1380 | 215 | inet_num = getattr(netifaces, inet_type) | ||
1381 | 216 | except AttributeError: | ||
1382 | 217 | raise Exception("Unknown inet type '%s'" % str(inet_type)) | ||
1383 | 218 | |||
1384 | 219 | interfaces = netifaces.interfaces() | ||
1385 | 220 | if inc_aliases: | ||
1386 | 221 | ifaces = [] | ||
1387 | 222 | for _iface in interfaces: | ||
1388 | 223 | if iface == _iface or _iface.split(':')[0] == iface: | ||
1389 | 224 | ifaces.append(_iface) | ||
1390 | 225 | |||
1391 | 226 | if fatal and not ifaces: | ||
1392 | 227 | raise Exception("Invalid interface '%s'" % iface) | ||
1393 | 228 | |||
1394 | 229 | ifaces.sort() | ||
1395 | 230 | else: | ||
1396 | 231 | if iface not in interfaces: | ||
1397 | 232 | if fatal: | ||
1398 | 233 | raise Exception("Interface '%s' not found " % (iface)) | ||
1399 | 234 | else: | ||
1400 | 235 | return [] | ||
1401 | 236 | |||
1402 | 237 | else: | ||
1403 | 238 | ifaces = [iface] | ||
1404 | 239 | |||
1405 | 240 | addresses = [] | ||
1406 | 241 | for netiface in ifaces: | ||
1407 | 242 | net_info = netifaces.ifaddresses(netiface) | ||
1408 | 243 | if inet_num in net_info: | ||
1409 | 244 | for entry in net_info[inet_num]: | ||
1410 | 245 | if 'addr' in entry and entry['addr'] not in exc_list: | ||
1411 | 246 | addresses.append(entry['addr']) | ||
1412 | 247 | |||
1413 | 248 | if fatal and not addresses: | ||
1414 | 249 | raise Exception("Interface '%s' doesn't have any %s addresses." % | ||
1415 | 250 | (iface, inet_type)) | ||
1416 | 251 | |||
1417 | 252 | return sorted(addresses) | ||
1418 | 253 | |||
1419 | 254 | |||
1420 | 255 | get_ipv4_addr = partial(get_iface_addr, inet_type='AF_INET') | ||
1421 | 256 | |||
1422 | 257 | |||
1423 | 258 | def get_iface_from_addr(addr): | ||
1424 | 259 | """Work out on which interface the provided address is configured.""" | ||
1425 | 260 | for iface in netifaces.interfaces(): | ||
1426 | 261 | addresses = netifaces.ifaddresses(iface) | ||
1427 | 262 | for inet_type in addresses: | ||
1428 | 263 | for _addr in addresses[inet_type]: | ||
1429 | 264 | _addr = _addr['addr'] | ||
1430 | 265 | # link local | ||
1431 | 266 | ll_key = re.compile("(.+)%.*") | ||
1432 | 267 | raw = re.match(ll_key, _addr) | ||
1433 | 268 | if raw: | ||
1434 | 269 | _addr = raw.group(1) | ||
1435 | 270 | |||
1436 | 271 | if _addr == addr: | ||
1437 | 272 | log("Address '%s' is configured on iface '%s'" % | ||
1438 | 273 | (addr, iface)) | ||
1439 | 274 | return iface | ||
1440 | 275 | |||
1441 | 276 | msg = "Unable to infer net iface on which '%s' is configured" % (addr) | ||
1442 | 277 | raise Exception(msg) | ||
1443 | 278 | |||
1444 | 279 | |||
1445 | 280 | def sniff_iface(f): | ||
1446 | 281 | """Ensure decorated function is called with a value for iface. | ||
1447 | 282 | |||
1448 | 283 | If no iface provided, inject net iface inferred from unit private address. | ||
1449 | 284 | """ | ||
1450 | 285 | def iface_sniffer(*args, **kwargs): | ||
1451 | 286 | if not kwargs.get('iface', None): | ||
1452 | 287 | kwargs['iface'] = get_iface_from_addr(unit_get('private-address')) | ||
1453 | 288 | |||
1454 | 289 | return f(*args, **kwargs) | ||
1455 | 290 | |||
1456 | 291 | return iface_sniffer | ||
1457 | 292 | |||
1458 | 293 | |||
1459 | 294 | @sniff_iface | ||
1460 | 295 | def get_ipv6_addr(iface=None, inc_aliases=False, fatal=True, exc_list=None, | ||
1461 | 296 | dynamic_only=True): | ||
1462 | 297 | """Get assigned IPv6 address for a given interface. | ||
1463 | 298 | |||
1464 | 299 | Returns list of addresses found. If no address found, returns empty list. | ||
1465 | 300 | |||
1466 | 301 | If iface is None, we infer the current primary interface by doing a reverse | ||
1467 | 302 | lookup on the unit private-address. | ||
1468 | 303 | |||
1469 | 304 | We currently only support scope global IPv6 addresses i.e. non-temporary | ||
1470 | 305 | addresses. If no global IPv6 address is found, return the first one found | ||
1471 | 306 | in the ipv6 address list. | ||
1472 | 307 | """ | ||
1473 | 308 | addresses = get_iface_addr(iface=iface, inet_type='AF_INET6', | ||
1474 | 309 | inc_aliases=inc_aliases, fatal=fatal, | ||
1475 | 310 | exc_list=exc_list) | ||
1476 | 311 | |||
1477 | 312 | if addresses: | ||
1478 | 313 | global_addrs = [] | ||
1479 | 314 | for addr in addresses: | ||
1480 | 315 | key_scope_link_local = re.compile("^fe80::..(.+)%(.+)") | ||
1481 | 316 | m = re.match(key_scope_link_local, addr) | ||
1482 | 317 | if m: | ||
1483 | 318 | eui_64_mac = m.group(1) | ||
1484 | 319 | iface = m.group(2) | ||
1485 | 320 | else: | ||
1486 | 321 | global_addrs.append(addr) | ||
1487 | 322 | |||
1488 | 323 | if global_addrs: | ||
1489 | 324 | # Make sure any found global addresses are not temporary | ||
1490 | 325 | cmd = ['ip', 'addr', 'show', iface] | ||
1491 | 326 | out = subprocess.check_output(cmd).decode('UTF-8') | ||
1492 | 327 | if dynamic_only: | ||
1493 | 328 | key = re.compile("inet6 (.+)/[0-9]+ scope global dynamic.*") | ||
1494 | 329 | else: | ||
1495 | 330 | key = re.compile("inet6 (.+)/[0-9]+ scope global.*") | ||
1496 | 331 | |||
1497 | 332 | addrs = [] | ||
1498 | 333 | for line in out.split('\n'): | ||
1499 | 334 | line = line.strip() | ||
1500 | 335 | m = re.match(key, line) | ||
1501 | 336 | if m and 'temporary' not in line: | ||
1502 | 337 | # Return the first valid address we find | ||
1503 | 338 | for addr in global_addrs: | ||
1504 | 339 | if m.group(1) == addr: | ||
1505 | 340 | if not dynamic_only or \ | ||
1506 | 341 | m.group(1).endswith(eui_64_mac): | ||
1507 | 342 | addrs.append(addr) | ||
1508 | 343 | |||
1509 | 344 | if addrs: | ||
1510 | 345 | return addrs | ||
1511 | 346 | |||
1512 | 347 | if fatal: | ||
1513 | 348 | raise Exception("Interface '%s' does not have a scope global " | ||
1514 | 349 | "non-temporary ipv6 address." % iface) | ||
1515 | 350 | |||
1516 | 351 | return [] | ||
1517 | 352 | |||
1518 | 353 | |||
1519 | 354 | def get_bridges(vnic_dir='/sys/devices/virtual/net'): | ||
1520 | 355 | """Return a list of bridges on the system.""" | ||
1521 | 356 | b_regex = "%s/*/bridge" % vnic_dir | ||
1522 | 357 | return [x.replace(vnic_dir, '').split('/')[1] for x in glob.glob(b_regex)] | ||
1523 | 358 | |||
1524 | 359 | |||
1525 | 360 | def get_bridge_nics(bridge, vnic_dir='/sys/devices/virtual/net'): | ||
1526 | 361 | """Return a list of nics comprising a given bridge on the system.""" | ||
1527 | 362 | brif_regex = "%s/%s/brif/*" % (vnic_dir, bridge) | ||
1528 | 363 | return [x.split('/')[-1] for x in glob.glob(brif_regex)] | ||
1529 | 364 | |||
1530 | 365 | |||
1531 | 366 | def is_bridge_member(nic): | ||
1532 | 367 | """Check if a given nic is a member of a bridge.""" | ||
1533 | 368 | for bridge in get_bridges(): | ||
1534 | 369 | if nic in get_bridge_nics(bridge): | ||
1535 | 370 | return True | ||
1536 | 371 | |||
1537 | 372 | return False | ||
1538 | 373 | |||
1539 | 374 | |||
1540 | 375 | def is_ip(address): | ||
1541 | 376 | """ | ||
1542 | 377 | Returns True if address is a valid IP address. | ||
1543 | 378 | """ | ||
1544 | 379 | try: | ||
1545 | 380 | # Test to see if already an IPv4 address | ||
1546 | 381 | socket.inet_aton(address) | ||
1547 | 382 | return True | ||
1548 | 383 | except socket.error: | ||
1549 | 384 | return False | ||
1550 | 385 | |||
1551 | 386 | |||
1552 | 387 | def ns_query(address): | ||
1553 | 388 | try: | ||
1554 | 389 | import dns.resolver | ||
1555 | 390 | except ImportError: | ||
1556 | 391 | apt_install('python-dnspython') | ||
1557 | 392 | import dns.resolver | ||
1558 | 393 | |||
1559 | 394 | if isinstance(address, dns.name.Name): | ||
1560 | 395 | rtype = 'PTR' | ||
1561 | 396 | elif isinstance(address, six.string_types): | ||
1562 | 397 | rtype = 'A' | ||
1563 | 398 | else: | ||
1564 | 399 | return None | ||
1565 | 400 | |||
1566 | 401 | answers = dns.resolver.query(address, rtype) | ||
1567 | 402 | if answers: | ||
1568 | 403 | return str(answers[0]) | ||
1569 | 404 | return None | ||
1570 | 405 | |||
1571 | 406 | |||
1572 | 407 | def get_host_ip(hostname, fallback=None): | ||
1573 | 408 | """ | ||
1574 | 409 | Resolves the IP for a given hostname, or returns | ||
1575 | 410 | the input if it is already an IP. | ||
1576 | 411 | """ | ||
1577 | 412 | if is_ip(hostname): | ||
1578 | 413 | return hostname | ||
1579 | 414 | |||
1580 | 415 | ip_addr = ns_query(hostname) | ||
1581 | 416 | if not ip_addr: | ||
1582 | 417 | try: | ||
1583 | 418 | ip_addr = socket.gethostbyname(hostname) | ||
1584 | 419 | except: | ||
1585 | 420 | log("Failed to resolve hostname '%s'" % (hostname), | ||
1586 | 421 | level=WARNING) | ||
1587 | 422 | return fallback | ||
1588 | 423 | return ip_addr | ||
1589 | 424 | |||
1590 | 425 | |||
1591 | 426 | def get_hostname(address, fqdn=True): | ||
1592 | 427 | """ | ||
1593 | 428 | Resolves hostname for given IP, or returns the input | ||
1594 | 429 | if it is already a hostname. | ||
1595 | 430 | """ | ||
1596 | 431 | if is_ip(address): | ||
1597 | 432 | try: | ||
1598 | 433 | import dns.reversename | ||
1599 | 434 | except ImportError: | ||
1600 | 435 | apt_install("python-dnspython") | ||
1601 | 436 | import dns.reversename | ||
1602 | 437 | |||
1603 | 438 | rev = dns.reversename.from_address(address) | ||
1604 | 439 | result = ns_query(rev) | ||
1605 | 440 | |||
1606 | 441 | if not result: | ||
1607 | 442 | try: | ||
1608 | 443 | result = socket.gethostbyaddr(address)[0] | ||
1609 | 444 | except: | ||
1610 | 445 | return None | ||
1611 | 446 | else: | ||
1612 | 447 | result = address | ||
1613 | 448 | |||
1614 | 449 | if fqdn: | ||
1615 | 450 | # strip trailing . | ||
1616 | 451 | if result.endswith('.'): | ||
1617 | 452 | return result[:-1] | ||
1618 | 453 | else: | ||
1619 | 454 | return result | ||
1620 | 455 | else: | ||
1621 | 456 | return result.split('.')[0] | ||
1622 | 0 | 457 | ||
1623 | === added directory 'charmhelpers/contrib/openstack' | |||
1624 | === added file 'charmhelpers/contrib/openstack/__init__.py' | |||
1625 | --- charmhelpers/contrib/openstack/__init__.py 1970-01-01 00:00:00 +0000 | |||
1626 | +++ charmhelpers/contrib/openstack/__init__.py 2015-10-08 20:44:37 +0000 | |||
1627 | @@ -0,0 +1,15 @@ | |||
1628 | 1 | # Copyright 2014-2015 Canonical Limited. | ||
1629 | 2 | # | ||
1630 | 3 | # This file is part of charm-helpers. | ||
1631 | 4 | # | ||
1632 | 5 | # charm-helpers is free software: you can redistribute it and/or modify | ||
1633 | 6 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
1634 | 7 | # published by the Free Software Foundation. | ||
1635 | 8 | # | ||
1636 | 9 | # charm-helpers is distributed in the hope that it will be useful, | ||
1637 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1638 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1639 | 12 | # GNU Lesser General Public License for more details. | ||
1640 | 13 | # | ||
1641 | 14 | # You should have received a copy of the GNU Lesser General Public License | ||
1642 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
1643 | 0 | 16 | ||
1644 | === added file 'charmhelpers/contrib/openstack/alternatives.py' | |||
1645 | --- charmhelpers/contrib/openstack/alternatives.py 1970-01-01 00:00:00 +0000 | |||
1646 | +++ charmhelpers/contrib/openstack/alternatives.py 2015-10-08 20:44:37 +0000 | |||
1647 | @@ -0,0 +1,33 @@ | |||
1648 | 1 | # Copyright 2014-2015 Canonical Limited. | ||
1649 | 2 | # | ||
1650 | 3 | # This file is part of charm-helpers. | ||
1651 | 4 | # | ||
1652 | 5 | # charm-helpers is free software: you can redistribute it and/or modify | ||
1653 | 6 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
1654 | 7 | # published by the Free Software Foundation. | ||
1655 | 8 | # | ||
1656 | 9 | # charm-helpers is distributed in the hope that it will be useful, | ||
1657 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1658 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1659 | 12 | # GNU Lesser General Public License for more details. | ||
1660 | 13 | # | ||
1661 | 14 | # You should have received a copy of the GNU Lesser General Public License | ||
1662 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
1663 | 16 | |||
1664 | 17 | ''' Helper for managing alternatives for file conflict resolution ''' | ||
1665 | 18 | |||
1666 | 19 | import subprocess | ||
1667 | 20 | import shutil | ||
1668 | 21 | import os | ||
1669 | 22 | |||
1670 | 23 | |||
1671 | 24 | def install_alternative(name, target, source, priority=50): | ||
1672 | 25 | ''' Install alternative configuration ''' | ||
1673 | 26 | if (os.path.exists(target) and not os.path.islink(target)): | ||
1674 | 27 | # Move existing file/directory away before installing | ||
1675 | 28 | shutil.move(target, '{}.bak'.format(target)) | ||
1676 | 29 | cmd = [ | ||
1677 | 30 | 'update-alternatives', '--force', '--install', | ||
1678 | 31 | target, name, source, str(priority) | ||
1679 | 32 | ] | ||
1680 | 33 | subprocess.check_call(cmd) | ||
1681 | 0 | 34 | ||
1682 | === added directory 'charmhelpers/contrib/openstack/amulet' | |||
1683 | === added file 'charmhelpers/contrib/openstack/amulet/__init__.py' | |||
1684 | --- charmhelpers/contrib/openstack/amulet/__init__.py 1970-01-01 00:00:00 +0000 | |||
1685 | +++ charmhelpers/contrib/openstack/amulet/__init__.py 2015-10-08 20:44:37 +0000 | |||
1686 | @@ -0,0 +1,15 @@ | |||
1687 | 1 | # Copyright 2014-2015 Canonical Limited. | ||
1688 | 2 | # | ||
1689 | 3 | # This file is part of charm-helpers. | ||
1690 | 4 | # | ||
1691 | 5 | # charm-helpers is free software: you can redistribute it and/or modify | ||
1692 | 6 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
1693 | 7 | # published by the Free Software Foundation. | ||
1694 | 8 | # | ||
1695 | 9 | # charm-helpers is distributed in the hope that it will be useful, | ||
1696 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1697 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1698 | 12 | # GNU Lesser General Public License for more details. | ||
1699 | 13 | # | ||
1700 | 14 | # You should have received a copy of the GNU Lesser General Public License | ||
1701 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
1702 | 0 | 16 | ||
1703 | === added file 'charmhelpers/contrib/openstack/amulet/deployment.py' | |||
1704 | --- charmhelpers/contrib/openstack/amulet/deployment.py 1970-01-01 00:00:00 +0000 | |||
1705 | +++ charmhelpers/contrib/openstack/amulet/deployment.py 2015-10-08 20:44:37 +0000 | |||
1706 | @@ -0,0 +1,197 @@ | |||
1707 | 1 | # Copyright 2014-2015 Canonical Limited. | ||
1708 | 2 | # | ||
1709 | 3 | # This file is part of charm-helpers. | ||
1710 | 4 | # | ||
1711 | 5 | # charm-helpers is free software: you can redistribute it and/or modify | ||
1712 | 6 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
1713 | 7 | # published by the Free Software Foundation. | ||
1714 | 8 | # | ||
1715 | 9 | # charm-helpers is distributed in the hope that it will be useful, | ||
1716 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1717 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1718 | 12 | # GNU Lesser General Public License for more details. | ||
1719 | 13 | # | ||
1720 | 14 | # You should have received a copy of the GNU Lesser General Public License | ||
1721 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
1722 | 16 | |||
1723 | 17 | import six | ||
1724 | 18 | from collections import OrderedDict | ||
1725 | 19 | from charmhelpers.contrib.amulet.deployment import ( | ||
1726 | 20 | AmuletDeployment | ||
1727 | 21 | ) | ||
1728 | 22 | |||
1729 | 23 | |||
1730 | 24 | class OpenStackAmuletDeployment(AmuletDeployment): | ||
1731 | 25 | """OpenStack amulet deployment. | ||
1732 | 26 | |||
1733 | 27 | This class inherits from AmuletDeployment and has additional support | ||
1734 | 28 | that is specifically for use by OpenStack charms. | ||
1735 | 29 | """ | ||
1736 | 30 | |||
1737 | 31 | def __init__(self, series=None, openstack=None, source=None, stable=True): | ||
1738 | 32 | """Initialize the deployment environment.""" | ||
1739 | 33 | super(OpenStackAmuletDeployment, self).__init__(series) | ||
1740 | 34 | self.openstack = openstack | ||
1741 | 35 | self.source = source | ||
1742 | 36 | self.stable = stable | ||
1743 | 37 | # Note(coreycb): this needs to be changed when new next branches come | ||
1744 | 38 | # out. | ||
1745 | 39 | self.current_next = "trusty" | ||
1746 | 40 | |||
1747 | 41 | def _determine_branch_locations(self, other_services): | ||
1748 | 42 | """Determine the branch locations for the other services. | ||
1749 | 43 | |||
1750 | 44 | Determine if the local branch being tested is derived from its | ||
1751 | 45 | stable or next (dev) branch, and based on this, use the corresonding | ||
1752 | 46 | stable or next branches for the other_services.""" | ||
1753 | 47 | |||
1754 | 48 | # Charms outside the lp:~openstack-charmers namespace | ||
1755 | 49 | base_charms = ['mysql', 'mongodb', 'nrpe'] | ||
1756 | 50 | |||
1757 | 51 | # Force these charms to current series even when using an older series. | ||
1758 | 52 | # ie. Use trusty/nrpe even when series is precise, as the P charm | ||
1759 | 53 | # does not possess the necessary external master config and hooks. | ||
1760 | 54 | force_series_current = ['nrpe'] | ||
1761 | 55 | |||
1762 | 56 | if self.series in ['precise', 'trusty']: | ||
1763 | 57 | base_series = self.series | ||
1764 | 58 | else: | ||
1765 | 59 | base_series = self.current_next | ||
1766 | 60 | |||
1767 | 61 | for svc in other_services: | ||
1768 | 62 | if svc['name'] in force_series_current: | ||
1769 | 63 | base_series = self.current_next | ||
1770 | 64 | # If a location has been explicitly set, use it | ||
1771 | 65 | if svc.get('location'): | ||
1772 | 66 | continue | ||
1773 | 67 | if self.stable: | ||
1774 | 68 | temp = 'lp:charms/{}/{}' | ||
1775 | 69 | svc['location'] = temp.format(base_series, | ||
1776 | 70 | svc['name']) | ||
1777 | 71 | else: | ||
1778 | 72 | if svc['name'] in base_charms: | ||
1779 | 73 | temp = 'lp:charms/{}/{}' | ||
1780 | 74 | svc['location'] = temp.format(base_series, | ||
1781 | 75 | svc['name']) | ||
1782 | 76 | else: | ||
1783 | 77 | temp = 'lp:~openstack-charmers/charms/{}/{}/next' | ||
1784 | 78 | svc['location'] = temp.format(self.current_next, | ||
1785 | 79 | svc['name']) | ||
1786 | 80 | |||
1787 | 81 | return other_services | ||
1788 | 82 | |||
1789 | 83 | def _add_services(self, this_service, other_services): | ||
1790 | 84 | """Add services to the deployment and set openstack-origin/source.""" | ||
1791 | 85 | other_services = self._determine_branch_locations(other_services) | ||
1792 | 86 | |||
1793 | 87 | super(OpenStackAmuletDeployment, self)._add_services(this_service, | ||
1794 | 88 | other_services) | ||
1795 | 89 | |||
1796 | 90 | services = other_services | ||
1797 | 91 | services.append(this_service) | ||
1798 | 92 | |||
1799 | 93 | # Charms which should use the source config option | ||
1800 | 94 | use_source = ['mysql', 'mongodb', 'rabbitmq-server', 'ceph', | ||
1801 | 95 | 'ceph-osd', 'ceph-radosgw'] | ||
1802 | 96 | |||
1803 | 97 | # Charms which can not use openstack-origin, ie. many subordinates | ||
1804 | 98 | no_origin = ['cinder-ceph', 'hacluster', 'neutron-openvswitch', 'nrpe'] | ||
1805 | 99 | |||
1806 | 100 | if self.openstack: | ||
1807 | 101 | for svc in services: | ||
1808 | 102 | if svc['name'] not in use_source + no_origin: | ||
1809 | 103 | config = {'openstack-origin': self.openstack} | ||
1810 | 104 | self.d.configure(svc['name'], config) | ||
1811 | 105 | |||
1812 | 106 | if self.source: | ||
1813 | 107 | for svc in services: | ||
1814 | 108 | if svc['name'] in use_source and svc['name'] not in no_origin: | ||
1815 | 109 | config = {'source': self.source} | ||
1816 | 110 | self.d.configure(svc['name'], config) | ||
1817 | 111 | |||
1818 | 112 | def _configure_services(self, configs): | ||
1819 | 113 | """Configure all of the services.""" | ||
1820 | 114 | for service, config in six.iteritems(configs): | ||
1821 | 115 | self.d.configure(service, config) | ||
1822 | 116 | |||
1823 | 117 | def _get_openstack_release(self): | ||
1824 | 118 | """Get openstack release. | ||
1825 | 119 | |||
1826 | 120 | Return an integer representing the enum value of the openstack | ||
1827 | 121 | release. | ||
1828 | 122 | """ | ||
1829 | 123 | # Must be ordered by OpenStack release (not by Ubuntu release): | ||
1830 | 124 | (self.precise_essex, self.precise_folsom, self.precise_grizzly, | ||
1831 | 125 | self.precise_havana, self.precise_icehouse, | ||
1832 | 126 | self.trusty_icehouse, self.trusty_juno, self.utopic_juno, | ||
1833 | 127 | self.trusty_kilo, self.vivid_kilo, self.trusty_liberty, | ||
1834 | 128 | self.wily_liberty) = range(12) | ||
1835 | 129 | |||
1836 | 130 | releases = { | ||
1837 | 131 | ('precise', None): self.precise_essex, | ||
1838 | 132 | ('precise', 'cloud:precise-folsom'): self.precise_folsom, | ||
1839 | 133 | ('precise', 'cloud:precise-grizzly'): self.precise_grizzly, | ||
1840 | 134 | ('precise', 'cloud:precise-havana'): self.precise_havana, | ||
1841 | 135 | ('precise', 'cloud:precise-icehouse'): self.precise_icehouse, | ||
1842 | 136 | ('trusty', None): self.trusty_icehouse, | ||
1843 | 137 | ('trusty', 'cloud:trusty-juno'): self.trusty_juno, | ||
1844 | 138 | ('trusty', 'cloud:trusty-kilo'): self.trusty_kilo, | ||
1845 | 139 | ('trusty', 'cloud:trusty-liberty'): self.trusty_liberty, | ||
1846 | 140 | ('utopic', None): self.utopic_juno, | ||
1847 | 141 | ('vivid', None): self.vivid_kilo, | ||
1848 | 142 | ('wily', None): self.wily_liberty} | ||
1849 | 143 | return releases[(self.series, self.openstack)] | ||
1850 | 144 | |||
1851 | 145 | def _get_openstack_release_string(self): | ||
1852 | 146 | """Get openstack release string. | ||
1853 | 147 | |||
1854 | 148 | Return a string representing the openstack release. | ||
1855 | 149 | """ | ||
1856 | 150 | releases = OrderedDict([ | ||
1857 | 151 | ('precise', 'essex'), | ||
1858 | 152 | ('quantal', 'folsom'), | ||
1859 | 153 | ('raring', 'grizzly'), | ||
1860 | 154 | ('saucy', 'havana'), | ||
1861 | 155 | ('trusty', 'icehouse'), | ||
1862 | 156 | ('utopic', 'juno'), | ||
1863 | 157 | ('vivid', 'kilo'), | ||
1864 | 158 | ('wily', 'liberty'), | ||
1865 | 159 | ]) | ||
1866 | 160 | if self.openstack: | ||
1867 | 161 | os_origin = self.openstack.split(':')[1] | ||
1868 | 162 | return os_origin.split('%s-' % self.series)[1].split('/')[0] | ||
1869 | 163 | else: | ||
1870 | 164 | return releases[self.series] | ||
1871 | 165 | |||
1872 | 166 | def get_ceph_expected_pools(self, radosgw=False): | ||
1873 | 167 | """Return a list of expected ceph pools in a ceph + cinder + glance | ||
1874 | 168 | test scenario, based on OpenStack release and whether ceph radosgw | ||
1875 | 169 | is flagged as present or not.""" | ||
1876 | 170 | |||
1877 | 171 | if self._get_openstack_release() >= self.trusty_kilo: | ||
1878 | 172 | # Kilo or later | ||
1879 | 173 | pools = [ | ||
1880 | 174 | 'rbd', | ||
1881 | 175 | 'cinder', | ||
1882 | 176 | 'glance' | ||
1883 | 177 | ] | ||
1884 | 178 | else: | ||
1885 | 179 | # Juno or earlier | ||
1886 | 180 | pools = [ | ||
1887 | 181 | 'data', | ||
1888 | 182 | 'metadata', | ||
1889 | 183 | 'rbd', | ||
1890 | 184 | 'cinder', | ||
1891 | 185 | 'glance' | ||
1892 | 186 | ] | ||
1893 | 187 | |||
1894 | 188 | if radosgw: | ||
1895 | 189 | pools.extend([ | ||
1896 | 190 | '.rgw.root', | ||
1897 | 191 | '.rgw.control', | ||
1898 | 192 | '.rgw', | ||
1899 | 193 | '.rgw.gc', | ||
1900 | 194 | '.users.uid' | ||
1901 | 195 | ]) | ||
1902 | 196 | |||
1903 | 197 | return pools | ||
1904 | 0 | 198 | ||
1905 | === added file 'charmhelpers/contrib/openstack/amulet/utils.py' | |||
1906 | --- charmhelpers/contrib/openstack/amulet/utils.py 1970-01-01 00:00:00 +0000 | |||
1907 | +++ charmhelpers/contrib/openstack/amulet/utils.py 2015-10-08 20:44:37 +0000 | |||
1908 | @@ -0,0 +1,963 @@ | |||
1909 | 1 | # Copyright 2014-2015 Canonical Limited. | ||
1910 | 2 | # | ||
1911 | 3 | # This file is part of charm-helpers. | ||
1912 | 4 | # | ||
1913 | 5 | # charm-helpers is free software: you can redistribute it and/or modify | ||
1914 | 6 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
1915 | 7 | # published by the Free Software Foundation. | ||
1916 | 8 | # | ||
1917 | 9 | # charm-helpers is distributed in the hope that it will be useful, | ||
1918 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1919 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1920 | 12 | # GNU Lesser General Public License for more details. | ||
1921 | 13 | # | ||
1922 | 14 | # You should have received a copy of the GNU Lesser General Public License | ||
1923 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
1924 | 16 | |||
1925 | 17 | import amulet | ||
1926 | 18 | import json | ||
1927 | 19 | import logging | ||
1928 | 20 | import os | ||
1929 | 21 | import six | ||
1930 | 22 | import time | ||
1931 | 23 | import urllib | ||
1932 | 24 | |||
1933 | 25 | import cinderclient.v1.client as cinder_client | ||
1934 | 26 | import glanceclient.v1.client as glance_client | ||
1935 | 27 | import heatclient.v1.client as heat_client | ||
1936 | 28 | import keystoneclient.v2_0 as keystone_client | ||
1937 | 29 | import novaclient.v1_1.client as nova_client | ||
1938 | 30 | import pika | ||
1939 | 31 | import swiftclient | ||
1940 | 32 | |||
1941 | 33 | from charmhelpers.contrib.amulet.utils import ( | ||
1942 | 34 | AmuletUtils | ||
1943 | 35 | ) | ||
1944 | 36 | |||
1945 | 37 | DEBUG = logging.DEBUG | ||
1946 | 38 | ERROR = logging.ERROR | ||
1947 | 39 | |||
1948 | 40 | |||
1949 | 41 | class OpenStackAmuletUtils(AmuletUtils): | ||
1950 | 42 | """OpenStack amulet utilities. | ||
1951 | 43 | |||
1952 | 44 | This class inherits from AmuletUtils and has additional support | ||
1953 | 45 | that is specifically for use by OpenStack charm tests. | ||
1954 | 46 | """ | ||
1955 | 47 | |||
1956 | 48 | def __init__(self, log_level=ERROR): | ||
1957 | 49 | """Initialize the deployment environment.""" | ||
1958 | 50 | super(OpenStackAmuletUtils, self).__init__(log_level) | ||
1959 | 51 | |||
1960 | 52 | def validate_endpoint_data(self, endpoints, admin_port, internal_port, | ||
1961 | 53 | public_port, expected): | ||
1962 | 54 | """Validate endpoint data. | ||
1963 | 55 | |||
1964 | 56 | Validate actual endpoint data vs expected endpoint data. The ports | ||
1965 | 57 | are used to find the matching endpoint. | ||
1966 | 58 | """ | ||
1967 | 59 | self.log.debug('Validating endpoint data...') | ||
1968 | 60 | self.log.debug('actual: {}'.format(repr(endpoints))) | ||
1969 | 61 | found = False | ||
1970 | 62 | for ep in endpoints: | ||
1971 | 63 | self.log.debug('endpoint: {}'.format(repr(ep))) | ||
1972 | 64 | if (admin_port in ep.adminurl and | ||
1973 | 65 | internal_port in ep.internalurl and | ||
1974 | 66 | public_port in ep.publicurl): | ||
1975 | 67 | found = True | ||
1976 | 68 | actual = {'id': ep.id, | ||
1977 | 69 | 'region': ep.region, | ||
1978 | 70 | 'adminurl': ep.adminurl, | ||
1979 | 71 | 'internalurl': ep.internalurl, | ||
1980 | 72 | 'publicurl': ep.publicurl, | ||
1981 | 73 | 'service_id': ep.service_id} | ||
1982 | 74 | ret = self._validate_dict_data(expected, actual) | ||
1983 | 75 | if ret: | ||
1984 | 76 | return 'unexpected endpoint data - {}'.format(ret) | ||
1985 | 77 | |||
1986 | 78 | if not found: | ||
1987 | 79 | return 'endpoint not found' | ||
1988 | 80 | |||
1989 | 81 | def validate_svc_catalog_endpoint_data(self, expected, actual): | ||
1990 | 82 | """Validate service catalog endpoint data. | ||
1991 | 83 | |||
1992 | 84 | Validate a list of actual service catalog endpoints vs a list of | ||
1993 | 85 | expected service catalog endpoints. | ||
1994 | 86 | """ | ||
1995 | 87 | self.log.debug('Validating service catalog endpoint data...') | ||
1996 | 88 | self.log.debug('actual: {}'.format(repr(actual))) | ||
1997 | 89 | for k, v in six.iteritems(expected): | ||
1998 | 90 | if k in actual: | ||
1999 | 91 | ret = self._validate_dict_data(expected[k][0], actual[k][0]) | ||
2000 | 92 | if ret: | ||
2001 | 93 | return self.endpoint_error(k, ret) | ||
2002 | 94 | else: | ||
2003 | 95 | return "endpoint {} does not exist".format(k) | ||
2004 | 96 | return ret | ||
2005 | 97 | |||
2006 | 98 | def validate_tenant_data(self, expected, actual): | ||
2007 | 99 | """Validate tenant data. | ||
2008 | 100 | |||
2009 | 101 | Validate a list of actual tenant data vs list of expected tenant | ||
2010 | 102 | data. | ||
2011 | 103 | """ | ||
2012 | 104 | self.log.debug('Validating tenant data...') | ||
2013 | 105 | self.log.debug('actual: {}'.format(repr(actual))) | ||
2014 | 106 | for e in expected: | ||
2015 | 107 | found = False | ||
2016 | 108 | for act in actual: | ||
2017 | 109 | a = {'enabled': act.enabled, 'description': act.description, | ||
2018 | 110 | 'name': act.name, 'id': act.id} | ||
2019 | 111 | if e['name'] == a['name']: | ||
2020 | 112 | found = True | ||
2021 | 113 | ret = self._validate_dict_data(e, a) | ||
2022 | 114 | if ret: | ||
2023 | 115 | return "unexpected tenant data - {}".format(ret) | ||
2024 | 116 | if not found: | ||
2025 | 117 | return "tenant {} does not exist".format(e['name']) | ||
2026 | 118 | return ret | ||
2027 | 119 | |||
2028 | 120 | def validate_role_data(self, expected, actual): | ||
2029 | 121 | """Validate role data. | ||
2030 | 122 | |||
2031 | 123 | Validate a list of actual role data vs a list of expected role | ||
2032 | 124 | data. | ||
2033 | 125 | """ | ||
2034 | 126 | self.log.debug('Validating role data...') | ||
2035 | 127 | self.log.debug('actual: {}'.format(repr(actual))) | ||
2036 | 128 | for e in expected: | ||
2037 | 129 | found = False | ||
2038 | 130 | for act in actual: | ||
2039 | 131 | a = {'name': act.name, 'id': act.id} | ||
2040 | 132 | if e['name'] == a['name']: | ||
2041 | 133 | found = True | ||
2042 | 134 | ret = self._validate_dict_data(e, a) | ||
2043 | 135 | if ret: | ||
2044 | 136 | return "unexpected role data - {}".format(ret) | ||
2045 | 137 | if not found: | ||
2046 | 138 | return "role {} does not exist".format(e['name']) | ||
2047 | 139 | return ret | ||
2048 | 140 | |||
2049 | 141 | def validate_user_data(self, expected, actual): | ||
2050 | 142 | """Validate user data. | ||
2051 | 143 | |||
2052 | 144 | Validate a list of actual user data vs a list of expected user | ||
2053 | 145 | data. | ||
2054 | 146 | """ | ||
2055 | 147 | self.log.debug('Validating user data...') | ||
2056 | 148 | self.log.debug('actual: {}'.format(repr(actual))) | ||
2057 | 149 | for e in expected: | ||
2058 | 150 | found = False | ||
2059 | 151 | for act in actual: | ||
2060 | 152 | a = {'enabled': act.enabled, 'name': act.name, | ||
2061 | 153 | 'email': act.email, 'tenantId': act.tenantId, | ||
2062 | 154 | 'id': act.id} | ||
2063 | 155 | if e['name'] == a['name']: | ||
2064 | 156 | found = True | ||
2065 | 157 | ret = self._validate_dict_data(e, a) | ||
2066 | 158 | if ret: | ||
2067 | 159 | return "unexpected user data - {}".format(ret) | ||
2068 | 160 | if not found: | ||
2069 | 161 | return "user {} does not exist".format(e['name']) | ||
2070 | 162 | return ret | ||
2071 | 163 | |||
2072 | 164 | def validate_flavor_data(self, expected, actual): | ||
2073 | 165 | """Validate flavor data. | ||
2074 | 166 | |||
2075 | 167 | Validate a list of actual flavors vs a list of expected flavors. | ||
2076 | 168 | """ | ||
2077 | 169 | self.log.debug('Validating flavor data...') | ||
2078 | 170 | self.log.debug('actual: {}'.format(repr(actual))) | ||
2079 | 171 | act = [a.name for a in actual] | ||
2080 | 172 | return self._validate_list_data(expected, act) | ||
2081 | 173 | |||
2082 | 174 | def tenant_exists(self, keystone, tenant): | ||
2083 | 175 | """Return True if tenant exists.""" | ||
2084 | 176 | self.log.debug('Checking if tenant exists ({})...'.format(tenant)) | ||
2085 | 177 | return tenant in [t.name for t in keystone.tenants.list()] | ||
2086 | 178 | |||
2087 | 179 | def authenticate_cinder_admin(self, keystone_sentry, username, | ||
2088 | 180 | password, tenant): | ||
2089 | 181 | """Authenticates admin user with cinder.""" | ||
2090 | 182 | # NOTE(beisner): cinder python client doesn't accept tokens. | ||
2091 | 183 | service_ip = \ | ||
2092 | 184 | keystone_sentry.relation('shared-db', | ||
2093 | 185 | 'mysql:shared-db')['private-address'] | ||
2094 | 186 | ept = "http://{}:5000/v2.0".format(service_ip.strip().decode('utf-8')) | ||
2095 | 187 | return cinder_client.Client(username, password, tenant, ept) | ||
2096 | 188 | |||
2097 | 189 | def authenticate_keystone_admin(self, keystone_sentry, user, password, | ||
2098 | 190 | tenant): | ||
2099 | 191 | """Authenticates admin user with the keystone admin endpoint.""" | ||
2100 | 192 | self.log.debug('Authenticating keystone admin...') | ||
2101 | 193 | unit = keystone_sentry | ||
2102 | 194 | service_ip = unit.relation('shared-db', | ||
2103 | 195 | 'mysql:shared-db')['private-address'] | ||
2104 | 196 | ep = "http://{}:35357/v2.0".format(service_ip.strip().decode('utf-8')) | ||
2105 | 197 | return keystone_client.Client(username=user, password=password, | ||
2106 | 198 | tenant_name=tenant, auth_url=ep) | ||
2107 | 199 | |||
2108 | 200 | def authenticate_keystone_user(self, keystone, user, password, tenant): | ||
2109 | 201 | """Authenticates a regular user with the keystone public endpoint.""" | ||
2110 | 202 | self.log.debug('Authenticating keystone user ({})...'.format(user)) | ||
2111 | 203 | ep = keystone.service_catalog.url_for(service_type='identity', | ||
2112 | 204 | endpoint_type='publicURL') | ||
2113 | 205 | return keystone_client.Client(username=user, password=password, | ||
2114 | 206 | tenant_name=tenant, auth_url=ep) | ||
2115 | 207 | |||
2116 | 208 | def authenticate_glance_admin(self, keystone): | ||
2117 | 209 | """Authenticates admin user with glance.""" | ||
2118 | 210 | self.log.debug('Authenticating glance admin...') | ||
2119 | 211 | ep = keystone.service_catalog.url_for(service_type='image', | ||
2120 | 212 | endpoint_type='adminURL') | ||
2121 | 213 | return glance_client.Client(ep, token=keystone.auth_token) | ||
2122 | 214 | |||
2123 | 215 | def authenticate_heat_admin(self, keystone): | ||
2124 | 216 | """Authenticates the admin user with heat.""" | ||
2125 | 217 | self.log.debug('Authenticating heat admin...') | ||
2126 | 218 | ep = keystone.service_catalog.url_for(service_type='orchestration', | ||
2127 | 219 | endpoint_type='publicURL') | ||
2128 | 220 | return heat_client.Client(endpoint=ep, token=keystone.auth_token) | ||
2129 | 221 | |||
2130 | 222 | def authenticate_nova_user(self, keystone, user, password, tenant): | ||
2131 | 223 | """Authenticates a regular user with nova-api.""" | ||
2132 | 224 | self.log.debug('Authenticating nova user ({})...'.format(user)) | ||
2133 | 225 | ep = keystone.service_catalog.url_for(service_type='identity', | ||
2134 | 226 | endpoint_type='publicURL') | ||
2135 | 227 | return nova_client.Client(username=user, api_key=password, | ||
2136 | 228 | project_id=tenant, auth_url=ep) | ||
2137 | 229 | |||
2138 | 230 | def authenticate_swift_user(self, keystone, user, password, tenant): | ||
2139 | 231 | """Authenticates a regular user with swift api.""" | ||
2140 | 232 | self.log.debug('Authenticating swift user ({})...'.format(user)) | ||
2141 | 233 | ep = keystone.service_catalog.url_for(service_type='identity', | ||
2142 | 234 | endpoint_type='publicURL') | ||
2143 | 235 | return swiftclient.Connection(authurl=ep, | ||
2144 | 236 | user=user, | ||
2145 | 237 | key=password, | ||
2146 | 238 | tenant_name=tenant, | ||
2147 | 239 | auth_version='2.0') | ||
2148 | 240 | |||
2149 | 241 | def create_cirros_image(self, glance, image_name): | ||
2150 | 242 | """Download the latest cirros image and upload it to glance, | ||
2151 | 243 | validate and return a resource pointer. | ||
2152 | 244 | |||
2153 | 245 | :param glance: pointer to authenticated glance connection | ||
2154 | 246 | :param image_name: display name for new image | ||
2155 | 247 | :returns: glance image pointer | ||
2156 | 248 | """ | ||
2157 | 249 | self.log.debug('Creating glance cirros image ' | ||
2158 | 250 | '({})...'.format(image_name)) | ||
2159 | 251 | |||
2160 | 252 | # Download cirros image | ||
2161 | 253 | http_proxy = os.getenv('AMULET_HTTP_PROXY') | ||
2162 | 254 | self.log.debug('AMULET_HTTP_PROXY: {}'.format(http_proxy)) | ||
2163 | 255 | if http_proxy: | ||
2164 | 256 | proxies = {'http': http_proxy} | ||
2165 | 257 | opener = urllib.FancyURLopener(proxies) | ||
2166 | 258 | else: | ||
2167 | 259 | opener = urllib.FancyURLopener() | ||
2168 | 260 | |||
2169 | 261 | f = opener.open('http://download.cirros-cloud.net/version/released') | ||
2170 | 262 | version = f.read().strip() | ||
2171 | 263 | cirros_img = 'cirros-{}-x86_64-disk.img'.format(version) | ||
2172 | 264 | local_path = os.path.join('tests', cirros_img) | ||
2173 | 265 | |||
2174 | 266 | if not os.path.exists(local_path): | ||
2175 | 267 | cirros_url = 'http://{}/{}/{}'.format('download.cirros-cloud.net', | ||
2176 | 268 | version, cirros_img) | ||
2177 | 269 | opener.retrieve(cirros_url, local_path) | ||
2178 | 270 | f.close() | ||
2179 | 271 | |||
2180 | 272 | # Create glance image | ||
2181 | 273 | with open(local_path) as f: | ||
2182 | 274 | image = glance.images.create(name=image_name, is_public=True, | ||
2183 | 275 | disk_format='qcow2', | ||
2184 | 276 | container_format='bare', data=f) | ||
2185 | 277 | |||
2186 | 278 | # Wait for image to reach active status | ||
2187 | 279 | img_id = image.id | ||
2188 | 280 | ret = self.resource_reaches_status(glance.images, img_id, | ||
2189 | 281 | expected_stat='active', | ||
2190 | 282 | msg='Image status wait') | ||
2191 | 283 | if not ret: | ||
2192 | 284 | msg = 'Glance image failed to reach expected state.' | ||
2193 | 285 | amulet.raise_status(amulet.FAIL, msg=msg) | ||
2194 | 286 | |||
2195 | 287 | # Re-validate new image | ||
2196 | 288 | self.log.debug('Validating image attributes...') | ||
2197 | 289 | val_img_name = glance.images.get(img_id).name | ||
2198 | 290 | val_img_stat = glance.images.get(img_id).status | ||
2199 | 291 | val_img_pub = glance.images.get(img_id).is_public | ||
2200 | 292 | val_img_cfmt = glance.images.get(img_id).container_format | ||
2201 | 293 | val_img_dfmt = glance.images.get(img_id).disk_format | ||
2202 | 294 | msg_attr = ('Image attributes - name:{} public:{} id:{} stat:{} ' | ||
2203 | 295 | 'container fmt:{} disk fmt:{}'.format( | ||
2204 | 296 | val_img_name, val_img_pub, img_id, | ||
2205 | 297 | val_img_stat, val_img_cfmt, val_img_dfmt)) | ||
2206 | 298 | |||
2207 | 299 | if val_img_name == image_name and val_img_stat == 'active' \ | ||
2208 | 300 | and val_img_pub is True and val_img_cfmt == 'bare' \ | ||
2209 | 301 | and val_img_dfmt == 'qcow2': | ||
2210 | 302 | self.log.debug(msg_attr) | ||
2211 | 303 | else: | ||
2212 | 304 | msg = ('Volume validation failed, {}'.format(msg_attr)) | ||
2213 | 305 | amulet.raise_status(amulet.FAIL, msg=msg) | ||
2214 | 306 | |||
2215 | 307 | return image | ||
2216 | 308 | |||
2217 | 309 | def delete_image(self, glance, image): | ||
2218 | 310 | """Delete the specified image.""" | ||
2219 | 311 | |||
2220 | 312 | # /!\ DEPRECATION WARNING | ||
2221 | 313 | self.log.warn('/!\\ DEPRECATION WARNING: use ' | ||
2222 | 314 | 'delete_resource instead of delete_image.') | ||
2223 | 315 | self.log.debug('Deleting glance image ({})...'.format(image)) | ||
2224 | 316 | return self.delete_resource(glance.images, image, msg='glance image') | ||
2225 | 317 | |||
2226 | 318 | def create_instance(self, nova, image_name, instance_name, flavor): | ||
2227 | 319 | """Create the specified instance.""" | ||
2228 | 320 | self.log.debug('Creating instance ' | ||
2229 | 321 | '({}|{}|{})'.format(instance_name, image_name, flavor)) | ||
2230 | 322 | image = nova.images.find(name=image_name) | ||
2231 | 323 | flavor = nova.flavors.find(name=flavor) | ||
2232 | 324 | instance = nova.servers.create(name=instance_name, image=image, | ||
2233 | 325 | flavor=flavor) | ||
2234 | 326 | |||
2235 | 327 | count = 1 | ||
2236 | 328 | status = instance.status | ||
2237 | 329 | while status != 'ACTIVE' and count < 60: | ||
2238 | 330 | time.sleep(3) | ||
2239 | 331 | instance = nova.servers.get(instance.id) | ||
2240 | 332 | status = instance.status | ||
2241 | 333 | self.log.debug('instance status: {}'.format(status)) | ||
2242 | 334 | count += 1 | ||
2243 | 335 | |||
2244 | 336 | if status != 'ACTIVE': | ||
2245 | 337 | self.log.error('instance creation timed out') | ||
2246 | 338 | return None | ||
2247 | 339 | |||
2248 | 340 | return instance | ||
2249 | 341 | |||
2250 | 342 | def delete_instance(self, nova, instance): | ||
2251 | 343 | """Delete the specified instance.""" | ||
2252 | 344 | |||
2253 | 345 | # /!\ DEPRECATION WARNING | ||
2254 | 346 | self.log.warn('/!\\ DEPRECATION WARNING: use ' | ||
2255 | 347 | 'delete_resource instead of delete_instance.') | ||
2256 | 348 | self.log.debug('Deleting instance ({})...'.format(instance)) | ||
2257 | 349 | return self.delete_resource(nova.servers, instance, | ||
2258 | 350 | msg='nova instance') | ||
2259 | 351 | |||
2260 | 352 | def create_or_get_keypair(self, nova, keypair_name="testkey"): | ||
2261 | 353 | """Create a new keypair, or return pointer if it already exists.""" | ||
2262 | 354 | try: | ||
2263 | 355 | _keypair = nova.keypairs.get(keypair_name) | ||
2264 | 356 | self.log.debug('Keypair ({}) already exists, ' | ||
2265 | 357 | 'using it.'.format(keypair_name)) | ||
2266 | 358 | return _keypair | ||
2267 | 359 | except: | ||
2268 | 360 | self.log.debug('Keypair ({}) does not exist, ' | ||
2269 | 361 | 'creating it.'.format(keypair_name)) | ||
2270 | 362 | |||
2271 | 363 | _keypair = nova.keypairs.create(name=keypair_name) | ||
2272 | 364 | return _keypair | ||
2273 | 365 | |||
2274 | 366 | def create_cinder_volume(self, cinder, vol_name="demo-vol", vol_size=1, | ||
2275 | 367 | img_id=None, src_vol_id=None, snap_id=None): | ||
2276 | 368 | """Create cinder volume, optionally from a glance image, OR | ||
2277 | 369 | optionally as a clone of an existing volume, OR optionally | ||
2278 | 370 | from a snapshot. Wait for the new volume status to reach | ||
2279 | 371 | the expected status, validate and return a resource pointer. | ||
2280 | 372 | |||
2281 | 373 | :param vol_name: cinder volume display name | ||
2282 | 374 | :param vol_size: size in gigabytes | ||
2283 | 375 | :param img_id: optional glance image id | ||
2284 | 376 | :param src_vol_id: optional source volume id to clone | ||
2285 | 377 | :param snap_id: optional snapshot id to use | ||
2286 | 378 | :returns: cinder volume pointer | ||
2287 | 379 | """ | ||
2288 | 380 | # Handle parameter input and avoid impossible combinations | ||
2289 | 381 | if img_id and not src_vol_id and not snap_id: | ||
2290 | 382 | # Create volume from image | ||
2291 | 383 | self.log.debug('Creating cinder volume from glance image...') | ||
2292 | 384 | bootable = 'true' | ||
2293 | 385 | elif src_vol_id and not img_id and not snap_id: | ||
2294 | 386 | # Clone an existing volume | ||
2295 | 387 | self.log.debug('Cloning cinder volume...') | ||
2296 | 388 | bootable = cinder.volumes.get(src_vol_id).bootable | ||
2297 | 389 | elif snap_id and not src_vol_id and not img_id: | ||
2298 | 390 | # Create volume from snapshot | ||
2299 | 391 | self.log.debug('Creating cinder volume from snapshot...') | ||
2300 | 392 | snap = cinder.volume_snapshots.find(id=snap_id) | ||
2301 | 393 | vol_size = snap.size | ||
2302 | 394 | snap_vol_id = cinder.volume_snapshots.get(snap_id).volume_id | ||
2303 | 395 | bootable = cinder.volumes.get(snap_vol_id).bootable | ||
2304 | 396 | elif not img_id and not src_vol_id and not snap_id: | ||
2305 | 397 | # Create volume | ||
2306 | 398 | self.log.debug('Creating cinder volume...') | ||
2307 | 399 | bootable = 'false' | ||
2308 | 400 | else: | ||
2309 | 401 | # Impossible combination of parameters | ||
2310 | 402 | msg = ('Invalid method use - name:{} size:{} img_id:{} ' | ||
2311 | 403 | 'src_vol_id:{} snap_id:{}'.format(vol_name, vol_size, | ||
2312 | 404 | img_id, src_vol_id, | ||
2313 | 405 | snap_id)) | ||
2314 | 406 | amulet.raise_status(amulet.FAIL, msg=msg) | ||
2315 | 407 | |||
2316 | 408 | # Create new volume | ||
2317 | 409 | try: | ||
2318 | 410 | vol_new = cinder.volumes.create(display_name=vol_name, | ||
2319 | 411 | imageRef=img_id, | ||
2320 | 412 | size=vol_size, | ||
2321 | 413 | source_volid=src_vol_id, | ||
2322 | 414 | snapshot_id=snap_id) | ||
2323 | 415 | vol_id = vol_new.id | ||
2324 | 416 | except Exception as e: | ||
2325 | 417 | msg = 'Failed to create volume: {}'.format(e) | ||
2326 | 418 | amulet.raise_status(amulet.FAIL, msg=msg) | ||
2327 | 419 | |||
2328 | 420 | # Wait for volume to reach available status | ||
2329 | 421 | ret = self.resource_reaches_status(cinder.volumes, vol_id, | ||
2330 | 422 | expected_stat="available", | ||
2331 | 423 | msg="Volume status wait") | ||
2332 | 424 | if not ret: | ||
2333 | 425 | msg = 'Cinder volume failed to reach expected state.' | ||
2334 | 426 | amulet.raise_status(amulet.FAIL, msg=msg) | ||
2335 | 427 | |||
2336 | 428 | # Re-validate new volume | ||
2337 | 429 | self.log.debug('Validating volume attributes...') | ||
2338 | 430 | val_vol_name = cinder.volumes.get(vol_id).display_name | ||
2339 | 431 | val_vol_boot = cinder.volumes.get(vol_id).bootable | ||
2340 | 432 | val_vol_stat = cinder.volumes.get(vol_id).status | ||
2341 | 433 | val_vol_size = cinder.volumes.get(vol_id).size | ||
2342 | 434 | msg_attr = ('Volume attributes - name:{} id:{} stat:{} boot:' | ||
2343 | 435 | '{} size:{}'.format(val_vol_name, vol_id, | ||
2344 | 436 | val_vol_stat, val_vol_boot, | ||
2345 | 437 | val_vol_size)) | ||
2346 | 438 | |||
2347 | 439 | if val_vol_boot == bootable and val_vol_stat == 'available' \ | ||
2348 | 440 | and val_vol_name == vol_name and val_vol_size == vol_size: | ||
2349 | 441 | self.log.debug(msg_attr) | ||
2350 | 442 | else: | ||
2351 | 443 | msg = ('Volume validation failed, {}'.format(msg_attr)) | ||
2352 | 444 | amulet.raise_status(amulet.FAIL, msg=msg) | ||
2353 | 445 | |||
2354 | 446 | return vol_new | ||
2355 | 447 | |||
2356 | 448 | def delete_resource(self, resource, resource_id, | ||
2357 | 449 | msg="resource", max_wait=120): | ||
2358 | 450 | """Delete one openstack resource, such as one instance, keypair, | ||
2359 | 451 | image, volume, stack, etc., and confirm deletion within max wait time. | ||
2360 | 452 | |||
2361 | 453 | :param resource: pointer to os resource type, ex:glance_client.images | ||
2362 | 454 | :param resource_id: unique name or id for the openstack resource | ||
2363 | 455 | :param msg: text to identify purpose in logging | ||
2364 | 456 | :param max_wait: maximum wait time in seconds | ||
2365 | 457 | :returns: True if successful, otherwise False | ||
2366 | 458 | """ | ||
2367 | 459 | self.log.debug('Deleting OpenStack resource ' | ||
2368 | 460 | '{} ({})'.format(resource_id, msg)) | ||
2369 | 461 | num_before = len(list(resource.list())) | ||
2370 | 462 | resource.delete(resource_id) | ||
2371 | 463 | |||
2372 | 464 | tries = 0 | ||
2373 | 465 | num_after = len(list(resource.list())) | ||
2374 | 466 | while num_after != (num_before - 1) and tries < (max_wait / 4): | ||
2375 | 467 | self.log.debug('{} delete check: ' | ||
2376 | 468 | '{} [{}:{}] {}'.format(msg, tries, | ||
2377 | 469 | num_before, | ||
2378 | 470 | num_after, | ||
2379 | 471 | resource_id)) | ||
2380 | 472 | time.sleep(4) | ||
2381 | 473 | num_after = len(list(resource.list())) | ||
2382 | 474 | tries += 1 | ||
2383 | 475 | |||
2384 | 476 | self.log.debug('{}: expected, actual count = {}, ' | ||
2385 | 477 | '{}'.format(msg, num_before - 1, num_after)) | ||
2386 | 478 | |||
2387 | 479 | if num_after == (num_before - 1): | ||
2388 | 480 | return True | ||
2389 | 481 | else: | ||
2390 | 482 | self.log.error('{} delete timed out'.format(msg)) | ||
2391 | 483 | return False | ||
2392 | 484 | |||
2393 | 485 | def resource_reaches_status(self, resource, resource_id, | ||
2394 | 486 | expected_stat='available', | ||
2395 | 487 | msg='resource', max_wait=120): | ||
2396 | 488 | """Wait for an openstack resources status to reach an | ||
2397 | 489 | expected status within a specified time. Useful to confirm that | ||
2398 | 490 | nova instances, cinder vols, snapshots, glance images, heat stacks | ||
2399 | 491 | and other resources eventually reach the expected status. | ||
2400 | 492 | |||
2401 | 493 | :param resource: pointer to os resource type, ex: heat_client.stacks | ||
2402 | 494 | :param resource_id: unique id for the openstack resource | ||
2403 | 495 | :param expected_stat: status to expect resource to reach | ||
2404 | 496 | :param msg: text to identify purpose in logging | ||
2405 | 497 | :param max_wait: maximum wait time in seconds | ||
2406 | 498 | :returns: True if successful, False if status is not reached | ||
2407 | 499 | """ | ||
2408 | 500 | |||
2409 | 501 | tries = 0 | ||
2410 | 502 | resource_stat = resource.get(resource_id).status | ||
2411 | 503 | while resource_stat != expected_stat and tries < (max_wait / 4): | ||
2412 | 504 | self.log.debug('{} status check: ' | ||
2413 | 505 | '{} [{}:{}] {}'.format(msg, tries, | ||
2414 | 506 | resource_stat, | ||
2415 | 507 | expected_stat, | ||
2416 | 508 | resource_id)) | ||
2417 | 509 | time.sleep(4) | ||
2418 | 510 | resource_stat = resource.get(resource_id).status | ||
2419 | 511 | tries += 1 | ||
2420 | 512 | |||
2421 | 513 | self.log.debug('{}: expected, actual status = {}, ' | ||
2422 | 514 | '{}'.format(msg, resource_stat, expected_stat)) | ||
2423 | 515 | |||
2424 | 516 | if resource_stat == expected_stat: | ||
2425 | 517 | return True | ||
2426 | 518 | else: | ||
2427 | 519 | self.log.debug('{} never reached expected status: ' | ||
2428 | 520 | '{}'.format(resource_id, expected_stat)) | ||
2429 | 521 | return False | ||
2430 | 522 | |||
2431 | 523 | def get_ceph_osd_id_cmd(self, index): | ||
2432 | 524 | """Produce a shell command that will return a ceph-osd id.""" | ||
2433 | 525 | return ("`initctl list | grep 'ceph-osd ' | " | ||
2434 | 526 | "awk 'NR=={} {{ print $2 }}' | " | ||
2435 | 527 | "grep -o '[0-9]*'`".format(index + 1)) | ||
2436 | 528 | |||
2437 | 529 | def get_ceph_pools(self, sentry_unit): | ||
2438 | 530 | """Return a dict of ceph pools from a single ceph unit, with | ||
2439 | 531 | pool name as keys, pool id as vals.""" | ||
2440 | 532 | pools = {} | ||
2441 | 533 | cmd = 'sudo ceph osd lspools' | ||
2442 | 534 | output, code = sentry_unit.run(cmd) | ||
2443 | 535 | if code != 0: | ||
2444 | 536 | msg = ('{} `{}` returned {} ' | ||
2445 | 537 | '{}'.format(sentry_unit.info['unit_name'], | ||
2446 | 538 | cmd, code, output)) | ||
2447 | 539 | amulet.raise_status(amulet.FAIL, msg=msg) | ||
2448 | 540 | |||
2449 | 541 | # Example output: 0 data,1 metadata,2 rbd,3 cinder,4 glance, | ||
2450 | 542 | for pool in str(output).split(','): | ||
2451 | 543 | pool_id_name = pool.split(' ') | ||
2452 | 544 | if len(pool_id_name) == 2: | ||
2453 | 545 | pool_id = pool_id_name[0] | ||
2454 | 546 | pool_name = pool_id_name[1] | ||
2455 | 547 | pools[pool_name] = int(pool_id) | ||
2456 | 548 | |||
2457 | 549 | self.log.debug('Pools on {}: {}'.format(sentry_unit.info['unit_name'], | ||
2458 | 550 | pools)) | ||
2459 | 551 | return pools | ||
2460 | 552 | |||
2461 | 553 | def get_ceph_df(self, sentry_unit): | ||
2462 | 554 | """Return dict of ceph df json output, including ceph pool state. | ||
2463 | 555 | |||
2464 | 556 | :param sentry_unit: Pointer to amulet sentry instance (juju unit) | ||
2465 | 557 | :returns: Dict of ceph df output | ||
2466 | 558 | """ | ||
2467 | 559 | cmd = 'sudo ceph df --format=json' | ||
2468 | 560 | output, code = sentry_unit.run(cmd) | ||
2469 | 561 | if code != 0: | ||
2470 | 562 | msg = ('{} `{}` returned {} ' | ||
2471 | 563 | '{}'.format(sentry_unit.info['unit_name'], | ||
2472 | 564 | cmd, code, output)) | ||
2473 | 565 | amulet.raise_status(amulet.FAIL, msg=msg) | ||
2474 | 566 | return json.loads(output) | ||
2475 | 567 | |||
2476 | 568 | def get_ceph_pool_sample(self, sentry_unit, pool_id=0): | ||
2477 | 569 | """Take a sample of attributes of a ceph pool, returning ceph | ||
2478 | 570 | pool name, object count and disk space used for the specified | ||
2479 | 571 | pool ID number. | ||
2480 | 572 | |||
2481 | 573 | :param sentry_unit: Pointer to amulet sentry instance (juju unit) | ||
2482 | 574 | :param pool_id: Ceph pool ID | ||
2483 | 575 | :returns: List of pool name, object count, kb disk space used | ||
2484 | 576 | """ | ||
2485 | 577 | df = self.get_ceph_df(sentry_unit) | ||
2486 | 578 | pool_name = df['pools'][pool_id]['name'] | ||
2487 | 579 | obj_count = df['pools'][pool_id]['stats']['objects'] | ||
2488 | 580 | kb_used = df['pools'][pool_id]['stats']['kb_used'] | ||
2489 | 581 | self.log.debug('Ceph {} pool (ID {}): {} objects, ' | ||
2490 | 582 | '{} kb used'.format(pool_name, pool_id, | ||
2491 | 583 | obj_count, kb_used)) | ||
2492 | 584 | return pool_name, obj_count, kb_used | ||
2493 | 585 | |||
2494 | 586 | def validate_ceph_pool_samples(self, samples, sample_type="resource pool"): | ||
2495 | 587 | """Validate ceph pool samples taken over time, such as pool | ||
2496 | 588 | object counts or pool kb used, before adding, after adding, and | ||
2497 | 589 | after deleting items which affect those pool attributes. The | ||
2498 | 590 | 2nd element is expected to be greater than the 1st; 3rd is expected | ||
2499 | 591 | to be less than the 2nd. | ||
2500 | 592 | |||
2501 | 593 | :param samples: List containing 3 data samples | ||
2502 | 594 | :param sample_type: String for logging and usage context | ||
2503 | 595 | :returns: None if successful, Failure message otherwise | ||
2504 | 596 | """ | ||
2505 | 597 | original, created, deleted = range(3) | ||
2506 | 598 | if samples[created] <= samples[original] or \ | ||
2507 | 599 | samples[deleted] >= samples[created]: | ||
2508 | 600 | return ('Ceph {} samples ({}) ' | ||
2509 | 601 | 'unexpected.'.format(sample_type, samples)) | ||
2510 | 602 | else: | ||
2511 | 603 | self.log.debug('Ceph {} samples (OK): ' | ||
2512 | 604 | '{}'.format(sample_type, samples)) | ||
2513 | 605 | return None | ||
2514 | 606 | |||
2515 | 607 | # rabbitmq/amqp specific helpers: | ||
2516 | 608 | def add_rmq_test_user(self, sentry_units, | ||
2517 | 609 | username="testuser1", password="changeme"): | ||
2518 | 610 | """Add a test user via the first rmq juju unit, check connection as | ||
2519 | 611 | the new user against all sentry units. | ||
2520 | 612 | |||
2521 | 613 | :param sentry_units: list of sentry unit pointers | ||
2522 | 614 | :param username: amqp user name, default to testuser1 | ||
2523 | 615 | :param password: amqp user password | ||
2524 | 616 | :returns: None if successful. Raise on error. | ||
2525 | 617 | """ | ||
2526 | 618 | self.log.debug('Adding rmq user ({})...'.format(username)) | ||
2527 | 619 | |||
2528 | 620 | # Check that user does not already exist | ||
2529 | 621 | cmd_user_list = 'rabbitmqctl list_users' | ||
2530 | 622 | output, _ = self.run_cmd_unit(sentry_units[0], cmd_user_list) | ||
2531 | 623 | if username in output: | ||
2532 | 624 | self.log.warning('User ({}) already exists, returning ' | ||
2533 | 625 | 'gracefully.'.format(username)) | ||
2534 | 626 | return | ||
2535 | 627 | |||
2536 | 628 | perms = '".*" ".*" ".*"' | ||
2537 | 629 | cmds = ['rabbitmqctl add_user {} {}'.format(username, password), | ||
2538 | 630 | 'rabbitmqctl set_permissions {} {}'.format(username, perms)] | ||
2539 | 631 | |||
2540 | 632 | # Add user via first unit | ||
2541 | 633 | for cmd in cmds: | ||
2542 | 634 | output, _ = self.run_cmd_unit(sentry_units[0], cmd) | ||
2543 | 635 | |||
2544 | 636 | # Check connection against the other sentry_units | ||
2545 | 637 | self.log.debug('Checking user connect against units...') | ||
2546 | 638 | for sentry_unit in sentry_units: | ||
2547 | 639 | connection = self.connect_amqp_by_unit(sentry_unit, ssl=False, | ||
2548 | 640 | username=username, | ||
2549 | 641 | password=password) | ||
2550 | 642 | connection.close() | ||
2551 | 643 | |||
2552 | 644 | def delete_rmq_test_user(self, sentry_units, username="testuser1"): | ||
2553 | 645 | """Delete a rabbitmq user via the first rmq juju unit. | ||
2554 | 646 | |||
2555 | 647 | :param sentry_units: list of sentry unit pointers | ||
2556 | 648 | :param username: amqp user name, default to testuser1 | ||
2557 | 649 | :param password: amqp user password | ||
2558 | 650 | :returns: None if successful or no such user. | ||
2559 | 651 | """ | ||
2560 | 652 | self.log.debug('Deleting rmq user ({})...'.format(username)) | ||
2561 | 653 | |||
2562 | 654 | # Check that the user exists | ||
2563 | 655 | cmd_user_list = 'rabbitmqctl list_users' | ||
2564 | 656 | output, _ = self.run_cmd_unit(sentry_units[0], cmd_user_list) | ||
2565 | 657 | |||
2566 | 658 | if username not in output: | ||
2567 | 659 | self.log.warning('User ({}) does not exist, returning ' | ||
2568 | 660 | 'gracefully.'.format(username)) | ||
2569 | 661 | return | ||
2570 | 662 | |||
2571 | 663 | # Delete the user | ||
2572 | 664 | cmd_user_del = 'rabbitmqctl delete_user {}'.format(username) | ||
2573 | 665 | output, _ = self.run_cmd_unit(sentry_units[0], cmd_user_del) | ||
2574 | 666 | |||
2575 | 667 | def get_rmq_cluster_status(self, sentry_unit): | ||
2576 | 668 | """Execute rabbitmq cluster status command on a unit and return | ||
2577 | 669 | the full output. | ||
2578 | 670 | |||
2579 | 671 | :param unit: sentry unit | ||
2580 | 672 | :returns: String containing console output of cluster status command | ||
2581 | 673 | """ | ||
2582 | 674 | cmd = 'rabbitmqctl cluster_status' | ||
2583 | 675 | output, _ = self.run_cmd_unit(sentry_unit, cmd) | ||
2584 | 676 | self.log.debug('{} cluster_status:\n{}'.format( | ||
2585 | 677 | sentry_unit.info['unit_name'], output)) | ||
2586 | 678 | return str(output) | ||
2587 | 679 | |||
2588 | 680 | def get_rmq_cluster_running_nodes(self, sentry_unit): | ||
2589 | 681 | """Parse rabbitmqctl cluster_status output string, return list of | ||
2590 | 682 | running rabbitmq cluster nodes. | ||
2591 | 683 | |||
2592 | 684 | :param unit: sentry unit | ||
2593 | 685 | :returns: List containing node names of running nodes | ||
2594 | 686 | """ | ||
2595 | 687 | # NOTE(beisner): rabbitmqctl cluster_status output is not | ||
2596 | 688 | # json-parsable, do string chop foo, then json.loads that. | ||
2597 | 689 | str_stat = self.get_rmq_cluster_status(sentry_unit) | ||
2598 | 690 | if 'running_nodes' in str_stat: | ||
2599 | 691 | pos_start = str_stat.find("{running_nodes,") + 15 | ||
2600 | 692 | pos_end = str_stat.find("]},", pos_start) + 1 | ||
2601 | 693 | str_run_nodes = str_stat[pos_start:pos_end].replace("'", '"') | ||
2602 | 694 | run_nodes = json.loads(str_run_nodes) | ||
2603 | 695 | return run_nodes | ||
2604 | 696 | else: | ||
2605 | 697 | return [] | ||
2606 | 698 | |||
2607 | 699 | def validate_rmq_cluster_running_nodes(self, sentry_units): | ||
2608 | 700 | """Check that all rmq unit hostnames are represented in the | ||
2609 | 701 | cluster_status output of all units. | ||
2610 | 702 | |||
2611 | 703 | :param host_names: dict of juju unit names to host names | ||
2612 | 704 | :param units: list of sentry unit pointers (all rmq units) | ||
2613 | 705 | :returns: None if successful, otherwise return error message | ||
2614 | 706 | """ | ||
2615 | 707 | host_names = self.get_unit_hostnames(sentry_units) | ||
2616 | 708 | errors = [] | ||
2617 | 709 | |||
2618 | 710 | # Query every unit for cluster_status running nodes | ||
2619 | 711 | for query_unit in sentry_units: | ||
2620 | 712 | query_unit_name = query_unit.info['unit_name'] | ||
2621 | 713 | running_nodes = self.get_rmq_cluster_running_nodes(query_unit) | ||
2622 | 714 | |||
2623 | 715 | # Confirm that every unit is represented in the queried unit's | ||
2624 | 716 | # cluster_status running nodes output. | ||
2625 | 717 | for validate_unit in sentry_units: | ||
2626 | 718 | val_host_name = host_names[validate_unit.info['unit_name']] | ||
2627 | 719 | val_node_name = 'rabbit@{}'.format(val_host_name) | ||
2628 | 720 | |||
2629 | 721 | if val_node_name not in running_nodes: | ||
2630 | 722 | errors.append('Cluster member check failed on {}: {} not ' | ||
2631 | 723 | 'in {}\n'.format(query_unit_name, | ||
2632 | 724 | val_node_name, | ||
2633 | 725 | running_nodes)) | ||
2634 | 726 | if errors: | ||
2635 | 727 | return ''.join(errors) | ||
2636 | 728 | |||
2637 | 729 | def rmq_ssl_is_enabled_on_unit(self, sentry_unit, port=None): | ||
2638 | 730 | """Check a single juju rmq unit for ssl and port in the config file.""" | ||
2639 | 731 | host = sentry_unit.info['public-address'] | ||
2640 | 732 | unit_name = sentry_unit.info['unit_name'] | ||
2641 | 733 | |||
2642 | 734 | conf_file = '/etc/rabbitmq/rabbitmq.config' | ||
2643 | 735 | conf_contents = str(self.file_contents_safe(sentry_unit, | ||
2644 | 736 | conf_file, max_wait=16)) | ||
2645 | 737 | # Checks | ||
2646 | 738 | conf_ssl = 'ssl' in conf_contents | ||
2647 | 739 | conf_port = str(port) in conf_contents | ||
2648 | 740 | |||
2649 | 741 | # Port explicitly checked in config | ||
2650 | 742 | if port and conf_port and conf_ssl: | ||
2651 | 743 | self.log.debug('SSL is enabled @{}:{} ' | ||
2652 | 744 | '({})'.format(host, port, unit_name)) | ||
2653 | 745 | return True | ||
2654 | 746 | elif port and not conf_port and conf_ssl: | ||
2655 | 747 | self.log.debug('SSL is enabled @{} but not on port {} ' | ||
2656 | 748 | '({})'.format(host, port, unit_name)) | ||
2657 | 749 | return False | ||
2658 | 750 | # Port not checked (useful when checking that ssl is disabled) | ||
2659 | 751 | elif not port and conf_ssl: | ||
2660 | 752 | self.log.debug('SSL is enabled @{}:{} ' | ||
2661 | 753 | '({})'.format(host, port, unit_name)) | ||
2662 | 754 | return True | ||
2663 | 755 | elif not conf_ssl: | ||
2664 | 756 | self.log.debug('SSL not enabled @{}:{} ' | ||
2665 | 757 | '({})'.format(host, port, unit_name)) | ||
2666 | 758 | return False | ||
2667 | 759 | else: | ||
2668 | 760 | msg = ('Unknown condition when checking SSL status @{}:{} ' | ||
2669 | 761 | '({})'.format(host, port, unit_name)) | ||
2670 | 762 | amulet.raise_status(amulet.FAIL, msg) | ||
2671 | 763 | |||
2672 | 764 | def validate_rmq_ssl_enabled_units(self, sentry_units, port=None): | ||
2673 | 765 | """Check that ssl is enabled on rmq juju sentry units. | ||
2674 | 766 | |||
2675 | 767 | :param sentry_units: list of all rmq sentry units | ||
2676 | 768 | :param port: optional ssl port override to validate | ||
2677 | 769 | :returns: None if successful, otherwise return error message | ||
2678 | 770 | """ | ||
2679 | 771 | for sentry_unit in sentry_units: | ||
2680 | 772 | if not self.rmq_ssl_is_enabled_on_unit(sentry_unit, port=port): | ||
2681 | 773 | return ('Unexpected condition: ssl is disabled on unit ' | ||
2682 | 774 | '({})'.format(sentry_unit.info['unit_name'])) | ||
2683 | 775 | return None | ||
2684 | 776 | |||
2685 | 777 | def validate_rmq_ssl_disabled_units(self, sentry_units): | ||
2686 | 778 | """Check that ssl is enabled on listed rmq juju sentry units. | ||
2687 | 779 | |||
2688 | 780 | :param sentry_units: list of all rmq sentry units | ||
2689 | 781 | :returns: True if successful. Raise on error. | ||
2690 | 782 | """ | ||
2691 | 783 | for sentry_unit in sentry_units: | ||
2692 | 784 | if self.rmq_ssl_is_enabled_on_unit(sentry_unit): | ||
2693 | 785 | return ('Unexpected condition: ssl is enabled on unit ' | ||
2694 | 786 | '({})'.format(sentry_unit.info['unit_name'])) | ||
2695 | 787 | return None | ||
2696 | 788 | |||
2697 | 789 | def configure_rmq_ssl_on(self, sentry_units, deployment, | ||
2698 | 790 | port=None, max_wait=60): | ||
2699 | 791 | """Turn ssl charm config option on, with optional non-default | ||
2700 | 792 | ssl port specification. Confirm that it is enabled on every | ||
2701 | 793 | unit. | ||
2702 | 794 | |||
2703 | 795 | :param sentry_units: list of sentry units | ||
2704 | 796 | :param deployment: amulet deployment object pointer | ||
2705 | 797 | :param port: amqp port, use defaults if None | ||
2706 | 798 | :param max_wait: maximum time to wait in seconds to confirm | ||
2707 | 799 | :returns: None if successful. Raise on error. | ||
2708 | 800 | """ | ||
2709 | 801 | self.log.debug('Setting ssl charm config option: on') | ||
2710 | 802 | |||
2711 | 803 | # Enable RMQ SSL | ||
2712 | 804 | config = {'ssl': 'on'} | ||
2713 | 805 | if port: | ||
2714 | 806 | config['ssl_port'] = port | ||
2715 | 807 | |||
2716 | 808 | deployment.configure('rabbitmq-server', config) | ||
2717 | 809 | |||
2718 | 810 | # Confirm | ||
2719 | 811 | tries = 0 | ||
2720 | 812 | ret = self.validate_rmq_ssl_enabled_units(sentry_units, port=port) | ||
2721 | 813 | while ret and tries < (max_wait / 4): | ||
2722 | 814 | time.sleep(4) | ||
2723 | 815 | self.log.debug('Attempt {}: {}'.format(tries, ret)) | ||
2724 | 816 | ret = self.validate_rmq_ssl_enabled_units(sentry_units, port=port) | ||
2725 | 817 | tries += 1 | ||
2726 | 818 | |||
2727 | 819 | if ret: | ||
2728 | 820 | amulet.raise_status(amulet.FAIL, ret) | ||
2729 | 821 | |||
2730 | 822 | def configure_rmq_ssl_off(self, sentry_units, deployment, max_wait=60): | ||
2731 | 823 | """Turn ssl charm config option off, confirm that it is disabled | ||
2732 | 824 | on every unit. | ||
2733 | 825 | |||
2734 | 826 | :param sentry_units: list of sentry units | ||
2735 | 827 | :param deployment: amulet deployment object pointer | ||
2736 | 828 | :param max_wait: maximum time to wait in seconds to confirm | ||
2737 | 829 | :returns: None if successful. Raise on error. | ||
2738 | 830 | """ | ||
2739 | 831 | self.log.debug('Setting ssl charm config option: off') | ||
2740 | 832 | |||
2741 | 833 | # Disable RMQ SSL | ||
2742 | 834 | config = {'ssl': 'off'} | ||
2743 | 835 | deployment.configure('rabbitmq-server', config) | ||
2744 | 836 | |||
2745 | 837 | # Confirm | ||
2746 | 838 | tries = 0 | ||
2747 | 839 | ret = self.validate_rmq_ssl_disabled_units(sentry_units) | ||
2748 | 840 | while ret and tries < (max_wait / 4): | ||
2749 | 841 | time.sleep(4) | ||
2750 | 842 | self.log.debug('Attempt {}: {}'.format(tries, ret)) | ||
2751 | 843 | ret = self.validate_rmq_ssl_disabled_units(sentry_units) | ||
2752 | 844 | tries += 1 | ||
2753 | 845 | |||
2754 | 846 | if ret: | ||
2755 | 847 | amulet.raise_status(amulet.FAIL, ret) | ||
2756 | 848 | |||
2757 | 849 | def connect_amqp_by_unit(self, sentry_unit, ssl=False, | ||
2758 | 850 | port=None, fatal=True, | ||
2759 | 851 | username="testuser1", password="changeme"): | ||
2760 | 852 | """Establish and return a pika amqp connection to the rabbitmq service | ||
2761 | 853 | running on a rmq juju unit. | ||
2762 | 854 | |||
2763 | 855 | :param sentry_unit: sentry unit pointer | ||
2764 | 856 | :param ssl: boolean, default to False | ||
2765 | 857 | :param port: amqp port, use defaults if None | ||
2766 | 858 | :param fatal: boolean, default to True (raises on connect error) | ||
2767 | 859 | :param username: amqp user name, default to testuser1 | ||
2768 | 860 | :param password: amqp user password | ||
2769 | 861 | :returns: pika amqp connection pointer or None if failed and non-fatal | ||
2770 | 862 | """ | ||
2771 | 863 | host = sentry_unit.info['public-address'] | ||
2772 | 864 | unit_name = sentry_unit.info['unit_name'] | ||
2773 | 865 | |||
2774 | 866 | # Default port logic if port is not specified | ||
2775 | 867 | if ssl and not port: | ||
2776 | 868 | port = 5671 | ||
2777 | 869 | elif not ssl and not port: | ||
2778 | 870 | port = 5672 | ||
2779 | 871 | |||
2780 | 872 | self.log.debug('Connecting to amqp on {}:{} ({}) as ' | ||
2781 | 873 | '{}...'.format(host, port, unit_name, username)) | ||
2782 | 874 | |||
2783 | 875 | try: | ||
2784 | 876 | credentials = pika.PlainCredentials(username, password) | ||
2785 | 877 | parameters = pika.ConnectionParameters(host=host, port=port, | ||
2786 | 878 | credentials=credentials, | ||
2787 | 879 | ssl=ssl, | ||
2788 | 880 | connection_attempts=3, | ||
2789 | 881 | retry_delay=5, | ||
2790 | 882 | socket_timeout=1) | ||
2791 | 883 | connection = pika.BlockingConnection(parameters) | ||
2792 | 884 | assert connection.server_properties['product'] == 'RabbitMQ' | ||
2793 | 885 | self.log.debug('Connect OK') | ||
2794 | 886 | return connection | ||
2795 | 887 | except Exception as e: | ||
2796 | 888 | msg = ('amqp connection failed to {}:{} as ' | ||
2797 | 889 | '{} ({})'.format(host, port, username, str(e))) | ||
2798 | 890 | if fatal: | ||
2799 | 891 | amulet.raise_status(amulet.FAIL, msg) | ||
2800 | 892 | else: | ||
2801 | 893 | self.log.warn(msg) | ||
2802 | 894 | return None | ||
2803 | 895 | |||
2804 | 896 | def publish_amqp_message_by_unit(self, sentry_unit, message, | ||
2805 | 897 | queue="test", ssl=False, | ||
2806 | 898 | username="testuser1", | ||
2807 | 899 | password="changeme", | ||
2808 | 900 | port=None): | ||
2809 | 901 | """Publish an amqp message to a rmq juju unit. | ||
2810 | 902 | |||
2811 | 903 | :param sentry_unit: sentry unit pointer | ||
2812 | 904 | :param message: amqp message string | ||
2813 | 905 | :param queue: message queue, default to test | ||
2814 | 906 | :param username: amqp user name, default to testuser1 | ||
2815 | 907 | :param password: amqp user password | ||
2816 | 908 | :param ssl: boolean, default to False | ||
2817 | 909 | :param port: amqp port, use defaults if None | ||
2818 | 910 | :returns: None. Raises exception if publish failed. | ||
2819 | 911 | """ | ||
2820 | 912 | self.log.debug('Publishing message to {} queue:\n{}'.format(queue, | ||
2821 | 913 | message)) | ||
2822 | 914 | connection = self.connect_amqp_by_unit(sentry_unit, ssl=ssl, | ||
2823 | 915 | port=port, | ||
2824 | 916 | username=username, | ||
2825 | 917 | password=password) | ||
2826 | 918 | |||
2827 | 919 | # NOTE(beisner): extra debug here re: pika hang potential: | ||
2828 | 920 | # https://github.com/pika/pika/issues/297 | ||
2829 | 921 | # https://groups.google.com/forum/#!topic/rabbitmq-users/Ja0iyfF0Szw | ||
2830 | 922 | self.log.debug('Defining channel...') | ||
2831 | 923 | channel = connection.channel() | ||
2832 | 924 | self.log.debug('Declaring queue...') | ||
2833 | 925 | channel.queue_declare(queue=queue, auto_delete=False, durable=True) | ||
2834 | 926 | self.log.debug('Publishing message...') | ||
2835 | 927 | channel.basic_publish(exchange='', routing_key=queue, body=message) | ||
2836 | 928 | self.log.debug('Closing channel...') | ||
2837 | 929 | channel.close() | ||
2838 | 930 | self.log.debug('Closing connection...') | ||
2839 | 931 | connection.close() | ||
2840 | 932 | |||
2841 | 933 | def get_amqp_message_by_unit(self, sentry_unit, queue="test", | ||
2842 | 934 | username="testuser1", | ||
2843 | 935 | password="changeme", | ||
2844 | 936 | ssl=False, port=None): | ||
2845 | 937 | """Get an amqp message from a rmq juju unit. | ||
2846 | 938 | |||
2847 | 939 | :param sentry_unit: sentry unit pointer | ||
2848 | 940 | :param queue: message queue, default to test | ||
2849 | 941 | :param username: amqp user name, default to testuser1 | ||
2850 | 942 | :param password: amqp user password | ||
2851 | 943 | :param ssl: boolean, default to False | ||
2852 | 944 | :param port: amqp port, use defaults if None | ||
2853 | 945 | :returns: amqp message body as string. Raise if get fails. | ||
2854 | 946 | """ | ||
2855 | 947 | connection = self.connect_amqp_by_unit(sentry_unit, ssl=ssl, | ||
2856 | 948 | port=port, | ||
2857 | 949 | username=username, | ||
2858 | 950 | password=password) | ||
2859 | 951 | channel = connection.channel() | ||
2860 | 952 | method_frame, _, body = channel.basic_get(queue) | ||
2861 | 953 | |||
2862 | 954 | if method_frame: | ||
2863 | 955 | self.log.debug('Retreived message from {} queue:\n{}'.format(queue, | ||
2864 | 956 | body)) | ||
2865 | 957 | channel.basic_ack(method_frame.delivery_tag) | ||
2866 | 958 | channel.close() | ||
2867 | 959 | connection.close() | ||
2868 | 960 | return body | ||
2869 | 961 | else: | ||
2870 | 962 | msg = 'No message retrieved.' | ||
2871 | 963 | amulet.raise_status(amulet.FAIL, msg) | ||
2872 | 0 | 964 | ||
2873 | === added file 'charmhelpers/contrib/openstack/context.py' | |||
2874 | --- charmhelpers/contrib/openstack/context.py 1970-01-01 00:00:00 +0000 | |||
2875 | +++ charmhelpers/contrib/openstack/context.py 2015-10-08 20:44:37 +0000 | |||
2876 | @@ -0,0 +1,1427 @@ | |||
2877 | 1 | # Copyright 2014-2015 Canonical Limited. | ||
2878 | 2 | # | ||
2879 | 3 | # This file is part of charm-helpers. | ||
2880 | 4 | # | ||
2881 | 5 | # charm-helpers is free software: you can redistribute it and/or modify | ||
2882 | 6 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
2883 | 7 | # published by the Free Software Foundation. | ||
2884 | 8 | # | ||
2885 | 9 | # charm-helpers is distributed in the hope that it will be useful, | ||
2886 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2887 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2888 | 12 | # GNU Lesser General Public License for more details. | ||
2889 | 13 | # | ||
2890 | 14 | # You should have received a copy of the GNU Lesser General Public License | ||
2891 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
2892 | 16 | |||
2893 | 17 | import glob | ||
2894 | 18 | import json | ||
2895 | 19 | import os | ||
2896 | 20 | import re | ||
2897 | 21 | import time | ||
2898 | 22 | from base64 import b64decode | ||
2899 | 23 | from subprocess import check_call | ||
2900 | 24 | |||
2901 | 25 | import six | ||
2902 | 26 | import yaml | ||
2903 | 27 | |||
2904 | 28 | from charmhelpers.fetch import ( | ||
2905 | 29 | apt_install, | ||
2906 | 30 | filter_installed_packages, | ||
2907 | 31 | ) | ||
2908 | 32 | from charmhelpers.core.hookenv import ( | ||
2909 | 33 | config, | ||
2910 | 34 | is_relation_made, | ||
2911 | 35 | local_unit, | ||
2912 | 36 | log, | ||
2913 | 37 | relation_get, | ||
2914 | 38 | relation_ids, | ||
2915 | 39 | related_units, | ||
2916 | 40 | relation_set, | ||
2917 | 41 | unit_get, | ||
2918 | 42 | unit_private_ip, | ||
2919 | 43 | charm_name, | ||
2920 | 44 | DEBUG, | ||
2921 | 45 | INFO, | ||
2922 | 46 | WARNING, | ||
2923 | 47 | ERROR, | ||
2924 | 48 | ) | ||
2925 | 49 | |||
2926 | 50 | from charmhelpers.core.sysctl import create as sysctl_create | ||
2927 | 51 | from charmhelpers.core.strutils import bool_from_string | ||
2928 | 52 | |||
2929 | 53 | from charmhelpers.core.host import ( | ||
2930 | 54 | get_bond_master, | ||
2931 | 55 | is_phy_iface, | ||
2932 | 56 | list_nics, | ||
2933 | 57 | get_nic_hwaddr, | ||
2934 | 58 | mkdir, | ||
2935 | 59 | write_file, | ||
2936 | 60 | ) | ||
2937 | 61 | from charmhelpers.contrib.hahelpers.cluster import ( | ||
2938 | 62 | determine_apache_port, | ||
2939 | 63 | determine_api_port, | ||
2940 | 64 | https, | ||
2941 | 65 | is_clustered, | ||
2942 | 66 | ) | ||
2943 | 67 | from charmhelpers.contrib.hahelpers.apache import ( | ||
2944 | 68 | get_cert, | ||
2945 | 69 | get_ca_cert, | ||
2946 | 70 | install_ca_cert, | ||
2947 | 71 | ) | ||
2948 | 72 | from charmhelpers.contrib.openstack.neutron import ( | ||
2949 | 73 | neutron_plugin_attribute, | ||
2950 | 74 | parse_data_port_mappings, | ||
2951 | 75 | ) | ||
2952 | 76 | from charmhelpers.contrib.openstack.ip import ( | ||
2953 | 77 | resolve_address, | ||
2954 | 78 | INTERNAL, | ||
2955 | 79 | ) | ||
2956 | 80 | from charmhelpers.contrib.network.ip import ( | ||
2957 | 81 | get_address_in_network, | ||
2958 | 82 | get_ipv4_addr, | ||
2959 | 83 | get_ipv6_addr, | ||
2960 | 84 | get_netmask_for_address, | ||
2961 | 85 | format_ipv6_addr, | ||
2962 | 86 | is_address_in_network, | ||
2963 | 87 | is_bridge_member, | ||
2964 | 88 | ) | ||
2965 | 89 | from charmhelpers.contrib.openstack.utils import get_host_ip | ||
2966 | 90 | CA_CERT_PATH = '/usr/local/share/ca-certificates/keystone_juju_ca_cert.crt' | ||
2967 | 91 | ADDRESS_TYPES = ['admin', 'internal', 'public'] | ||
2968 | 92 | |||
2969 | 93 | |||
2970 | 94 | class OSContextError(Exception): | ||
2971 | 95 | pass | ||
2972 | 96 | |||
2973 | 97 | |||
2974 | 98 | def ensure_packages(packages): | ||
2975 | 99 | """Install but do not upgrade required plugin packages.""" | ||
2976 | 100 | required = filter_installed_packages(packages) | ||
2977 | 101 | if required: | ||
2978 | 102 | apt_install(required, fatal=True) | ||
2979 | 103 | |||
2980 | 104 | |||
2981 | 105 | def context_complete(ctxt): | ||
2982 | 106 | _missing = [] | ||
2983 | 107 | for k, v in six.iteritems(ctxt): | ||
2984 | 108 | if v is None or v == '': | ||
2985 | 109 | _missing.append(k) | ||
2986 | 110 | |||
2987 | 111 | if _missing: | ||
2988 | 112 | log('Missing required data: %s' % ' '.join(_missing), level=INFO) | ||
2989 | 113 | return False | ||
2990 | 114 | |||
2991 | 115 | return True | ||
2992 | 116 | |||
2993 | 117 | |||
2994 | 118 | def config_flags_parser(config_flags): | ||
2995 | 119 | """Parses config flags string into dict. | ||
2996 | 120 | |||
2997 | 121 | This parsing method supports a few different formats for the config | ||
2998 | 122 | flag values to be parsed: | ||
2999 | 123 | |||
3000 | 124 | 1. A string in the simple format of key=value pairs, with the possibility | ||
3001 | 125 | of specifying multiple key value pairs within the same string. For | ||
3002 | 126 | example, a string in the format of 'key1=value1, key2=value2' will | ||
3003 | 127 | return a dict of: | ||
3004 | 128 | |||
3005 | 129 | {'key1': 'value1', | ||
3006 | 130 | 'key2': 'value2'}. | ||
3007 | 131 | |||
3008 | 132 | 2. A string in the above format, but supporting a comma-delimited list | ||
3009 | 133 | of values for the same key. For example, a string in the format of | ||
3010 | 134 | 'key1=value1, key2=value3,value4,value5' will return a dict of: | ||
3011 | 135 | |||
3012 | 136 | {'key1', 'value1', | ||
3013 | 137 | 'key2', 'value2,value3,value4'} | ||
3014 | 138 | |||
3015 | 139 | 3. A string containing a colon character (:) prior to an equal | ||
3016 | 140 | character (=) will be treated as yaml and parsed as such. This can be | ||
3017 | 141 | used to specify more complex key value pairs. For example, | ||
3018 | 142 | a string in the format of 'key1: subkey1=value1, subkey2=value2' will | ||
3019 | 143 | return a dict of: | ||
3020 | 144 | |||
3021 | 145 | {'key1', 'subkey1=value1, subkey2=value2'} | ||
3022 | 146 | |||
3023 | 147 | The provided config_flags string may be a list of comma-separated values | ||
3024 | 148 | which themselves may be comma-separated list of values. | ||
3025 | 149 | """ | ||
3026 | 150 | # If we find a colon before an equals sign then treat it as yaml. | ||
3027 | 151 | # Note: limit it to finding the colon first since this indicates assignment | ||
3028 | 152 | # for inline yaml. | ||
3029 | 153 | colon = config_flags.find(':') | ||
3030 | 154 | equals = config_flags.find('=') | ||
3031 | 155 | if colon > 0: | ||
3032 | 156 | if colon < equals or equals < 0: | ||
3033 | 157 | return yaml.safe_load(config_flags) | ||
3034 | 158 | |||
3035 | 159 | if config_flags.find('==') >= 0: | ||
3036 | 160 | log("config_flags is not in expected format (key=value)", level=ERROR) | ||
3037 | 161 | raise OSContextError | ||
3038 | 162 | |||
3039 | 163 | # strip the following from each value. | ||
3040 | 164 | post_strippers = ' ,' | ||
3041 | 165 | # we strip any leading/trailing '=' or ' ' from the string then | ||
3042 | 166 | # split on '='. | ||
3043 | 167 | split = config_flags.strip(' =').split('=') | ||
3044 | 168 | limit = len(split) | ||
3045 | 169 | flags = {} | ||
3046 | 170 | for i in range(0, limit - 1): | ||
3047 | 171 | current = split[i] | ||
3048 | 172 | next = split[i + 1] | ||
3049 | 173 | vindex = next.rfind(',') | ||
3050 | 174 | if (i == limit - 2) or (vindex < 0): | ||
3051 | 175 | value = next | ||
3052 | 176 | else: | ||
3053 | 177 | value = next[:vindex] | ||
3054 | 178 | |||
3055 | 179 | if i == 0: | ||
3056 | 180 | key = current | ||
3057 | 181 | else: | ||
3058 | 182 | # if this not the first entry, expect an embedded key. | ||
3059 | 183 | index = current.rfind(',') | ||
3060 | 184 | if index < 0: | ||
3061 | 185 | log("Invalid config value(s) at index %s" % (i), level=ERROR) | ||
3062 | 186 | raise OSContextError | ||
3063 | 187 | key = current[index + 1:] | ||
3064 | 188 | |||
3065 | 189 | # Add to collection. | ||
3066 | 190 | flags[key.strip(post_strippers)] = value.rstrip(post_strippers) | ||
3067 | 191 | |||
3068 | 192 | return flags | ||
3069 | 193 | |||
3070 | 194 | |||
3071 | 195 | class OSContextGenerator(object): | ||
3072 | 196 | """Base class for all context generators.""" | ||
3073 | 197 | interfaces = [] | ||
3074 | 198 | related = False | ||
3075 | 199 | complete = False | ||
3076 | 200 | missing_data = [] | ||
3077 | 201 | |||
3078 | 202 | def __call__(self): | ||
3079 | 203 | raise NotImplementedError | ||
3080 | 204 | |||
3081 | 205 | def context_complete(self, ctxt): | ||
3082 | 206 | """Check for missing data for the required context data. | ||
3083 | 207 | Set self.missing_data if it exists and return False. | ||
3084 | 208 | Set self.complete if no missing data and return True. | ||
3085 | 209 | """ | ||
3086 | 210 | # Fresh start | ||
3087 | 211 | self.complete = False | ||
3088 | 212 | self.missing_data = [] | ||
3089 | 213 | for k, v in six.iteritems(ctxt): | ||
3090 | 214 | if v is None or v == '': | ||
3091 | 215 | if k not in self.missing_data: | ||
3092 | 216 | self.missing_data.append(k) | ||
3093 | 217 | |||
3094 | 218 | if self.missing_data: | ||
3095 | 219 | self.complete = False | ||
3096 | 220 | log('Missing required data: %s' % ' '.join(self.missing_data), level=INFO) | ||
3097 | 221 | else: | ||
3098 | 222 | self.complete = True | ||
3099 | 223 | return self.complete | ||
3100 | 224 | |||
3101 | 225 | def get_related(self): | ||
3102 | 226 | """Check if any of the context interfaces have relation ids. | ||
3103 | 227 | Set self.related and return True if one of the interfaces | ||
3104 | 228 | has relation ids. | ||
3105 | 229 | """ | ||
3106 | 230 | # Fresh start | ||
3107 | 231 | self.related = False | ||
3108 | 232 | try: | ||
3109 | 233 | for interface in self.interfaces: | ||
3110 | 234 | if relation_ids(interface): | ||
3111 | 235 | self.related = True | ||
3112 | 236 | return self.related | ||
3113 | 237 | except AttributeError as e: | ||
3114 | 238 | log("{} {}" | ||
3115 | 239 | "".format(self, e), 'INFO') | ||
3116 | 240 | return self.related | ||
3117 | 241 | |||
3118 | 242 | |||
3119 | 243 | class SharedDBContext(OSContextGenerator): | ||
3120 | 244 | interfaces = ['shared-db'] | ||
3121 | 245 | |||
3122 | 246 | def __init__(self, | ||
3123 | 247 | database=None, user=None, relation_prefix=None, ssl_dir=None): | ||
3124 | 248 | """Allows inspecting relation for settings prefixed with | ||
3125 | 249 | relation_prefix. This is useful for parsing access for multiple | ||
3126 | 250 | databases returned via the shared-db interface (eg, nova_password, | ||
3127 | 251 | quantum_password) | ||
3128 | 252 | """ | ||
3129 | 253 | self.relation_prefix = relation_prefix | ||
3130 | 254 | self.database = database | ||
3131 | 255 | self.user = user | ||
3132 | 256 | self.ssl_dir = ssl_dir | ||
3133 | 257 | self.rel_name = self.interfaces[0] | ||
3134 | 258 | |||
3135 | 259 | def __call__(self): | ||
3136 | 260 | self.database = self.database or config('database') | ||
3137 | 261 | self.user = self.user or config('database-user') | ||
3138 | 262 | if None in [self.database, self.user]: | ||
3139 | 263 | log("Could not generate shared_db context. Missing required charm " | ||
3140 | 264 | "config options. (database name and user)", level=ERROR) | ||
3141 | 265 | raise OSContextError | ||
3142 | 266 | |||
3143 | 267 | ctxt = {} | ||
3144 | 268 | |||
3145 | 269 | # NOTE(jamespage) if mysql charm provides a network upon which | ||
3146 | 270 | # access to the database should be made, reconfigure relation | ||
3147 | 271 | # with the service units local address and defer execution | ||
3148 | 272 | access_network = relation_get('access-network') | ||
3149 | 273 | if access_network is not None: | ||
3150 | 274 | if self.relation_prefix is not None: | ||
3151 | 275 | hostname_key = "{}_hostname".format(self.relation_prefix) | ||
3152 | 276 | else: | ||
3153 | 277 | hostname_key = "hostname" | ||
3154 | 278 | access_hostname = get_address_in_network(access_network, | ||
3155 | 279 | unit_get('private-address')) | ||
3156 | 280 | set_hostname = relation_get(attribute=hostname_key, | ||
3157 | 281 | unit=local_unit()) | ||
3158 | 282 | if set_hostname != access_hostname: | ||
3159 | 283 | relation_set(relation_settings={hostname_key: access_hostname}) | ||
3160 | 284 | return None # Defer any further hook execution for now.... | ||
3161 | 285 | |||
3162 | 286 | password_setting = 'password' | ||
3163 | 287 | if self.relation_prefix: | ||
3164 | 288 | password_setting = self.relation_prefix + '_password' | ||
3165 | 289 | |||
3166 | 290 | for rid in relation_ids(self.interfaces[0]): | ||
3167 | 291 | self.related = True | ||
3168 | 292 | for unit in related_units(rid): | ||
3169 | 293 | rdata = relation_get(rid=rid, unit=unit) | ||
3170 | 294 | host = rdata.get('db_host') | ||
3171 | 295 | host = format_ipv6_addr(host) or host | ||
3172 | 296 | ctxt = { | ||
3173 | 297 | 'database_host': host, | ||
3174 | 298 | 'database': self.database, | ||
3175 | 299 | 'database_user': self.user, | ||
3176 | 300 | 'database_password': rdata.get(password_setting), | ||
3177 | 301 | 'database_type': 'mysql' | ||
3178 | 302 | } | ||
3179 | 303 | if self.context_complete(ctxt): | ||
3180 | 304 | db_ssl(rdata, ctxt, self.ssl_dir) | ||
3181 | 305 | return ctxt | ||
3182 | 306 | return {} | ||
3183 | 307 | |||
3184 | 308 | |||
3185 | 309 | class PostgresqlDBContext(OSContextGenerator): | ||
3186 | 310 | interfaces = ['pgsql-db'] | ||
3187 | 311 | |||
3188 | 312 | def __init__(self, database=None): | ||
3189 | 313 | self.database = database | ||
3190 | 314 | |||
3191 | 315 | def __call__(self): | ||
3192 | 316 | self.database = self.database or config('database') | ||
3193 | 317 | if self.database is None: | ||
3194 | 318 | log('Could not generate postgresql_db context. Missing required ' | ||
3195 | 319 | 'charm config options. (database name)', level=ERROR) | ||
3196 | 320 | raise OSContextError | ||
3197 | 321 | |||
3198 | 322 | ctxt = {} | ||
3199 | 323 | for rid in relation_ids(self.interfaces[0]): | ||
3200 | 324 | self.related = True | ||
3201 | 325 | for unit in related_units(rid): | ||
3202 | 326 | rel_host = relation_get('host', rid=rid, unit=unit) | ||
3203 | 327 | rel_user = relation_get('user', rid=rid, unit=unit) | ||
3204 | 328 | rel_passwd = relation_get('password', rid=rid, unit=unit) | ||
3205 | 329 | ctxt = {'database_host': rel_host, | ||
3206 | 330 | 'database': self.database, | ||
3207 | 331 | 'database_user': rel_user, | ||
3208 | 332 | 'database_password': rel_passwd, | ||
3209 | 333 | 'database_type': 'postgresql'} | ||
3210 | 334 | if self.context_complete(ctxt): | ||
3211 | 335 | return ctxt | ||
3212 | 336 | |||
3213 | 337 | return {} | ||
3214 | 338 | |||
3215 | 339 | |||
3216 | 340 | def db_ssl(rdata, ctxt, ssl_dir): | ||
3217 | 341 | if 'ssl_ca' in rdata and ssl_dir: | ||
3218 | 342 | ca_path = os.path.join(ssl_dir, 'db-client.ca') | ||
3219 | 343 | with open(ca_path, 'w') as fh: | ||
3220 | 344 | fh.write(b64decode(rdata['ssl_ca'])) | ||
3221 | 345 | |||
3222 | 346 | ctxt['database_ssl_ca'] = ca_path | ||
3223 | 347 | elif 'ssl_ca' in rdata: | ||
3224 | 348 | log("Charm not setup for ssl support but ssl ca found", level=INFO) | ||
3225 | 349 | return ctxt | ||
3226 | 350 | |||
3227 | 351 | if 'ssl_cert' in rdata: | ||
3228 | 352 | cert_path = os.path.join( | ||
3229 | 353 | ssl_dir, 'db-client.cert') | ||
3230 | 354 | if not os.path.exists(cert_path): | ||
3231 | 355 | log("Waiting 1m for ssl client cert validity", level=INFO) | ||
3232 | 356 | time.sleep(60) | ||
3233 | 357 | |||
3234 | 358 | with open(cert_path, 'w') as fh: | ||
3235 | 359 | fh.write(b64decode(rdata['ssl_cert'])) | ||
3236 | 360 | |||
3237 | 361 | ctxt['database_ssl_cert'] = cert_path | ||
3238 | 362 | key_path = os.path.join(ssl_dir, 'db-client.key') | ||
3239 | 363 | with open(key_path, 'w') as fh: | ||
3240 | 364 | fh.write(b64decode(rdata['ssl_key'])) | ||
3241 | 365 | |||
3242 | 366 | ctxt['database_ssl_key'] = key_path | ||
3243 | 367 | |||
3244 | 368 | return ctxt | ||
3245 | 369 | |||
3246 | 370 | |||
3247 | 371 | class IdentityServiceContext(OSContextGenerator): | ||
3248 | 372 | |||
3249 | 373 | def __init__(self, service=None, service_user=None, rel_name='identity-service'): | ||
3250 | 374 | self.service = service | ||
3251 | 375 | self.service_user = service_user | ||
3252 | 376 | self.rel_name = rel_name | ||
3253 | 377 | self.interfaces = [self.rel_name] | ||
3254 | 378 | |||
3255 | 379 | def __call__(self): | ||
3256 | 380 | log('Generating template context for ' + self.rel_name, level=DEBUG) | ||
3257 | 381 | ctxt = {} | ||
3258 | 382 | |||
3259 | 383 | if self.service and self.service_user: | ||
3260 | 384 | # This is required for pki token signing if we don't want /tmp to | ||
3261 | 385 | # be used. | ||
3262 | 386 | cachedir = '/var/cache/%s' % (self.service) | ||
3263 | 387 | if not os.path.isdir(cachedir): | ||
3264 | 388 | log("Creating service cache dir %s" % (cachedir), level=DEBUG) | ||
3265 | 389 | mkdir(path=cachedir, owner=self.service_user, | ||
3266 | 390 | group=self.service_user, perms=0o700) | ||
3267 | 391 | |||
3268 | 392 | ctxt['signing_dir'] = cachedir | ||
3269 | 393 | |||
3270 | 394 | for rid in relation_ids(self.rel_name): | ||
3271 | 395 | self.related = True | ||
3272 | 396 | for unit in related_units(rid): | ||
3273 | 397 | rdata = relation_get(rid=rid, unit=unit) | ||
3274 | 398 | serv_host = rdata.get('service_host') | ||
3275 | 399 | serv_host = format_ipv6_addr(serv_host) or serv_host | ||
3276 | 400 | auth_host = rdata.get('auth_host') | ||
3277 | 401 | auth_host = format_ipv6_addr(auth_host) or auth_host | ||
3278 | 402 | svc_protocol = rdata.get('service_protocol') or 'http' | ||
3279 | 403 | auth_protocol = rdata.get('auth_protocol') or 'http' | ||
3280 | 404 | ctxt.update({'service_port': rdata.get('service_port'), | ||
3281 | 405 | 'service_host': serv_host, | ||
3282 | 406 | 'auth_host': auth_host, | ||
3283 | 407 | 'auth_port': rdata.get('auth_port'), | ||
3284 | 408 | 'admin_tenant_name': rdata.get('service_tenant'), | ||
3285 | 409 | 'admin_user': rdata.get('service_username'), | ||
3286 | 410 | 'admin_password': rdata.get('service_password'), | ||
3287 | 411 | 'service_protocol': svc_protocol, | ||
3288 | 412 | 'auth_protocol': auth_protocol}) | ||
3289 | 413 | |||
3290 | 414 | if self.context_complete(ctxt): | ||
3291 | 415 | # NOTE(jamespage) this is required for >= icehouse | ||
3292 | 416 | # so a missing value just indicates keystone needs | ||
3293 | 417 | # upgrading | ||
3294 | 418 | ctxt['admin_tenant_id'] = rdata.get('service_tenant_id') | ||
3295 | 419 | return ctxt | ||
3296 | 420 | |||
3297 | 421 | return {} | ||
3298 | 422 | |||
3299 | 423 | |||
3300 | 424 | class AMQPContext(OSContextGenerator): | ||
3301 | 425 | |||
3302 | 426 | def __init__(self, ssl_dir=None, rel_name='amqp', relation_prefix=None): | ||
3303 | 427 | self.ssl_dir = ssl_dir | ||
3304 | 428 | self.rel_name = rel_name | ||
3305 | 429 | self.relation_prefix = relation_prefix | ||
3306 | 430 | self.interfaces = [rel_name] | ||
3307 | 431 | |||
3308 | 432 | def __call__(self): | ||
3309 | 433 | log('Generating template context for amqp', level=DEBUG) | ||
3310 | 434 | conf = config() | ||
3311 | 435 | if self.relation_prefix: | ||
3312 | 436 | user_setting = '%s-rabbit-user' % (self.relation_prefix) | ||
3313 | 437 | vhost_setting = '%s-rabbit-vhost' % (self.relation_prefix) | ||
3314 | 438 | else: | ||
3315 | 439 | user_setting = 'rabbit-user' | ||
3316 | 440 | vhost_setting = 'rabbit-vhost' | ||
3317 | 441 | |||
3318 | 442 | try: | ||
3319 | 443 | username = conf[user_setting] | ||
3320 | 444 | vhost = conf[vhost_setting] | ||
3321 | 445 | except KeyError as e: | ||
3322 | 446 | log('Could not generate shared_db context. Missing required charm ' | ||
3323 | 447 | 'config options: %s.' % e, level=ERROR) | ||
3324 | 448 | raise OSContextError | ||
3325 | 449 | |||
3326 | 450 | ctxt = {} | ||
3327 | 451 | for rid in relation_ids(self.rel_name): | ||
3328 | 452 | ha_vip_only = False | ||
3329 | 453 | self.related = True | ||
3330 | 454 | for unit in related_units(rid): | ||
3331 | 455 | if relation_get('clustered', rid=rid, unit=unit): | ||
3332 | 456 | ctxt['clustered'] = True | ||
3333 | 457 | vip = relation_get('vip', rid=rid, unit=unit) | ||
3334 | 458 | vip = format_ipv6_addr(vip) or vip | ||
3335 | 459 | ctxt['rabbitmq_host'] = vip | ||
3336 | 460 | else: | ||
3337 | 461 | host = relation_get('private-address', rid=rid, unit=unit) | ||
3338 | 462 | host = format_ipv6_addr(host) or host | ||
3339 | 463 | ctxt['rabbitmq_host'] = host | ||
3340 | 464 | |||
3341 | 465 | ctxt.update({ | ||
3342 | 466 | 'rabbitmq_user': username, | ||
3343 | 467 | 'rabbitmq_password': relation_get('password', rid=rid, | ||
3344 | 468 | unit=unit), | ||
3345 | 469 | 'rabbitmq_virtual_host': vhost, | ||
3346 | 470 | }) | ||
3347 | 471 | |||
3348 | 472 | ssl_port = relation_get('ssl_port', rid=rid, unit=unit) | ||
3349 | 473 | if ssl_port: | ||
3350 | 474 | ctxt['rabbit_ssl_port'] = ssl_port | ||
3351 | 475 | |||
3352 | 476 | ssl_ca = relation_get('ssl_ca', rid=rid, unit=unit) | ||
3353 | 477 | if ssl_ca: | ||
3354 | 478 | ctxt['rabbit_ssl_ca'] = ssl_ca | ||
3355 | 479 | |||
3356 | 480 | if relation_get('ha_queues', rid=rid, unit=unit) is not None: | ||
3357 | 481 | ctxt['rabbitmq_ha_queues'] = True | ||
3358 | 482 | |||
3359 | 483 | ha_vip_only = relation_get('ha-vip-only', | ||
3360 | 484 | rid=rid, unit=unit) is not None | ||
3361 | 485 | |||
3362 | 486 | if self.context_complete(ctxt): | ||
3363 | 487 | if 'rabbit_ssl_ca' in ctxt: | ||
3364 | 488 | if not self.ssl_dir: | ||
3365 | 489 | log("Charm not setup for ssl support but ssl ca " | ||
3366 | 490 | "found", level=INFO) | ||
3367 | 491 | break | ||
3368 | 492 | |||
3369 | 493 | ca_path = os.path.join( | ||
3370 | 494 | self.ssl_dir, 'rabbit-client-ca.pem') | ||
3371 | 495 | with open(ca_path, 'w') as fh: | ||
3372 | 496 | fh.write(b64decode(ctxt['rabbit_ssl_ca'])) | ||
3373 | 497 | ctxt['rabbit_ssl_ca'] = ca_path | ||
3374 | 498 | |||
3375 | 499 | # Sufficient information found = break out! | ||
3376 | 500 | break | ||
3377 | 501 | |||
3378 | 502 | # Used for active/active rabbitmq >= grizzly | ||
3379 | 503 | if (('clustered' not in ctxt or ha_vip_only) and | ||
3380 | 504 | len(related_units(rid)) > 1): | ||
3381 | 505 | rabbitmq_hosts = [] | ||
3382 | 506 | for unit in related_units(rid): | ||
3383 | 507 | host = relation_get('private-address', rid=rid, unit=unit) | ||
3384 | 508 | host = format_ipv6_addr(host) or host | ||
3385 | 509 | rabbitmq_hosts.append(host) | ||
3386 | 510 | |||
3387 | 511 | ctxt['rabbitmq_hosts'] = ','.join(sorted(rabbitmq_hosts)) | ||
3388 | 512 | |||
3389 | 513 | oslo_messaging_flags = conf.get('oslo-messaging-flags', None) | ||
3390 | 514 | if oslo_messaging_flags: | ||
3391 | 515 | ctxt['oslo_messaging_flags'] = config_flags_parser( | ||
3392 | 516 | oslo_messaging_flags) | ||
3393 | 517 | |||
3394 | 518 | if not self.complete: | ||
3395 | 519 | return {} | ||
3396 | 520 | |||
3397 | 521 | return ctxt | ||
3398 | 522 | |||
3399 | 523 | |||
3400 | 524 | class CephContext(OSContextGenerator): | ||
3401 | 525 | """Generates context for /etc/ceph/ceph.conf templates.""" | ||
3402 | 526 | interfaces = ['ceph'] | ||
3403 | 527 | |||
3404 | 528 | def __call__(self): | ||
3405 | 529 | if not relation_ids('ceph'): | ||
3406 | 530 | return {} | ||
3407 | 531 | |||
3408 | 532 | log('Generating template context for ceph', level=DEBUG) | ||
3409 | 533 | mon_hosts = [] | ||
3410 | 534 | ctxt = { | ||
3411 | 535 | 'use_syslog': str(config('use-syslog')).lower() | ||
3412 | 536 | } | ||
3413 | 537 | for rid in relation_ids('ceph'): | ||
3414 | 538 | for unit in related_units(rid): | ||
3415 | 539 | if not ctxt.get('auth'): | ||
3416 | 540 | ctxt['auth'] = relation_get('auth', rid=rid, unit=unit) | ||
3417 | 541 | if not ctxt.get('key'): | ||
3418 | 542 | ctxt['key'] = relation_get('key', rid=rid, unit=unit) | ||
3419 | 543 | ceph_pub_addr = relation_get('ceph-public-address', rid=rid, | ||
3420 | 544 | unit=unit) | ||
3421 | 545 | unit_priv_addr = relation_get('private-address', rid=rid, | ||
3422 | 546 | unit=unit) | ||
3423 | 547 | ceph_addr = ceph_pub_addr or unit_priv_addr | ||
3424 | 548 | ceph_addr = format_ipv6_addr(ceph_addr) or ceph_addr | ||
3425 | 549 | mon_hosts.append(ceph_addr) | ||
3426 | 550 | |||
3427 | 551 | ctxt['mon_hosts'] = ' '.join(sorted(mon_hosts)) | ||
3428 | 552 | |||
3429 | 553 | if not os.path.isdir('/etc/ceph'): | ||
3430 | 554 | os.mkdir('/etc/ceph') | ||
3431 | 555 | |||
3432 | 556 | if not self.context_complete(ctxt): | ||
3433 | 557 | return {} | ||
3434 | 558 | |||
3435 | 559 | ensure_packages(['ceph-common']) | ||
3436 | 560 | return ctxt | ||
3437 | 561 | |||
3438 | 562 | |||
3439 | 563 | class HAProxyContext(OSContextGenerator): | ||
3440 | 564 | """Provides half a context for the haproxy template, which describes | ||
3441 | 565 | all peers to be included in the cluster. Each charm needs to include | ||
3442 | 566 | its own context generator that describes the port mapping. | ||
3443 | 567 | """ | ||
3444 | 568 | interfaces = ['cluster'] | ||
3445 | 569 | |||
3446 | 570 | def __init__(self, singlenode_mode=False): | ||
3447 | 571 | self.singlenode_mode = singlenode_mode | ||
3448 | 572 | |||
3449 | 573 | def __call__(self): | ||
3450 | 574 | if not relation_ids('cluster') and not self.singlenode_mode: | ||
3451 | 575 | return {} | ||
3452 | 576 | |||
3453 | 577 | if config('prefer-ipv6'): | ||
3454 | 578 | addr = get_ipv6_addr(exc_list=[config('vip')])[0] | ||
3455 | 579 | else: | ||
3456 | 580 | addr = get_host_ip(unit_get('private-address')) | ||
3457 | 581 | |||
3458 | 582 | l_unit = local_unit().replace('/', '-') | ||
3459 | 583 | cluster_hosts = {} | ||
3460 | 584 | |||
3461 | 585 | # NOTE(jamespage): build out map of configured network endpoints | ||
3462 | 586 | # and associated backends | ||
3463 | 587 | for addr_type in ADDRESS_TYPES: | ||
3464 | 588 | cfg_opt = 'os-{}-network'.format(addr_type) | ||
3465 | 589 | laddr = get_address_in_network(config(cfg_opt)) | ||
3466 | 590 | if laddr: | ||
3467 | 591 | netmask = get_netmask_for_address(laddr) | ||
3468 | 592 | cluster_hosts[laddr] = {'network': "{}/{}".format(laddr, | ||
3469 | 593 | netmask), | ||
3470 | 594 | 'backends': {l_unit: laddr}} | ||
3471 | 595 | for rid in relation_ids('cluster'): | ||
3472 | 596 | for unit in related_units(rid): | ||
3473 | 597 | _laddr = relation_get('{}-address'.format(addr_type), | ||
3474 | 598 | rid=rid, unit=unit) | ||
3475 | 599 | if _laddr: | ||
3476 | 600 | _unit = unit.replace('/', '-') | ||
3477 | 601 | cluster_hosts[laddr]['backends'][_unit] = _laddr | ||
3478 | 602 | |||
3479 | 603 | # NOTE(jamespage) add backend based on private address - this | ||
3480 | 604 | # with either be the only backend or the fallback if no acls | ||
3481 | 605 | # match in the frontend | ||
3482 | 606 | cluster_hosts[addr] = {} | ||
3483 | 607 | netmask = get_netmask_for_address(addr) | ||
3484 | 608 | cluster_hosts[addr] = {'network': "{}/{}".format(addr, netmask), | ||
3485 | 609 | 'backends': {l_unit: addr}} | ||
3486 | 610 | for rid in relation_ids('cluster'): | ||
3487 | 611 | for unit in related_units(rid): | ||
3488 | 612 | _laddr = relation_get('private-address', | ||
3489 | 613 | rid=rid, unit=unit) | ||
3490 | 614 | if _laddr: | ||
3491 | 615 | _unit = unit.replace('/', '-') | ||
3492 | 616 | cluster_hosts[addr]['backends'][_unit] = _laddr | ||
3493 | 617 | |||
3494 | 618 | ctxt = { | ||
3495 | 619 | 'frontends': cluster_hosts, | ||
3496 | 620 | 'default_backend': addr | ||
3497 | 621 | } | ||
3498 | 622 | |||
3499 | 623 | if config('haproxy-server-timeout'): | ||
3500 | 624 | ctxt['haproxy_server_timeout'] = config('haproxy-server-timeout') | ||
3501 | 625 | |||
3502 | 626 | if config('haproxy-client-timeout'): | ||
3503 | 627 | ctxt['haproxy_client_timeout'] = config('haproxy-client-timeout') | ||
3504 | 628 | |||
3505 | 629 | if config('prefer-ipv6'): | ||
3506 | 630 | ctxt['ipv6'] = True | ||
3507 | 631 | ctxt['local_host'] = 'ip6-localhost' | ||
3508 | 632 | ctxt['haproxy_host'] = '::' | ||
3509 | 633 | ctxt['stat_port'] = ':::8888' | ||
3510 | 634 | else: | ||
3511 | 635 | ctxt['local_host'] = '127.0.0.1' | ||
3512 | 636 | ctxt['haproxy_host'] = '0.0.0.0' | ||
3513 | 637 | ctxt['stat_port'] = ':8888' | ||
3514 | 638 | |||
3515 | 639 | for frontend in cluster_hosts: | ||
3516 | 640 | if (len(cluster_hosts[frontend]['backends']) > 1 or | ||
3517 | 641 | self.singlenode_mode): | ||
3518 | 642 | # Enable haproxy when we have enough peers. | ||
3519 | 643 | log('Ensuring haproxy enabled in /etc/default/haproxy.', | ||
3520 | 644 | level=DEBUG) | ||
3521 | 645 | with open('/etc/default/haproxy', 'w') as out: | ||
3522 | 646 | out.write('ENABLED=1\n') | ||
3523 | 647 | |||
3524 | 648 | return ctxt | ||
3525 | 649 | |||
3526 | 650 | log('HAProxy context is incomplete, this unit has no peers.', | ||
3527 | 651 | level=INFO) | ||
3528 | 652 | return {} | ||
3529 | 653 | |||
3530 | 654 | |||
3531 | 655 | class ImageServiceContext(OSContextGenerator): | ||
3532 | 656 | interfaces = ['image-service'] | ||
3533 | 657 | |||
3534 | 658 | def __call__(self): | ||
3535 | 659 | """Obtains the glance API server from the image-service relation. | ||
3536 | 660 | Useful in nova and cinder (currently). | ||
3537 | 661 | """ | ||
3538 | 662 | log('Generating template context for image-service.', level=DEBUG) | ||
3539 | 663 | rids = relation_ids('image-service') | ||
3540 | 664 | if not rids: | ||
3541 | 665 | return {} | ||
3542 | 666 | |||
3543 | 667 | for rid in rids: | ||
3544 | 668 | for unit in related_units(rid): | ||
3545 | 669 | api_server = relation_get('glance-api-server', | ||
3546 | 670 | rid=rid, unit=unit) | ||
3547 | 671 | if api_server: | ||
3548 | 672 | return {'glance_api_servers': api_server} | ||
3549 | 673 | |||
3550 | 674 | log("ImageService context is incomplete. Missing required relation " | ||
3551 | 675 | "data.", level=INFO) | ||
3552 | 676 | return {} | ||
3553 | 677 | |||
3554 | 678 | |||
3555 | 679 | class ApacheSSLContext(OSContextGenerator): | ||
3556 | 680 | """Generates a context for an apache vhost configuration that configures | ||
3557 | 681 | HTTPS reverse proxying for one or many endpoints. Generated context | ||
3558 | 682 | looks something like:: | ||
3559 | 683 | |||
3560 | 684 | { | ||
3561 | 685 | 'namespace': 'cinder', | ||
3562 | 686 | 'private_address': 'iscsi.mycinderhost.com', | ||
3563 | 687 | 'endpoints': [(8776, 8766), (8777, 8767)] | ||
3564 | 688 | } | ||
3565 | 689 | |||
3566 | 690 | The endpoints list consists of a tuples mapping external ports | ||
3567 | 691 | to internal ports. | ||
3568 | 692 | """ | ||
3569 | 693 | interfaces = ['https'] | ||
3570 | 694 | |||
3571 | 695 | # charms should inherit this context and set external ports | ||
3572 | 696 | # and service namespace accordingly. | ||
3573 | 697 | external_ports = [] | ||
3574 | 698 | service_namespace = None | ||
3575 | 699 | |||
3576 | 700 | def enable_modules(self): | ||
3577 | 701 | cmd = ['a2enmod', 'ssl', 'proxy', 'proxy_http'] | ||
3578 | 702 | check_call(cmd) | ||
3579 | 703 | |||
3580 | 704 | def configure_cert(self, cn=None): | ||
3581 | 705 | ssl_dir = os.path.join('/etc/apache2/ssl/', self.service_namespace) | ||
3582 | 706 | mkdir(path=ssl_dir) | ||
3583 | 707 | cert, key = get_cert(cn) | ||
3584 | 708 | if cn: | ||
3585 | 709 | cert_filename = 'cert_{}'.format(cn) | ||
3586 | 710 | key_filename = 'key_{}'.format(cn) | ||
3587 | 711 | else: | ||
3588 | 712 | cert_filename = 'cert' | ||
3589 | 713 | key_filename = 'key' | ||
3590 | 714 | |||
3591 | 715 | write_file(path=os.path.join(ssl_dir, cert_filename), | ||
3592 | 716 | content=b64decode(cert)) | ||
3593 | 717 | write_file(path=os.path.join(ssl_dir, key_filename), | ||
3594 | 718 | content=b64decode(key)) | ||
3595 | 719 | |||
3596 | 720 | def configure_ca(self): | ||
3597 | 721 | ca_cert = get_ca_cert() | ||
3598 | 722 | if ca_cert: | ||
3599 | 723 | install_ca_cert(b64decode(ca_cert)) | ||
3600 | 724 | |||
3601 | 725 | def canonical_names(self): | ||
3602 | 726 | """Figure out which canonical names clients will access this service. | ||
3603 | 727 | """ | ||
3604 | 728 | cns = [] | ||
3605 | 729 | for r_id in relation_ids('identity-service'): | ||
3606 | 730 | for unit in related_units(r_id): | ||
3607 | 731 | rdata = relation_get(rid=r_id, unit=unit) | ||
3608 | 732 | for k in rdata: | ||
3609 | 733 | if k.startswith('ssl_key_'): | ||
3610 | 734 | cns.append(k.lstrip('ssl_key_')) | ||
3611 | 735 | |||
3612 | 736 | return sorted(list(set(cns))) | ||
3613 | 737 | |||
3614 | 738 | def get_network_addresses(self): | ||
3615 | 739 | """For each network configured, return corresponding address and vip | ||
3616 | 740 | (if available). | ||
3617 | 741 | |||
3618 | 742 | Returns a list of tuples of the form: | ||
3619 | 743 | |||
3620 | 744 | [(address_in_net_a, vip_in_net_a), | ||
3621 | 745 | (address_in_net_b, vip_in_net_b), | ||
3622 | 746 | ...] | ||
3623 | 747 | |||
3624 | 748 | or, if no vip(s) available: | ||
3625 | 749 | |||
3626 | 750 | [(address_in_net_a, address_in_net_a), | ||
3627 | 751 | (address_in_net_b, address_in_net_b), | ||
3628 | 752 | ...] | ||
3629 | 753 | """ | ||
3630 | 754 | addresses = [] | ||
3631 | 755 | if config('vip'): | ||
3632 | 756 | vips = config('vip').split() | ||
3633 | 757 | else: | ||
3634 | 758 | vips = [] | ||
3635 | 759 | |||
3636 | 760 | for net_type in ['os-internal-network', 'os-admin-network', | ||
3637 | 761 | 'os-public-network']: | ||
3638 | 762 | addr = get_address_in_network(config(net_type), | ||
3639 | 763 | unit_get('private-address')) | ||
3640 | 764 | if len(vips) > 1 and is_clustered(): | ||
3641 | 765 | if not config(net_type): | ||
3642 | 766 | log("Multiple networks configured but net_type " | ||
3643 | 767 | "is None (%s)." % net_type, level=WARNING) | ||
3644 | 768 | continue | ||
3645 | 769 | |||
3646 | 770 | for vip in vips: | ||
3647 | 771 | if is_address_in_network(config(net_type), vip): | ||
3648 | 772 | addresses.append((addr, vip)) | ||
3649 | 773 | break | ||
3650 | 774 | |||
3651 | 775 | elif is_clustered() and config('vip'): | ||
3652 | 776 | addresses.append((addr, config('vip'))) | ||
3653 | 777 | else: | ||
3654 | 778 | addresses.append((addr, addr)) | ||
3655 | 779 | |||
3656 | 780 | return sorted(addresses) | ||
3657 | 781 | |||
3658 | 782 | def __call__(self): | ||
3659 | 783 | if isinstance(self.external_ports, six.string_types): | ||
3660 | 784 | self.external_ports = [self.external_ports] | ||
3661 | 785 | |||
3662 | 786 | if not self.external_ports or not https(): | ||
3663 | 787 | return {} | ||
3664 | 788 | |||
3665 | 789 | self.configure_ca() | ||
3666 | 790 | self.enable_modules() | ||
3667 | 791 | |||
3668 | 792 | ctxt = {'namespace': self.service_namespace, | ||
3669 | 793 | 'endpoints': [], | ||
3670 | 794 | 'ext_ports': []} | ||
3671 | 795 | |||
3672 | 796 | cns = self.canonical_names() | ||
3673 | 797 | if cns: | ||
3674 | 798 | for cn in cns: | ||
3675 | 799 | self.configure_cert(cn) | ||
3676 | 800 | else: | ||
3677 | 801 | # Expect cert/key provided in config (currently assumed that ca | ||
3678 | 802 | # uses ip for cn) | ||
3679 | 803 | cn = resolve_address(endpoint_type=INTERNAL) | ||
3680 | 804 | self.configure_cert(cn) | ||
3681 | 805 | |||
3682 | 806 | addresses = self.get_network_addresses() | ||
3683 | 807 | for address, endpoint in sorted(set(addresses)): | ||
3684 | 808 | for api_port in self.external_ports: | ||
3685 | 809 | ext_port = determine_apache_port(api_port, | ||
3686 | 810 | singlenode_mode=True) | ||
3687 | 811 | int_port = determine_api_port(api_port, singlenode_mode=True) | ||
3688 | 812 | portmap = (address, endpoint, int(ext_port), int(int_port)) | ||
3689 | 813 | ctxt['endpoints'].append(portmap) | ||
3690 | 814 | ctxt['ext_ports'].append(int(ext_port)) | ||
3691 | 815 | |||
3692 | 816 | ctxt['ext_ports'] = sorted(list(set(ctxt['ext_ports']))) | ||
3693 | 817 | return ctxt | ||
3694 | 818 | |||
3695 | 819 | |||
3696 | 820 | class NeutronContext(OSContextGenerator): | ||
3697 | 821 | interfaces = [] | ||
3698 | 822 | |||
3699 | 823 | @property | ||
3700 | 824 | def plugin(self): | ||
3701 | 825 | return None | ||
3702 | 826 | |||
3703 | 827 | @property | ||
3704 | 828 | def network_manager(self): | ||
3705 | 829 | return None | ||
3706 | 830 | |||
3707 | 831 | @property | ||
3708 | 832 | def packages(self): | ||
3709 | 833 | return neutron_plugin_attribute(self.plugin, 'packages', | ||
3710 | 834 | self.network_manager) | ||
3711 | 835 | |||
3712 | 836 | @property | ||
3713 | 837 | def neutron_security_groups(self): | ||
3714 | 838 | return None | ||
3715 | 839 | |||
3716 | 840 | def _ensure_packages(self): | ||
3717 | 841 | for pkgs in self.packages: | ||
3718 | 842 | ensure_packages(pkgs) | ||
3719 | 843 | |||
3720 | 844 | def _save_flag_file(self): | ||
3721 | 845 | if self.network_manager == 'quantum': | ||
3722 | 846 | _file = '/etc/nova/quantum_plugin.conf' | ||
3723 | 847 | else: | ||
3724 | 848 | _file = '/etc/nova/neutron_plugin.conf' | ||
3725 | 849 | |||
3726 | 850 | with open(_file, 'wb') as out: | ||
3727 | 851 | out.write(self.plugin + '\n') | ||
3728 | 852 | |||
3729 | 853 | def ovs_ctxt(self): | ||
3730 | 854 | driver = neutron_plugin_attribute(self.plugin, 'driver', | ||
3731 | 855 | self.network_manager) | ||
3732 | 856 | config = neutron_plugin_attribute(self.plugin, 'config', | ||
3733 | 857 | self.network_manager) | ||
3734 | 858 | ovs_ctxt = {'core_plugin': driver, | ||
3735 | 859 | 'neutron_plugin': 'ovs', | ||
3736 | 860 | 'neutron_security_groups': self.neutron_security_groups, | ||
3737 | 861 | 'local_ip': unit_private_ip(), | ||
3738 | 862 | 'config': config} | ||
3739 | 863 | |||
3740 | 864 | return ovs_ctxt | ||
3741 | 865 | |||
3742 | 866 | def nuage_ctxt(self): | ||
3743 | 867 | driver = neutron_plugin_attribute(self.plugin, 'driver', | ||
3744 | 868 | self.network_manager) | ||
3745 | 869 | config = neutron_plugin_attribute(self.plugin, 'config', | ||
3746 | 870 | self.network_manager) | ||
3747 | 871 | nuage_ctxt = {'core_plugin': driver, | ||
3748 | 872 | 'neutron_plugin': 'vsp', | ||
3749 | 873 | 'neutron_security_groups': self.neutron_security_groups, | ||
3750 | 874 | 'local_ip': unit_private_ip(), | ||
3751 | 875 | 'config': config} | ||
3752 | 876 | |||
3753 | 877 | return nuage_ctxt | ||
3754 | 878 | |||
3755 | 879 | def nvp_ctxt(self): | ||
3756 | 880 | driver = neutron_plugin_attribute(self.plugin, 'driver', | ||
3757 | 881 | self.network_manager) | ||
3758 | 882 | config = neutron_plugin_attribute(self.plugin, 'config', | ||
3759 | 883 | self.network_manager) | ||
3760 | 884 | nvp_ctxt = {'core_plugin': driver, | ||
3761 | 885 | 'neutron_plugin': 'nvp', | ||
3762 | 886 | 'neutron_security_groups': self.neutron_security_groups, | ||
3763 | 887 | 'local_ip': unit_private_ip(), | ||
3764 | 888 | 'config': config} | ||
3765 | 889 | |||
3766 | 890 | return nvp_ctxt | ||
3767 | 891 | |||
3768 | 892 | def n1kv_ctxt(self): | ||
3769 | 893 | driver = neutron_plugin_attribute(self.plugin, 'driver', | ||
3770 | 894 | self.network_manager) | ||
3771 | 895 | n1kv_config = neutron_plugin_attribute(self.plugin, 'config', | ||
3772 | 896 | self.network_manager) | ||
3773 | 897 | n1kv_user_config_flags = config('n1kv-config-flags') | ||
3774 | 898 | restrict_policy_profiles = config('n1kv-restrict-policy-profiles') | ||
3775 | 899 | n1kv_ctxt = {'core_plugin': driver, | ||
3776 | 900 | 'neutron_plugin': 'n1kv', | ||
3777 | 901 | 'neutron_security_groups': self.neutron_security_groups, | ||
3778 | 902 | 'local_ip': unit_private_ip(), | ||
3779 | 903 | 'config': n1kv_config, | ||
3780 | 904 | 'vsm_ip': config('n1kv-vsm-ip'), | ||
3781 | 905 | 'vsm_username': config('n1kv-vsm-username'), | ||
3782 | 906 | 'vsm_password': config('n1kv-vsm-password'), | ||
3783 | 907 | 'restrict_policy_profiles': restrict_policy_profiles} | ||
3784 | 908 | |||
3785 | 909 | if n1kv_user_config_flags: | ||
3786 | 910 | flags = config_flags_parser(n1kv_user_config_flags) | ||
3787 | 911 | n1kv_ctxt['user_config_flags'] = flags | ||
3788 | 912 | |||
3789 | 913 | return n1kv_ctxt | ||
3790 | 914 | |||
3791 | 915 | def calico_ctxt(self): | ||
3792 | 916 | driver = neutron_plugin_attribute(self.plugin, 'driver', | ||
3793 | 917 | self.network_manager) | ||
3794 | 918 | config = neutron_plugin_attribute(self.plugin, 'config', | ||
3795 | 919 | self.network_manager) | ||
3796 | 920 | calico_ctxt = {'core_plugin': driver, | ||
3797 | 921 | 'neutron_plugin': 'Calico', | ||
3798 | 922 | 'neutron_security_groups': self.neutron_security_groups, | ||
3799 | 923 | 'local_ip': unit_private_ip(), | ||
3800 | 924 | 'config': config} | ||
3801 | 925 | |||
3802 | 926 | return calico_ctxt | ||
3803 | 927 | |||
3804 | 928 | def neutron_ctxt(self): | ||
3805 | 929 | if https(): | ||
3806 | 930 | proto = 'https' | ||
3807 | 931 | else: | ||
3808 | 932 | proto = 'http' | ||
3809 | 933 | |||
3810 | 934 | if is_clustered(): | ||
3811 | 935 | host = config('vip') | ||
3812 | 936 | else: | ||
3813 | 937 | host = unit_get('private-address') | ||
3814 | 938 | |||
3815 | 939 | ctxt = {'network_manager': self.network_manager, | ||
3816 | 940 | 'neutron_url': '%s://%s:%s' % (proto, host, '9696')} | ||
3817 | 941 | return ctxt | ||
3818 | 942 | |||
3819 | 943 | def pg_ctxt(self): | ||
3820 | 944 | driver = neutron_plugin_attribute(self.plugin, 'driver', | ||
3821 | 945 | self.network_manager) | ||
3822 | 946 | config = neutron_plugin_attribute(self.plugin, 'config', | ||
3823 | 947 | self.network_manager) | ||
3824 | 948 | ovs_ctxt = {'core_plugin': driver, | ||
3825 | 949 | 'neutron_plugin': 'plumgrid', | ||
3826 | 950 | 'neutron_security_groups': self.neutron_security_groups, | ||
3827 | 951 | 'local_ip': unit_private_ip(), | ||
3828 | 952 | 'config': config} | ||
3829 | 953 | return ovs_ctxt | ||
3830 | 954 | |||
3831 | 955 | def __call__(self): | ||
3832 | 956 | if self.network_manager not in ['quantum', 'neutron']: | ||
3833 | 957 | return {} | ||
3834 | 958 | |||
3835 | 959 | if not self.plugin: | ||
3836 | 960 | return {} | ||
3837 | 961 | |||
3838 | 962 | ctxt = self.neutron_ctxt() | ||
3839 | 963 | |||
3840 | 964 | if self.plugin == 'ovs': | ||
3841 | 965 | ctxt.update(self.ovs_ctxt()) | ||
3842 | 966 | elif self.plugin in ['nvp', 'nsx']: | ||
3843 | 967 | ctxt.update(self.nvp_ctxt()) | ||
3844 | 968 | elif self.plugin == 'n1kv': | ||
3845 | 969 | ctxt.update(self.n1kv_ctxt()) | ||
3846 | 970 | elif self.plugin == 'Calico': | ||
3847 | 971 | ctxt.update(self.calico_ctxt()) | ||
3848 | 972 | elif self.plugin == 'vsp': | ||
3849 | 973 | ctxt.update(self.nuage_ctxt()) | ||
3850 | 974 | elif self.plugin == 'plumgrid': | ||
3851 | 975 | ctxt.update(self.pg_ctxt()) | ||
3852 | 976 | |||
3853 | 977 | alchemy_flags = config('neutron-alchemy-flags') | ||
3854 | 978 | if alchemy_flags: | ||
3855 | 979 | flags = config_flags_parser(alchemy_flags) | ||
3856 | 980 | ctxt['neutron_alchemy_flags'] = flags | ||
3857 | 981 | |||
3858 | 982 | self._save_flag_file() | ||
3859 | 983 | return ctxt | ||
3860 | 984 | |||
3861 | 985 | |||
3862 | 986 | class NeutronPortContext(OSContextGenerator): | ||
3863 | 987 | |||
3864 | 988 | def resolve_ports(self, ports): | ||
3865 | 989 | """Resolve NICs not yet bound to bridge(s) | ||
3866 | 990 | |||
3867 | 991 | If hwaddress provided then returns resolved hwaddress otherwise NIC. | ||
3868 | 992 | """ | ||
3869 | 993 | if not ports: | ||
3870 | 994 | return None | ||
3871 | 995 | |||
3872 | 996 | hwaddr_to_nic = {} | ||
3873 | 997 | hwaddr_to_ip = {} | ||
3874 | 998 | for nic in list_nics(): | ||
3875 | 999 | # Ignore virtual interfaces (bond masters will be identified from | ||
3876 | 1000 | # their slaves) | ||
3877 | 1001 | if not is_phy_iface(nic): | ||
3878 | 1002 | continue | ||
3879 | 1003 | |||
3880 | 1004 | _nic = get_bond_master(nic) | ||
3881 | 1005 | if _nic: | ||
3882 | 1006 | log("Replacing iface '%s' with bond master '%s'" % (nic, _nic), | ||
3883 | 1007 | level=DEBUG) | ||
3884 | 1008 | nic = _nic | ||
3885 | 1009 | |||
3886 | 1010 | hwaddr = get_nic_hwaddr(nic) | ||
3887 | 1011 | hwaddr_to_nic[hwaddr] = nic | ||
3888 | 1012 | addresses = get_ipv4_addr(nic, fatal=False) | ||
3889 | 1013 | addresses += get_ipv6_addr(iface=nic, fatal=False) | ||
3890 | 1014 | hwaddr_to_ip[hwaddr] = addresses | ||
3891 | 1015 | |||
3892 | 1016 | resolved = [] | ||
3893 | 1017 | mac_regex = re.compile(r'([0-9A-F]{2}[:-]){5}([0-9A-F]{2})', re.I) | ||
3894 | 1018 | for entry in ports: | ||
3895 | 1019 | if re.match(mac_regex, entry): | ||
3896 | 1020 | # NIC is in known NICs and does NOT hace an IP address | ||
3897 | 1021 | if entry in hwaddr_to_nic and not hwaddr_to_ip[entry]: | ||
3898 | 1022 | # If the nic is part of a bridge then don't use it | ||
3899 | 1023 | if is_bridge_member(hwaddr_to_nic[entry]): | ||
3900 | 1024 | continue | ||
3901 | 1025 | |||
3902 | 1026 | # Entry is a MAC address for a valid interface that doesn't | ||
3903 | 1027 | # have an IP address assigned yet. | ||
3904 | 1028 | resolved.append(hwaddr_to_nic[entry]) | ||
3905 | 1029 | else: | ||
3906 | 1030 | # If the passed entry is not a MAC address, assume it's a valid | ||
3907 | 1031 | # interface, and that the user put it there on purpose (we can | ||
3908 | 1032 | # trust it to be the real external network). | ||
3909 | 1033 | resolved.append(entry) | ||
3910 | 1034 | |||
3911 | 1035 | # Ensure no duplicates | ||
3912 | 1036 | return list(set(resolved)) | ||
3913 | 1037 | |||
3914 | 1038 | |||
3915 | 1039 | class OSConfigFlagContext(OSContextGenerator): | ||
3916 | 1040 | """Provides support for user-defined config flags. | ||
3917 | 1041 | |||
3918 | 1042 | Users can define a comma-seperated list of key=value pairs | ||
3919 | 1043 | in the charm configuration and apply them at any point in | ||
3920 | 1044 | any file by using a template flag. | ||
3921 | 1045 | |||
3922 | 1046 | Sometimes users might want config flags inserted within a | ||
3923 | 1047 | specific section so this class allows users to specify the | ||
3924 | 1048 | template flag name, allowing for multiple template flags | ||
3925 | 1049 | (sections) within the same context. | ||
3926 | 1050 | |||
3927 | 1051 | NOTE: the value of config-flags may be a comma-separated list of | ||
3928 | 1052 | key=value pairs and some Openstack config files support | ||
3929 | 1053 | comma-separated lists as values. | ||
3930 | 1054 | """ | ||
3931 | 1055 | |||
3932 | 1056 | def __init__(self, charm_flag='config-flags', | ||
3933 | 1057 | template_flag='user_config_flags'): | ||
3934 | 1058 | """ | ||
3935 | 1059 | :param charm_flag: config flags in charm configuration. | ||
3936 | 1060 | :param template_flag: insert point for user-defined flags in template | ||
3937 | 1061 | file. | ||
3938 | 1062 | """ | ||
3939 | 1063 | super(OSConfigFlagContext, self).__init__() | ||
3940 | 1064 | self._charm_flag = charm_flag | ||
3941 | 1065 | self._template_flag = template_flag | ||
3942 | 1066 | |||
3943 | 1067 | def __call__(self): | ||
3944 | 1068 | config_flags = config(self._charm_flag) | ||
3945 | 1069 | if not config_flags: | ||
3946 | 1070 | return {} | ||
3947 | 1071 | |||
3948 | 1072 | return {self._template_flag: | ||
3949 | 1073 | config_flags_parser(config_flags)} | ||
3950 | 1074 | |||
3951 | 1075 | |||
3952 | 1076 | class SubordinateConfigContext(OSContextGenerator): | ||
3953 | 1077 | |||
3954 | 1078 | """ | ||
3955 | 1079 | Responsible for inspecting relations to subordinates that | ||
3956 | 1080 | may be exporting required config via a json blob. | ||
3957 | 1081 | |||
3958 | 1082 | The subordinate interface allows subordinates to export their | ||
3959 | 1083 | configuration requirements to the principle for multiple config | ||
3960 | 1084 | files and multiple serivces. Ie, a subordinate that has interfaces | ||
3961 | 1085 | to both glance and nova may export to following yaml blob as json:: | ||
3962 | 1086 | |||
3963 | 1087 | glance: | ||
3964 | 1088 | /etc/glance/glance-api.conf: | ||
3965 | 1089 | sections: | ||
3966 | 1090 | DEFAULT: | ||
3967 | 1091 | - [key1, value1] | ||
3968 | 1092 | /etc/glance/glance-registry.conf: | ||
3969 | 1093 | MYSECTION: | ||
3970 | 1094 | - [key2, value2] | ||
3971 | 1095 | nova: | ||
3972 | 1096 | /etc/nova/nova.conf: | ||
3973 | 1097 | sections: | ||
3974 | 1098 | DEFAULT: | ||
3975 | 1099 | - [key3, value3] | ||
3976 | 1100 | |||
3977 | 1101 | |||
3978 | 1102 | It is then up to the principle charms to subscribe this context to | ||
3979 | 1103 | the service+config file it is interestd in. Configuration data will | ||
3980 | 1104 | be available in the template context, in glance's case, as:: | ||
3981 | 1105 | |||
3982 | 1106 | ctxt = { | ||
3983 | 1107 | ... other context ... | ||
3984 | 1108 | 'subordinate_config': { | ||
3985 | 1109 | 'DEFAULT': { | ||
3986 | 1110 | 'key1': 'value1', | ||
3987 | 1111 | }, | ||
3988 | 1112 | 'MYSECTION': { | ||
3989 | 1113 | 'key2': 'value2', | ||
3990 | 1114 | }, | ||
3991 | 1115 | } | ||
3992 | 1116 | } | ||
3993 | 1117 | """ | ||
3994 | 1118 | |||
3995 | 1119 | def __init__(self, service, config_file, interface): | ||
3996 | 1120 | """ | ||
3997 | 1121 | :param service : Service name key to query in any subordinate | ||
3998 | 1122 | data found | ||
3999 | 1123 | :param config_file : Service's config file to query sections | ||
4000 | 1124 | :param interface : Subordinate interface to inspect | ||
4001 | 1125 | """ | ||
4002 | 1126 | self.config_file = config_file | ||
4003 | 1127 | if isinstance(service, list): | ||
4004 | 1128 | self.services = service | ||
4005 | 1129 | else: | ||
4006 | 1130 | self.services = [service] | ||
4007 | 1131 | if isinstance(interface, list): | ||
4008 | 1132 | self.interfaces = interface | ||
4009 | 1133 | else: | ||
4010 | 1134 | self.interfaces = [interface] | ||
4011 | 1135 | |||
4012 | 1136 | def __call__(self): | ||
4013 | 1137 | ctxt = {'sections': {}} | ||
4014 | 1138 | rids = [] | ||
4015 | 1139 | for interface in self.interfaces: | ||
4016 | 1140 | rids.extend(relation_ids(interface)) | ||
4017 | 1141 | for rid in rids: | ||
4018 | 1142 | for unit in related_units(rid): | ||
4019 | 1143 | sub_config = relation_get('subordinate_configuration', | ||
4020 | 1144 | rid=rid, unit=unit) | ||
4021 | 1145 | if sub_config and sub_config != '': | ||
4022 | 1146 | try: | ||
4023 | 1147 | sub_config = json.loads(sub_config) | ||
4024 | 1148 | except: | ||
4025 | 1149 | log('Could not parse JSON from subordinate_config ' | ||
4026 | 1150 | 'setting from %s' % rid, level=ERROR) | ||
4027 | 1151 | continue | ||
4028 | 1152 | |||
4029 | 1153 | for service in self.services: | ||
4030 | 1154 | if service not in sub_config: | ||
4031 | 1155 | log('Found subordinate_config on %s but it contained' | ||
4032 | 1156 | 'nothing for %s service' % (rid, service), | ||
4033 | 1157 | level=INFO) | ||
4034 | 1158 | continue | ||
4035 | 1159 | |||
4036 | 1160 | sub_config = sub_config[service] | ||
4037 | 1161 | if self.config_file not in sub_config: | ||
4038 | 1162 | log('Found subordinate_config on %s but it contained' | ||
4039 | 1163 | 'nothing for %s' % (rid, self.config_file), | ||
4040 | 1164 | level=INFO) | ||
4041 | 1165 | continue | ||
4042 | 1166 | |||
4043 | 1167 | sub_config = sub_config[self.config_file] | ||
4044 | 1168 | for k, v in six.iteritems(sub_config): | ||
4045 | 1169 | if k == 'sections': | ||
4046 | 1170 | for section, config_list in six.iteritems(v): | ||
4047 | 1171 | log("adding section '%s'" % (section), | ||
4048 | 1172 | level=DEBUG) | ||
4049 | 1173 | if ctxt[k].get(section): | ||
4050 | 1174 | ctxt[k][section].extend(config_list) | ||
4051 | 1175 | else: | ||
4052 | 1176 | ctxt[k][section] = config_list | ||
4053 | 1177 | else: | ||
4054 | 1178 | ctxt[k] = v | ||
4055 | 1179 | log("%d section(s) found" % (len(ctxt['sections'])), level=DEBUG) | ||
4056 | 1180 | return ctxt | ||
4057 | 1181 | |||
4058 | 1182 | |||
4059 | 1183 | class LogLevelContext(OSContextGenerator): | ||
4060 | 1184 | |||
4061 | 1185 | def __call__(self): | ||
4062 | 1186 | ctxt = {} | ||
4063 | 1187 | ctxt['debug'] = \ | ||
4064 | 1188 | False if config('debug') is None else config('debug') | ||
4065 | 1189 | ctxt['verbose'] = \ | ||
4066 | 1190 | False if config('verbose') is None else config('verbose') | ||
4067 | 1191 | |||
4068 | 1192 | return ctxt | ||
4069 | 1193 | |||
4070 | 1194 | |||
4071 | 1195 | class SyslogContext(OSContextGenerator): | ||
4072 | 1196 | |||
4073 | 1197 | def __call__(self): | ||
4074 | 1198 | ctxt = {'use_syslog': config('use-syslog')} | ||
4075 | 1199 | return ctxt | ||
4076 | 1200 | |||
4077 | 1201 | |||
4078 | 1202 | class BindHostContext(OSContextGenerator): | ||
4079 | 1203 | |||
4080 | 1204 | def __call__(self): | ||
4081 | 1205 | if config('prefer-ipv6'): | ||
4082 | 1206 | return {'bind_host': '::'} | ||
4083 | 1207 | else: | ||
4084 | 1208 | return {'bind_host': '0.0.0.0'} | ||
4085 | 1209 | |||
4086 | 1210 | |||
4087 | 1211 | class WorkerConfigContext(OSContextGenerator): | ||
4088 | 1212 | |||
4089 | 1213 | @property | ||
4090 | 1214 | def num_cpus(self): | ||
4091 | 1215 | try: | ||
4092 | 1216 | from psutil import NUM_CPUS | ||
4093 | 1217 | except ImportError: | ||
4094 | 1218 | apt_install('python-psutil', fatal=True) | ||
4095 | 1219 | from psutil import NUM_CPUS | ||
4096 | 1220 | |||
4097 | 1221 | return NUM_CPUS | ||
4098 | 1222 | |||
4099 | 1223 | def __call__(self): | ||
4100 | 1224 | multiplier = config('worker-multiplier') or 0 | ||
4101 | 1225 | ctxt = {"workers": self.num_cpus * multiplier} | ||
4102 | 1226 | return ctxt | ||
4103 | 1227 | |||
4104 | 1228 | |||
4105 | 1229 | class ZeroMQContext(OSContextGenerator): | ||
4106 | 1230 | interfaces = ['zeromq-configuration'] | ||
4107 | 1231 | |||
4108 | 1232 | def __call__(self): | ||
4109 | 1233 | ctxt = {} | ||
4110 | 1234 | if is_relation_made('zeromq-configuration', 'host'): | ||
4111 | 1235 | for rid in relation_ids('zeromq-configuration'): | ||
4112 | 1236 | for unit in related_units(rid): | ||
4113 | 1237 | ctxt['zmq_nonce'] = relation_get('nonce', unit, rid) | ||
4114 | 1238 | ctxt['zmq_host'] = relation_get('host', unit, rid) | ||
4115 | 1239 | ctxt['zmq_redis_address'] = relation_get( | ||
4116 | 1240 | 'zmq_redis_address', unit, rid) | ||
4117 | 1241 | |||
4118 | 1242 | return ctxt | ||
4119 | 1243 | |||
4120 | 1244 | |||
4121 | 1245 | class NotificationDriverContext(OSContextGenerator): | ||
4122 | 1246 | |||
4123 | 1247 | def __init__(self, zmq_relation='zeromq-configuration', | ||
4124 | 1248 | amqp_relation='amqp'): | ||
4125 | 1249 | """ | ||
4126 | 1250 | :param zmq_relation: Name of Zeromq relation to check | ||
4127 | 1251 | """ | ||
4128 | 1252 | self.zmq_relation = zmq_relation | ||
4129 | 1253 | self.amqp_relation = amqp_relation | ||
4130 | 1254 | |||
4131 | 1255 | def __call__(self): | ||
4132 | 1256 | ctxt = {'notifications': 'False'} | ||
4133 | 1257 | if is_relation_made(self.amqp_relation): | ||
4134 | 1258 | ctxt['notifications'] = "True" | ||
4135 | 1259 | |||
4136 | 1260 | return ctxt | ||
4137 | 1261 | |||
4138 | 1262 | |||
4139 | 1263 | class SysctlContext(OSContextGenerator): | ||
4140 | 1264 | """This context check if the 'sysctl' option exists on configuration | ||
4141 | 1265 | then creates a file with the loaded contents""" | ||
4142 | 1266 | def __call__(self): | ||
4143 | 1267 | sysctl_dict = config('sysctl') | ||
4144 | 1268 | if sysctl_dict: | ||
4145 | 1269 | sysctl_create(sysctl_dict, | ||
4146 | 1270 | '/etc/sysctl.d/50-{0}.conf'.format(charm_name())) | ||
4147 | 1271 | return {'sysctl': sysctl_dict} | ||
4148 | 1272 | |||
4149 | 1273 | |||
4150 | 1274 | class NeutronAPIContext(OSContextGenerator): | ||
4151 | 1275 | ''' | ||
4152 | 1276 | Inspects current neutron-plugin-api relation for neutron settings. Return | ||
4153 | 1277 | defaults if it is not present. | ||
4154 | 1278 | ''' | ||
4155 | 1279 | interfaces = ['neutron-plugin-api'] | ||
4156 | 1280 | |||
4157 | 1281 | def __call__(self): | ||
4158 | 1282 | self.neutron_defaults = { | ||
4159 | 1283 | 'l2_population': { | ||
4160 | 1284 | 'rel_key': 'l2-population', | ||
4161 | 1285 | 'default': False, | ||
4162 | 1286 | }, | ||
4163 | 1287 | 'overlay_network_type': { | ||
4164 | 1288 | 'rel_key': 'overlay-network-type', | ||
4165 | 1289 | 'default': 'gre', | ||
4166 | 1290 | }, | ||
4167 | 1291 | 'neutron_security_groups': { | ||
4168 | 1292 | 'rel_key': 'neutron-security-groups', | ||
4169 | 1293 | 'default': False, | ||
4170 | 1294 | }, | ||
4171 | 1295 | 'network_device_mtu': { | ||
4172 | 1296 | 'rel_key': 'network-device-mtu', | ||
4173 | 1297 | 'default': None, | ||
4174 | 1298 | }, | ||
4175 | 1299 | 'enable_dvr': { | ||
4176 | 1300 | 'rel_key': 'enable-dvr', | ||
4177 | 1301 | 'default': False, | ||
4178 | 1302 | }, | ||
4179 | 1303 | 'enable_l3ha': { | ||
4180 | 1304 | 'rel_key': 'enable-l3ha', | ||
4181 | 1305 | 'default': False, | ||
4182 | 1306 | }, | ||
4183 | 1307 | } | ||
4184 | 1308 | ctxt = self.get_neutron_options({}) | ||
4185 | 1309 | for rid in relation_ids('neutron-plugin-api'): | ||
4186 | 1310 | for unit in related_units(rid): | ||
4187 | 1311 | rdata = relation_get(rid=rid, unit=unit) | ||
4188 | 1312 | if 'l2-population' in rdata: | ||
4189 | 1313 | ctxt.update(self.get_neutron_options(rdata)) | ||
4190 | 1314 | |||
4191 | 1315 | return ctxt | ||
4192 | 1316 | |||
4193 | 1317 | def get_neutron_options(self, rdata): | ||
4194 | 1318 | settings = {} | ||
4195 | 1319 | for nkey in self.neutron_defaults.keys(): | ||
4196 | 1320 | defv = self.neutron_defaults[nkey]['default'] | ||
4197 | 1321 | rkey = self.neutron_defaults[nkey]['rel_key'] | ||
4198 | 1322 | if rkey in rdata.keys(): | ||
4199 | 1323 | if type(defv) is bool: | ||
4200 | 1324 | settings[nkey] = bool_from_string(rdata[rkey]) | ||
4201 | 1325 | else: | ||
4202 | 1326 | settings[nkey] = rdata[rkey] | ||
4203 | 1327 | else: | ||
4204 | 1328 | settings[nkey] = defv | ||
4205 | 1329 | return settings | ||
4206 | 1330 | |||
4207 | 1331 | |||
4208 | 1332 | class ExternalPortContext(NeutronPortContext): | ||
4209 | 1333 | |||
4210 | 1334 | def __call__(self): | ||
4211 | 1335 | ctxt = {} | ||
4212 | 1336 | ports = config('ext-port') | ||
4213 | 1337 | if ports: | ||
4214 | 1338 | ports = [p.strip() for p in ports.split()] | ||
4215 | 1339 | ports = self.resolve_ports(ports) | ||
4216 | 1340 | if ports: | ||
4217 | 1341 | ctxt = {"ext_port": ports[0]} | ||
4218 | 1342 | napi_settings = NeutronAPIContext()() | ||
4219 | 1343 | mtu = napi_settings.get('network_device_mtu') | ||
4220 | 1344 | if mtu: | ||
4221 | 1345 | ctxt['ext_port_mtu'] = mtu | ||
4222 | 1346 | |||
4223 | 1347 | return ctxt | ||
4224 | 1348 | |||
4225 | 1349 | |||
4226 | 1350 | class DataPortContext(NeutronPortContext): | ||
4227 | 1351 | |||
4228 | 1352 | def __call__(self): | ||
4229 | 1353 | ports = config('data-port') | ||
4230 | 1354 | if ports: | ||
4231 | 1355 | # Map of {port/mac:bridge} | ||
4232 | 1356 | portmap = parse_data_port_mappings(ports) | ||
4233 | 1357 | ports = portmap.keys() | ||
4234 | 1358 | # Resolve provided ports or mac addresses and filter out those | ||
4235 | 1359 | # already attached to a bridge. | ||
4236 | 1360 | resolved = self.resolve_ports(ports) | ||
4237 | 1361 | # FIXME: is this necessary? | ||
4238 | 1362 | normalized = {get_nic_hwaddr(port): port for port in resolved | ||
4239 | 1363 | if port not in ports} | ||
4240 | 1364 | normalized.update({port: port for port in resolved | ||
4241 | 1365 | if port in ports}) | ||
4242 | 1366 | if resolved: | ||
4243 | 1367 | return {bridge: normalized[port] for port, bridge in | ||
4244 | 1368 | six.iteritems(portmap) if port in normalized.keys()} | ||
4245 | 1369 | |||
4246 | 1370 | return None | ||
4247 | 1371 | |||
4248 | 1372 | |||
4249 | 1373 | class PhyNICMTUContext(DataPortContext): | ||
4250 | 1374 | |||
4251 | 1375 | def __call__(self): | ||
4252 | 1376 | ctxt = {} | ||
4253 | 1377 | mappings = super(PhyNICMTUContext, self).__call__() | ||
4254 | 1378 | if mappings and mappings.values(): | ||
4255 | 1379 | ports = mappings.values() | ||
4256 | 1380 | napi_settings = NeutronAPIContext()() | ||
4257 | 1381 | mtu = napi_settings.get('network_device_mtu') | ||
4258 | 1382 | all_ports = set() | ||
4259 | 1383 | # If any of ports is a vlan device, its underlying device must have | ||
4260 | 1384 | # mtu applied first. | ||
4261 | 1385 | for port in ports: | ||
4262 | 1386 | for lport in glob.glob("/sys/class/net/%s/lower_*" % port): | ||
4263 | 1387 | lport = os.path.basename(lport) | ||
4264 | 1388 | all_ports.add(lport.split('_')[1]) | ||
4265 | 1389 | |||
4266 | 1390 | all_ports = list(all_ports) | ||
4267 | 1391 | all_ports.extend(ports) | ||
4268 | 1392 | if mtu: | ||
4269 | 1393 | ctxt["devs"] = '\\n'.join(all_ports) | ||
4270 | 1394 | ctxt['mtu'] = mtu | ||
4271 | 1395 | |||
4272 | 1396 | return ctxt | ||
4273 | 1397 | |||
4274 | 1398 | |||
4275 | 1399 | class NetworkServiceContext(OSContextGenerator): | ||
4276 | 1400 | |||
4277 | 1401 | def __init__(self, rel_name='quantum-network-service'): | ||
4278 | 1402 | self.rel_name = rel_name | ||
4279 | 1403 | self.interfaces = [rel_name] | ||
4280 | 1404 | |||
4281 | 1405 | def __call__(self): | ||
4282 | 1406 | for rid in relation_ids(self.rel_name): | ||
4283 | 1407 | for unit in related_units(rid): | ||
4284 | 1408 | rdata = relation_get(rid=rid, unit=unit) | ||
4285 | 1409 | ctxt = { | ||
4286 | 1410 | 'keystone_host': rdata.get('keystone_host'), | ||
4287 | 1411 | 'service_port': rdata.get('service_port'), | ||
4288 | 1412 | 'auth_port': rdata.get('auth_port'), | ||
4289 | 1413 | 'service_tenant': rdata.get('service_tenant'), | ||
4290 | 1414 | 'service_username': rdata.get('service_username'), | ||
4291 | 1415 | 'service_password': rdata.get('service_password'), | ||
4292 | 1416 | 'quantum_host': rdata.get('quantum_host'), | ||
4293 | 1417 | 'quantum_port': rdata.get('quantum_port'), | ||
4294 | 1418 | 'quantum_url': rdata.get('quantum_url'), | ||
4295 | 1419 | 'region': rdata.get('region'), | ||
4296 | 1420 | 'service_protocol': | ||
4297 | 1421 | rdata.get('service_protocol') or 'http', | ||
4298 | 1422 | 'auth_protocol': | ||
4299 | 1423 | rdata.get('auth_protocol') or 'http', | ||
4300 | 1424 | } | ||
4301 | 1425 | if self.context_complete(ctxt): | ||
4302 | 1426 | return ctxt | ||
4303 | 1427 | return {} | ||
4304 | 0 | 1428 | ||
4305 | === added directory 'charmhelpers/contrib/openstack/files' | |||
4306 | === added file 'charmhelpers/contrib/openstack/files/__init__.py' | |||
4307 | --- charmhelpers/contrib/openstack/files/__init__.py 1970-01-01 00:00:00 +0000 | |||
4308 | +++ charmhelpers/contrib/openstack/files/__init__.py 2015-10-08 20:44:37 +0000 | |||
4309 | @@ -0,0 +1,18 @@ | |||
4310 | 1 | # Copyright 2014-2015 Canonical Limited. | ||
4311 | 2 | # | ||
4312 | 3 | # This file is part of charm-helpers. | ||
4313 | 4 | # | ||
4314 | 5 | # charm-helpers is free software: you can redistribute it and/or modify | ||
4315 | 6 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
4316 | 7 | # published by the Free Software Foundation. | ||
4317 | 8 | # | ||
4318 | 9 | # charm-helpers is distributed in the hope that it will be useful, | ||
4319 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
4320 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
4321 | 12 | # GNU Lesser General Public License for more details. | ||
4322 | 13 | # | ||
4323 | 14 | # You should have received a copy of the GNU Lesser General Public License | ||
4324 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
4325 | 16 | |||
4326 | 17 | # dummy __init__.py to fool syncer into thinking this is a syncable python | ||
4327 | 18 | # module | ||
4328 | 0 | 19 | ||
4329 | === added file 'charmhelpers/contrib/openstack/files/check_haproxy.sh' | |||
4330 | --- charmhelpers/contrib/openstack/files/check_haproxy.sh 1970-01-01 00:00:00 +0000 | |||
4331 | +++ charmhelpers/contrib/openstack/files/check_haproxy.sh 2015-10-08 20:44:37 +0000 | |||
4332 | @@ -0,0 +1,32 @@ | |||
4333 | 1 | #!/bin/bash | ||
4334 | 2 | #-------------------------------------------- | ||
4335 | 3 | # This file is managed by Juju | ||
4336 | 4 | #-------------------------------------------- | ||
4337 | 5 | # | ||
4338 | 6 | # Copyright 2009,2012 Canonical Ltd. | ||
4339 | 7 | # Author: Tom Haddon | ||
4340 | 8 | |||
4341 | 9 | CRITICAL=0 | ||
4342 | 10 | NOTACTIVE='' | ||
4343 | 11 | LOGFILE=/var/log/nagios/check_haproxy.log | ||
4344 | 12 | AUTH=$(grep -r "stats auth" /etc/haproxy | head -1 | awk '{print $4}') | ||
4345 | 13 | |||
4346 | 14 | for appserver in $(grep ' server' /etc/haproxy/haproxy.cfg | awk '{print $2'}); | ||
4347 | 15 | do | ||
4348 | 16 | output=$(/usr/lib/nagios/plugins/check_http -a ${AUTH} -I 127.0.0.1 -p 8888 --regex="class=\"(active|backup)(2|3).*${appserver}" -e ' 200 OK') | ||
4349 | 17 | if [ $? != 0 ]; then | ||
4350 | 18 | date >> $LOGFILE | ||
4351 | 19 | echo $output >> $LOGFILE | ||
4352 | 20 | /usr/lib/nagios/plugins/check_http -a ${AUTH} -I 127.0.0.1 -p 8888 -v | grep $appserver >> $LOGFILE 2>&1 | ||
4353 | 21 | CRITICAL=1 | ||
4354 | 22 | NOTACTIVE="${NOTACTIVE} $appserver" | ||
4355 | 23 | fi | ||
4356 | 24 | done | ||
4357 | 25 | |||
4358 | 26 | if [ $CRITICAL = 1 ]; then | ||
4359 | 27 | echo "CRITICAL:${NOTACTIVE}" | ||
4360 | 28 | exit 2 | ||
4361 | 29 | fi | ||
4362 | 30 | |||
4363 | 31 | echo "OK: All haproxy instances looking good" | ||
4364 | 32 | exit 0 | ||
4365 | 0 | 33 | ||
4366 | === added file 'charmhelpers/contrib/openstack/files/check_haproxy_queue_depth.sh' | |||
4367 | --- charmhelpers/contrib/openstack/files/check_haproxy_queue_depth.sh 1970-01-01 00:00:00 +0000 | |||
4368 | +++ charmhelpers/contrib/openstack/files/check_haproxy_queue_depth.sh 2015-10-08 20:44:37 +0000 | |||
4369 | @@ -0,0 +1,30 @@ | |||
4370 | 1 | #!/bin/bash | ||
4371 | 2 | #-------------------------------------------- | ||
4372 | 3 | # This file is managed by Juju | ||
4373 | 4 | #-------------------------------------------- | ||
4374 | 5 | # | ||
4375 | 6 | # Copyright 2009,2012 Canonical Ltd. | ||
4376 | 7 | # Author: Tom Haddon | ||
4377 | 8 | |||
4378 | 9 | # These should be config options at some stage | ||
4379 | 10 | CURRQthrsh=0 | ||
4380 | 11 | MAXQthrsh=100 | ||
4381 | 12 | |||
4382 | 13 | AUTH=$(grep -r "stats auth" /etc/haproxy | head -1 | awk '{print $4}') | ||
4383 | 14 | |||
4384 | 15 | HAPROXYSTATS=$(/usr/lib/nagios/plugins/check_http -a ${AUTH} -I 127.0.0.1 -p 8888 -u '/;csv' -v) | ||
4385 | 16 | |||
4386 | 17 | for BACKEND in $(echo $HAPROXYSTATS| xargs -n1 | grep BACKEND | awk -F , '{print $1}') | ||
4387 | 18 | do | ||
4388 | 19 | CURRQ=$(echo "$HAPROXYSTATS" | grep $BACKEND | grep BACKEND | cut -d , -f 3) | ||
4389 | 20 | MAXQ=$(echo "$HAPROXYSTATS" | grep $BACKEND | grep BACKEND | cut -d , -f 4) | ||
4390 | 21 | |||
4391 | 22 | if [[ $CURRQ -gt $CURRQthrsh || $MAXQ -gt $MAXQthrsh ]] ; then | ||
4392 | 23 | echo "CRITICAL: queue depth for $BACKEND - CURRENT:$CURRQ MAX:$MAXQ" | ||
4393 | 24 | exit 2 | ||
4394 | 25 | fi | ||
4395 | 26 | done | ||
4396 | 27 | |||
4397 | 28 | echo "OK: All haproxy queue depths looking good" | ||
4398 | 29 | exit 0 | ||
4399 | 30 | |||
4400 | 0 | 31 | ||
4401 | === added file 'charmhelpers/contrib/openstack/ip.py' | |||
4402 | --- charmhelpers/contrib/openstack/ip.py 1970-01-01 00:00:00 +0000 | |||
4403 | +++ charmhelpers/contrib/openstack/ip.py 2015-10-08 20:44:37 +0000 | |||
4404 | @@ -0,0 +1,151 @@ | |||
4405 | 1 | # Copyright 2014-2015 Canonical Limited. | ||
4406 | 2 | # | ||
4407 | 3 | # This file is part of charm-helpers. | ||
4408 | 4 | # | ||
4409 | 5 | # charm-helpers is free software: you can redistribute it and/or modify | ||
4410 | 6 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
4411 | 7 | # published by the Free Software Foundation. | ||
4412 | 8 | # | ||
4413 | 9 | # charm-helpers is distributed in the hope that it will be useful, | ||
4414 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
4415 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
4416 | 12 | # GNU Lesser General Public License for more details. | ||
4417 | 13 | # | ||
4418 | 14 | # You should have received a copy of the GNU Lesser General Public License | ||
4419 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
4420 | 16 | |||
4421 | 17 | from charmhelpers.core.hookenv import ( | ||
4422 | 18 | config, | ||
4423 | 19 | unit_get, | ||
4424 | 20 | service_name, | ||
4425 | 21 | ) | ||
4426 | 22 | from charmhelpers.contrib.network.ip import ( | ||
4427 | 23 | get_address_in_network, | ||
4428 | 24 | is_address_in_network, | ||
4429 | 25 | is_ipv6, | ||
4430 | 26 | get_ipv6_addr, | ||
4431 | 27 | ) | ||
4432 | 28 | from charmhelpers.contrib.hahelpers.cluster import is_clustered | ||
4433 | 29 | |||
4434 | 30 | PUBLIC = 'public' | ||
4435 | 31 | INTERNAL = 'int' | ||
4436 | 32 | ADMIN = 'admin' | ||
4437 | 33 | |||
4438 | 34 | ADDRESS_MAP = { | ||
4439 | 35 | PUBLIC: { | ||
4440 | 36 | 'config': 'os-public-network', | ||
4441 | 37 | 'fallback': 'public-address', | ||
4442 | 38 | 'override': 'os-public-hostname', | ||
4443 | 39 | }, | ||
4444 | 40 | INTERNAL: { | ||
4445 | 41 | 'config': 'os-internal-network', | ||
4446 | 42 | 'fallback': 'private-address', | ||
4447 | 43 | 'override': 'os-internal-hostname', | ||
4448 | 44 | }, | ||
4449 | 45 | ADMIN: { | ||
4450 | 46 | 'config': 'os-admin-network', | ||
4451 | 47 | 'fallback': 'private-address', | ||
4452 | 48 | 'override': 'os-admin-hostname', | ||
4453 | 49 | } | ||
4454 | 50 | } | ||
4455 | 51 | |||
4456 | 52 | |||
4457 | 53 | def canonical_url(configs, endpoint_type=PUBLIC): | ||
4458 | 54 | """Returns the correct HTTP URL to this host given the state of HTTPS | ||
4459 | 55 | configuration, hacluster and charm configuration. | ||
4460 | 56 | |||
4461 | 57 | :param configs: OSTemplateRenderer config templating object to inspect | ||
4462 | 58 | for a complete https context. | ||
4463 | 59 | :param endpoint_type: str endpoint type to resolve. | ||
4464 | 60 | :param returns: str base URL for services on the current service unit. | ||
4465 | 61 | """ | ||
4466 | 62 | scheme = _get_scheme(configs) | ||
4467 | 63 | |||
4468 | 64 | address = resolve_address(endpoint_type) | ||
4469 | 65 | if is_ipv6(address): | ||
4470 | 66 | address = "[{}]".format(address) | ||
4471 | 67 | |||
4472 | 68 | return '%s://%s' % (scheme, address) | ||
4473 | 69 | |||
4474 | 70 | |||
4475 | 71 | def _get_scheme(configs): | ||
4476 | 72 | """Returns the scheme to use for the url (either http or https) | ||
4477 | 73 | depending upon whether https is in the configs value. | ||
4478 | 74 | |||
4479 | 75 | :param configs: OSTemplateRenderer config templating object to inspect | ||
4480 | 76 | for a complete https context. | ||
4481 | 77 | :returns: either 'http' or 'https' depending on whether https is | ||
4482 | 78 | configured within the configs context. | ||
4483 | 79 | """ | ||
4484 | 80 | scheme = 'http' | ||
4485 | 81 | if configs and 'https' in configs.complete_contexts(): | ||
4486 | 82 | scheme = 'https' | ||
4487 | 83 | return scheme | ||
4488 | 84 | |||
4489 | 85 | |||
4490 | 86 | def _get_address_override(endpoint_type=PUBLIC): | ||
4491 | 87 | """Returns any address overrides that the user has defined based on the | ||
4492 | 88 | endpoint type. | ||
4493 | 89 | |||
4494 | 90 | Note: this function allows for the service name to be inserted into the | ||
4495 | 91 | address if the user specifies {service_name}.somehost.org. | ||
4496 | 92 | |||
4497 | 93 | :param endpoint_type: the type of endpoint to retrieve the override | ||
4498 | 94 | value for. | ||
4499 | 95 | :returns: any endpoint address or hostname that the user has overridden | ||
4500 | 96 | or None if an override is not present. | ||
4501 | 97 | """ | ||
4502 | 98 | override_key = ADDRESS_MAP[endpoint_type]['override'] | ||
4503 | 99 | addr_override = config(override_key) | ||
4504 | 100 | if not addr_override: | ||
4505 | 101 | return None | ||
4506 | 102 | else: | ||
4507 | 103 | return addr_override.format(service_name=service_name()) | ||
4508 | 104 | |||
4509 | 105 | |||
4510 | 106 | def resolve_address(endpoint_type=PUBLIC): | ||
4511 | 107 | """Return unit address depending on net config. | ||
4512 | 108 | |||
4513 | 109 | If unit is clustered with vip(s) and has net splits defined, return vip on | ||
4514 | 110 | correct network. If clustered with no nets defined, return primary vip. | ||
4515 | 111 | |||
4516 | 112 | If not clustered, return unit address ensuring address is on configured net | ||
4517 | 113 | split if one is configured. | ||
4518 | 114 | |||
4519 | 115 | :param endpoint_type: Network endpoing type | ||
4520 | 116 | """ | ||
4521 | 117 | resolved_address = _get_address_override(endpoint_type) | ||
4522 | 118 | if resolved_address: | ||
4523 | 119 | return resolved_address | ||
4524 | 120 | |||
4525 | 121 | vips = config('vip') | ||
4526 | 122 | if vips: | ||
4527 | 123 | vips = vips.split() | ||
4528 | 124 | |||
4529 | 125 | net_type = ADDRESS_MAP[endpoint_type]['config'] | ||
4530 | 126 | net_addr = config(net_type) | ||
4531 | 127 | net_fallback = ADDRESS_MAP[endpoint_type]['fallback'] | ||
4532 | 128 | clustered = is_clustered() | ||
4533 | 129 | if clustered: | ||
4534 | 130 | if not net_addr: | ||
4535 | 131 | # If no net-splits defined, we expect a single vip | ||
4536 | 132 | resolved_address = vips[0] | ||
4537 | 133 | else: | ||
4538 | 134 | for vip in vips: | ||
4539 | 135 | if is_address_in_network(net_addr, vip): | ||
4540 | 136 | resolved_address = vip | ||
4541 | 137 | break | ||
4542 | 138 | else: | ||
4543 | 139 | if config('prefer-ipv6'): | ||
4544 | 140 | fallback_addr = get_ipv6_addr(exc_list=vips)[0] | ||
4545 | 141 | else: | ||
4546 | 142 | fallback_addr = unit_get(net_fallback) | ||
4547 | 143 | |||
4548 | 144 | resolved_address = get_address_in_network(net_addr, fallback_addr) | ||
4549 | 145 | |||
4550 | 146 | if resolved_address is None: | ||
4551 | 147 | raise ValueError("Unable to resolve a suitable IP address based on " | ||
4552 | 148 | "charm state and configuration. (net_type=%s, " | ||
4553 | 149 | "clustered=%s)" % (net_type, clustered)) | ||
4554 | 150 | |||
4555 | 151 | return resolved_address | ||
4556 | 0 | 152 | ||
4557 | === added file 'charmhelpers/contrib/openstack/neutron.py' | |||
4558 | --- charmhelpers/contrib/openstack/neutron.py 1970-01-01 00:00:00 +0000 | |||
4559 | +++ charmhelpers/contrib/openstack/neutron.py 2015-10-08 20:44:37 +0000 | |||
4560 | @@ -0,0 +1,356 @@ | |||
4561 | 1 | # Copyright 2014-2015 Canonical Limited. | ||
4562 | 2 | # | ||
4563 | 3 | # This file is part of charm-helpers. | ||
4564 | 4 | # | ||
4565 | 5 | # charm-helpers is free software: you can redistribute it and/or modify | ||
4566 | 6 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
4567 | 7 | # published by the Free Software Foundation. | ||
4568 | 8 | # | ||
4569 | 9 | # charm-helpers is distributed in the hope that it will be useful, | ||
4570 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
4571 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
4572 | 12 | # GNU Lesser General Public License for more details. | ||
4573 | 13 | # | ||
4574 | 14 | # You should have received a copy of the GNU Lesser General Public License | ||
4575 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
4576 | 16 | |||
4577 | 17 | # Various utilies for dealing with Neutron and the renaming from Quantum. | ||
4578 | 18 | |||
4579 | 19 | import six | ||
4580 | 20 | from subprocess import check_output | ||
4581 | 21 | |||
4582 | 22 | from charmhelpers.core.hookenv import ( | ||
4583 | 23 | config, | ||
4584 | 24 | log, | ||
4585 | 25 | ERROR, | ||
4586 | 26 | ) | ||
4587 | 27 | |||
4588 | 28 | from charmhelpers.contrib.openstack.utils import os_release | ||
4589 | 29 | |||
4590 | 30 | |||
4591 | 31 | def headers_package(): | ||
4592 | 32 | """Ensures correct linux-headers for running kernel are installed, | ||
4593 | 33 | for building DKMS package""" | ||
4594 | 34 | kver = check_output(['uname', '-r']).decode('UTF-8').strip() | ||
4595 | 35 | return 'linux-headers-%s' % kver | ||
4596 | 36 | |||
4597 | 37 | QUANTUM_CONF_DIR = '/etc/quantum' | ||
4598 | 38 | |||
4599 | 39 | |||
4600 | 40 | def kernel_version(): | ||
4601 | 41 | """ Retrieve the current major kernel version as a tuple e.g. (3, 13) """ | ||
4602 | 42 | kver = check_output(['uname', '-r']).decode('UTF-8').strip() | ||
4603 | 43 | kver = kver.split('.') | ||
4604 | 44 | return (int(kver[0]), int(kver[1])) | ||
4605 | 45 | |||
4606 | 46 | |||
4607 | 47 | def determine_dkms_package(): | ||
4608 | 48 | """ Determine which DKMS package should be used based on kernel version """ | ||
4609 | 49 | # NOTE: 3.13 kernels have support for GRE and VXLAN native | ||
4610 | 50 | if kernel_version() >= (3, 13): | ||
4611 | 51 | return [] | ||
4612 | 52 | else: | ||
4613 | 53 | return ['openvswitch-datapath-dkms'] | ||
4614 | 54 | |||
4615 | 55 | |||
4616 | 56 | # legacy | ||
4617 | 57 | |||
4618 | 58 | |||
4619 | 59 | def quantum_plugins(): | ||
4620 | 60 | from charmhelpers.contrib.openstack import context | ||
4621 | 61 | return { | ||
4622 | 62 | 'ovs': { | ||
4623 | 63 | 'config': '/etc/quantum/plugins/openvswitch/' | ||
4624 | 64 | 'ovs_quantum_plugin.ini', | ||
4625 | 65 | 'driver': 'quantum.plugins.openvswitch.ovs_quantum_plugin.' | ||
4626 | 66 | 'OVSQuantumPluginV2', | ||
4627 | 67 | 'contexts': [ | ||
4628 | 68 | context.SharedDBContext(user=config('neutron-database-user'), | ||
4629 | 69 | database=config('neutron-database'), | ||
4630 | 70 | relation_prefix='neutron', | ||
4631 | 71 | ssl_dir=QUANTUM_CONF_DIR)], | ||
4632 | 72 | 'services': ['quantum-plugin-openvswitch-agent'], | ||
4633 | 73 | 'packages': [[headers_package()] + determine_dkms_package(), | ||
4634 | 74 | ['quantum-plugin-openvswitch-agent']], | ||
4635 | 75 | 'server_packages': ['quantum-server', | ||
4636 | 76 | 'quantum-plugin-openvswitch'], | ||
4637 | 77 | 'server_services': ['quantum-server'] | ||
4638 | 78 | }, | ||
4639 | 79 | 'nvp': { | ||
4640 | 80 | 'config': '/etc/quantum/plugins/nicira/nvp.ini', | ||
4641 | 81 | 'driver': 'quantum.plugins.nicira.nicira_nvp_plugin.' | ||
4642 | 82 | 'QuantumPlugin.NvpPluginV2', | ||
4643 | 83 | 'contexts': [ | ||
4644 | 84 | context.SharedDBContext(user=config('neutron-database-user'), | ||
4645 | 85 | database=config('neutron-database'), | ||
4646 | 86 | relation_prefix='neutron', | ||
4647 | 87 | ssl_dir=QUANTUM_CONF_DIR)], | ||
4648 | 88 | 'services': [], | ||
4649 | 89 | 'packages': [], | ||
4650 | 90 | 'server_packages': ['quantum-server', | ||
4651 | 91 | 'quantum-plugin-nicira'], | ||
4652 | 92 | 'server_services': ['quantum-server'] | ||
4653 | 93 | } | ||
4654 | 94 | } | ||
4655 | 95 | |||
4656 | 96 | NEUTRON_CONF_DIR = '/etc/neutron' | ||
4657 | 97 | |||
4658 | 98 | |||
4659 | 99 | def neutron_plugins(): | ||
4660 | 100 | from charmhelpers.contrib.openstack import context | ||
4661 | 101 | release = os_release('nova-common') | ||
4662 | 102 | plugins = { | ||
4663 | 103 | 'ovs': { | ||
4664 | 104 | 'config': '/etc/neutron/plugins/openvswitch/' | ||
4665 | 105 | 'ovs_neutron_plugin.ini', | ||
4666 | 106 | 'driver': 'neutron.plugins.openvswitch.ovs_neutron_plugin.' | ||
4667 | 107 | 'OVSNeutronPluginV2', | ||
4668 | 108 | 'contexts': [ | ||
4669 | 109 | context.SharedDBContext(user=config('neutron-database-user'), | ||
4670 | 110 | database=config('neutron-database'), | ||
4671 | 111 | relation_prefix='neutron', | ||
4672 | 112 | ssl_dir=NEUTRON_CONF_DIR)], | ||
4673 | 113 | 'services': ['neutron-plugin-openvswitch-agent'], | ||
4674 | 114 | 'packages': [[headers_package()] + determine_dkms_package(), | ||
4675 | 115 | ['neutron-plugin-openvswitch-agent']], | ||
4676 | 116 | 'server_packages': ['neutron-server', | ||
4677 | 117 | 'neutron-plugin-openvswitch'], | ||
4678 | 118 | 'server_services': ['neutron-server'] | ||
4679 | 119 | }, | ||
4680 | 120 | 'nvp': { | ||
4681 | 121 | 'config': '/etc/neutron/plugins/nicira/nvp.ini', | ||
4682 | 122 | 'driver': 'neutron.plugins.nicira.nicira_nvp_plugin.' | ||
4683 | 123 | 'NeutronPlugin.NvpPluginV2', | ||
4684 | 124 | 'contexts': [ | ||
4685 | 125 | context.SharedDBContext(user=config('neutron-database-user'), | ||
4686 | 126 | database=config('neutron-database'), | ||
4687 | 127 | relation_prefix='neutron', | ||
4688 | 128 | ssl_dir=NEUTRON_CONF_DIR)], | ||
4689 | 129 | 'services': [], | ||
4690 | 130 | 'packages': [], | ||
4691 | 131 | 'server_packages': ['neutron-server', | ||
4692 | 132 | 'neutron-plugin-nicira'], | ||
4693 | 133 | 'server_services': ['neutron-server'] | ||
4694 | 134 | }, | ||
4695 | 135 | 'nsx': { | ||
4696 | 136 | 'config': '/etc/neutron/plugins/vmware/nsx.ini', | ||
4697 | 137 | 'driver': 'vmware', | ||
4698 | 138 | 'contexts': [ | ||
4699 | 139 | context.SharedDBContext(user=config('neutron-database-user'), | ||
4700 | 140 | database=config('neutron-database'), | ||
4701 | 141 | relation_prefix='neutron', | ||
4702 | 142 | ssl_dir=NEUTRON_CONF_DIR)], | ||
4703 | 143 | 'services': [], | ||
4704 | 144 | 'packages': [], | ||
4705 | 145 | 'server_packages': ['neutron-server', | ||
4706 | 146 | 'neutron-plugin-vmware'], | ||
4707 | 147 | 'server_services': ['neutron-server'] | ||
4708 | 148 | }, | ||
4709 | 149 | 'n1kv': { | ||
4710 | 150 | 'config': '/etc/neutron/plugins/cisco/cisco_plugins.ini', | ||
4711 | 151 | 'driver': 'neutron.plugins.cisco.network_plugin.PluginV2', | ||
4712 | 152 | 'contexts': [ | ||
4713 | 153 | context.SharedDBContext(user=config('neutron-database-user'), | ||
4714 | 154 | database=config('neutron-database'), | ||
4715 | 155 | relation_prefix='neutron', | ||
4716 | 156 | ssl_dir=NEUTRON_CONF_DIR)], | ||
4717 | 157 | 'services': [], | ||
4718 | 158 | 'packages': [[headers_package()] + determine_dkms_package(), | ||
4719 | 159 | ['neutron-plugin-cisco']], | ||
4720 | 160 | 'server_packages': ['neutron-server', | ||
4721 | 161 | 'neutron-plugin-cisco'], | ||
4722 | 162 | 'server_services': ['neutron-server'] | ||
4723 | 163 | }, | ||
4724 | 164 | 'Calico': { | ||
4725 | 165 | 'config': '/etc/neutron/plugins/ml2/ml2_conf.ini', | ||
4726 | 166 | 'driver': 'neutron.plugins.ml2.plugin.Ml2Plugin', | ||
4727 | 167 | 'contexts': [ | ||
4728 | 168 | context.SharedDBContext(user=config('neutron-database-user'), | ||
4729 | 169 | database=config('neutron-database'), | ||
4730 | 170 | relation_prefix='neutron', | ||
4731 | 171 | ssl_dir=NEUTRON_CONF_DIR)], | ||
4732 | 172 | 'services': ['calico-felix', | ||
4733 | 173 | 'bird', | ||
4734 | 174 | 'neutron-dhcp-agent', | ||
4735 | 175 | 'nova-api-metadata', | ||
4736 | 176 | 'etcd'], | ||
4737 | 177 | 'packages': [[headers_package()] + determine_dkms_package(), | ||
4738 | 178 | ['calico-compute', | ||
4739 | 179 | 'bird', | ||
4740 | 180 | 'neutron-dhcp-agent', | ||
4741 | 181 | 'nova-api-metadata', | ||
4742 | 182 | 'etcd']], | ||
4743 | 183 | 'server_packages': ['neutron-server', 'calico-control', 'etcd'], | ||
4744 | 184 | 'server_services': ['neutron-server', 'etcd'] | ||
4745 | 185 | }, | ||
4746 | 186 | 'vsp': { | ||
4747 | 187 | 'config': '/etc/neutron/plugins/nuage/nuage_plugin.ini', | ||
4748 | 188 | 'driver': 'neutron.plugins.nuage.plugin.NuagePlugin', | ||
4749 | 189 | 'contexts': [ | ||
4750 | 190 | context.SharedDBContext(user=config('neutron-database-user'), | ||
4751 | 191 | database=config('neutron-database'), | ||
4752 | 192 | relation_prefix='neutron', | ||
4753 | 193 | ssl_dir=NEUTRON_CONF_DIR)], | ||
4754 | 194 | 'services': [], | ||
4755 | 195 | 'packages': [], | ||
4756 | 196 | 'server_packages': ['neutron-server', 'neutron-plugin-nuage'], | ||
4757 | 197 | 'server_services': ['neutron-server'] | ||
4758 | 198 | }, | ||
4759 | 199 | 'plumgrid': { | ||
4760 | 200 | 'config': '/etc/neutron/plugins/plumgrid/plumgrid.ini', | ||
4761 | 201 | 'driver': 'neutron.plugins.plumgrid.plumgrid_plugin.plumgrid_plugin.NeutronPluginPLUMgridV2', | ||
4762 | 202 | 'contexts': [ | ||
4763 | 203 | context.SharedDBContext(user=config('database-user'), | ||
4764 | 204 | database=config('database'), | ||
4765 | 205 | ssl_dir=NEUTRON_CONF_DIR)], | ||
4766 | 206 | 'services': [], | ||
4767 | 207 | 'packages': [['plumgrid-lxc'], | ||
4768 | 208 | ['iovisor-dkms']], | ||
4769 | 209 | 'server_packages': ['neutron-server', | ||
4770 | 210 | 'neutron-plugin-plumgrid'], | ||
4771 | 211 | 'server_services': ['neutron-server'] | ||
4772 | 212 | } | ||
4773 | 213 | } | ||
4774 | 214 | if release >= 'icehouse': | ||
4775 | 215 | # NOTE: patch in ml2 plugin for icehouse onwards | ||
4776 | 216 | plugins['ovs']['config'] = '/etc/neutron/plugins/ml2/ml2_conf.ini' | ||
4777 | 217 | plugins['ovs']['driver'] = 'neutron.plugins.ml2.plugin.Ml2Plugin' | ||
4778 | 218 | plugins['ovs']['server_packages'] = ['neutron-server', | ||
4779 | 219 | 'neutron-plugin-ml2'] | ||
4780 | 220 | # NOTE: patch in vmware renames nvp->nsx for icehouse onwards | ||
4781 | 221 | plugins['nvp'] = plugins['nsx'] | ||
4782 | 222 | return plugins | ||
4783 | 223 | |||
4784 | 224 | |||
4785 | 225 | def neutron_plugin_attribute(plugin, attr, net_manager=None): | ||
4786 | 226 | manager = net_manager or network_manager() | ||
4787 | 227 | if manager == 'quantum': | ||
4788 | 228 | plugins = quantum_plugins() | ||
4789 | 229 | elif manager == 'neutron': | ||
4790 | 230 | plugins = neutron_plugins() | ||
4791 | 231 | else: | ||
4792 | 232 | log("Network manager '%s' does not support plugins." % (manager), | ||
4793 | 233 | level=ERROR) | ||
4794 | 234 | raise Exception | ||
4795 | 235 | |||
4796 | 236 | try: | ||
4797 | 237 | _plugin = plugins[plugin] | ||
4798 | 238 | except KeyError: | ||
4799 | 239 | log('Unrecognised plugin for %s: %s' % (manager, plugin), level=ERROR) | ||
4800 | 240 | raise Exception | ||
4801 | 241 | |||
4802 | 242 | try: | ||
4803 | 243 | return _plugin[attr] | ||
4804 | 244 | except KeyError: | ||
4805 | 245 | return None | ||
4806 | 246 | |||
4807 | 247 | |||
4808 | 248 | def network_manager(): | ||
4809 | 249 | ''' | ||
4810 | 250 | Deals with the renaming of Quantum to Neutron in H and any situations | ||
4811 | 251 | that require compatability (eg, deploying H with network-manager=quantum, | ||
4812 | 252 | upgrading from G). | ||
4813 | 253 | ''' | ||
4814 | 254 | release = os_release('nova-common') | ||
4815 | 255 | manager = config('network-manager').lower() | ||
4816 | 256 | |||
4817 | 257 | if manager not in ['quantum', 'neutron']: | ||
4818 | 258 | return manager | ||
4819 | 259 | |||
4820 | 260 | if release in ['essex']: | ||
4821 | 261 | # E does not support neutron | ||
4822 | 262 | log('Neutron networking not supported in Essex.', level=ERROR) | ||
4823 | 263 | raise Exception | ||
4824 | 264 | elif release in ['folsom', 'grizzly']: | ||
4825 | 265 | # neutron is named quantum in F and G | ||
4826 | 266 | return 'quantum' | ||
4827 | 267 | else: | ||
4828 | 268 | # ensure accurate naming for all releases post-H | ||
4829 | 269 | return 'neutron' | ||
4830 | 270 | |||
4831 | 271 | |||
4832 | 272 | def parse_mappings(mappings, key_rvalue=False): | ||
4833 | 273 | """By default mappings are lvalue keyed. | ||
4834 | 274 | |||
4835 | 275 | If key_rvalue is True, the mapping will be reversed to allow multiple | ||
4836 | 276 | configs for the same lvalue. | ||
4837 | 277 | """ | ||
4838 | 278 | parsed = {} | ||
4839 | 279 | if mappings: | ||
4840 | 280 | mappings = mappings.split() | ||
4841 | 281 | for m in mappings: | ||
4842 | 282 | p = m.partition(':') | ||
4843 | 283 | |||
4844 | 284 | if key_rvalue: | ||
4845 | 285 | key_index = 2 | ||
4846 | 286 | val_index = 0 | ||
4847 | 287 | # if there is no rvalue skip to next | ||
4848 | 288 | if not p[1]: | ||
4849 | 289 | continue | ||
4850 | 290 | else: | ||
4851 | 291 | key_index = 0 | ||
4852 | 292 | val_index = 2 | ||
4853 | 293 | |||
4854 | 294 | key = p[key_index].strip() | ||
4855 | 295 | parsed[key] = p[val_index].strip() | ||
4856 | 296 | |||
4857 | 297 | return parsed | ||
4858 | 298 | |||
4859 | 299 | |||
4860 | 300 | def parse_bridge_mappings(mappings): | ||
4861 | 301 | """Parse bridge mappings. | ||
4862 | 302 | |||
4863 | 303 | Mappings must be a space-delimited list of provider:bridge mappings. | ||
4864 | 304 | |||
4865 | 305 | Returns dict of the form {provider:bridge}. | ||
4866 | 306 | """ | ||
4867 | 307 | return parse_mappings(mappings) | ||
4868 | 308 | |||
4869 | 309 | |||
4870 | 310 | def parse_data_port_mappings(mappings, default_bridge='br-data'): | ||
4871 | 311 | """Parse data port mappings. | ||
4872 | 312 | |||
4873 | 313 | Mappings must be a space-delimited list of port:bridge mappings. | ||
4874 | 314 | |||
4875 | 315 | Returns dict of the form {port:bridge} where port may be an mac address or | ||
4876 | 316 | interface name. | ||
4877 | 317 | """ | ||
4878 | 318 | |||
4879 | 319 | # NOTE(dosaboy): we use rvalue for key to allow multiple values to be | ||
4880 | 320 | # proposed for <port> since it may be a mac address which will differ | ||
4881 | 321 | # across units this allowing first-known-good to be chosen. | ||
4882 | 322 | _mappings = parse_mappings(mappings, key_rvalue=True) | ||
4883 | 323 | if not _mappings or list(_mappings.values()) == ['']: | ||
4884 | 324 | if not mappings: | ||
4885 | 325 | return {} | ||
4886 | 326 | |||
4887 | 327 | # For backwards-compatibility we need to support port-only provided in | ||
4888 | 328 | # config. | ||
4889 | 329 | _mappings = {mappings.split()[0]: default_bridge} | ||
4890 | 330 | |||
4891 | 331 | ports = _mappings.keys() | ||
4892 | 332 | if len(set(ports)) != len(ports): | ||
4893 | 333 | raise Exception("It is not allowed to have the same port configured " | ||
4894 | 334 | "on more than one bridge") | ||
4895 | 335 | |||
4896 | 336 | return _mappings | ||
4897 | 337 | |||
4898 | 338 | |||
4899 | 339 | def parse_vlan_range_mappings(mappings): | ||
4900 | 340 | """Parse vlan range mappings. | ||
4901 | 341 | |||
4902 | 342 | Mappings must be a space-delimited list of provider:start:end mappings. | ||
4903 | 343 | |||
4904 | 344 | The start:end range is optional and may be omitted. | ||
4905 | 345 | |||
4906 | 346 | Returns dict of the form {provider: (start, end)}. | ||
4907 | 347 | """ | ||
4908 | 348 | _mappings = parse_mappings(mappings) | ||
4909 | 349 | if not _mappings: | ||
4910 | 350 | return {} | ||
4911 | 351 | |||
4912 | 352 | mappings = {} | ||
4913 | 353 | for p, r in six.iteritems(_mappings): | ||
4914 | 354 | mappings[p] = tuple(r.split(':')) | ||
4915 | 355 | |||
4916 | 356 | return mappings | ||
4917 | 0 | 357 | ||
4918 | === added directory 'charmhelpers/contrib/openstack/templates' | |||
4919 | === added file 'charmhelpers/contrib/openstack/templates/__init__.py' | |||
4920 | --- charmhelpers/contrib/openstack/templates/__init__.py 1970-01-01 00:00:00 +0000 | |||
4921 | +++ charmhelpers/contrib/openstack/templates/__init__.py 2015-10-08 20:44:37 +0000 | |||
4922 | @@ -0,0 +1,18 @@ | |||
4923 | 1 | # Copyright 2014-2015 Canonical Limited. | ||
4924 | 2 | # | ||
4925 | 3 | # This file is part of charm-helpers. | ||
4926 | 4 | # | ||
4927 | 5 | # charm-helpers is free software: you can redistribute it and/or modify | ||
4928 | 6 | # it under the terms of the GNU Lesser General Public License version 3 as | ||
4929 | 7 | # published by the Free Software Foundation. | ||
4930 | 8 | # | ||
4931 | 9 | # charm-helpers is distributed in the hope that it will be useful, | ||
4932 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
4933 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
4934 | 12 | # GNU Lesser General Public License for more details. | ||
4935 | 13 | # | ||
4936 | 14 | # You should have received a copy of the GNU Lesser General Public License | ||
4937 | 15 | # along with charm-helpers. If not, see <http://www.gnu.org/licenses/>. | ||
4938 | 16 | |||
4939 | 17 | # dummy __init__.py to fool syncer into thinking this is a syncable python | ||
4940 | 18 | # module | ||
4941 | 0 | 19 | ||
4942 | === added file 'charmhelpers/contrib/openstack/templates/ceph.conf' | |||
4943 | --- charmhelpers/contrib/openstack/templates/ceph.conf 1970-01-01 00:00:00 +0000 | |||
4944 | +++ charmhelpers/contrib/openstack/templates/ceph.conf 2015-10-08 20:44:37 +0000 | |||
4945 | @@ -0,0 +1,21 @@ | |||
4946 | 1 | ############################################################################### | ||
4947 | 2 | # [ WARNING ] | ||
4948 | 3 | # cinder configuration file maintained by Juju | ||
4949 | 4 | # local changes may be overwritten. | ||
4950 | 5 | ############################################################################### | ||
4951 | 6 | [global] | ||
4952 | 7 | {% if auth -%} | ||
4953 | 8 | auth_supported = {{ auth }} | ||
4954 | 9 | keyring = /etc/ceph/$cluster.$name.keyring | ||
4955 | 10 | mon host = {{ mon_hosts }} | ||
4956 | 11 | {% endif -%} | ||
4957 | 12 | log to syslog = {{ use_syslog }} | ||
4958 | 13 | err to syslog = {{ use_syslog }} | ||
4959 | 14 | clog to syslog = {{ use_syslog }} | ||
4960 | 15 | |||
4961 | 16 | [client] | ||
4962 | 17 | {% if rbd_client_cache_settings -%} | ||
4963 | 18 | {% for key, value in rbd_client_cache_settings.iteritems() -%} | ||
4964 | 19 | {{ key }} = {{ value }} | ||
4965 | 20 | {% endfor -%} | ||
4966 | 21 | {%- endif %} | ||
4967 | 0 | \ No newline at end of file | 22 | \ No newline at end of file |
4968 | 1 | 23 | ||
4969 | === added file 'charmhelpers/contrib/openstack/templates/git.upstart' | |||
4970 | --- charmhelpers/contrib/openstack/templates/git.upstart 1970-01-01 00:00:00 +0000 | |||
4971 | +++ charmhelpers/contrib/openstack/templates/git.upstart 2015-10-08 20:44:37 +0000 | |||
4972 | @@ -0,0 +1,17 @@ | |||
4973 | 1 | description "{{ service_description }}" | ||
4974 | 2 | author "Juju {{ service_name }} Charm <juju@localhost>" | ||
4975 | 3 | |||
4976 | 4 | start on runlevel [2345] | ||
4977 | 5 | stop on runlevel [!2345] | ||
4978 | 6 | |||
4979 | 7 | respawn | ||
4980 | 8 | |||
4981 | 9 | exec start-stop-daemon --start --chuid {{ user_name }} \ | ||
4982 | 10 | --chdir {{ start_dir }} --name {{ process_name }} \ | ||
4983 | 11 | --exec {{ executable_name }} -- \ | ||
4984 | 12 | {% for config_file in config_files -%} | ||
4985 | 13 | --config-file={{ config_file }} \ | ||
4986 | 14 | {% endfor -%} | ||
4987 | 15 | {% if log_file -%} | ||
4988 | 16 | --log-file={{ log_file }} | ||
4989 | 17 | {% endif -%} | ||
4990 | 0 | 18 | ||
4991 | === added file 'charmhelpers/contrib/openstack/templates/haproxy.cfg' | |||
4992 | --- charmhelpers/contrib/openstack/templates/haproxy.cfg 1970-01-01 00:00:00 +0000 | |||
4993 | +++ charmhelpers/contrib/openstack/templates/haproxy.cfg 2015-10-08 20:44:37 +0000 | |||
4994 | @@ -0,0 +1,58 @@ | |||
4995 | 1 | global | ||
4996 | 2 | log {{ local_host }} local0 | ||
4997 | 3 | log {{ local_host }} local1 notice | ||
4998 | 4 | maxconn 20000 | ||
4999 | 5 | user haproxy | ||
5000 | 6 | group haproxy |
The diff has been truncated for viewing.
Felipe, thanks for the submission! A few comments inline below, thanks!