43bd4b6d44b7b68e763cdba79f8117b04d7e7673
[genesis.git] / fuel / deploy / cloud / deployment.py
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 ###############################################################################
9
10
11 import common
12 import os
13 import shutil
14 import glob
15 import yaml
16 import io
17 import time
18
19 N = common.N
20 E = common.E
21 R = common.R
22 RO = common.RO
23 exec_cmd = common.exec_cmd
24 run_proc = common.run_proc
25 parse = common.parse
26 err = common.err
27 log = common.log
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
32
33
34 class Deployment(object):
35
36     def __init__(self, dea, yaml_config_dir, env_id, node_id_roles_dict,
37                  no_health_check):
38         self.dea = dea
39         self.yaml_config_dir = yaml_config_dir
40         self.env_id = env_id
41         self.node_id_roles_dict = node_id_roles_dict
42         self.no_health_check = no_health_check
43
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))
52
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))
57
58     def __update_opnfv_dict(self, opnfv_dict, key, node_type, val):
59         if val:
60             if key not in opnfv_dict:
61                 opnfv_dict.update({key: {}})
62             opnfv_dict[key].update({node_type: val})
63
64     def config_opnfv(self):
65         log('Configure OPNFV settings on environment %s' % self.env_id)
66         self.download_deployment_info()
67
68         opnfv = {'opnfv': {}}
69         dns_list = self.dea.get_dns_list()
70         host_list = self.dea.get_hosts()
71
72         ntp_list_for_controller = ''
73         for ntp in self.dea.get_ntp_list():
74             ntp_list_for_controller += 'server %s\n' % ntp
75
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']
83
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))
94
95         if host_list:
96             opnfv['opnfv'].update({'hosts': host_list})
97
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)
102                 node.update(opnfv)
103             with io.open(node_file, 'w') as stream:
104                 yaml.dump(node, stream, default_flow_style=False)
105
106         self.upload_deployment_info()
107
108     def run_deploy(self):
109         WAIT_LOOP = 180
110         SLEEP_TIME = 60
111         LOG_FILE = 'cloud.log'
112
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))
116
117         ready = False
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)
122             if r:
123                 log(r)
124             if env[0][E['status']] == 'operational':
125                 ready = True
126                 break
127             elif (env[0][E['status']] == 'error'
128                   or env[0][E['status']] == 'stopped'):
129                 break
130             else:
131                 time.sleep(SLEEP_TIME)
132         exec_cmd('rm %s' % LOG_FILE)
133
134         if ready:
135             log('Environment %s successfully deployed' % self.env_id)
136         else:
137             err('Deployment failed, environment %s is not operational'
138                 % self.env_id)
139
140     def verify_node_status(self):
141         node_list = parse(exec_cmd('fuel node list'))
142         failed_nodes = []
143         for node in node_list:
144             if node[N['status']] != 'ready':
145                 failed_nodes.append((node[N['id']], node[N['status']]))
146
147         if failed_nodes:
148             summary = ''
149             for node, status in failed_nodes:
150                 summary += '[node %s, status %s]\n' % (node, status)
151             err('Deployment failed: %s' % summary)
152
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'
156                      % self.env_id)
157         log(r)
158         if 'failure' in r:
159             err('Healthcheck failed!')
160
161     def deploy(self):
162         self.config_opnfv()
163         self.run_deploy()
164         self.verify_node_status()
165         if not self.no_health_check:
166             self.health_check()