summaryrefslogtreecommitdiffstats
path: root/fuel/deploy/deploy_env.py
blob: 9bc8fbb34da0a988baa39ef0860c8571b0b872ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
import io
import yaml
import glob

from ssh_client import SSHClient
import common

exec_cmd = common.exec_cmd
err = common.err
check_file_exists = common.check_file_exists
log = common.log

CLOUD_DEPLOY_FILE = 'deploy.py'


class CloudDeploy(object):

    def __init__(self, dha, fuel_ip, fuel_username, fuel_password, dea_file,
                 work_dir):
        self.dha = dha
        self.fuel_ip = fuel_ip
        self.fuel_username = fuel_username
        self.fuel_password = fuel_password
        self.dea_file = dea_file
        self.work_dir = work_dir
        self.file_dir = os.path.dirname(os.path.realpath(__file__))
        self.ssh = SSHClient(self.fuel_ip, self.fuel_username,
                             self.fuel_password)
        self.macs_file = '%s/macs.yaml' % self.file_dir
        self.node_ids = self.dha.get_node_ids()

    def upload_cloud_deployment_files(self):
        dest ='~/%s/' % self.work_dir

        with self.ssh as s:
            s.exec_cmd('rm -rf %s' % self.work_dir, check=False)
            s.exec_cmd('mkdir ~/%s' % self.work_dir)
            s.scp_put(self.dea_file, dest)
            s.scp_put(self.macs_file, dest)
            s.scp_put('%s/common.py' % self.file_dir, dest)
            s.scp_put('%s/dea.py' % self.file_dir, dest)
            for f in glob.glob('%s/cloud/*' % self.file_dir):
                s.scp_put(f, dest)

    def power_off_nodes(self):
        for node_id in self.node_ids:
            self.dha.node_power_off(node_id)

    def power_on_nodes(self):
        for node_id in self.node_ids:
            self.dha.node_power_on(node_id)

    def set_boot_order(self, boot_order_list):
        for node_id in self.node_ids:
            self.dha.node_set_boot_order(node_id, boot_order_list)

    def get_mac_addresses(self):
        macs_per_node = {}
        for node_id in self.node_ids:
            macs_per_node[node_id] = self.dha.get_node_pxe_mac(node_id)
        with io.open(self.macs_file, 'w') as stream:
            yaml.dump(macs_per_node, stream, default_flow_style=False)

    def run_cloud_deploy(self, deploy_app):
        log('START CLOUD DEPLOYMENT')
        deploy_app = '%s/%s' % (self.work_dir, deploy_app)
        dea_file = '%s/%s' % (self.work_dir, os.path.basename(self.dea_file))
        macs_file = '%s/%s' % (self.work_dir, os.path.basename(self.macs_file))
        with self.ssh:
            self.ssh.run('python %s %s %s' % (deploy_app, dea_file, macs_file))

    def deploy(self):

        self.power_off_nodes()

        self.set_boot_order(['pxe', 'disk'])

        self.power_on_nodes()

        self.get_mac_addresses()

        check_file_exists(self.macs_file)

        self.upload_cloud_deployment_files()

        self.run_cloud_deploy(CLOUD_DEPLOY_FILE)