From 7841190b274ceddc254100856c34fc68b033f7f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Thu, 13 Dec 2012 13:41:38 +0100 Subject: [PATCH] Feature #1691: Add attach/detach nic methods to CLI and Ruby OCA --- src/cli/one_helper/onevm_helper.rb | 11 +++++ src/cli/onevm | 56 +++++++++++++++++++++++ src/oca/ruby/OpenNebula/VirtualMachine.rb | 44 ++++++++++++------ 3 files changed, 98 insertions(+), 13 deletions(-) diff --git a/src/cli/one_helper/onevm_helper.rb b/src/cli/one_helper/onevm_helper.rb index e6bafadab4..594ed7f11f 100644 --- a/src/cli/one_helper/onevm_helper.rb +++ b/src/cli/one_helper/onevm_helper.rb @@ -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", diff --git a/src/cli/onevm b/src/cli/onevm index 0e1f3c15de..a506f04952 100755 --- a/src/cli/onevm +++ b/src/cli/onevm @@ -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 " + STDERR.puts "\t--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 diff --git a/src/oca/ruby/OpenNebula/VirtualMachine.rb b/src/oca/ruby/OpenNebula/VirtualMachine.rb index 5e7c061c8b..130142c64b 100644 --- a/src/oca/ruby/OpenNebula/VirtualMachine.rb +++ b/src/oca/ruby/OpenNebula/VirtualMachine.rb @@ -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