From d6c547a35f24c2e33cb2f105f67b227b4ca46e90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tino=20V=C3=A1zquez?= Date: Tue, 15 Jun 2010 19:31:54 +0200 Subject: [PATCH] feature #200: Initial commit of "oneimage" command --- src/cli/client_utilities.rb | 7 + src/cli/oneimage | 369 ++++++++++++++++++++++++++++++++++++ src/oca/ruby/OpenNebula.rb | 2 + 3 files changed, 378 insertions(+) create mode 100755 src/cli/oneimage diff --git a/src/cli/client_utilities.rb b/src/cli/client_utilities.rb index c73d506abc..4419b9dd91 100644 --- a/src/cli/client_utilities.rb +++ b/src/cli/client_utilities.rb @@ -297,6 +297,13 @@ def str_running_time(data) "%02d %02d:%02d:%02d" % [dtime.yday-1, dtime.hour, dtime.min, dtime.sec] end +def str_register_time(data) + regtime=Time.at(data["REGTIME"].to_i) + + "%02d %02d:%02d:%02d" % [regtime.yday-1, regtime.hour, + regtime.min, regtime.sec] +end + REG_RANGE=/(.*)\[(\d+)([+-])(\d+)\](.*)/ diff --git a/src/cli/oneimage b/src/cli/oneimage new file mode 100755 index 0000000000..b96ce18d71 --- /dev/null +++ b/src/cli/oneimage @@ -0,0 +1,369 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2002-2010, 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 + + +require 'OpenNebula' +require 'client_utilities' +require 'command_parse' + + +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 => 8, + :proc => lambda {|d,e| + d.name + } + }, + :type => { + :name => "TYPE", + :desc => "Type of the Image", + :size => 2, + :proc => lambda {|d,e| + d.short_type_str + } + }, + :regtime => { + :name => "REGTIME", + :desc => "Registration time of the Image", + :size => 3, + :proc => lambda {|d,e| + str_register_time(d) + } + }, + :public => { + :name => "PUBLIC", + :desc => "Whether the Image is public or not", + :size => 1, + :proc => lambda {|d,e| if d["PUBLIC"].to_i == 1 then "Y" else "N" end} + }, + :state => { + :name => "ST", + :desc => "State of the Image", + :size => 2, + :proc => lambda {|d,e| + d.short_state_str + } + }, + :runningvms => { + :name => "RUNNING_VMS", + :desc => "Number of VMs currently running from this Image", + :size => 2, + :proc => lambda {|d,e| d['RUNNING_VMS'] } + }, + + :default => [:id, :user, :name, :type, :regtime, + :public, :state, :runningvms] +} + + + +class ImageShow + + def initialize(client, filter_flag="-2") + @imagepool=OpenNebula::ImagePool.new(client, filter_flag.to_i) + @table=ShowTable.new(ShowTableImage) + end + + def header_image_small + scr_bold + scr_underline + print @table.header_str + scr_restore + puts "" + end + + def list_short(options=nil) + res=@imagepool.info() + + if options + @table.columns=options[:columns] if options[:columns] + end + + if OpenNebula.is_error?(res) + result=res + puts res.message + exit -1 + else + + if options[:filter_flag] + vms=@imagepool.select{|element| + element['USERNAME']==options[:filter_flag] } + else + vms=@imagepool + end + + result=[true, ""] + header_vm_small + + if options + puts @table.data_str(vms, options) + else + puts @table.data_str(vms) + end + + result + 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 + end +end + + +########################## +## COMMAND LINE PARSING ## +########################## + +class OneImageParse < CommandParse + + COMMANDS_HELP=<<-EOT +Commands: + +* register (Registers an image, copying it to the repository if it applys) + oneimage register