opennebula-cli 3.8.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
data/bin/onevm ADDED
@@ -0,0 +1,532 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # -------------------------------------------------------------------------- #
4
+ # Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) #
5
+ # #
6
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
7
+ # not use this file except in compliance with the License. You may obtain #
8
+ # a copy of the License at #
9
+ # #
10
+ # http://www.apache.org/licenses/LICENSE-2.0 #
11
+ # #
12
+ # Unless required by applicable law or agreed to in writing, software #
13
+ # distributed under the License is distributed on an "AS IS" BASIS, #
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15
+ # See the License for the specific language governing permissions and #
16
+ # limitations under the License. #
17
+ #--------------------------------------------------------------------------- #
18
+
19
+ ONE_LOCATION=ENV["ONE_LOCATION"]
20
+
21
+ if !ONE_LOCATION
22
+ RUBY_LIB_LOCATION="/usr/lib/one/ruby"
23
+ else
24
+ RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
25
+ end
26
+
27
+ $: << RUBY_LIB_LOCATION
28
+ $: << RUBY_LIB_LOCATION+"/cli"
29
+
30
+ require 'command_parser'
31
+ require 'one_helper/onevm_helper'
32
+
33
+ cmd=CommandParser::CmdParser.new(ARGV) do
34
+ usage "`onevm` <command> [<args>] [<options>]"
35
+ version OpenNebulaHelper::ONE_VERSION
36
+
37
+ helper = OneVMHelper.new
38
+
39
+
40
+ TYPE={
41
+ :name => "type",
42
+ :short => "-t type",
43
+ :large => "--type type",
44
+ :format => String,
45
+ :description => "Type of the new Image"
46
+ }
47
+
48
+ TARGET={
49
+ :name => "target",
50
+ :short => "-t type",
51
+ :large => "--target type",
52
+ :format => String,
53
+ :description => "Device where the image will be attached"
54
+ }
55
+
56
+ ########################################################################
57
+ # Global Options
58
+ ########################################################################
59
+ set :option, CommandParser::OPTIONS
60
+
61
+ ########################################################################
62
+ # Formatters for arguments
63
+ ########################################################################
64
+ set :format, :hostid, OpenNebulaHelper.rname_to_id_desc("HOST") do |arg|
65
+ OpenNebulaHelper.rname_to_id(arg, "HOST")
66
+ end
67
+
68
+ set :format, :groupid, OpenNebulaHelper.rname_to_id_desc("GROUP") do |arg|
69
+ OpenNebulaHelper.rname_to_id(arg, "GROUP")
70
+ end
71
+
72
+ set :format, :userid, OpenNebulaHelper.rname_to_id_desc("USER") do |arg|
73
+ OpenNebulaHelper.rname_to_id(arg, "USER")
74
+ end
75
+
76
+ set :format, :vmid, OneVMHelper.to_id_desc do |arg|
77
+ helper.to_id(arg)
78
+ end
79
+
80
+ set :format, :vmid_list, OneVMHelper.list_to_id_desc do |arg|
81
+ helper.list_to_id(arg)
82
+ end
83
+
84
+ set :format, :filterflag, OneVMHelper.filterflag_to_i_desc do |arg|
85
+ helper.filterflag_to_i(arg)
86
+ end
87
+
88
+ set :format, :diskid, "Integer" do |arg|
89
+ format_int(arg)
90
+ end
91
+
92
+ ########################################################################
93
+ # Commands
94
+ ########################################################################
95
+
96
+ create_desc = <<-EOT.unindent
97
+ Creates a new VM from the given description instead of using a previously
98
+ defined template (see 'onetemplate create' and 'onetemplate instantiate').
99
+
100
+ Examples:
101
+
102
+ - using a template description file:
103
+
104
+ onevm create vm_description.tmpl
105
+
106
+ - new VM named "arch vm" with a disk and a nic
107
+
108
+ onevm create --name "arch vm" --memory 128 --cpu 1 --disk arch \\
109
+ --network private_lan
110
+
111
+ - a vm with two disks
112
+
113
+ onevm create --name "test vm" --memory 128 --cpu 1 --disk arch,data
114
+
115
+ EOT
116
+
117
+ command :create, create_desc, [:file, nil], :options =>
118
+ [OneVMHelper::MULTIPLE]+OpenNebulaHelper::TEMPLATE_OPTIONS_VM do
119
+
120
+ number = options[:multiple] || 1
121
+ exit_code = nil
122
+
123
+ if args[0] && OpenNebulaHelper.create_template_options_used?(options)
124
+ STDERR.puts "You can not use both template file and template"<<
125
+ " creation options."
126
+ next -1
127
+ end
128
+
129
+ begin
130
+ if args[0]
131
+ template=File.read(args[0])
132
+ else
133
+ res = OpenNebulaHelper.create_template(options)
134
+
135
+ if res.first != 0
136
+ STDERR.puts res.last
137
+ next -1
138
+ end
139
+
140
+ template = res.last
141
+ end
142
+ rescue Exception => e
143
+ STDERR.puts "Error reading template."
144
+ next -1
145
+ end
146
+
147
+ number.times do
148
+ exit_code = helper.create_resource(options) do |vm|
149
+ error = vm.allocate(template)
150
+ end
151
+
152
+ break if exit_code == -1
153
+ end
154
+
155
+ exit_code
156
+ end
157
+
158
+ delete_desc = <<-EOT.unindent
159
+ Deletes the given VM
160
+
161
+ States: ANY
162
+ EOT
163
+
164
+ command :delete, delete_desc, [:range, :vmid_list] do
165
+ helper.perform_actions(args[0],options,"deleted") do |vm|
166
+ vm.finalize
167
+ end
168
+ end
169
+
170
+ hold_desc = <<-EOT.unindent
171
+ Sets the given VM on hold. A VM on hold is not scheduled until it is
172
+ released. It can be, however, deployed manually; see 'onevm deploy'
173
+
174
+ States: PENDING
175
+ EOT
176
+
177
+ command :hold, hold_desc, [:range,:vmid_list] do
178
+ helper.perform_actions(args[0],options,"put on hold") do |vm|
179
+ vm.hold
180
+ end
181
+ end
182
+
183
+ release_desc = <<-EOT.unindent
184
+ Releases a VM on hold. See 'onevm hold'
185
+
186
+ States: HOLD
187
+ EOT
188
+
189
+ command :release, release_desc, [:range,:vmid_list] do
190
+ helper.perform_actions(args[0],options,"released") do |vm|
191
+ vm.release
192
+ end
193
+ end
194
+
195
+ saveas_desc = <<-EOT.unindent
196
+ Sets the specified VM disk to be saved in a new Image. The Image is
197
+ created immediately, but the contents are saved only if the VM is
198
+ shut down gracefully (i.e., using 'onevm shutdown' and not
199
+ 'onevm delete')
200
+
201
+ States: ANY
202
+ EOT
203
+
204
+ command :saveas, saveas_desc, :vmid, :diskid, :img_name, :options=>[TYPE] do
205
+ disk_id = args[1].to_i
206
+ image_name = args[2]
207
+ image_type = options[:type] || ""
208
+
209
+ verbose = "disk #{disk_id} prepared to be saved in " <<
210
+ "the image #{image_name}"
211
+
212
+ helper.perform_action(args[0],options,verbose) do |vm|
213
+ res = vm.save_as(disk_id, image_name, image_type)
214
+
215
+ if !OpenNebula.is_error?(res)
216
+ puts "Image ID: #{res}"
217
+ end
218
+
219
+ res
220
+ end
221
+ end
222
+
223
+ shutdown_desc = <<-EOT.unindent
224
+ Shuts down the given VM. The VM life cycle will end.
225
+
226
+ States: RUNNING
227
+ EOT
228
+
229
+ command :shutdown, shutdown_desc, [:range,:vmid_list] do
230
+ helper.perform_actions(args[0],options,"shutting down") do |vm|
231
+ vm.shutdown
232
+ end
233
+ end
234
+
235
+ poweroff_desc = <<-EOT.unindent
236
+ Powers off the given VM. The VM will remain in the poweroff state, and
237
+ can be powered on with the 'onevm restart' command.
238
+
239
+ States: RUNNING
240
+ EOT
241
+
242
+ command :poweroff, poweroff_desc, [:range,:vmid_list] do
243
+ helper.perform_actions(args[0],options,"shutting down") do |vm|
244
+ vm.poweroff
245
+ end
246
+ end
247
+
248
+ reboot_desc = <<-EOT.unindent
249
+ Reboots the given VM, this is equivalent to execute the reboot command
250
+ from the VM console.
251
+
252
+ States: RUNNING
253
+ EOT
254
+
255
+ command :reboot, reboot_desc, [:range,:vmid_list] do
256
+ helper.perform_actions(args[0],options,"rebooting") do |vm|
257
+ vm.reboot
258
+ end
259
+ end
260
+
261
+ reset_desc = <<-EOT.unindent
262
+ Resets the given VM
263
+
264
+ States: RUNNING
265
+ EOT
266
+
267
+ command :reset, reset_desc, [:range,:vmid_list] do
268
+ helper.perform_actions(args[0],options,"resetting") do |vm|
269
+ vm.reset
270
+ end
271
+ end
272
+
273
+ deploy_desc = <<-EOT.unindent
274
+ Deploys the given VM in the specified Host. This command forces the
275
+ deployment, in a standard installation the Scheduler is in charge
276
+ of this decision
277
+
278
+ States: PENDING
279
+ EOT
280
+
281
+ command :deploy, deploy_desc, [:range,:vmid_list], :hostid do
282
+ host_id = args[1]
283
+ verbose = "deploying in host #{host_id}"
284
+
285
+ helper.perform_actions(args[0],options,verbose) do |vm|
286
+ vm.deploy(host_id)
287
+ end
288
+ end
289
+
290
+ livemigrate_desc = <<-EOT.unindent
291
+ Migrates the given running VM to another Host without downtime
292
+
293
+ States: RUNNING
294
+ EOT
295
+
296
+ command :livemigrate, livemigrate_desc, [:range,:vmid_list], :hostid do
297
+ host_id = args[1]
298
+ verbose = "live migrating to #{host_id}"
299
+
300
+ helper.perform_actions(args[0],options,verbose) do |vm|
301
+ vm.live_migrate(host_id)
302
+ end
303
+ end
304
+
305
+ migrate_desc = <<-EOT.unindent
306
+ Saves the given running VM and starts it again in the specified Host
307
+
308
+ States: RUNNING
309
+ EOT
310
+
311
+ command :migrate, migrate_desc, [:range,:vmid_list], :hostid do
312
+ host_id = args[1]
313
+ verbose = "migrating to #{host_id}"
314
+
315
+ helper.perform_actions(args[0],options,verbose) do |vm|
316
+ vm.migrate(host_id)
317
+ end
318
+ end
319
+
320
+ restart_desc = <<-EOT.unindent
321
+ Boots the given VM.
322
+
323
+ States: UNKNOWN, BOOT, POWEROFF
324
+ EOT
325
+
326
+ command :restart, restart_desc, [:range,:vmid_list] do
327
+ helper.perform_actions(args[0],options,"restarting") do |vm|
328
+ vm.restart
329
+ end
330
+ end
331
+
332
+ resubmit_desc = <<-EOT.unindent
333
+ Resubmits the VM to PENDING state. This is intended for VMs stuck in a
334
+ transient state. To re-deploy a fresh copy of the same VM, create a
335
+ Template and instantiate it, see 'onetemplate instantiate'
336
+
337
+ States: ANY, except SUSPENDED or DONE
338
+ EOT
339
+
340
+ command :resubmit, resubmit_desc, [:range,:vmid_list] do
341
+ helper.perform_actions(args[0],options,"resubmiting") do |vm|
342
+ vm.resubmit
343
+ end
344
+ end
345
+
346
+ cancel_desc = <<-EOT.unindent
347
+ Cancels the given VM. The process is checked by OpenNebula, so
348
+ if the process fails the VM remains in running state. If the action
349
+ succeeds the VMDIR in the remote machine is not deleted
350
+
351
+ States: RUNNING
352
+ EOT
353
+
354
+ command :cancel, cancel_desc, [:range,:vmid_list] do
355
+ helper.perform_actions(args[0],options,"canceling") do |vm|
356
+ vm.cancel
357
+ end
358
+ end
359
+
360
+ stop_desc = <<-EOT.unindent
361
+ Stops a running VM. The VM state is saved and transferred back to the
362
+ front-end along with the disk files
363
+
364
+ States: RUNNING
365
+ EOT
366
+
367
+ command :stop, stop_desc, [:range,:vmid_list] do
368
+ helper.perform_actions(args[0],options,"stopping") do |vm|
369
+ vm.stop
370
+ end
371
+ end
372
+
373
+ suspend_desc = <<-EOT.unindent
374
+ Saves a running VM. It is the same as 'onevm stop', but the files
375
+ are left in the remote machine to later restart the VM there
376
+ (i.e. the resources are not freed and there is no need to
377
+ re-schedule the VM).
378
+
379
+ States: RUNNING
380
+ EOT
381
+
382
+ command :suspend, suspend_desc, [:range,:vmid_list] do
383
+ helper.perform_actions(args[0],options,"suspending") do |vm|
384
+ vm.suspend
385
+ end
386
+ end
387
+
388
+ resume_desc = <<-EOT.unindent
389
+ Resumes the execution of the a saved VM
390
+
391
+ States: STOPPED, SUSPENDED
392
+ EOT
393
+
394
+ command :resume, resume_desc, [:range,:vmid_list] do
395
+ helper.perform_actions(args[0],options,"resuming") do |vm|
396
+ vm.resume
397
+ end
398
+ end
399
+
400
+ attachdisk_desc = <<-EOT.unindent
401
+ Attaches a disk to a running VM
402
+
403
+ States: RUNNING
404
+ EOT
405
+
406
+ command :attachdisk, attachdisk_desc, :vmid,
407
+ :options => [OneVMHelper::FILE, OneVMHelper::IMAGE, TARGET] do
408
+
409
+ if options[:file].nil? and options[:image].nil?
410
+ STDERR.puts "Provide a template file or an image:"
411
+ STDERR.puts "\t--file <file>"
412
+ STDERR.puts "\t--image <image>"
413
+ exit -1
414
+ end
415
+
416
+ if options[:file]
417
+ template = File.read(options[:file])
418
+ else
419
+ image_id = options[:image]
420
+ target = options[:target]
421
+ if target
422
+ template =
423
+ "DISK = [ IMAGE_ID = #{image_id}, TARGET = #{target} ]"
424
+ else
425
+ template =
426
+ "DISK = [ IMAGE_ID = #{image_id}, DEV_PREFIX = sd ]"
427
+ end
428
+ end
429
+
430
+ helper.perform_action(args[0],options,"Attach disk") do |vm|
431
+ vm.attachdisk(template)
432
+ end
433
+ end
434
+
435
+ detachdisk_desc = <<-EOT.unindent
436
+ Detaches a disk from a running VM
437
+
438
+ States: RUNNING
439
+ EOT
440
+
441
+ command :detachdisk, detachdisk_desc, :vmid, :diskid do
442
+ diskid = args[1].to_i
443
+
444
+ helper.perform_action(args[0],options,"Detach disk") do |vm|
445
+ vm.detachdisk(diskid)
446
+ end
447
+ end
448
+
449
+ chgrp_desc = <<-EOT.unindent
450
+ Changes the VM group
451
+ EOT
452
+
453
+ command :chgrp, chgrp_desc,[:range, :vmid_list], :groupid do
454
+ helper.perform_actions(args[0],options,"Group changed") do |vm|
455
+ vm.chown(-1, args[1].to_i)
456
+ end
457
+ end
458
+
459
+ chown_desc = <<-EOT.unindent
460
+ Changes the VM owner and group
461
+ EOT
462
+
463
+ command :chown, chown_desc, [:range, :vmid_list], :userid,
464
+ [:groupid,nil] do
465
+ gid = args[2].nil? ? -1 : args[2].to_i
466
+ helper.perform_actions(args[0],options,"Owner/Group changed") do |vm|
467
+ vm.chown(args[1].to_i, gid)
468
+ end
469
+ end
470
+
471
+ chmod_desc = <<-EOT.unindent
472
+ Changes the VM permissions
473
+ EOT
474
+
475
+ command :chmod, chmod_desc, [:range, :vmid_list], :octet do
476
+ helper.perform_actions(args[0],options, "Permissions changed") do |vm|
477
+ vm.chmod_octet(args[1])
478
+ end
479
+ end
480
+
481
+ resched_desc = <<-EOT.unindent
482
+ Sets the rescheduling flag for the VM.
483
+
484
+ States: RUNNING
485
+ EOT
486
+
487
+ command :resched, resched_desc, [:range,:vmid_list] do
488
+ helper.perform_actions(args[0],options,"Setting resched flag") do |vm|
489
+ vm.resched
490
+ end
491
+ end
492
+
493
+ unresched_desc = <<-EOT.unindent
494
+ Clears the rescheduling flag for the VM.
495
+
496
+ States: RUNNING
497
+ EOT
498
+
499
+ command :unresched, unresched_desc, [:range,:vmid_list] do
500
+ helper.perform_actions(args[0],options,"Clearing resched flag") do |vm|
501
+ vm.unresched
502
+ end
503
+ end
504
+
505
+ list_desc = <<-EOT.unindent
506
+ Lists VMs in the pool
507
+ EOT
508
+
509
+ command :list, list_desc, [:filterflag, nil],
510
+ :options=>CLIHelper::OPTIONS+OpenNebulaHelper::OPTIONS+
511
+ [OpenNebulaHelper::DESCRIBE] do
512
+ helper.list_pool(options, false, args[0])
513
+ end
514
+
515
+ show_desc = <<-EOT.unindent
516
+ Shows information for the given VM
517
+ EOT
518
+
519
+ command :show, show_desc, :vmid,
520
+ :options=>OpenNebulaHelper::XML do
521
+ helper.show_resource(args[0],options)
522
+ end
523
+
524
+ top_desc = <<-EOT.unindent
525
+ Lists Images continuously
526
+ EOT
527
+
528
+ command :top, top_desc, [:filterflag, nil],
529
+ :options=>CLIHelper::OPTIONS+OpenNebulaHelper::OPTIONS do
530
+ helper.list_pool(options, true, args[0])
531
+ end
532
+ end