Make number of cpus configurable in dha file
[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 )
26
27 YAML_CONF_DIR = '/var/lib/opnfv'
28
29
30 class Deploy(object):
31
32     def __init__(self, dea_file, no_health_check, deploy_timeout):
33         self.dea = DeploymentEnvironmentAdapter(dea_file)
34         self.no_health_check = no_health_check
35         self.deploy_timeout = deploy_timeout
36         self.macs_per_blade = {}
37         self.blades = self.dea.get_node_ids()
38         self.blade_node_dict = self.dea.get_blade_node_map()
39         self.node_roles_dict = {}
40         self.env_id = None
41         self.wanted_release = self.dea.get_property('wanted_release')
42
43     def assign_roles_to_cluster_node_ids(self):
44         self.node_roles_dict = {}
45         for blade, node in self.blade_node_dict.iteritems():
46             if self.dea.get_node_roles(blade):
47                 roles = commafy(self.dea.get_node_roles(blade))
48                 self.node_roles_dict[node] = (roles, blade)
49
50     def configure_environment(self):
51         release_list = parse(exec_cmd('fuel release -l'))
52         for release in release_list:
53             if release[R['name']] == self.wanted_release:
54                 break
55         config_env = ConfigureEnvironment(self.dea, YAML_CONF_DIR,
56                                           release[R['id']],
57                                           self.node_roles_dict)
58         config_env.configure_environment()
59         self.env_id = config_env.env_id
60
61     def deploy_cloud(self):
62         dep = Deployment(self.dea, YAML_CONF_DIR, self.env_id,
63                          self.node_roles_dict, self.no_health_check,
64                          self.deploy_timeout)
65         dep.deploy()
66
67     def deploy(self):
68
69         self.assign_roles_to_cluster_node_ids()
70
71         self.configure_environment()
72
73         self.deploy_cloud()
74
75
76 def parse_arguments():
77     parser = ArgParser(prog='python %s' % __file__)
78     parser.add_argument('-nh', dest='no_health_check', action='store_true',
79                         default=False,
80                         help='Don\'t run health check after deployment')
81     parser.add_argument('-dt', dest='deploy_timeout', action='store',
82                         default=240, help='Deployment timeout (in minutes) '
83                         '[default: 240]')
84     parser.add_argument('dea_file', action='store',
85                         help='Deployment Environment Adapter: dea.yaml')
86     args = parser.parse_args()
87     check_file_exists(args.dea_file)
88
89     kwargs = {'dea_file': args.dea_file,
90               'no_health_check': args.no_health_check,
91               'deploy_timeout': args.deploy_timeout}
92     return kwargs
93
94
95 def main():
96     kwargs = parse_arguments()
97     deploy = Deploy(**kwargs)
98     deploy.deploy()
99
100 if __name__ == '__main__':
101     main()