Updates docs for SR1 with final revision
[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
29
30 class Deployment(object):
31
32     def __init__(self, dea, yaml_config_dir, env_id, node_id_roles_dict,
33                  no_health_check):
34         self.dea = dea
35         self.yaml_config_dir = yaml_config_dir
36         self.env_id = env_id
37         self.node_id_roles_dict = node_id_roles_dict
38         self.no_health_check = no_health_check
39
40     def run_deploy(self):
41         WAIT_LOOP = 180
42         SLEEP_TIME = 60
43         LOG_FILE = 'cloud.log'
44
45         log('Starting deployment of environment %s' % self.env_id)
46         run_proc('fuel --env %s deploy-changes | strings | tee %s'
47                  % (self.env_id, LOG_FILE))
48
49         ready = False
50         for i in range(WAIT_LOOP):
51             env = parse(exec_cmd('fuel env --env %s' % self.env_id))
52             log('Environment status: %s' % env[0][E['status']])
53             r, _ = exec_cmd('tail -2 %s | head -1' % LOG_FILE, False)
54             if r:
55                 log(r)
56             if env[0][E['status']] == 'operational':
57                 ready = True
58                 break
59             elif (env[0][E['status']] == 'error'
60                   or env[0][E['status']] == 'stopped'):
61                 break
62             else:
63                 time.sleep(SLEEP_TIME)
64         exec_cmd('rm %s' % LOG_FILE)
65
66         if ready:
67             log('Environment %s successfully deployed' % self.env_id)
68         else:
69             err('Deployment failed, environment %s is not operational'
70                 % self.env_id)
71
72     def verify_node_status(self):
73         node_list = parse(exec_cmd('fuel node list'))
74         failed_nodes = []
75         for node in node_list:
76             if node[N['status']] != 'ready':
77                 failed_nodes.append((node[N['id']], node[N['status']]))
78
79         if failed_nodes:
80             summary = ''
81             for node, status in failed_nodes:
82                 summary += '[node %s, status %s]\n' % (node, status)
83             err('Deployment failed: %s' % summary)
84
85     def health_check(self):
86         log('Now running sanity and smoke health checks')
87         r = exec_cmd('fuel health --env %s --check sanity,smoke --force'
88                      % self.env_id)
89         log(r)
90         if 'failure' in r:
91             err('Healthcheck failed!')
92
93     def deploy(self):
94         self.run_deploy()
95         self.verify_node_status()
96         if not self.no_health_check:
97             self.health_check()