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