Automatic Deployment
[genesis.git] / fuel / deploy / cloud_deploy / cloud / deployment.py
1 import common
2 import os
3 import shutil
4 import glob
5 import yaml
6 import io
7 import time
8
9 N = common.N
10 E = common.E
11 R = common.R
12 RO = common.RO
13 exec_cmd = common.exec_cmd
14 run_proc = common.run_proc
15 parse = common.parse
16 err = common.err
17 LOG = common.LOG
18
19
20 class Deployment(object):
21
22     def __init__(self, dea, yaml_config_dir, env_id, node_id_roles_dict):
23         self.dea = dea
24         self.env_name = dea.get_environment_name()
25         self.yaml_config_dir = yaml_config_dir
26         self.env_id = env_id
27         self.node_id_roles_dict = node_id_roles_dict
28         self.node_id_list = []
29         for node_id in self.node_id_roles_dict.iterkeys():
30             self.node_id_list.append(node_id)
31         self.node_id_list.sort()
32
33     def download_deployment_info(self):
34         LOG.debug('Download deployment info for environment %s\n' % self.env_id)
35         deployment_dir = self.yaml_config_dir + '/deployment_%s' % self.env_id
36         if os.path.exists(deployment_dir):
37             shutil.rmtree(deployment_dir)
38         r, c = exec_cmd('fuel --env %s deployment --default --dir %s'
39                         % (self.env_id, self.yaml_config_dir))
40         if c > 0:
41             err('Error: Could not download deployment info for env %s,'
42                 ' reason: %s\n' % (self.env_id, r))
43
44     def upload_deployment_info(self):
45         LOG.debug('Upload deployment info for environment %s\n' % self.env_id)
46         r, c = exec_cmd('fuel --env %s deployment --upload --dir %s'
47                         % (self.env_id, self.yaml_config_dir))
48         if c > 0:
49             err('Error: Could not upload deployment info for env %s,'
50                 ' reason: %s\n' % (self.env_id, r))
51
52     def pre_deploy(self):
53         LOG.debug('Running pre-deploy on environment %s\n' % self.env_name)
54         self.download_deployment_info()
55         opnfv = {'opnfv': {}}
56
57         for node_file in glob.glob('%s/deployment_%s/*.yaml'
58                                    % (self.yaml_config_dir, self.env_id)):
59              with io.open(node_file) as stream:
60                  node = yaml.load(stream)
61
62              if 'opnfv' not in node:
63                  node.update(opnfv)
64
65              with io.open(node_file, 'w') as stream:
66                  yaml.dump(node, stream, default_flow_style=False)
67         self.upload_deployment_info()
68
69
70     def deploy(self):
71         WAIT_LOOP = 180
72         SLEEP_TIME = 60
73
74         self.pre_deploy()
75
76         log_file = 'cloud.log'
77
78         LOG.debug('Starting deployment of environment %s\n' % self.env_name)
79         run_proc('fuel --env %s deploy-changes | strings | tee %s'
80                  % (self.env_id, log_file))
81
82         ready = False
83         for i in range(WAIT_LOOP):
84             env = parse(exec_cmd('fuel env --env %s' % self.env_id))
85             LOG.debug('Environment status: %s\n' % env[0][E['status']])
86             r, _ = exec_cmd('tail -2 %s | head -1' % log_file)
87             if r:
88                 LOG.debug('%s\n' % r)
89             if env[0][E['status']] == 'operational':
90                 ready = True
91                 break
92             else:
93                 time.sleep(SLEEP_TIME)
94         exec_cmd('rm %s' % log_file)
95
96         if ready:
97             LOG.debug('Environment %s successfully deployed\n' % self.env_name)
98         else:
99             err('Deployment failed, environment %s is not operational\n'
100                 % self.env_name)