1 ###############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 # szilard.cserey@ericsson.com
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ###############################################################################
23 exec_cmd = common.exec_cmd
24 run_proc = common.run_proc
28 literal_unicode = common.literal_unicode
29 literal_unicode_representer = common.literal_unicode_representer
30 yaml.add_representer(literal_unicode, literal_unicode_representer)
31 backup = common.backup
34 class Deployment(object):
36 def __init__(self, dea, yaml_config_dir, env_id, node_id_roles_dict,
39 self.yaml_config_dir = yaml_config_dir
41 self.node_id_roles_dict = node_id_roles_dict
42 self.no_health_check = no_health_check
44 def download_deployment_info(self):
45 log('Download deployment info for environment %s' % self.env_id)
46 deployment_dir = ('%s/deployment_%s'
47 % (self.yaml_config_dir, self.env_id))
48 if os.path.exists(deployment_dir):
49 shutil.rmtree(deployment_dir)
50 exec_cmd('fuel deployment --env %s --download --dir %s'
51 % (self.env_id, self.yaml_config_dir))
53 def upload_deployment_info(self):
54 log('Upload deployment info for environment %s' % self.env_id)
55 exec_cmd('fuel --env %s deployment --upload --dir %s'
56 % (self.env_id, self.yaml_config_dir))
58 def __update_opnfv_dict(self, opnfv_dict, key, node_type, val):
60 if key not in opnfv_dict:
61 opnfv_dict.update({key: {}})
62 opnfv_dict[key].update({node_type: val})
64 def config_opnfv(self):
65 log('Configure OPNFV settings on environment %s' % self.env_id)
66 self.download_deployment_info()
69 dns_list = self.dea.get_dns_list()
70 host_list = self.dea.get_hosts()
72 ntp_list_for_controller = ''
73 for ntp in self.dea.get_ntp_list():
74 ntp_list_for_controller += 'server %s\n' % ntp
76 ntp_list_for_compute = ''
77 for controller_file in glob.glob(
78 '%s/deployment_%s/*controller*.yaml'
79 % (self.yaml_config_dir, self.env_id)):
80 with io.open(controller_file) as stream:
81 controller = yaml.load(stream)
82 ntp_list_for_compute += 'server %s\n' % controller['fqdn']
84 self.__update_opnfv_dict(
85 opnfv['opnfv'], 'dns', 'controller', dns_list[:])
86 self.__update_opnfv_dict(
87 opnfv['opnfv'], 'dns', 'compute', dns_list[:])
88 self.__update_opnfv_dict(
89 opnfv['opnfv'], 'ntp', 'controller',
90 literal_unicode(ntp_list_for_controller))
91 self.__update_opnfv_dict(
92 opnfv['opnfv'], 'ntp', 'compute',
93 literal_unicode(ntp_list_for_compute))
96 opnfv['opnfv'].update({'hosts': host_list})
98 for node_file in glob.glob('%s/deployment_%s/*.yaml'
99 % (self.yaml_config_dir, self.env_id)):
100 with io.open(node_file) as stream:
101 node = yaml.load(stream)
103 with io.open(node_file, 'w') as stream:
104 yaml.dump(node, stream, default_flow_style=False)
106 self.upload_deployment_info()
108 def run_deploy(self):
111 LOG_FILE = 'cloud.log'
113 log('Starting deployment of environment %s' % self.env_id)
114 run_proc('fuel --env %s deploy-changes | strings | tee %s'
115 % (self.env_id, LOG_FILE))
118 for i in range(WAIT_LOOP):
119 env = parse(exec_cmd('fuel env --env %s' % self.env_id))
120 log('Environment status: %s' % env[0][E['status']])
121 r, _ = exec_cmd('tail -2 %s | head -1' % LOG_FILE, False)
124 if env[0][E['status']] == 'operational':
127 elif (env[0][E['status']] == 'error'
128 or env[0][E['status']] == 'stopped'):
131 time.sleep(SLEEP_TIME)
132 exec_cmd('rm %s' % LOG_FILE)
135 log('Environment %s successfully deployed' % self.env_id)
137 err('Deployment failed, environment %s is not operational'
140 def verify_node_status(self):
141 node_list = parse(exec_cmd('fuel node list'))
143 for node in node_list:
144 if node[N['status']] != 'ready':
145 failed_nodes.append((node[N['id']], node[N['status']]))
149 for node, status in failed_nodes:
150 summary += '[node %s, status %s]\n' % (node, status)
151 err('Deployment failed: %s' % summary)
153 def health_check(self):
154 log('Now running sanity and smoke health checks')
155 r = exec_cmd('fuel health --env %s --check sanity,smoke --force'
159 err('Healthcheck failed!')
164 self.verify_node_status()
165 if not self.no_health_check: