-
Notifications
You must be signed in to change notification settings - Fork 656
/
openstackdriver.py
340 lines (288 loc) · 12.1 KB
/
openstackdriver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# Copyright (c) 2015-2016 Cisco Systems, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
import collections
import hashlib
import os
import socket
import sys
import time
import paramiko
try:
import shade
except ImportError: # pragma: no cover
sys.exit('ERROR: Driver missing, install shade.')
from molecule import util
from molecule.driver import basedriver
class OpenstackDriver(basedriver.BaseDriver):
def __init__(self, molecule):
super(OpenstackDriver, self).__init__(molecule)
self._provider = self._get_provider()
self._platform = self._get_platform()
self._openstack = shade.openstack_cloud()
@property
def name(self):
return 'openstack'
@property
def instances(self):
return self.molecule.config.config['openstack']['instances']
@property
def default_provider(self):
return self._provider
@property
def default_platform(self):
return self._platform
@property
def provider(self):
return self._provider
@property
def platform(self):
return self._platform
@platform.setter
def platform(self, val):
self._platform = val
@property
def valid_providers(self):
return [{'name': self.provider}]
@property
def valid_platforms(self):
return [{'name': self.platform}]
@property
def ssh_config_file(self):
return
@property
def ansible_connection_params(self):
return {'connection': 'ssh'}
@property
def testinfra_args(self):
return {
'ansible_inventory':
self.molecule.config.config['ansible']['inventory_file'],
'connection': 'ansible'
}
@property
def serverspec_args(self):
return {}
def up(self, no_provision=True):
self.molecule.state.change_state('driver', self.name)
kpn = self._get_keypair()
active_instances = self._openstack.list_servers()
active_instance_names = {
instance['name']: instance['status']
for instance in active_instances
}
util.print_warn('Creating openstack instances...')
for instance in self.instances:
if instance['name'] not in active_instance_names:
msg = '\tBringing up {}...'.format(instance['name'])
util.print_info(msg)
server = self._openstack.create_server(
name=instance['name'],
image=self._openstack.get_image(instance['image']),
flavor=self._openstack.get_flavor(instance['flavor']),
auto_ip=True,
wait=True,
key_name=kpn,
security_groups=instance['security_groups']
if 'security_groups' in instance else None)
self._reset_known_host_key(server['interface_ip'])
instance['created'] = True
num_retries = 0
while not self._check_ssh_availability(
server['interface_ip'],
instance['sshuser'],
timeout=6,
sshkey_filename=self._get_keyfile(
)) or num_retries == 5:
util.print_info('\t Waiting for ssh availability...')
num_retries += 1
def destroy(self):
util.print_info('Deleting openstack instances...')
active_instances = self._openstack.list_servers()
active_instance_names = {
instance['name']: instance['id']
for instance in active_instances
}
for instance in self.instances:
util.print_warn('\tRemoving {}...'.format(instance['name']))
if instance['name'] in active_instance_names:
if not self._openstack.delete_server(
active_instance_names[instance['name']], wait=True):
msg = 'Unable to remove {}.'.format(instance['name'])
util.print_error(msg)
else:
util.print_success('\tRemoved {}.'.format(instance[
'name']))
instance['created'] = False
# cleanup any molecule generated ssh keysfiles
self._cleanup_temp_keypair()
self._cleanup_temp_keyfile()
def status(self):
Status = collections.namedtuple('Status',
['name', 'state', 'provider'])
status_list = []
for instance in self.instances:
if self._instance_is_accessible(instance):
status_list.append(
Status(
name=instance['name'],
state='UP',
provider=self.provider))
else:
status_list.append(
Status(
name=instance['name'],
state='not_created',
provider=self.provider))
return status_list
def conf(self, name=None, ssh_config=False):
inventory_file = self.molecule.config.config['ansible'][
'inventory_file']
if os.path.exists(inventory_file):
with open(inventory_file) as stream:
for line in stream:
if len(line.split()) > 0 and line.split()[0] == name:
ansible_host = line.split()[1]
host_address = ansible_host.split('=')[1]
return host_address
return
def inventory_entry(self, instance):
template = self._host_template()
for server in self._openstack.list_servers(detailed=False):
if server['name'] == instance['name']:
server_config = {
'hostname': instance['name'],
'interface_ip_address': server['interface_ip'],
'ssh_username': instance['sshuser'],
'ssh_key_filename': self._get_keyfile()
}
return template.format(**server_config)
return ''
def login_cmd(self, instance_name):
return 'ssh {} -l {} -i {}'
def login_args(self, instance_name):
# Try to retrieve the SSH configuration of the host.
conf = self.conf(name=instance_name)
user = ''
keyfile = self._get_keyfile()
for instance in self.instances:
if instance_name == instance['name']:
user = instance['sshuser']
return [conf, user, keyfile]
def _get_provider(self):
return 'openstack'
def _get_platform(self):
return 'openstack'
def _get_keypair(self):
if ('keypair' in self.molecule.config.config['openstack']):
return self.molecule.config.config['openstack']['keypair']
else:
return self._get_temp_keypair()
def _get_keyfile(self):
if ('keyfile' in self.molecule.config.config['openstack']):
return os.path.expanduser(self.molecule.config.config['openstack'][
'keyfile'])
else:
return self._get_temp_keyfile()
def _get_temp_keypair(self):
kpn = self._get_temp_keyname()
if not self._openstack.search_keypairs(kpn):
msg = '\tCreating openstack keypair {}...'.format(kpn)
util.print_info(msg)
pub_key_file = self._get_keyfile() + '.pub'
self._openstack.create_keypair(kpn,
open(pub_key_file,
'r').read().strip())
return kpn
def _get_temp_keyfile(self):
kn = self._get_temp_keyname()
kl = self._get_temp_keylocation()
pvtloc = kl + '/' + kn
publoc = kl + '/' + kn + '.pub'
if not os.path.exists(pvtloc):
util.print_info('\tCreating local ssh key {}...'.format(pvtloc))
k = paramiko.RSAKey.generate(2048)
k.write_private_key_file(pvtloc)
# write the public key too
pub = paramiko.RSAKey(filename=pvtloc)
with open(publoc, 'w') as f:
f.write("%s %s" % (pub.get_name(), pub.get_base64()))
return pvtloc
def _get_temp_keyname(self):
mpath = os.path.abspath(self.molecule.config.config['molecule'][
'molecule_dir'])
return 'molecule_' + hashlib.sha256(mpath).hexdigest()[:10]
def _get_temp_keylocation(self):
loc = self.molecule.config.config['molecule']['molecule_dir'] + '/.ssh'
if not os.path.exists(loc):
os.makedirs(loc)
return os.path.abspath(loc)
def _reset_known_host_key(self, hostname):
return os.system('ssh-keygen -R {}'.format(hostname))
def _check_ssh_availability(self, hostip, user, timeout, sshkey_filename):
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(hostip, username=user, key_filename=sshkey_filename)
return True
except (paramiko.BadHostKeyException, paramiko.AuthenticationException,
paramiko.SSHException, socket.error):
time.sleep(timeout)
return False
def _cleanup_temp_keypair(self):
# if we don't have a keypair config, delete the temp one
if ('keypair' not in self.molecule.config.config['openstack']):
kpn = self._get_temp_keyname()
if self._openstack.search_keypairs(kpn):
msg = '\tRemoving openstack keypair {}...'.format(kpn)
util.print_warn(msg)
if not self._openstack.delete_keypair(kpn):
msg = 'Unable to remove openstack keypair {}.'.format(kpn)
util.print_error(msg)
else:
msg = '\tRemoved openstack keypair {}.'.format(kpn)
util.print_success(msg)
def _cleanup_temp_keyfile(self):
# if we don't have a keyfile config, delete the temp one
if ('keyfile' not in self.molecule.config.config['openstack']):
kn = self._get_temp_keyname()
kl = self._get_temp_keylocation()
pvtloc = kl + '/' + kn
publoc = kl + '/' + kn + '.pub'
if os.path.exists(pvtloc):
util.print_warn('\tRemoving {}...'.format(pvtloc))
os.remove(pvtloc)
if os.path.exists(publoc):
util.print_warn('\tRemoving {}...'.format(publoc))
os.remove(publoc)
def _host_template(self):
return ('{hostname} ansible_ssh_host={interface_ip_address} '
'ansible_ssh_user={ssh_username} '
'ansible_ssh_private_key_file={ssh_key_filename} '
'ansible_ssh_extra_args="-o ConnectionAttempts=5"\n')
def _instance_is_accessible(self, instance):
instance_ip = self.conf(instance['name'])
if instance_ip is not None:
return self._check_ssh_availability(
instance_ip,
instance['sshuser'],
timeout=0,
sshkey_filename=self._get_keyfile())
return False