Corrected links associated with release docs. To be updated along with the previous...
[genesis.git] / fuel / deploy / deploy_env.py
1 import os
2 import io
3 import yaml
4 import glob
5
6 from ssh_client import SSHClient
7 import common
8
9 exec_cmd = common.exec_cmd
10 err = common.err
11 check_file_exists = common.check_file_exists
12 log = common.log
13
14 CLOUD_DEPLOY_FILE = 'deploy.py'
15
16
17 class CloudDeploy(object):
18
19     def __init__(self, dha, fuel_ip, fuel_username, fuel_password, dea_file,
20                  work_dir):
21         self.dha = dha
22         self.fuel_ip = fuel_ip
23         self.fuel_username = fuel_username
24         self.fuel_password = fuel_password
25         self.dea_file = dea_file
26         self.work_dir = work_dir
27         self.file_dir = os.path.dirname(os.path.realpath(__file__))
28         self.ssh = SSHClient(self.fuel_ip, self.fuel_username,
29                              self.fuel_password)
30         self.macs_file = '%s/macs.yaml' % self.file_dir
31         self.node_ids = self.dha.get_node_ids()
32
33     def upload_cloud_deployment_files(self):
34         dest ='~/%s/' % self.work_dir
35
36         with self.ssh as s:
37             s.exec_cmd('rm -rf %s' % self.work_dir, check=False)
38             s.exec_cmd('mkdir ~/%s' % self.work_dir)
39             s.scp_put(self.dea_file, dest)
40             s.scp_put(self.macs_file, dest)
41             s.scp_put('%s/common.py' % self.file_dir, dest)
42             s.scp_put('%s/dea.py' % self.file_dir, dest)
43             for f in glob.glob('%s/cloud/*' % self.file_dir):
44                 s.scp_put(f, dest)
45
46     def power_off_nodes(self):
47         for node_id in self.node_ids:
48             self.dha.node_power_off(node_id)
49
50     def power_on_nodes(self):
51         for node_id in self.node_ids:
52             self.dha.node_power_on(node_id)
53
54     def set_boot_order(self, boot_order_list):
55         for node_id in self.node_ids:
56             self.dha.node_set_boot_order(node_id, boot_order_list)
57
58     def get_mac_addresses(self):
59         macs_per_node = {}
60         for node_id in self.node_ids:
61             macs_per_node[node_id] = self.dha.get_node_pxe_mac(node_id)
62         with io.open(self.macs_file, 'w') as stream:
63             yaml.dump(macs_per_node, stream, default_flow_style=False)
64
65     def run_cloud_deploy(self, deploy_app):
66         log('START CLOUD DEPLOYMENT')
67         deploy_app = '%s/%s' % (self.work_dir, deploy_app)
68         dea_file = '%s/%s' % (self.work_dir, os.path.basename(self.dea_file))
69         macs_file = '%s/%s' % (self.work_dir, os.path.basename(self.macs_file))
70         with self.ssh:
71             self.ssh.run('python %s %s %s' % (deploy_app, dea_file, macs_file))
72
73     def deploy(self):
74
75         self.power_off_nodes()
76
77         self.set_boot_order(['pxe', 'disk'])
78
79         self.power_on_nodes()
80
81         self.get_mac_addresses()
82
83         check_file_exists(self.macs_file)
84
85         self.upload_cloud_deployment_files()
86
87         self.run_cloud_deploy(CLOUD_DEPLOY_FILE)