5 from hashlib import md5
6 from paramiko import RSAKey
7 from paramiko.ssh_exception import PasswordRequiredException
11 """ Utilities useful for constructing OpenStack HEAT requests """
13 def get_yaml(self, template_file):
14 """Template Yaml To Json reads a YAML Heat template file returns a JSON string that can be used included in an Openstack Add Stack Request"""
15 if isinstance(template_file, str) or isinstance(template_file, unicode):
16 fin = open(template_file, 'r')
17 yamlobj = yaml.load(fin)
21 def template_yaml_to_json(self, template_file):
22 """Template Yaml To Json reads a YAML Heat template file returns a JSON string that can be used included in an Openstack Add Stack Request"""
24 if isinstance(template_file, str) or isinstance(template_file, unicode):
25 fin = open(template_file, 'r')
26 yamlobj = yaml.load(fin)
28 if 'heat_template_version' in yamlobj:
29 datetime = yamlobj['heat_template_version']
30 yamlobj['heat_template_version'] = str(datetime)
32 json.dump(yamlobj, fout)
33 contents = fout.getvalue()
37 def env_yaml_to_json(self, template_file):
38 """Env Yaml To JSon reads a YAML Heat env file and returns a JSON string that can be used included in an Openstack Add Stack Request"""
39 if isinstance(template_file, str) or isinstance(template_file, unicode):
40 fin = open(template_file, 'r')
41 yamlobj = yaml.load(fin)
43 if 'parameters' in yamlobj:
45 json.dump(yamlobj['parameters'], fout)
46 contents = fout.getvalue()
51 def stack_info_parse(self, stack_info):
52 """ returns a flattened version of the Openstack Find Stack results """
54 if isinstance(stack_info, dict):
55 s = stack_info['stack']
59 d['name'] = s['stack_name']
60 d['stack_status'] = s['stack_status']
63 def match_fingerprint(self, pvt_file, pw, fingerprint):
65 sshKey = RSAKey.from_private_key_file(pvt_file, pw)
66 keybytes = md5(sshKey.asbytes()).hexdigest()
67 printableFingerprint = ':'.join(a + b for a, b in zip(keybytes[::2], keybytes[1::2]))
68 return printableFingerprint == fingerprint.__str__()
69 except PasswordRequiredException:
72 def match_private_key_file_to_keypair(self, files, keypair):
74 if (self.match_fingerprint(keyfile, None, keypair['keypair']['fingerprint'])):
78 def get_openstack_server_ip(self, server, network_name="public", ipversion=4):
81 versions = server['addresses'][network_name]
82 for version in versions:
83 if version['version'] == ipversion:
84 ipaddr = version['addr']