Merge "Allows domain name to be configured"
[genesis.git] / fuel / 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 os
12 import yaml
13 import io
14 import glob
15
16 import common
17 from dea import DeploymentEnvironmentAdapter
18 from configure_environment import ConfigureEnvironment
19 from deployment import Deployment
20
21 YAML_CONF_DIR = '/var/lib/opnfv'
22
23 N = common.N
24 E = common.E
25 R = common.R
26 RO = common.RO
27 exec_cmd = common.exec_cmd
28 parse = common.parse
29 err = common.err
30 check_file_exists = common.check_file_exists
31 log = common.log
32 commafy = common.commafy
33 ArgParser = common.ArgParser
34
35
36 class Deploy(object):
37
38     def __init__(self, dea_file, blade_node_file, plugins_dir,
39                  no_health_check):
40         self.dea = DeploymentEnvironmentAdapter(dea_file)
41         self.blade_node_file = blade_node_file
42         self.plugins_dir = plugins_dir
43         self.no_health_check = no_health_check
44         self.macs_per_blade = {}
45         self.blades = self.dea.get_node_ids()
46         self.blade_node_dict = {}
47         self.node_roles_dict = {}
48         self.env_id = None
49         self.wanted_release = self.dea.get_property('wanted_release')
50
51     def get_blade_node_mapping(self):
52         with io.open(self.blade_node_file, 'r') as stream:
53             self.blade_node_dict = yaml.load(stream)
54
55     def assign_roles_to_cluster_node_ids(self):
56         self.node_roles_dict = {}
57         for blade, node in self.blade_node_dict.iteritems():
58             roles = commafy(self.dea.get_node_role(blade))
59             self.node_roles_dict[node] = (roles, blade)
60
61     def configure_environment(self):
62         release_list = parse(exec_cmd('fuel release -l'))
63         for release in release_list:
64             if release[R['name']] == self.wanted_release:
65                 break
66         config_env = ConfigureEnvironment(self.dea, YAML_CONF_DIR,
67                                           release[R['id']],
68                                           self.node_roles_dict)
69         config_env.configure_environment()
70         self.env_id = config_env.env_id
71
72     def deploy_cloud(self):
73         dep = Deployment(self.dea, YAML_CONF_DIR, self.env_id,
74                          self.node_roles_dict, self.no_health_check)
75         dep.deploy()
76
77     def install_plugins(self):
78         log('Installing Fuel Plugins')
79         if self.plugins_dir and os.path.isdir(self.plugins_dir):
80             for f in glob.glob('%s/*.rpm' % self.plugins_dir):
81                 log('Found plugin %s, installing ...' % f)
82                 r, c = exec_cmd('fuel plugins --install %s' % f, False)
83                 if c > 0 and 'does not update installed package' not in r:
84                     err('Installation of Fuel Plugin %s failed' % f)
85
86     def deploy(self):
87
88         self.install_plugins()
89
90         self.get_blade_node_mapping()
91
92         self.assign_roles_to_cluster_node_ids()
93
94         self.configure_environment()
95
96         self.deploy_cloud()
97
98
99 def parse_arguments():
100     parser = ArgParser(prog='python %s' % __file__)
101     parser.add_argument('-nh', dest='no_health_check', action='store_true',
102                         default=False,
103                         help='Don\'t run health check after deployment')
104     parser.add_argument('dea_file', action='store',
105                         help='Deployment Environment Adapter: dea.yaml')
106     parser.add_argument('blade_node_file', action='store',
107                         help='Blade Node mapping: blade_node.yaml')
108     parser.add_argument('plugins_dir', nargs='?', action='store',
109                         help='Plugins directory')
110     args = parser.parse_args()
111     check_file_exists(args.dea_file)
112     check_file_exists(args.blade_node_file)
113     return (args.dea_file, args.blade_node_file, args.plugins_dir,
114             args.no_health_check)
115
116
117 def main():
118
119     dea_file, blade_node_file, plugins_dir, no_health_check = parse_arguments()
120
121     deploy = Deploy(dea_file, blade_node_file, plugins_dir, no_health_check)
122     deploy.deploy()
123
124 if __name__ == '__main__':
125     main()