summaryrefslogtreecommitdiffstats
path: root/deploy/tempest.py
blob: 011f1e5c0ac00166c0caf25e9a1cdc398defffc0 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/python
##############################################################################
# Copyright (c) 2016 ZTE Coreporation and others.
# hu.zhijiang@zte.com.cn
# lu.yao135@zte.com.cn
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
from oslo_config import cfg
import sys
from daisyclient.v1 import client as daisy_client
import get_conf
import traceback
import time
import subprocess

daisy_version = 1.0
daisy_endpoint = "http://127.0.0.1:19292"
client = daisy_client.Client(version=daisy_version, endpoint=daisy_endpoint)

cluster_name = "clustertest"

_CLI_OPTS = [
    cfg.StrOpt('dha',
               help='The dha file path'),
    cfg.StrOpt('network',
               help='The network file path'),
]


# ------------------------------------------------------------------------------------------
def parse(conf, args):
    conf.register_cli_opts(_CLI_OPTS)
    conf(args=args)


def print_bar(msg):
    print ("--------------------------------------------")
    print (msg)
    print ("--------------------------------------------")


def foo():
    try:
        print("get config...")
        conf = cfg.ConfigOpts()
        parse(conf, sys.argv[1:])
        host_interface_map, host_role_map, \
            host_ip_passwd_map, network_map, vip = \
            get_conf.config(conf['dha'], conf['network'])
        print("clean deploy host...")
        clean_deploy_host(host_ip_passwd_map)
        print("discover host...")
        discover_host(host_ip_passwd_map)
        print("add cluster...")
        cluster_meta = {'name': cluster_name, 'description': ''}
        clusters_info = client.clusters.add(**cluster_meta)
        cluster_id = clusters_info.id
        print("cluster_id=%s." % cluster_id)
        print("update network...")
        update_network(cluster_id, network_map)
        print("update hosts interface...")
        hosts_info = get_hosts()
        add_hosts_interface(cluster_id, hosts_info, host_interface_map,
                            host_role_map, vip)
    except Exception:
        print("Deploy failed!!!.%s." % traceback.format_exc())
    else:
        print_bar("Everything is done!")


def clean_deploy_host(host_ip_passwd_map):
    for host_ip_passwd in host_ip_passwd_map:
        command = 'sshpass -p %s ssh %s -o UserKnownHostsFile=/dev/null \
                  -oStrictHostKeyChecking=no \
                  "/home/daisy/forDel/tools/cleanup-containers"' % \
                  (host_ip_passwd['passwd'], host_ip_passwd['ip'])
        subprocess.call(command,
                        shell=True,
                        stdout=open('/dev/null', 'w'),
                        stderr=subprocess.STDOUT)
        command = 'sshpass -p %s ssh %s -o UserKnownHostsFile=/dev/null \
                  -oStrictHostKeyChecking=no \
                  "/home/daisy/forDel/tools/cleanup-images"' % \
                  (host_ip_passwd['passwd'], host_ip_passwd['ip'])
        subprocess.call(command,
                        shell=True,
                        stdout=open('/dev/null', 'w'),
                        stderr=subprocess.STDOUT)


def discover_host(host_ip_passwd_map):
    for host_ip_passwd in host_ip_passwd_map:
        client.hosts.add_discover_host(**host_ip_passwd)
        client.hosts.discover_host()
    while True:
        hosts_info = get_hosts()
        if len(hosts_info) == len(host_ip_passwd_map):
            print('discover hosts success!')
            break
        else:
            time.sleep(10)


def update_network(cluster_id, network_map):
    network_meta = {'filters': {'cluster_id': cluster_id}}
    network_info_gernerator = client.networks.list(**network_meta)
    network_info_list = [net for net in network_info_gernerator]
    for net in network_info_list:
        network_id = net.id
        network_name = net.name
        if network_map.get(network_name):
            network_meta = network_map[network_name]
            client.networks.update(network_id, **network_meta)


def get_hosts():
    hosts_list_generator = client.hosts.list()
    hosts_list = [host for host in hosts_list_generator]
    hosts_info = []
    for host in hosts_list:
        host_info = client.hosts.get(host.id)
        hosts_info.append(host_info)
    return hosts_info


def add_hosts_interface(cluster_id, hosts_info, host_interface_map,
                        host_role_map, vip):
    for host in hosts_info:
        host = host.to_dict()
        host['cluster'] = cluster_id
        host_name = host['name']
        for interface in host['interfaces']:
            interface_name = interface['name']
            interface['assigned_networks'] = \
                host_interface_map[host_name][interface_name]
        client.hosts.update(host['id'], **host)
        print("update role...")
        add_host_role(cluster_id, host['id'], host['name'],
                      host_role_map, vip)


def add_host_role(cluster_id, host_id, host_name, host_role_map, vip):
    role_meta = {'filters': {'cluster_id': cluster_id}}
    role_list_generator = client.roles.list(**role_meta)
    role_list = [role for role in role_list_generator]
    lb_role_id = [role.id for role in role_list if
                  role.name == "CONTROLLER_LB"][0]
    computer_role_id = [role.id for role in role_list if
                        role.name == "COMPUTER"][0]
    if "CONTROLLER_LB" in host_role_map[host_name]:
        role_lb_update_meta = {'nodes': [host_id],
                               'cluster_id': cluster_id, 'vip': vip}
        client.roles.update(lb_role_id, **role_lb_update_meta)
    if "COMPUTER" in host_role_map[host_name]:
        role_computer_update_meta = {'nodes': [host_id],
                                     'cluster_id': cluster_id}
        client.roles.update(computer_role_id, **role_computer_update_meta)


if __name__ == "__main__":
    foo()