4197519a3a91b9bdf482c7477266305ec54a1fd0
[genesis.git] / fuel / deploy / cloud_deploy / cloud_deploy.py
1 import os
2 import io
3 import yaml
4
5 from cloud import common
6 from cloud.dea import DeploymentEnvironmentAdapter
7 from hardware_adapters.dha import DeploymentHardwareAdapter
8 from ssh_client import SSHClient
9
10 exec_cmd = common.exec_cmd
11 err = common.err
12 check_file_exists = common.check_file_exists
13 LOG = common.LOG
14
15 class CloudDeploy(object):
16
17     def __init__(self, fuel_ip, fuel_username, fuel_password):
18         self.fuel_ip = fuel_ip
19         self.fuel_username = fuel_username
20         self.fuel_password = fuel_password
21         self.shelf_blades_dict = {}
22         self.macs_per_shelf_dict = {}
23
24     def copy_to_fuel_master(self, dir_path=None, file_path=None, target='~'):
25         if dir_path:
26             path = '-r ' + dir_path
27         elif file_path:
28             path = file_path
29         LOG.debug('Copying %s to Fuel Master %s' % (path, target))
30         if path:
31             exec_cmd('sshpass -p %s scp -o UserKnownHostsFile=/dev/null'
32                      ' -o StrictHostKeyChecking=no -o ConnectTimeout=15'
33                      ' %s %s@%s:%s'
34                      % (self.fuel_password, path, self.fuel_username,
35                         self.fuel_ip, target))
36
37     def run_cloud_deploy(self, deploy_dir, deploy_app):
38         LOG.debug('START CLOUD DEPLOYMENT')
39         ssh = SSHClient(self.fuel_ip, self.fuel_username, self.fuel_password)
40         ssh.open()
41         ssh.run('python %s/%s' % (deploy_dir, deploy_app))
42         ssh.close()
43
44     def power_off_blades(self, dea):
45         for shelf, blade_list in self.shelf_blades_dict.iteritems():
46             type, mgmt_ip, username, password = dea.get_shelf_info(shelf)
47             dha = DeploymentHardwareAdapter(type, mgmt_ip, username, password)
48             dha.power_off_blades(shelf, blade_list)
49
50     def power_on_blades(self, dea):
51         for shelf, blade_list in self.shelf_blades_dict.iteritems():
52             type, mgmt_ip, username, password = dea.get_shelf_info(shelf)
53             dha = DeploymentHardwareAdapter(type, mgmt_ip, username, password)
54             dha.power_on_blades(shelf, blade_list)
55
56     def set_boot_order(self, dea):
57         for shelf, blade_list in self.shelf_blades_dict.iteritems():
58             type, mgmt_ip, username, password = dea.get_shelf_info(shelf)
59             dha = DeploymentHardwareAdapter(type, mgmt_ip, username, password)
60             dha.set_boot_order_blades(shelf, blade_list)
61
62     def get_mac_addresses(self, dea, macs_yaml):
63         self.macs_per_shelf_dict = {}
64         for shelf, blade_list in self.shelf_blades_dict.iteritems():
65             type, mgmt_ip, username, password = dea.get_shelf_info(shelf)
66             dha = DeploymentHardwareAdapter(type, mgmt_ip, username, password)
67             self.macs_per_shelf_dict[shelf] = dha.get_blades_mac_addresses(
68                 shelf, blade_list)
69
70         with io.open(macs_yaml, 'w') as stream:
71             yaml.dump(self.macs_per_shelf_dict, stream,
72                       default_flow_style=False)
73
74     def collect_blade_ids_per_shelves(self, dea):
75         self.shelf_blades_dict = dea.get_blade_ids_per_shelves()
76
77
78
79 def main():
80
81     fuel_ip = '10.20.0.2'
82     fuel_username = 'root'
83     fuel_password = 'r00tme'
84     deploy_dir = '~/cloud'
85
86     cloud = CloudDeploy(fuel_ip, fuel_username, fuel_password)
87
88     base_dir = os.path.dirname(os.path.realpath(__file__))
89     deployment_dir = base_dir + '/cloud'
90     macs_yaml = base_dir + '/macs.yaml'
91     dea_yaml = base_dir + '/dea.yaml'
92     check_file_exists(dea_yaml)
93
94     cloud.copy_to_fuel_master(dir_path=deployment_dir)
95     cloud.copy_to_fuel_master(file_path=dea_yaml, target=deploy_dir)
96
97     dea = DeploymentEnvironmentAdapter()
98     dea.parse_yaml(dea_yaml)
99
100     cloud.collect_blade_ids_per_shelves(dea)
101
102     cloud.power_off_blades(dea)
103
104     cloud.set_boot_order(dea)
105
106     cloud.power_on_blades(dea)
107
108     cloud.get_mac_addresses(dea, macs_yaml)
109     check_file_exists(dea_yaml)
110
111     cloud.copy_to_fuel_master(file_path=macs_yaml, target=deploy_dir)
112
113     cloud.run_cloud_deploy(deploy_dir, 'deploy.py')
114
115
116 if __name__ == '__main__':
117     main()