From 2ea07428822ba5e9eacbb99a8398ebfa738b8e33 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Fri, 10 Jun 2011 12:25:53 +0200 Subject: [PATCH] feature #661: Update oneimage command --- src/cli/one_helper/oneimage_helper.rb | 123 ++++++ src/cli/oneimage | 572 +++++--------------------- 2 files changed, 218 insertions(+), 477 deletions(-) create mode 100644 src/cli/one_helper/oneimage_helper.rb diff --git a/src/cli/one_helper/oneimage_helper.rb b/src/cli/one_helper/oneimage_helper.rb new file mode 100644 index 0000000000..d737aada3d --- /dev/null +++ b/src/cli/one_helper/oneimage_helper.rb @@ -0,0 +1,123 @@ +# -------------------------------------------------------------------------- # +# 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. # +#--------------------------------------------------------------------------- # + +require 'one_helper' + +class OneImageHelper < OpenNebulaHelper::OneHelper + TABLE_CONF_FILE="#{OpenNebulaHelper::TABLE_CONF_PATH}/oneimage.yaml" + + def create_resource(template_file, options) + template=File.read(template_file) + super(template, options) + end + + def self.rname + "IMAGE" + end + + private + + def factory(id=nil) + if id + OpenNebula::Image.new_with_id(id, @client) + else + xml=OpenNebula::Image.build_xml + OpenNebula::Image.new(xml, @client) + end + end + + def factory_pool(user_flag=-2) + OpenNebula::ImagePool.new(@client, user_flag) + end + + def format_resource(image) + str="%-15s: %-20s" + str_h1="%-80s" + + CLIHelper.print_header(str_h1 % "IMAGE #{image['ID']} INFORMATION") + puts str % ["ID", image.id.to_s] + puts str % ["NAME", image.name] + puts str % ["TYPE", image.type_str] + puts str % ["REGISTER TIME", OpenNebulaHelper.time_to_str(image['REGTIME'])] + puts str % ["PUBLIC", OpenNebulaHelper.public_to_str(image['PUBLIC'])] + puts str % ["PERSISTENT", OneImageHelper.persistent_to_str(image["PERSISTENT"])] + puts str % ["SOURCE", image['SOURCE']] + puts str % ["STATE", image.short_state_str] + puts str % ["RUNNING_VMS", image['RUNNING_VMS']] + puts + + CLIHelper.print_header(str_h1 % "IMAGE TEMPLATE",false) + puts image.template_str + end + + def format_pool(pool, options, top=false) + table=CLIHelper::ShowTable.new(TABLE_CONF_FILE, self) do + column :ID, "ONE identifier for the Image", :size=>4 do |d| + d["ID"] + end + + column :NAME, "Name of the Image", :left, :size=>15 do |d| + d["NAME"] + end + + column :USER, "Username of the Virtual Machine owner", :left, :size=>8 do |d| + helper.uid_to_str(d["UID"], options) + end + + column :GROUP, "Group of the Virtual Machine", :left, :size=>8 do |d| + helper.uid_to_str(d["GID"], options) + end + + column :TYPE, "Type of the Image", :size=>4 do |d,e| + d.short_type_str + end + + column :REGTIME, "Registration time of the Image", :size=>20 do |d| + OpenNebulaHelper.time_to_str(d["REGTIME"]) + end + + column :PUBLIC, "Whether the Image is public or not", :size=>3 do |d| + OpenNebulaHelper.public_to_str(d["PUBLIC"]) + end + + column :PERSISTENT, "Whether the Image is persistent or not", :size=>3 do |d| + OneImageHelper.persistent_to_str(d["PUBLIC"]) + end + + column :STAT, "State of the Image", :size=>4 do |d| + d.short_state_str + end + + column :RVMS, "Number of VMs currently running from this Image", :size=>5 do |d| + d['RUNNING_VMS'] + end + + default :ID, :USER, :GROUP, :NAME, :TYPE, :REGTIME, :PUBLIC, :PERSISTENT , :STAT, :RVMS + end + + if top + table.top(pool, options) + else + table.show(pool, options) + end + end + + private + + def self.persistent_to_str + image["PERSISTENT"].to_i==1 ? "Yes" : "No" + end +end \ No newline at end of file diff --git a/src/cli/oneimage b/src/cli/oneimage index 09cd59fd64..f64e4a3980 100755 --- a/src/cli/oneimage +++ b/src/cli/oneimage @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +#!/usr/bin/env ruby # -------------------------------------------------------------------------- # # Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) # @@ -25,499 +25,117 @@ else end $: << RUBY_LIB_LOCATION +$: << RUBY_LIB_LOCATION+"/cli" +require 'command_parser' +require 'one_helper/oneimage_helper' -require 'OpenNebula' -require 'CommandManager' -require 'client_utilities' -require 'command_parse' +cmd=CommandParser::CmdParser.new(ARGV) do + usage "oneimage COMMAND [args..] [options..]" + version OpenNebulaHelper::ONE_VERSION -ShowTableImage={ - :id => { - :name => "ID", - :desc => "ONE identifier for the Image", - :size => 4, - :proc => lambda {|d,e| d.id } - }, - :user=> { - :name => "USER", - :desc => "Name of the owner", - :size => 8, - :proc => lambda {|d,e| - d["USERNAME"] - } - }, - :name => { - :name => "NAME", - :desc => "Name of the Image", - :size => 20, - :proc => lambda {|d,e| - d.name - } - }, - :type => { - :name => "TYPE", - :desc => "Type of the Image", - :size => 4, - :proc => lambda {|d,e| - d.short_type_str - } - }, - :regtime => { - :name => "REGTIME", - :desc => "Registration time of the Image", - :size => 20, - :proc => lambda {|d,e| - str_register_time(d) - } - }, - :public => { - :name => "PUBLIC", - :desc => "Whether the Image is public or not", - :size => 3, - :proc => lambda {|d,e| - if d["PUBLIC"].to_i == 1 then "Yes" else "No" end} - }, - :persistent => { - :name => "PERSISTENT", - :desc => "Whether the Image is persistent or not", - :size => 3, - :proc => lambda {|d,e| - if d["PERSISTENT"].to_i == 1 then "Yes" else "No" end} - }, - :state => { - :name => "STAT", - :desc => "State of the Image", - :size => 4, - :proc => lambda {|d,e| - d.short_state_str - } - }, - :runningvms => { - :name => "#VMS", - :desc => "Number of VMs currently running from this Image", - :size => 5, - :proc => lambda {|d,e| d['RUNNING_VMS'] } - }, + helper = OneImageHelper.new + + ######################################################################## + # Global Options + ######################################################################## + set :option, CommandParser::OPTIONS - :default => [:id, :user, :name, :type, :regtime, - :public, :persistent, :state, :runningvms] -} + list_options = CLIHelper::OPTIONS + list_options << OpenNebulaHelper::XML + list_options << OpenNebulaHelper::NUMERIC - - -class ImageShow - - def initialize(client, filter_flag="-2") - @imagepool=OpenNebula::ImagePool.new(client, filter_flag.to_i) - @table=ShowTable.new(ShowTableImage) + ######################################################################## + # Formatters for arguments + ######################################################################## + set :format, :imageid, OneImageHelper.to_id_desc do |arg| + helper.to_id(arg) end - - def header_image_small - scr_bold - scr_underline - print @table.header_str - scr_restore - puts "" + + set :format, :imageid_list, OneImageHelper.list_to_id_desc do |arg| + helper.list_to_id(arg) end - - def top(options=nil) - delay=1 - delay=options[:delay] if options && options[:delay] - - result=nil - - begin - while true - scr_cls - scr_move(0,0) - result=list_short(options) - sleep delay - end - rescue Exception + + set :format, :filterflag, OneImageHelper.filterflag_to_i_desc do |arg| + helper.filterflag_to_i(arg) + end + + ######################################################################## + # Commands + ######################################################################## + command :create, 'Registers an Image', :file do + helper.create_resource(args[0], options) + end + + command :show, 'Gets info from an Image', :imageid, :options=>OpenNebulaHelper::XML do + helper.show_resource(args[0],options) + end + + command :list, 'Lists Images in the pool', [:filterflag, nil], :options=>list_options do + helper.list_pool(options) + end + + command :publish, 'Publishes an Image', [:range,:imageid_list] do + helper.perform_actions(args[0],options,"published") do |image| + image.publish end - result end - - def list_short(options=nil) - res=@imagepool.info() - if options - @table.columns=options[:columns] if options[:columns] + command :unpublish, 'Unpublishes an Image', [:range,:imageid_list] do + helper.perform_actions(args[0],options,"unpublished") do |image| + image.unpublish end + end - if OpenNebula.is_error?(res) - result=res - puts res.message - exit -1 - else + command :persistent, 'Makes an Image persistent', [:range,:imageid_list] do + helper.perform_actions(args[0],options,"made persistent") do |image| + image.publish + end + end - if options[:filter_flag] - vms=@imagepool.select{|element| - element['USERNAME']==options[:filter_flag] } - else - vms=@imagepool - end + command :enable, 'Enable an Image', [:range,:imageid_list] do + helper.perform_actions(args[0],options,"enabled") do |image| + image.enable + end + end - result=[true, ""] - header_image_small + command :disable, 'Disable an Image', [:range,:imageid_list] do + helper.perform_actions(args[0],options,"disabled") do |image| + image.disable + end + end - if options - puts @table.data_str(vms, options) - else - puts @table.data_str(vms) - end - - result + command :nonpersistent, 'Make an Image non persistent', [:range,:imageid_list] do + helper.perform_actions(args[0],options,"made non persistent") do |image| + image.unpublish + end + end + + command :update, 'Modifies an Image attribute', :imageid, :text, :text do + helper.perform_action(args[0],options,"modified") do |image| + image.update(args[1], args[2]) + end + end + + command :addattr, 'Add a new Image attribute', :imageid, :text, :text do + helper.perform_action(args[0],options,"att ribute added") do |image| + image.update(args[1], args[2]) + end + end + + command :rmattr, 'Delete an Image attribute', :imageid, :text do + helper.perform_action(args[0],options,"attribute removed") do |image| + image.remove_attr(args[1]) + end + end + + command :delete, 'Removes an Image', [:range, :imageid_list] do + helper.perform_actions(args[0],options,"deleted") do |image| + image.delete end end - def top(options=nil) - delay=1 - delay=options[:delay] if options && options[:delay] - - result=nil - - begin - while true - scr_cls - scr_move(0,0) - result=list_short(options) - sleep delay - end - rescue Exception - end - result + command :top, 'Tops Images in the pool', [:filterflag, nil], :options=>list_options do + helper.list_pool(options, true) end end - - -########################## -## COMMAND LINE PARSING ## -########################## - -class OneImageParse < CommandParse - - COMMANDS_HELP=<<-EOT - -Description: - -This command enables the user to manage images. - - -Commands: - -* register (Registers an image, copying it to the repository if it applies) - oneimage register