summaryrefslogtreecommitdiffstats
path: root/compass-deck/bin/csvdeploy.py
blob: 23b0c467a5a4376cdec5103dffd06f0978be4bd8 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env python
#
# Copyright 2014 Huawei Technologies Co. Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""deploy cluster from csv file."""
import ast
import copy
import csv
import os
import re
import sys

from multiprocessing import Process
from multiprocessing import Queue
from optparse import OptionParser

try:
    from compass.apiclient.restful import Client
except ImportError:
    curr_dir = os.path.dirname(os.path.realpath(__file__))
    apiclient_dir = os.path.dirname(curr_dir) + '/compass/apiclient'
    sys.path.append(apiclient_dir)
    from restful import Client


DELIMITER = ","

# Sqlite tables
TABLES = {
    'switch_config': {'columns': ['id', 'ip', 'filter_port']},
    'switch': {'columns': ['id', 'ip', 'credential_data']},
    'machine': {'columns': ['id', 'mac', 'port', 'vlan', 'switch_id']},
    'cluster': {'columns': ['id', 'name', 'security_config',
                            'networking_config', 'partition_config',
                            'adapter_id', 'state']},
    'cluster_host': {'columns': ['id', 'cluster_id', 'hostname', 'machine_id',
                                 'config_data', 'state']},
    'adapter': {'columns': ['id', 'name', 'os', 'target_system']},
    'role': {'columns': ['id', 'name', 'target_system', 'description']}
}


def start(csv_dir, compass_url):
    """Start deploy both failed clusters and new clusters."""
    # Get clusters and hosts data from CSV
    clusters_data = get_csv('cluster.csv', csv_dir)
    hosts_data = get_csv('cluster_host.csv', csv_dir)
    data = {}
    for cluster in clusters_data:
        tmp = {}
        tmp['cluster_data'] = cluster
        tmp['hosts_data'] = []
        data[cluster['id']] = tmp

    for host in hosts_data:
        cluster_id = host['cluster_id']
        if cluster_id not in data:
            print ("Unknown cluster_id=%s of the host! host_id=%s!"
                   % (cluster_id, host['id']))
            sys.exit(1)

        data[cluster_id]['hosts_data'].append(host)

    apiClient = _APIClient(compass_url)
    results_q = Queue()
    ps = []
    for elem in data:
        cluster_data = data[elem]['cluster_data']
        hosts_data = data[elem]['hosts_data']
        p = Process(target=apiClient.execute,
                    args=(cluster_data, hosts_data, results_q))
        ps.append(p)
        p.start()

    for p in ps:
        p.join()

    progress_file = '/'.join((csv_dir, 'progress.csv'))
    write_progress_to_file(results_q, progress_file)


def write_progress_to_file(results_q, progress_file):
    cluster_headers = ['cluster_id', 'progress_url']
    host_headers = ['host_id', 'progress_url']

    with open(progress_file, 'wb') as f:
        print "Writing all progress information to %s......" % progress_file
        writer = csv.writer(f, delimiter=DELIMITER, quoting=csv.QUOTE_MINIMAL)
        while not results_q.empty():
            record = results_q.get()
            hosts = []
            cluster = [record['deployment']['cluster']['cluster_id'],
                       record['deployment']['cluster']['url']]
            writer.writerow(cluster_headers)
            writer.writerow(cluster)

            for elem in record['deployment']['hosts']:
                host = [elem['host_id'], elem['url']]
                hosts.append(host)

            writer.writerow(host_headers)
            writer.writerows(hosts)
    print "Done!\n"


def get_csv(fname, csv_dir):
    """Parse csv files into python variables.

       .. note::
          all nested fields in db will be assembled.

        :param fname: CSV file name
        :param csv_dir: CSV files directory

        :returns: list of dict which key is column name and value is its data.
    """
    headers = []
    rows = []
    file_dir = '/'.join((csv_dir, fname))
    with open(file_dir) as f:
        reader = csv.reader(f, delimiter=DELIMITER, quoting=csv.QUOTE_MINIMAL)
        headers = reader.next()
        rows = [x for x in reader]

    result = []
    for row in rows:
        data = {}
        for col_name, value in zip(headers, row):
            if re.match(r'^[\d]+$', value):
                # the value should be an integer
                value = int(value)
            elif re.match(r'^\[(\'\w*\'){1}(\s*,\s*\'\w*\')*\]$', value):
                # the value should be a list
                value = ast.literal_eval(value)
            elif value == 'None':
                value = ''

            if col_name.find('.') > 0:
                tmp_result = {}
                tmp_result[col_name.split('.')[-1]] = value
                keys = col_name.split('.')[::-1][1:]
                for key in keys:
                    tmp = {}
                    tmp[key] = tmp_result
                    tmp_result = tmp
                    merge_dict(data, tmp_result)
            else:
                data[col_name] = value

        result.append(data)

    return result


