7 from dea import DeploymentEnvironmentAdapter
8 from configure_environment import ConfigureEnvironment
9 from deployment import Deployment
11 YAML_CONF_DIR = '/var/lib/opnfv'
17 exec_cmd = common.exec_cmd
20 check_file_exists = common.check_file_exists
22 commafy = common.commafy
23 ArgParser = common.ArgParser
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
36 self.wanted_release = self.dea.get_wanted_release()
38 def cleanup_fuel_environments(self, 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'))
48 time.sleep(SLEEP_TIME)
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])
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'
62 exec_cmd('cobbler system remove --name node-%s'
63 % node[N['id']], False)
65 def check_previous_installation(self):
66 log('Check previous installation')
67 env_list = parse(exec_cmd('fuel env list'))
69 self.cleanup_fuel_environments(env_list)
70 node_list = parse(exec_cmd('fuel node list'))
72 self.cleanup_fuel_nodes(node_list)
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
81 if not self.supported_release:
82 err('This Fuel does not contain the following release: %s'
83 % self.wanted_release)
85 def check_prerequisites(self):
86 log('Check prerequisites')
87 self.check_supported_release()
88 self.check_previous_installation()
90 def get_mac_addresses(self):
91 with io.open(self.macs_file, 'r') as stream:
92 self.macs_per_blade = yaml.load(stream)
94 def find_mac_in_dict(self, mac):
95 for blade, mac_list in self.macs_per_blade.iteritems():
99 def all_blades_discovered(self):
100 for blade, node_id in self.node_ids_dict.iteritems():
105 def not_discovered_blades_summary(self):
107 for blade, node_id in self.node_ids_dict.iteritems():
109 summary += '\n[blade %s]' % blade
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']])
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']]
124 def discovery_waiting_loop(self, discovered_macs):
127 all_discovered = False
128 for i in range(WAIT_LOOP):
129 node_list = parse(exec_cmd('fuel node list'))
131 self.node_discovery(node_list, discovered_macs)
132 if self.all_blades_discovered():
133 all_discovered = True
136 time.sleep(SLEEP_TIME)
137 return all_discovered
139 def wait_for_discovered_blades(self):
140 log('Wait for discovered blades')
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())
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)
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
162 def deploy_cloud(self):
163 dep = Deployment(self.dea, YAML_CONF_DIR, self.env_id,
164 self.node_id_roles_dict)
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()
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)
188 dea_file, macs_file = parse_arguments()
190 deploy = Deploy(dea_file, macs_file)
193 if __name__ == '__main__':