This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
/
vca_cli_compute.py
317 lines (311 loc) · 15 KB
/
vca_cli_compute.py
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
# vCloud Air CLI 0.1
#
# Copyright (c) 2014 VMware, Inc. All Rights Reserved.
#
# This product is licensed to you under the
# Apache License, Version 2.0 (the "License").
# You may not use this product except in compliance with the License.
#
# This product may include a number of subcomponents with
# separate copyright notices and license terms. Your use of the source
# code for the these subcomponents is subject to the terms and
# conditions of the subcomponent's license, as noted in the LICENSE file.
#
import sys
import click
import operator
from vca_cli import cli, utils, default_operation
@cli.command()
@click.pass_obj
@click.argument('operation', default=default_operation,
metavar='[list | use | info]',
type=click.Choice(['list', 'use', 'info']))
@click.option('-v', '--vdc', default=None, metavar='<vdc>',
help='Virtual Data Center Name')
def vdc(cmd_proc, operation, vdc):
"""Operations with Virtual Data Centers"""
result = cmd_proc.re_login()
if not result:
utils.print_error('Not logged in', cmd_proc)
sys.exit(1)
if 'list' == operation:
headers = ['Virtual Data Center', "Selected"]
table = ['', '']
if cmd_proc.vca.vcloud_session and \
cmd_proc.vca.vcloud_session.organization:
links = (cmd_proc.vca.vcloud_session.organization.Link if
cmd_proc.vca.vcloud_session.organization else [])
table1 = [[details.get_name(),
'*' if details.get_name() == cmd_proc.vdc_name else '']
for details in filter(lambda info: info.name and
(info.type_ == 'application/vnd.vmware.vcloud.vdc+xml'),
links)]
table = sorted(table1, key=operator.itemgetter(0), reverse=False)
utils.print_table(
"Available Virtual Data Centers in org '%s', profile '%s':" %
(cmd_proc.vca.org, cmd_proc.profile),
headers, table, cmd_proc)
elif 'use' == operation:
the_vdc = cmd_proc.vca.get_vdc(vdc)
if the_vdc is not None:
utils.print_message("Using vdc '%s', profile '%s'" %
(vdc, cmd_proc.profile), cmd_proc)
cmd_proc.vdc_name = vdc
else:
utils.print_error("Unable to select vdc '%s' in profile '%s'" %
(vdc, cmd_proc.profile), cmd_proc)
sys.exit(1)
elif 'info' == operation:
if vdc is None:
vdc = cmd_proc.vdc_name
the_vdc = cmd_proc.vca.get_vdc(vdc)
if the_vdc:
gateways = cmd_proc.vca.get_gateways(vdc)
headers1 = ['Type', 'Name']
table1 = cmd_proc.vdc_to_table(the_vdc, gateways)
headers2 = ['Resource', 'Allocated',
'Limit', 'Reserved', 'Used', 'Overhead']
table2 = cmd_proc.vdc_resources_to_table(the_vdc)
headers3 = ['Name', 'External IPs', 'DHCP', 'Firewall', 'NAT',
'VPN', 'Routed Networks', 'Syslog', 'Uplinks']
table3 = cmd_proc.gateways_to_table(gateways)
if cmd_proc.json_output:
json_object = {'vdc_entities':
utils.table_to_json(headers1, table1),
'vdc_resources':
utils.table_to_json(headers2, table2),
'gateways':
utils.table_to_json(headers3, table3)}
utils.print_json(json_object, cmd_proc=cmd_proc)
else:
utils.print_table(
"Details of Virtual Data Center '%s', profile '%s':" %
(vdc, cmd_proc.profile),
headers1, table1, cmd_proc)
utils.print_table("Compute capacity:",
headers2, table2, cmd_proc)
utils.print_table('Gateways:',
headers3, table3, cmd_proc)
else:
utils.print_error("Unable to select vdc '%s' in profile '%s'" %
(vdc, cmd_proc.profile), cmd_proc)
sys.exit(1)
cmd_proc.save_current_config()
@cli.command()
@click.pass_obj
@click.argument('operation', default=default_operation,
metavar='[list | info | create | delete | power-on'
' | power-off | deploy | undeploy | customize'
' | insert | eject | connect | disconnect'
' | attach | detach]',
type=click.Choice(
['list', 'info', 'create', 'delete', 'power.on',
'power.off', 'deploy', 'undeploy', 'customize',
'insert', 'eject', 'connect', 'disconnect',
'attach', 'detach']))
@click.option('-v', '--vdc', default=None,
metavar='<vdc>', help='Virtual Data Center Name')
@click.option('-a', '--vapp', 'vapp', default='',
metavar='<vapp>', help='vApp name')
@click.option('-c', '--catalog', default='',
metavar='<catalog>', help='Catalog name')
@click.option('-t', '--template', default='',
metavar='<template>', help='Template name')
@click.option('-n', '--network', default='',
metavar='<network>', help='Network name')
@click.option('-m', '--mode', default='POOL',
metavar='[pool, dhcp, manual]', help='Network connection mode',
type=click.Choice(['POOL', 'pool', 'DHCP', 'dhcp', 'MANUAL',
'manual']))
@click.option('-V', '--vm', 'vm_name', default=None,
metavar='<vm>', help='VM name')
@click.option('-f', '--file', 'cust_file',
default=None, metavar='<customization_file>',
help='Guest OS Customization script file', type=click.File('r'))
@click.option('-e', '--media', default='',
metavar='<media>', help='Virtual media name (ISO)')
@click.option('-d', '--disk', 'disk_name', default=None,
metavar='<disk_name>', help='Disk Name')
@click.option('-o', '--count', 'count', default=1,
metavar='<count>', help='Number of vApps to create')
@click.option('-p', '--cpu', 'cpu', default=None,
metavar='<virtual CPUs>', help='Virtual CPUs')
@click.option('-r', '--ram', 'ram', default=None,
metavar='<MB RAM>', help='Memory in MB')
@click.option('-i', '--ip', default='', metavar='<ip>', help='IP address')
def vapp(cmd_proc, operation, vdc, vapp, catalog, template,
network, mode, vm_name, cust_file,
media, disk_name, count, cpu, ram, ip):
"""Operations with vApps"""
result = cmd_proc.re_login()
if not result:
utils.print_error('Not logged in', cmd_proc)
sys.exit(1)
if vdc is None:
vdc = cmd_proc.vdc_name
the_vdc = cmd_proc.vca.get_vdc(vdc)
if 'list' == operation:
headers = ['vApp', "VMs", "Status", "Deployed", "Description"]
table = cmd_proc.vapps_to_table(the_vdc)
if cmd_proc.json_output:
json_object = {'vapps':
utils.table_to_json(headers, table)}
utils.print_json(json_object, cmd_proc=cmd_proc)
else:
utils.print_table("Available vApps in '%s', profile '%s':" %
(vdc, cmd_proc.profile),
headers, table, cmd_proc)
elif 'create' == operation:
for x in xrange(1, count + 1):
vapp_name = vapp
if count > 1:
vapp_name += '-' + str(x)
utils.print_message("creating vApp '%s' in VDC '%s'"
" from template '%s' in catalog '%s'" %
(vapp_name, vdc, template, catalog), cmd_proc)
task = None
if vm_name is not None:
if (cmd_proc.vca.version == "1.0") or \
(cmd_proc.vca.version == "1.5") or \
(cmd_proc.vca.version == "5.1") or \
(cmd_proc.vca.version == "5.5"):
task = cmd_proc.vca.create_vapp(vdc, vapp_name,
template, catalog)
else:
task = cmd_proc.vca.create_vapp(vdc, vapp_name,
template, catalog,
vm_name=vm_name)
if task:
utils.display_progress(task, cmd_proc,
cmd_proc.vca.vcloud_session.
get_vcloud_headers())
else:
utils.print_error("can't create the vApp", cmd_proc)
sys.exit(1)
the_vdc = cmd_proc.vca.get_vdc(vdc)
the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp_name)
if ((vm_name is not None) and
((cmd_proc.vca.version == "1.0") or
(cmd_proc.vca.version == "1.5") or
(cmd_proc.vca.version == "5.1") or
(cmd_proc.vca.version == "5.5"))):
if vm_name is not None:
utils.print_message(
"setting VM name to '%s'"
% (vm_name), cmd_proc)
task = the_vapp.modify_vm_name(1, vm_name)
if task:
utils.display_progress(task, cmd_proc,
cmd_proc.vca.vcloud_session.
get_vcloud_headers())
else:
utils.print_error("can't set VM name", cmd_proc)
sys.exit(1)
the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp_name)
if vm_name is not None:
utils.print_message(
"setting computer name for VM '%s'"
% (vm_name), cmd_proc)
task = the_vapp.customize_guest_os(vm_name,
computer_name=vm_name)
if task:
utils.display_progress(task, cmd_proc,
cmd_proc.vca.vcloud_session.
get_vcloud_headers())
else:
utils.print_error("can't set computer name", cmd_proc)
sys.exit(1)
the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp_name)
if cpu is not None:
utils.print_message(
"configuring '%s' vCPUs for VM '%s', vApp '%s'"
% (cpu, vm_name, vapp_name), cmd_proc)
task = the_vapp.modify_vm_cpu(vm_name, cpu)
if task:
utils.display_progress(task, cmd_proc,
cmd_proc.vca.vcloud_session.
get_vcloud_headers())
else:
utils.print_error("can't configure virtual CPUs", cmd_proc)
sys.exit(1)
the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp_name)
# if ram is not None:
# print_message("configuring '%s' MB of memory"
# " for VM '%s', vApp '%s'"
# % (ram, vm_name, vapp_name), ctx)
# task = the_vapp.modify_vm_memory(vm_name, ram)
# if task:
# display_progress(task, ctx,
# vca.vcloud_session.get_vcloud_headers())
# else:
# ctx.obj['response'] = the_vapp.response
# print_error("can't configure RAM", ctx)
# the_vapp = vca.get_vapp(the_vdc, vapp_name)
# if '' != network:
# print_message("disconnecting VM from networks"
# " pre-defined in the template", ctx)
# task = the_vapp.disconnect_vms()
# if task:
# display_progress(task, ctx,
# vca.vcloud_session.get_vcloud_headers())
# else:
# ctx.obj['response'] = the_vapp.response
# print_error("can't disconnect VM from networks", ctx)
# return
# print_message("disconnecting vApp from networks"
# " pre-defined in the template", ctx)
# task = the_vapp.disconnect_from_networks()
# if task:
# display_progress(task, ctx,
# vca.vcloud_session.get_vcloud_headers())
# else:
# ctx.obj['response'] = the_vapp.response
# print_error("can't disconnect vApp from networks", ctx)
# return
# nets = filter(lambda n: n.name == network,
# vca.get_networks(vdc))
# if len(nets) == 1:
# print_message(
# "connecting vApp to network"
# " '%s' with mode '%s'" % (network, mode), ctx)
# task = the_vapp.connect_to_network(
# nets[0].name, nets[0].href)
# if task:
# display_progress(
# task, ctx,
# vca.vcloud_session.get_vcloud_headers())
# else:
# ctx.obj['response'] = the_vapp.response
# print_error("can't connect the vApp to the network",
# ctx)
# return
# print_message("connecting VM to network '%s'"
# " with mode '%s'" % (network, mode), ctx)
# task = the_vapp.connect_vms(
# nets[0].name,
# connection_index=0,
# ip_allocation_mode=mode.upper(),
# mac_address=None, ip_address=ip)
# if task:
# display_progress(
# task, ctx,
# vca.vcloud_session.get_vcloud_headers())
# else:
# ctx.obj['response'] = the_vapp.response
# print_error(
# "can't connect the VM to the network", ctx)
elif 'delete' == operation:
utils.print_message("deleting vApp '%s' from VDC '%s'" % (vapp, vdc),
cmd_proc)
task = cmd_proc.vca.delete_vapp(vdc, vapp)
if task:
utils.display_progress(task, cmd_proc,
cmd_proc.vca.vcloud_session.
get_vcloud_headers())
else:
utils.print_error("can't delete the vApp", cmd_proc)
sys.exit(1)
else:
utils.print_message('not implemented', cmd_proc)
sys.exit(1)
cmd_proc.save_current_config()