def merge_dict(lhs, rhs, override=True):
    """Merge nested right dict into left nested dict recursively.

    :param lhs: dict to be merged into.
    :type lhs: dict
    :param rhs: dict to merge from.
    :type rhs: dict
    :param override: the value in rhs overide the value in left if True.
    :type override: str

    :raises: TypeError if lhs or rhs is not a dict.
    """
    if not rhs:
        return

    if not isinstance(lhs, dict):
        raise TypeError('lhs type is %s while expected is dict' % type(lhs),
                        lhs)

    if not isinstance(rhs, dict):
        raise TypeError('rhs type is %s while expected is dict' % type(rhs),
                        rhs)

    for key, value in rhs.items():
        if isinstance(value, dict) and key in lhs and isinstance(lhs[key],
                                                                 dict):
            merge_dict(lhs[key], value, override)
        else:
            if override or key not in lhs:
                lhs[key] = copy.deepcopy(value)


class _APIClient(Client):
    def __init__(self, url, headers=None, proxies=None, stream=None):
        super(_APIClient, self).__init__(url, headers, proxies, stream)

    def set_cluster_resource(self, cluster_id, resource, data):
        url = "/clusters/%d/%s" % (cluster_id, resource)
        return self._put(url, data=data)

    def execute(self, cluster_data, hosts_data, resp_results):
        """The process includes creating or updating a cluster.

        The cluster configuration, add or update a host in the cluster,
        and deploy the updated hosts.

        :param cluster_data: the dictionary of cluster data
        """
        cluster_id = cluster_data['id']
        code, resp = self.get_cluster(cluster_id)
        if code == 404:
            # Create a new cluster
            name = cluster_data['name']
            adapter_id = cluster_data['adapter_id']
            code, resp = self.add_cluster(name, adapter_id)

            if code != 200:
                print ("Failed to create the cluster which name is "
                       "%s!\nError message: %s" % (name, resp['message']))
                sys.exit(1)

        # Update the config(security, networking, partition) of the cluster
        security_req = {}
        networking_req = {}
        partition_req = {}

        security_req['security'] = cluster_data['security_config']
        networking_req['networking'] = cluster_data['networking_config']
        partition_req['partition'] = cluster_data['partition_config']

        print "Update Security config......."
        code, resp = self.set_cluster_resource(cluster_id, 'security',
                                               security_req)
        if code != 200:
            print ("Failed to update Security config for cluster id=%s!\n"
                   "Error message: " % (cluster_id, resp['message']))
            sys.exit(1)

        print "Update Networking config......."
        code, resp = self.set_cluster_resource(cluster_id, 'networking',
                                               networking_req)
        if code != 200:
            print ("Failed to update Networking config for cluster id=%s!\n"
                   "Error message: %s" % (cluster_id, resp['message']))
            sys.exit(1)

        print "Update Partition config......."
        code, resp = self.set_cluster_resource(cluster_id, 'partition',
                                               partition_req)
        if code != 200:
            print ("Failed to update Partition config for cluster id=%s!\n"
                   "Error message: " % (cluster_id, resp['message']))
            sys.exit(1)

        deploy_list = []
        deploy_hosts_data = []

        machines_list = []
        new_hosts_data = []
        for record in hosts_data:
            if record['state'] and int(record['deploy_action']):
                deploy_list.append(record['id'])
                deploy_hosts_data.append(record)

            elif int(record['deploy_action']):
                machines_list.append(record['machine_id'])
                new_hosts_data.append(record)

        if machines_list:
            # add new hosts to the cluster
            code, resp = self.add_hosts(cluster_id, machines_list)
            if code != 200:
                print ("Failed to add hosts to the cluster id=%s!\n"
                       "Error message: %s.\nfailed hosts are %s"
                       % (cluster_id, resp['message'], resp['failedMachines']))
                sys.exit(1)

            for record, host in zip(new_hosts_data, resp['cluster_hosts']):
                record['id'] = host['id']
                deploy_list.append(host['id'])
                deploy_hosts_data.append(record)

        # Update the config of each host in the cluster
        for host in deploy_hosts_data:
            req = {}
            host_id = host['id']
            print "Updating the config of host id=%s" % host['id']
            req['hostname'] = host['hostname']
            req.update(host['config_data'])
            code, resp = self.update_host_config(int(host_id), raw_data=req)
            if code != 200:
                print ("Failed to update the config of the host id=%s!\n"
                       "Error message: %s" % (host_id, resp['message']))
                sys.exit(1)

        # Start to deploy the cluster
        print "Start to deploy the cluster!....."
        deploy_req = {"deploy": deploy_list}
        code, resp = self.deploy_hosts(cluster_id, raw_data=deploy_req)
        print "---Cluster Info---"
        print "cluster_id  url"
        print ("    %s     %s"
               % (resp['deployment']['cluster']['cluster_id'],
                  resp['deployment']['cluster']['url']))
        print "---Hosts Info-----"
        print "host_id  url"
        for host in resp['deployment']['hosts']:
            print "    %s     %s" % (host['host_id'], host['url'])
        print "---------------------------------------------------------------"
        print "\n"
        resp_results.put(resp)


if __name__ == "__main__":
    usage = "usage: %prog [options]"
    parser = OptionParser(usage)

    parser.add_option("-d", "--csv-dir", dest="csv_dir",
                      help="The directory of CSV files used for depolyment")
    parser.add_option("-u", "--compass-url", dest="compass_url",
                      help="The URL of Compass server")
    (options, args) = parser.parse_args()

    if not os.exists(options.csv_dir):
        print "Cannot find the directory: %s" % options.csv_dir

    start(options.csv_dir, options.compass_url)