Merge "Corrected links associated with release docs. To be updated along with the...
[genesis.git] / fuel / deploy / cloud / deploy.py
1 import time
2 import yaml
3 import io
4 import sys
5
6 import common
7 from dea import DeploymentEnvironmentAdapter
8 from configure_environment import ConfigureEnvironment
9 from deployment import Deployment
10
11 YAML_CONF_DIR = '/var/lib/opnfv'
12
13 N = common.N
14 E = common.E
15 R = common.R
16 RO = common.RO
17 exec_cmd = common.exec_cmd
18 parse = common.parse
19 err = common.err
20 check_file_exists = common.check_file_exists
21 log = common.log
22 commafy = common.commafy
23 ArgParser = common.ArgParser
24
25 class Deploy(object):
26
27     def __init__(self, dea_file, macs_file):
28         self.dea = DeploymentEnvironmentAdapter(dea_file)
29         self.macs_file = macs_file
30         self.macs_per_blade = {}
31         self.blades = self.dea.get_node_ids()
32         self.node_ids_dict = {}
33         self.node_id_roles_dict = {}
34         self.supported_release = None
35         self.env_id = None
36         self.wanted_release = self.dea.get_wanted_release()
37
38     def cleanup_fuel_environments(self, env_list):
39         WAIT_LOOP = 60
40         SLEEP_TIME = 10
41         for env in env_list:
42             log('Deleting environment %s' % env[E['id']])
43             exec_cmd('fuel env --env %s --delete' % env[E['id']])
44         all_env_erased = False
45         for i in range(WAIT_LOOP):
46             env_list = parse(exec_cmd('fuel env list'))
47             if env_list:
48                time.sleep(SLEEP_TIME)
49             else:
50                all_env_erased = True
51                break
52         if not all_env_erased:
53             err('Could not erase these environments %s'
54                 % [(env[E['id']], env[E['status']]) for env in env_list])
55
56     def cleanup_fuel_nodes(self, node_list):
57         for node in node_list:
58             if node[N['status']] == 'discover':
59                 log('Deleting node %s' % node[N['id']])
60                 exec_cmd('fuel node --node-id %s --delete-from-db'
61                          % node[N['id']])
62                 exec_cmd('cobbler system remove --name node-%s'
63                          % node[N['id']], False)
64
65     def check_previous_installation(self):
66         log('Check previous installation')
67         env_list = parse(exec_cmd('fuel env list'))
68         if env_list:
69             self.cleanup_fuel_environments(env_list)
70             node_list = parse(exec_cmd('fuel node list'))
71             if node_list:
72                 self.cleanup_fuel_nodes(node_list)
73
74     def check_supported_release(self):
75         log('Check supported release: %s' % self.wanted_release)
76         release_list = parse(exec_cmd('fuel release -l'))
77         for release in release_list:
78             if release[R['name']] == self.wanted_release:
79                 self.supported_release = release
80                 break
81         if not self.supported_release:
82             err('This Fuel does not contain the following release: %s'
83                 % self.wanted_release)
84
85     def check_prerequisites(self):
86         log('Check prerequisites')
87         self.check_supported_release()
88         self.check_previous_installation()
89
90     def get_mac_addresses(self):
91         with io.open(self.macs_file, 'r') as stream:
92             self.macs_per_blade = yaml.load(stream)
93
94     def find_mac_in_dict(self, mac):
95         for blade, mac_list in self.macs_per_blade.iteritems():
96             if mac in mac_list:
97                 return blade
98
99     def all_blades_discovered(self):
100         for blade, node_id in self.node_ids_dict.iteritems():
101             if not node_id:
102                 return False
103         return True
104
105     def not_discovered_blades_summary(self):
106         summary = ''
107         for blade, node_id in self.node_ids_dict.iteritems():
108             if not node_id:
109                 summary += '\n[blade %s]' % blade
110         return summary
111
112     def node_discovery(self, node_list, discovered_macs):
113         for node in node_list:
114             if (node[N['status']] == 'discover' and
115                 node[N['online']] == 'True' and
116                 node[N['mac']] not in discovered_macs):
117                 discovered_macs.append(node[N['mac']])
118                 blade = self.find_mac_in_dict(node[N['mac']])
119                 if blade:
120                     log('Blade %s discovered as Node %s with MAC %s'
121                         % (blade, node[N['id']], node[N['mac']]))
122                     self.node_ids_dict[blade] = node[N['id']]
123
124     def discovery_waiting_loop(self, discovered_macs):
125         WAIT_LOOP = 320
126         SLEEP_TIME = 10
127         all_discovered = False
128         for i in range(WAIT_LOOP):
129             node_list = parse(exec_cmd('fuel node list'))
130             if node_list:
131                 self.node_discovery(node_list, discovered_macs)
132             if self.all_blades_discovered():
133                 all_discovered = True
134                 break
135             else:
136                 time.sleep(SLEEP_TIME)
137         return all_discovered
138
139     def wait_for_discovered_blades(self):
140         log('Wait for discovered blades')
141         discovered_macs = []
142         for blade in self.blades:
143             self.node_ids_dict[blade] = None
144         all_discovered = self.discovery_waiting_loop(discovered_macs)
145         if not all_discovered:
146             err('Not all blades have been discovered: %s'
147                 % self.not_discovered_blades_summary())
148
149     def assign_roles_to_cluster_node_ids(self):
150         self.node_id_roles_dict = {}
151         for blade, node_id in self.node_ids_dict.iteritems():
152             roles = commafy(self.dea.get_node_role(blade))
153             self.node_id_roles_dict[node_id] = (roles, blade)
154
155     def configure_environment(self):
156         config_env = ConfigureEnvironment(self.dea, YAML_CONF_DIR,
157                                           self.supported_release[R['id']],
158                                           self.node_id_roles_dict)
159         config_env.configure_environment()
160         self.env_id = config_env.env_id
161
162     def deploy_cloud(self):
163         dep = Deployment(self.dea, YAML_CONF_DIR, self.env_id,
164                          self.node_id_roles_dict)
165         dep.deploy()
166
167     def deploy(self):
168         self.get_mac_addresses()
169         self.check_prerequisites()
170         self.wait_for_discovered_blades()
171         self.assign_roles_to_cluster_node_ids()
172         self.configure_environment()
173         self.deploy_cloud()
174
175 def parse_arguments():
176     parser = ArgParser(prog='python %s' % __file__)
177     parser.add_argument('dea_file', action='store',
178                         help='Deployment Environment Adapter: dea.yaml')
179     parser.add_argument('macs_file', action='store',
180                         help='Blade MAC addresses: macs.yaml')
181     args = parser.parse_args()
182     check_file_exists(args.dea_file)
183     check_file_exists(args.macs_file)
184     return (args.dea_file, args.macs_file)
185
186 def main():
187
188     dea_file, macs_file = parse_arguments()
189
190     deploy = Deploy(dea_file, macs_file)
191     deploy.deploy()
192
193 if __name__ == '__main__':
194     main()