From e2927e871abdb0d1351008c15689c006840b4e99 Mon Sep 17 00:00:00 2001 From: Jaime Melis Date: Fri, 15 Jun 2012 13:27:06 +0200 Subject: [PATCH] Feature #1223: Add OCA and CLI for onevm attach and onevm detach --- src/cli/command_parser.rb | 4 ++ src/cli/one_helper/onevm_helper.rb | 18 ++++++++ src/cli/onevm | 53 +++++++++++++++++++++-- src/oca/ruby/OpenNebula/VirtualMachine.rb | 28 ++++++++++-- 4 files changed, 97 insertions(+), 6 deletions(-) diff --git a/src/cli/command_parser.rb b/src/cli/command_parser.rb index efe463d962..7c844738f1 100644 --- a/src/cli/command_parser.rb +++ b/src/cli/command_parser.rb @@ -405,6 +405,10 @@ EOT arg.instance_of?(String) ? [0,arg] : [-1] end + def format_int(arg) + arg.match(/^\d+$/) ? [0,arg] : [-1] + end + def format_file(arg) File.file?(arg) ? [0,arg] : [-1] end diff --git a/src/cli/one_helper/onevm_helper.rb b/src/cli/one_helper/onevm_helper.rb index b7d32be70d..99f638ac1f 100644 --- a/src/cli/one_helper/onevm_helper.rb +++ b/src/cli/one_helper/onevm_helper.rb @@ -25,6 +25,24 @@ class OneVMHelper < OpenNebulaHelper::OneHelper :description => "Instance multiple VMs" } + IMAGE = { + :name => "image", + :short => "-i id|name", + :large => "--image id|name" , + :description => "Selects the image", + :format => String, + :proc => lambda { |o, options| + rc, imid = OpenNebulaHelper.rname_to_id(o, "IMAGE") + if rc == 0 + options[:image] = imid + else + puts imid + puts "option image: Parsing error" + exit -1 + end + } + } + def self.rname "VM" end diff --git a/src/cli/onevm b/src/cli/onevm index 720d7a1f84..7bfa063a0f 100755 --- a/src/cli/onevm +++ b/src/cli/onevm @@ -77,6 +77,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do helper.filterflag_to_i(arg) end + set :format, :diskid, "Integer" do |arg| + format_int(arg) + end + ######################################################################## # Commands ######################################################################## @@ -206,7 +210,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do vm.reset end end - + deploy_desc = <<-EOT.unindent Deploys the given VM in the specified Host. This command forces the deployment, in a standard installation the Scheduler is in charge @@ -334,6 +338,49 @@ cmd=CommandParser::CmdParser.new(ARGV) do end end + attachdisk_desc = <<-EOT.unindent + Attaches a disk to a running VM + + States: RUNNING + EOT + + command :attachdisk, attachdisk_desc, :vmid, [:file, nil], :options => OneVMHelper::IMAGE do + + template_file = args[1] + + if template_file.nil? and options[:image].nil? + STDERR.puts "Provide a template file or an image:" + STDERR.puts "\t" + STDERR.puts "\t--image " + exit -1 + end + + if template_file.nil? + image_id = options[:image] + template = "DISK = [ IMAGE_ID = #{image_id} ]" + else + template = File.read(template_file) + end + + helper.perform_action(args[0],options,"Attach disk") do |vm| + vm.attachdisk(template) + end + end + + detachdisk_desc = <<-EOT.unindent + Detaches a disk from a running VM + + States: RUNNING + EOT + + command :detachdisk, detachdisk_desc, :vmid, :diskid do + diskid = args[1].to_i + + helper.perform_action(args[0],options,"Detach disk") do |vm| + vm.detachdisk(diskid) + end + end + chgrp_desc = <<-EOT.unindent Changes the VM group EOT @@ -367,7 +414,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do end resched_desc = <<-EOT.unindent - Sets the rescheduling flag for the VM. + Sets the rescheduling flag for the VM. States: RUNNING EOT @@ -379,7 +426,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do end unresched_desc = <<-EOT.unindent - Clears the rescheduling flag for the VM. + Clears the rescheduling flag for the VM. States: RUNNING EOT diff --git a/src/oca/ruby/OpenNebula/VirtualMachine.rb b/src/oca/ruby/OpenNebula/VirtualMachine.rb index fd8f126366..69d0855800 100644 --- a/src/oca/ruby/OpenNebula/VirtualMachine.rb +++ b/src/oca/ruby/OpenNebula/VirtualMachine.rb @@ -33,7 +33,9 @@ module OpenNebula :savedisk => "vm.savedisk", :chown => "vm.chown", :chmod => "vm.chmod", - :monitoring => "vm.monitoring" + :monitoring => "vm.monitoring", + :attach => "vm.attach", + :detach => "vm.detach" } VM_STATE=%w{INIT PENDING HOLD ACTIVE STOPPED SUSPENDED DONE FAILED} @@ -154,7 +156,7 @@ module OpenNebula def reset action('reset') end - + # Cancels a running VM def cancel action('cancel') @@ -185,6 +187,26 @@ module OpenNebula action('resume') 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 + end + + # Detaches a disk from a running VM + def detachdisk(disk) + return Error.new('ID not defined') if !@pe_id + + rc = @client.call(VM_METHODS[:detach], @pe_id, disk) + rc = nil if !OpenNebula.is_error?(rc) + + return rc + end + # Deletes a VM from the pool def finalize action('finalize') @@ -291,7 +313,7 @@ module OpenNebula # @example # vm.monitoring( ['CPU', 'NET_TX', 'TEMPLATE/CUSTOM_PROBE'] ) # - # { "NET_TX" => + # { "NET_TX" => # [["1337264510", "210"], # ["1337264553", "220"], # ["1337264584", "230"]],