Feature #1691: Add attach/detach nic methods to CLI and Ruby OCA

This commit is contained in:
Carlos Martín committed 2012-12-13 13:41:38 +01:00
1 parent c60436e916
commit 7841190b27
3 files changed
+98 -13

No files matched your search

+11
View File
@@ -36,6 +36,17 @@ class OneVMHelper < OpenNebulaHelper::OneHelper
}
}
NETWORK = {
:name => "network",
:short => "-n id|name",
:large => "--network id|name" ,
:description => "Selects the virtual network",
:format => String,
:proc => lambda { |o, options|
OpenNebulaHelper.rname_to_id(o, "VNET")
}
}
FILE = {
:name => "file",
:short => "-f file",
+56
View File
@@ -56,6 +56,15 @@ cmd=CommandParser::CmdParser.new(ARGV) do
:description => "Device where the image will be attached"
}
IP={
:name => "ip",
:short => "-i ip",
:large => "--ip ip",
:format => String,
:description => "IP address for the new NIC"
}
########################################################################
# Global Options
########################################################################
@@ -455,6 +464,53 @@ cmd=CommandParser::CmdParser.new(ARGV) do
end
end
attachnic_desc = <<-EOT.unindent
Attaches a NIC to a running VM
States: RUNNING
EOT
command :attachnic, attachnic_desc, :vmid,
:options => [OneVMHelper::FILE, OneVMHelper::NETWORK, IP] do
if options[:file].nil? and options[:network].nil?
STDERR.puts "Provide a template file or a network:"
STDERR.puts "\t--file <file>"
STDERR.puts "\t--network <network>"
exit -1
end
if options[:file]
template = File.read(options[:file])
else
network_id = options[:network]
ip = options[:ip]
if ip
template = "NIC = [ NETWORK_ID = #{network_id}, IP = #{ip} ]"
else
template = "NIC = [ NETWORK_ID = #{network_id} ]"
end
end
helper.perform_action(args[0],options,"Attach NIC") do |vm|
vm.attachnic(template)
end
end
detachnic_desc = <<-EOT.unindent
Detaches a NIC from a running VM
States: RUNNING
EOT
command :detachnic, detachnic_desc, :vmid, :nicid do
nicid = args[1].to_i
helper.perform_action(args[0],options,"Detach NIC") do |vm|
vm.detachnic(nicid)
end
end
chgrp_desc = <<-EOT.unindent
Changes the VM group
EOT
+31 -13
View File
@@ -35,7 +35,9 @@ module OpenNebula
:chmod => "vm.chmod",
:monitoring => "vm.monitoring",
:attach => "vm.attach",
:detach => "vm.detach"
:detach => "vm.detach",
:attachnic => "vm.attachnic",
:detachnic => "vm.detachnic"
}
VM_STATE=%w{INIT PENDING HOLD ACTIVE STOPPED SUSPENDED DONE FAILED
@@ -202,23 +204,39 @@ module OpenNebula
end
# Attaches a disk to a running VM
def attachdisk(disk)
return Error.new('ID not defined') if !@pe_id
rc = @client.call(VM_METHODS[:attach], @pe_id, disk)
rc = nil if !OpenNebula.is_error?(rc)
return rc
#
# @param disk_template [String] Template containing a DISK element
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def attachdisk(disk_template)
return call(VM_METHODS[:attach], @pe_id, disk_template)
end
# Detaches a disk from a running VM
def detachdisk(disk)
return Error.new('ID not defined') if !@pe_id
#
# @param disk_id [Integer] Id of the disk to be detached
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def detachdisk(disk_id)
return call(VM_METHODS[:detach], @pe_id, disk_id)
end
rc = @client.call(VM_METHODS[:detach], @pe_id, disk)
rc = nil if !OpenNebula.is_error?(rc)
# Attaches a NIC to a running VM
#
# @param nic_template [String] Template containing a NIC element
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def attachnic(nic_template)
return call(VM_METHODS[:attachnic], @pe_id, nic_template)
end
return rc
# Detaches a NIC from a running VM
#
# @param disk_id [Integer] Id of the NIC to be detached
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def detachnic(nic_id)
return call(VM_METHODS[:detachnic], @pe_id, nic_id)
end
# Deletes a VM from the pool