summaryrefslogtreecommitdiffstats
path: root/compass-tasks/apiclient/example.py
blob: 4c01b98276011c7f16a0638a3510b0f89ede5f22 (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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#!/usr/bin/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.

"""Example code to deploy a cluster by compass client api."""
import os
import re
import sys
import time

# from compass.apiclient.restful import Client
from restful import Client

COMPASS_SERVER_URL = 'http://localhost/api'
COMPASS_LOGIN_EMAIL = 'admin@huawei.com'
COMPASS_LOGIN_PASSWORD = 'admin'
SWITCH_IP = '172.29.8.40'
SWITCH_SNMP_VERSION = '2c'
SWITCH_SNMP_COMMUNITY = 'public'
CLUSTER_NAME = 'test_cluster'
HOST_NAME_PREFIX = 'host'
SERVICE_USERNAME = 'service'
SERVICE_PASSWORD = 'service'
CONSOLE_USERNAME = 'console'
CONSOLE_PASSWORD = 'console'
HA_VIP = ''

MANAGEMENT_IP_START = '10.145.88.130'
MANAGEMENT_IP_END = '10.145.88.254'
MANAGEMENT_IP_GATEWAY = '10.145.88.1'
MANAGEMENT_NETMASK = '255.255.255.0'
MANAGEMENT_NIC = 'eth0'
MANAGEMENT_PROMISC = 0
TENANT_IP_START = '192.168.10.130'
TENANT_IP_END = '192.168.10.255'
TENANT_IP_GATEWAY = '192.168.10.1'
TENANT_NETMASK = '255.255.255.0'
TENANT_NIC = 'eth0'
TENANT_PROMISC = 0
PUBLIC_IP_START = '12.234.32.130'
PUBLIC_IP_END = '12.234.32.255'
PUBLIC_IP_GATEWAY = '12.234.32.1'
PUBLIC_NETMASK = '255.255.255.0'
PUBLIC_NIC = 'eth1'
PUBLIC_PROMISC = 1
STORAGE_IP_START = '172.16.100.130'
STORAGE_IP_END = '172.16.100.255'
STORAGE_NETMASK = '255.255.255.0'
STORAGE_IP_GATEWAY = '172.16.100.1'
STORAGE_NIC = 'eth0'
STORAGE_PROMISC = 0
HOME_PERCENTAGE = 5
TMP_PERCENTAGE = 5
VAR_PERCENTAGE = 10
HOST_OS = 'CentOS-6.5-x86_64'


PRESET_VALUES = {
    'LANGUAGE': 'EN',
    'TIMEZONE': 'GMT',
    'HTTPS_PROXY': 'http://10.145.89.100:3128',
    'NO_PROXY': ['127.0.0.1'],
    'DOMAIN': 'ods.com',
    'NAMESERVERS': ['10.145.89.100'],
    'NTP_SERVER': '10.145.89.100',
    'GATEWAY': '10.145.88.1',
    'PROXY': 'http://10.145.89.100:3128',
    'OS_NAME_PATTERN': 'CentOS.*',
    'ADAPTER_NAME': 'openstack_icehouse',
    'FLAVOR_PATTERN': 'allinone.*',
    'ROLES_LIST': ['allinone-compute'],
    'MACHINES_TO_ADD': ['00:0c:29:a7:ea:4b'],
    'BUILD_TIMEOUT': 60,
    'SEARCH_PATH': ['ods.com'],
    'SERVER_USERNAME': 'root',
    'SERVER_PASSWORD': 'root'
}
for v in PRESET_VALUES:
    if v in os.environ.keys():
        PRESET_VALUES[v] = os.environ.get(v)
        print (v + PRESET_VALUES[v] + " is set by env variables")
    else:
        print (PRESET_VALUES[v])

# instantiate a client
client = Client(COMPASS_SERVER_URL)

# login
status, response = client.login(COMPASS_LOGIN_EMAIL, COMPASS_LOGIN_PASSWORD)
print '============================================================'
print 'login status: %s response: %s' % (status, response)
if status >= 400:
    sys.exit(1)

# list all switches
status, response = client.list_switches()
print '============================================================='
print 'get all switches status: %s response: %s' % (status, response)

# add a switch
status, response = client.add_switch(
    SWITCH_IP,
    SWITCH_SNMP_VERSION,
    SWITCH_SNMP_COMMUNITY
)
print '============================================'
print 'adding a switch..status: %s, response: %s' % (status, response)

# if switch already exists, get one from all switches
switch = None
if status < 400:
    switch = response
else:
    status, response = client.list_switches()
    print '========================================='
    print 'list switches status %s response %s' % (status, response)
    if status >= 400:
        sys.exit(1)
    for switch_ in response:
        if switch_['ip'] == SWITCH_IP:
            switch = switch_
            break

switch_id = switch['id']
switch_ip = switch['ip']
print '======================'
print 'switch has been set as %s' % switch_ip

# wait till switch state becomes under_monitoring
while switch['state'] != 'under_monitoring':
    print 'waiting for state to become under_monitoring'
    client.poll_switch(switch_id)
    status, resp = client.get_switch(switch_id)
    print '====================================='
    print 'poll switch status %s response %s' % (status, resp)
    switch = resp
    print 'switch is in state: %s' % switch['state']
    time.sleep(5)

print '========================================='
print 'switch state now is %s' % (switch['state'])

# create a machine list
machine_macs = {}
machines = {}
for machine in PRESET_VALUES['MACHINES_TO_ADD']:
    status, response = client.list_machines(mac=machine)
    print '============================================'
    print 'list machines status %s response %s' % (status, response)
    if status >= 400:
        sys.exit(1)
    if status == 200 and response != []:
        machine_id = response[0]['id']
        machine_macs[machine_id] = response[0]['mac']
        machines = response

print '================================='
print 'found machines are : %s' % machines

machines_to_add = PRESET_VALUES['MACHINES_TO_ADD']
if set(machine_macs.values()) != set(machines_to_add):
    print 'only found macs %s while expected are %s' % (
        machine_macs.values(), machines_to_add)
    sys.exit(1)

# list all adapters
status, response = client.list_adapters()
print '==============================='
print 'all adapters are: %s' % response
if status >= 400:
    sys.exit(1)

adapters = response
adapter_id = None
os_id = None
flavor_id = None
adapter_name = PRESET_VALUES['ADPATER_NAME']
os_pattern = re.compile(PRESET_VALUES['OS_NAME_PATTERN'])
flavor_pattern = re.compile(PRESET_VALUES['FLAVOR_PATTERN'])
for adapter in adapters:
    if adapter_name == adapter['name']:
        adapter_id = adapter['id']
    for supported_os in adapter['supported_oses']:
        if os_pattern.match(supported_os['name']):
            os_id = supported_os['id']
            break
    for flavor in adapter['flavors']:
        if flavor_pattern.match(flavor['name']):
            flavor_id = flavor['id']
    if adapter_id and os_id and flavor_id:
        break

print '======================================================='
print 'using adapter %s os %s flavor %s to deploy cluster' % (
    adapter_id, os_id, flavor_id
)

# add a cluster
status, response = client.add_cluster(
    CLUSTER_NAME,
    adapter_id,
    os_id,
    flavor_id
)
print '==============================================================='
print 'add cluster %s status %s: %s' % (CLUSTER_NAME, status, response)
if status >= 400:
    sys.exit(1)

status, response = client.list_clusters(name=CLUSTER_NAME)
print '================================================================'
print 'list clusters %s status %s: %s' % (CLUSTER_NAME, status, response)
if status >= 400:
    sys.exit(1)

cluster = response[0]
cluster_id = cluster['id']

print '=================='
print 'cluster is %s' % cluster

# Add hosts to the cluster
machines_dict = {}
machine_id_list = []
for machine in machines:
    id_mapping = {}
    id_mapping['machine_id'] = machine['id']
    machine_id_list.append(id_mapping)

machines_dict['machines'] = machine_id_list

status, response = client.add_hosts_to_cluster(
    cluster_id, machines_dict
)
print '==================================='
print 'add hosts %s to cluster status %s response %s' % (
    machines_dict, status, response)
if status >= 400:
    sys.exit(1)

# Add two subnets
subnet_1 = '10.145.89.0/24'
subnet_2 = '192.168.100.0/24'

status, response = client.add_subnet(subnet_1)
print '=================='
print 'add subnet %s status %s: %s' % (subnet_1, status, response)
if status >= 400:
    sys.exit(1)

status, response = client.add_subnet(subnet_2)
print '=================='
print 'add subnet %s status %s: %s' % (subnet_2, status, response)
if status >= 400:
    sys.exit(1)

status, subnet1 = client.list_subnets(subnet=subnet_1)
print '==========================================================='
print 'list subnet %s status %s: %s' % (subnet_1, status, subnet1)
if status >= 400:
    sys.exit(1)

status, subnet2 = client.list_subnets(subnet=subnet_2)
print '==========================================================='
print 'list subnet %s status %s: %s' % (subnet_2, status, subnet2)
if status >= 400:
    sys.exit(1)

subnet1_id = subnet1[0]['id']
subnet2_id = subnet2[0]['id']
print '========================'
print 'subnet1 has id: %s, subnet is %s' % (subnet1_id, subnet1)
print 'subnet2 has id: %s, subnet is %s' % (subnet2_id, subnet2)

# Add host network
status, response = client.list_cluster_hosts(cluster_id)
print '================================================'
print 'list cluster hosts status %s: %s' % (status, response)
if status >= 400:
    sys.exit(1)

host = response[0]
host_id = host['id']
print '=================='
print 'host is: %s' % host

status, response = client.add_host_network(
    host_id,
    'eth0',
    '10.145.89.200',
    subnet1_id,
    is_mgmt=True
)
print '======================='
print 'add eth0 network status %s: %s' % (status, response)
if status >= 400:
    sys.exit(1)

status, response = client.add_host_network(
    host_id,
    'eth1',
    '192.168.100.200',
    subnet2_id,
    is_promiscuous=True
)
print '======================='
print 'add eth1 network status %s: %s' % (status, response)
if status >= 400:
    sys.exit(1)

# Update os config to cluster
cluster_os_config = {
    'general': {
        'language': PRESET_VALUES['LANGUAGE'],
        'timezone': PRESET_VALUES['TIMEZONE'],
        'http_proxy': PRESET_VALUES['PROXY'],
        'https_proxy': PRESET_VALUES['HTTPS_PROXY'],
        'no_proxy': PRESET_VALUES['NO_PROXY'],
        'ntp_server': PRESET_VALUES['NTP_SERVER'],
        'dns_servers': PRESET_VALUES['NAMESERVERS'],
        'domain': PRESET_VALUES['DOMAIN'],
        'search_path': PRESET_VALUES['SEARCH_PATH'],
        'default_gateway': PRESET_VALUES['GATEWAY']
    },
    'server_credentials': {
        'username': PRESET_VALUES['SERVER_USERNAME'],
        'password': PRESET_VALUES['SERVER_PASSWORD']
    },
    'partition': {
        '/var': {
            'percentage': VAR_PERCENTAGE,
        },
        '/home': {
            'percentage': HOME_PERCENTAGE,
        }
    }
}


cluster_package_config = {
    'security': {
        'service_credentials': {
            'image': {
                'username': SERVICE_USERNAME,
                'password': SERVICE_PASSWORD
            },
            'compute': {
                'username': SERVICE_USERNAME,
                'password': SERVICE_PASSWORD
            },
            'dashboard': {
                'username': SERVICE_USERNAME,
                'password': SERVICE_PASSWORD
            },
            'identity': {
                'username': SERVICE_USERNAME,
                'password': SERVICE_PASSWORD
            },
            'metering': {
                'username': SERVICE_USERNAME,
                'password': SERVICE_PASSWORD
            },
            'rabbitmq': {
                'username': SERVICE_USERNAME,
                'password': SERVICE_PASSWORD
            },
            'volume': {
                'username': SERVICE_USERNAME,
                'password': SERVICE_PASSWORD
            },
            'mysql': {
                'username': SERVICE_USERNAME,
                'password': SERVICE_PASSWORD
            }
        },
        'console_credentials': {
            'admin': {
                'username': CONSOLE_USERNAME,
                'password': CONSOLE_PASSWORD
            },
            'compute': {
                'username': CONSOLE_USERNAME,
                'password': CONSOLE_PASSWORD
            },
            'dashboard': {
                'username': CONSOLE_USERNAME,
                'password': CONSOLE_PASSWORD
            },
            'image': {
                'username': CONSOLE_USERNAME,
                'password': CONSOLE_PASSWORD
            },
            'metering': {
                'username': CONSOLE_USERNAME,
                'password': CONSOLE_PASSWORD
            },
            'network': {
                'username': CONSOLE_USERNAME,
                'password': CONSOLE_PASSWORD
            },
            'object-store': {
                'username': CONSOLE_USERNAME,
                'password': CONSOLE_PASSWORD
            },
            'volume': {
                'username': CONSOLE_USERNAME,
                'password': CONSOLE_PASSWORD
            }
        }
    },
    'network_mapping': {
        'management': MANAGEMENT_NIC,
        'tenant': TENANT_NIC,
        'storage': STORAGE_NIC,
        'public': PUBLIC_NIC
    }
}

status, response = client.update_cluster_config(
    cluster_id,
    cluster_os_config,
    cluster_package_config
)

print '======================================='
print 'cluster %s update status %s: %s' % (
    cluster_id, status, response)
if status >= 400:
    sys.exit(1)

status, response = client.update_cluster_host(
    cluster_id, host_id, roles=PRESET_VALUES['ROLES_LIST'])
print '================================================='
print 'update cluster host %s/%s status %s: %s' % (
    cluster_id, host_id, status, response)
if status >= 400:
    sys.exit(1)

# Review and deploy
status, response = client.review_cluster(
    cluster_id, review={'hosts': [host_id]})
print '======================================='
print 'reviewing cluster status %s: %s' % (status, response)
if status >= 400:
    sys.exit(1)

status, response = client.deploy_cluster(
    cluster_id, deploy={'hosts': [host_id]})
print '======================================='
print 'deploy cluster status %s: %s' % (status, response)
if status >= 400:
    sys.exit(1)