Files
one/src/cli/onevm
T

181 lines
6.3 KiB
Ruby
Executable File

#!/usr/bin/env ruby
# -------------------------------------------------------------------------- #
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) #
# #
# 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. #
#--------------------------------------------------------------------------- #
ONE_LOCATION=ENV["ONE_LOCATION"]
if !ONE_LOCATION
RUBY_LIB_LOCATION="/usr/lib/one/ruby"
else
RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
end
$: << RUBY_LIB_LOCATION
$: << RUBY_LIB_LOCATION+"/cli"
require 'command_parser'
require 'one_helper/onevm_helper'
cmd=CommandParser::CmdParser.new(ARGV) do
usage "onevm COMMAND [args..] [options..]"
helper = OneVMHelper.new
########################################################################
# Global Options
########################################################################
set :option, CommandParser::OPTIONS
########################################################################
# Formatters for arguments
########################################################################
oneid_desc='OpenNebula Virtual Machine name or id'
set :format, :vmid, oneid_desc do |arg|
helper.to_id(arg)
end
onelistid_desc='Comma-separated list of OpenNebula Virtual Machine names or ids'
set :format, :vmid_list, onelistid_desc do |arg|
helper.list_to_id(arg)
end
filterflag_desc=<<-EOT
a, all all the known Virtual Machine
m, mine the Virtual Machine belonging to the user in ONE_AUTH
g, group 'mine' plus the Virtual Machine belonging to the groups
the user is member of
uid Virtual Machine of the user identified by this uid
user Virtual Machine of the user identified by the username
EOT
set :format, :filterflag, filterflag_desc do |arg|
helper.filterflag_to_i(arg)
end
########################################################################
# Commands
########################################################################
command :cancel, 'Cancel a Virtual Machine', [:range,:vmid_list] do
helper.perform_actions(args[0],options,"canceling") do |vm|
vm.cancel
end
end
command :create, 'Create a new Virtual Machine', :file do
helper.create_resource(args.shift, options)
end
command :delete, 'Removes a Virtual Machine', [:range, :vmid_list] do
helper.perform_actions(args[0],options,"deleted") do |vm|
vm.delete
end
end
# TBD hostid instead of text in the second argument
command :deploy, 'Deploy a Virtual Machine', [:range,:vmid_list], :text do
host_id = args[1]
verbose = "deploying in host #{host_id}"
helper.perform_actions(args[0],options,verbose) do |vm|
vm.deploy(host_id)
end
end
command :hold, 'Hold a Virtual Machine', [:range,:vmid_list] do
helper.perform_actions(args[0],options,"holding") do |vm|
vm.hold
end
end
command :list, 'Lists Virtual Machine in the pool', [:filterflag, nil],
:options=>CLIHelper::OPTIONS<<OpenNebulaHelper::XML do
helper.list_pool(options)
end
# TBD hostid instead of text in the second argument
command :livemigrate, 'Livemigrate a Virtual Machine', [:range,:vmid_list], :text do
host_id = args[1]
verbose = "live migrating to #{host_id}"
helper.perform_actions(args[0],options,verbose) do |vm|
vm.livemigrate(host_id)
end
end
# TBD hostid instead of text in the second argument
command :migrate, 'Migrate a Virtual Machine', [:range,:vmid_list], :text do
host_id = args[1]
verbose = "migrating to #{host_id}"
helper.perform_actions(args[0],options,verbose) do |vm|
vm.migrate(host_id)
end
end
command :release, 'Release a Virtual Machine', [:range,:vmid_list] do
helper.perform_actions(args[0],options,"releasing") do |vm|
vm.release
end
end
command :restart, 'Restart a Virtual Machine', [:range,:vmid_list] do
helper.perform_actions(args[0],options,"restarting") do |vm|
vm.restart
end
end
command :resubmit, 'Resubmit a Virtual Machine', [:range,:vmid_list] do
helper.perform_actions(args[0],options,"resubmiting") do |vm|
vm.restart
end
end
command :resume, 'Resume a Virtual Machine', [:range,:vmid_list] do
helper.perform_actions(args[0],options,"resuming") do |vm|
vm.resume
end
end
command :saveas, 'Save as a Virtual Machine', :vmid, :text do
disk_id = args[1].to_i
image_name = args[2]
verbose = "disk #{disk_id} prepared to be saved in " <<
"the image #{image_name}"
helper.perform_action(args[0],options,verbose) do |vm|
vm.save_as(disk_id, image_name)
end
end
command :show, 'Gets info from a Virtual Machine', :vmid,
:options=>OpenNebulaHelper::XML do
helper.show_resource(args[0],options)
end
command :stop, 'Stop a Virtual Machine', [:range,:vmid_list] do
helper.perform_actions(args[0],options,"stopping") do |vm|
vm.stop
end
end
command :suspend, 'Suspend a Virtual Machine', [:range,:vmid_list] do
helper.perform_actions(args[0],options,"suspending") do |vm|
vm.suspend
end
end
# TBD command :top
end