Merge "Fix network connection check issue"
[fuel.git] / deploy / cloud / deploy.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 yaml
12 import io
13
14 from dea import DeploymentEnvironmentAdapter
15 from configure_environment import ConfigureEnvironment
16 from deployment import Deployment
17
18 from common import (
19     R,
20     exec_cmd,
21     parse,
22     check_file_exists,
23     commafy,
24     ArgParser,
25     log,
26 )
27
28 YAML_CONF_DIR = '/var/lib/opnfv'
29
30
31 class Deploy(object):
32
33     def __init__(self, dea_file, no_health_check, deploy_timeout,
34                  no_deploy_environment):
35         self.dea = DeploymentEnvironmentAdapter(dea_file)
36         self.no_health_check = no_health_check
37         self.deploy_timeout = deploy_timeout
38         self.no_deploy_environment = no_deploy_environment
39         self.macs_per_blade = {}
40         self.blades = self.dea.get_node_ids()
41         self.blade_node_dict = self.dea.get_blade_node_map()
42         self.node_roles_dict = {}
43         self.env_id = None
44         self.wanted_release = self.dea.get_property('wanted_release')
45
46     def assign_roles_to_cluster_node_ids(self):
47         self.node_roles_dict = {}
48         for blade, node in self.blade_node_dict.iteritems():
49             if self.dea.get_node_roles(blade):
50                 roles = commafy(self.dea.get_node_roles(blade))
51                 self.node_roles_dict[node] = (roles, blade)
52
53     def configure_environment(self):
54         release_list = parse(exec_cmd('fuel release -l'))
55         for release in release_list:
56             if release[R['name']] == self.wanted_release:
57                 break
58         config_env = ConfigureEnvironment(self.dea, YAML_CONF_DIR,
59                                           release[R['id']],
60                                           self.node_roles_dict)
61         config_env.configure_environment()
62         self.env_id = config_env.env_id
63
64     def deploy_cloud(self):
65         dep = Deployment(self.dea, YAML_CONF_DIR, self.env_id,
66                          self.node_roles_dict, self.no_health_check,
67                          self.deploy_timeout)
68         if not self.no_deploy_environment:
69             dep.deploy()
70         else:
71             log('Configuration is done. Deployment is not launched.')
72
73     def deploy(self):
74
75         self.assign_roles_to_cluster_node_ids()
76
77         self.configure_environment()
78
79         self.deploy_cloud()
80
81
82 def parse_arguments():
83     parser = ArgParser(prog='python %s' % __file__)
84     parser.add_argument('-nh', dest='no_health_check', action='store_true',
85                         default=False,
86                         help='Don\'t run health check after deployment')
87     parser.add_argument('-dt', dest='deploy_timeout', action='store',
88                         default=240, help='Deployment timeout (in minutes) '
89                         '[default: 240]')
90     parser.add_argument('-nde', dest='no_deploy_environment',
91                         action='store_true', default=False,
92                         help=('Do not launch environment deployment'))
93     parser.add_argument('dea_file', action='store',
94                         help='Deployment Environment Adapter: dea.yaml')
95
96     args = parser.parse_args()
97     check_file_exists(args.dea_file)
98
99     kwargs = {'dea_file': args.dea_file,
100               'no_health_check': args.no_health_check,
101               'deploy_timeout': args.deploy_timeout,
102               'no_deploy_environment': args.no_deploy_environment}
103     return kwargs
104
105
106 def main():
107     kwargs = parse_arguments()
108     deploy = Deploy(**kwargs)
109     deploy.deploy()
110
111 if __name__ == '__main__':
112     main()