1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-02-10 13:57:22 +03:00

feature #661: Add numeric option and top command

This commit is contained in:
Daniel Molina 2011-06-09 16:34:51 +02:00
parent 97871ef581
commit 08af7304af
6 changed files with 146 additions and 121 deletions

View File

@ -92,7 +92,7 @@ module CLIHelper
end
class ShowTable
include CLIHelper
require 'yaml'
def initialize(conf=nil, ext=nil, &block)
# merge del conf con la table
@ -142,12 +142,13 @@ module CLIHelper
begin
while true
scr_cls
scr_move(0,0)
CLIHelper.scr_cls
CLIHelper.scr_move(0,0)
show(data, options)
sleep delay
end
rescue Exception
rescue Exception => e
puts e.message
end
end
@ -179,7 +180,7 @@ module CLIHelper
def data_array(data, options)
res_data=data.collect {|d|
@default_columns.collect {|c|
@columns[c][:proc].call(d,@ext).to_s if @columns[c]
@columns[c][:proc].call(d).to_s if @columns[c]
}
}

View File

@ -45,6 +45,22 @@ EOT
:description => "Show the resource in xml format"
}
NUMERIC={
:name => "numeric",
:short => "-n",
:large => "--numeric",
:description => "Do not translate user and group IDs"
}
KILOBYTES={
:name => "kilobytes",
:short => "-k",
:large => "--kilobytes",
:description => "Show units in kilobytes"
}
OPTIONS = XML, NUMERIC, KILOBYTES
class OneHelper
def initialize
@client = OpenNebula::Client.new
@ -63,7 +79,7 @@ EOT
end
end
def list_pool(options)
def list_pool(options, top=false)
user_flag = options[:filter_flag] ? options[:filter_flag] : -2
pool = factory_pool(user_flag)
@ -73,8 +89,7 @@ EOT
if options[:xml]
return 0, pool.to_xml(true)
else
generate_translation_hash
format_pool(pool, options)
format_pool(pool, options, top)
return 0
end
end
@ -86,7 +101,6 @@ EOT
if options[:xml]
return 0, resource.to_xml(true)
else
generate_translation_hash
format_resource(resource)
return 0
end
@ -120,25 +134,14 @@ EOT
end
########################################################################
# Formatters descriptions
# Id translation
########################################################################
def self.filter_flag_desc
desc=<<-EOT
a, all all the known #{self.rname}s
m, mine the #{self.rname} belonging to the user in ONE_AUTH
g, group 'mine' plus the #{self.rname} belonging to the groups
the user is member of
uid #{self.rname} of the user identified by this uid
user #{self.rname} of the user identified by the username
EOT
def uid_to_str(uid, options)
rid_to_str(:users, uid, options)
end
def self.oneid_list_desc
"Comma-separated list of OpenNebula #{self.rname} names or ids"
end
def self.oneid_desc
"OpenNebula #{self.rname} name or id"
def gid_to_str(gid, options)
rid_to_str(:groups, gid, options)
end
########################################################################
@ -170,6 +173,10 @@ EOT
return 0, result
end
def self.to_id_desc
"OpenNebula #{self.rname} name or id"
end
def list_to_id(names)
user_flag = -2
pool = factory_pool(user_flag)
@ -188,6 +195,10 @@ EOT
return 0, result
end
def self.list_to_id_desc
"Comma-separated list of OpenNebula #{self.rname} names or ids"
end
def filterflag_to_i(str)
filter_flag = case str
when "a", "all" then "-2"
@ -197,8 +208,7 @@ EOT
if str.match(/^[0123456789]+$/)
str
else
generate_translation_hash
user = @translation_hash[:users].select { |k,v| v==str }
user = translation_hash[:users].select { |k,v| v==str }
user.length > 0 ? user.first.first : "-2"
end
end
@ -206,6 +216,17 @@ EOT
return 0, filter_flag
end
def self.filterflag_to_i_desc
desc=<<-EOT
a, all all the known #{self.rname}s
m, mine the #{self.rname} belonging to the user in ONE_AUTH
g, group 'mine' plus the #{self.rname} belonging to the groups
the user is member of
uid #{self.rname} of the user identified by this uid
user #{self.rname} of the user identified by the username
EOT
end
private
def retrieve_resource(id)
@ -215,35 +236,32 @@ EOT
OpenNebula.is_error?(rc) ? rc : resource
end
def generate_translation_hash
def translation_hash
@translation_hash ||= {
:users => generate_user_translation,
:groups => generate_group_translation
:users => generate_resource_translation(UserPool),
:groups => generate_resource_translation(GroupPool)
}
end
def generate_user_translation
user_pool = UserPool.new(@client)
user_pool.info
def generate_resource_translation(pool)
p = pool.new(@client)
p.info
hash = Hash.new
user_pool.each { |user|
hash[user["ID"]]=user["NAME"]
}
p.each { |r| hash[r["ID"]]=r["NAME"] }
hash
end
def generate_group_translation
group_pool = GroupPool.new(@client)
group_pool.info
hash = Hash.new
group_pool.each { |group|
hash[group["ID"]]=group["NAME"]
}
hash
def rid_to_str(resource, id, options)
if options[:numeric]
id
else
if name = translation_hash[resource][id]
name
else
id
end
end
end
end
@ -255,22 +273,6 @@ EOT
end
end
def OpenNebulaHelper.uid_to_str(uid, hash={})
if hash[:users] && hash[:users][uid]
hash[:users][uid]
else
uid
end
end
def OpenNebulaHelper.gid_to_str(gid, hash={})
if hash[:groups] && hash[:groups][gid]
hash[:groups][gid]
else
gid
end
end
def OpenNebulaHelper.time_to_str(time)
value=time.to_i
if value==0
@ -279,4 +281,24 @@ EOT
value=Time.at(value).strftime("%m/%d %H:%M:%S")
end
end
BinarySufix = ["K", "M", "G", "T" ]
def OpenNebulaHelper.unit_to_str(value, options)
if options[:kilobytes]
value
else
i=0
while value > 1024 && i < 3 do
value /= 1024.0
i+=1
end
value = (value * 10).round / 10.0
value = value.to_i if value - value.round == 0
st = value.to_s + BinarySufix[i]
end
end
end

View File

@ -72,41 +72,41 @@ class OneVMHelper < OpenNebulaHelper::OneHelper
puts vm.template_str
end
def format_pool(pool, options)
st=CLIHelper::ShowTable.new(nil, @translation_hash) do
column :ID, "ONE identifier for Virtual Machine", :size=>4 do |d,e|
def format_pool(pool, options, top=false)
table=CLIHelper::ShowTable.new(TABLE_CONF_FILE, self) do
column :ID, "ONE identifier for Virtual Machine", :size=>4 do |d|
d["ID"]
end
column :NAME, "Name of the Virtual Machine", :left, :size=>15 do |d,e|
column :NAME, "Name of the Virtual Machine", :left, :size=>15 do |d|
d["NAME"]
end
column :USER, "Username of the Virtual Machine owner", :left, :size=>8 do |d,e|
OpenNebulaHelper.uid_to_str(d["UID"],e)
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,e|
OpenNebulaHelper.gid_to_str(d["GID"],e)
column :GROUP, "Group of the Virtual Machine", :left, :size=>8 do |d|
helper.uid_to_str(d["GID"], options)
end
column :STAT, "Actual status", :size=>4 do |d,e|
d.status
end
column :CPU, "CPU percentage used by the VM", :size=>3 do |d,e|
column :CPU, "CPU percentage used by the VM", :size=>3 do |d|
d["CPU"]
end
column :MEM, "Memory used by the VM", :size=>7 do |d,e|
d["MEMORY"]
column :MEM, "Memory used by the VM", :size=>7 do |d|
OpenNebulaHelper.unit_to_str(d["MEMORY"].to_i, options)
end
column :HOSTNAME, "Host where the VM is running", :size=>15 do |d,e|
column :HOSTNAME, "Host where the VM is running", :size=>15 do |d|
d["HISTORY/HOSTNAME"]
end
column :TIME, "Time since the VM was submitted", :size=>11 do |d,e|
column :TIME, "Time since the VM was submitted", :size=>11 do |d|
stime = Time.at(d["STIME"].to_i)
etime = d["ETIME"]=="0" ? Time.now : Time.at(d["ETIME"].to_i)
dtime = Time.at(etime-stime).getgm
@ -116,6 +116,10 @@ class OneVMHelper < OpenNebulaHelper::OneHelper
default :ID, :USER, :GROUP, :NAME, :STAT, :CPU, :MEM, :HOSTNAME, :TIME
end
st.show(pool, options)
if top
table.top(pool, options)
else
table.show(pool, options)
end
end
end

View File

@ -65,56 +65,51 @@ class OneVNetHelper < OpenNebulaHelper::OneHelper
end
end
def format_pool(pool, options)
st=CLIHelper::ShowTable.new(nil, @translation_hash) do
column :ID, "ONE identifier for Virtual Network", :size=>4 do |d,e|
def format_pool(pool, options, top=false)
table=CLIHelper::ShowTable.new(TABLE_CONF_FILE, self) do
column :ID, "ONE identifier for Virtual Network", :size=>4 do |d|
d["ID"]
end
column :NAME, "Name of the Virtual Network", :left, :size=>15 do |d,e|
column :NAME, "Name of the Virtual Network", :left, :size=>15 do |d|
d["NAME"]
end
column :USER, "Username of the Virtual Network owner", :left, :size=>8 do |d,e|
OpenNebulaHelper.uid_to_str(d["UID"],e)
column :USER, "Username of the Virtual Network owner", :left, :size=>8 do |d|
helper.uid_to_str(d["UID"], options)
end
column :GROUP, "Group of the Virtual Network", :left, :size=>8 do |d,e|
OpenNebulaHelper.gid_to_str(d["GID"],e)
column :GROUP, "Group of the Virtual Network", :left, :size=>8 do |d|
helper.uid_to_str(d["GID"], options)
end
column :TYPE, "Type of Virtual Network", :size=>6 do |d,e|
OneVNetHelper.type_to_str(d["TYPE"])
column :TYPE, "Type of Virtual Network", :size=>6 do |d|
d.type_str
end
column :SIZE, "Size of the Virtual Network", :size=>6 do |d,e|
column :SIZE, "Size of the Virtual Network", :size=>6 do |d|
d["SIZE"]
end
column :BRIDGE, "Bridge associated to the Virtual Network", :size=>6 do |d,e|
column :BRIDGE, "Bridge associated to the Virtual Network", :size=>6 do |d|
d["BRIDGE"]
end
column :PUBLIC, "Whether the Virtual Network is public or not", :size=>1 do |d,e|
column :PUBLIC, "Whether the Virtual Network is public or not", :size=>1 do |d|
OpenNebulaHelper.public_to_str(d['PUBLIC'])
end
column :LEASES, "Number of this Virtual Network's given leases", :size=>7 do |d,e|
column :LEASES, "Number of this Virtual Network's given leases", :size=>7 do |d|
d["TOTAL_LEASES"]
end
default :ID, :USER, :GROUP, :NAME, :TYPE, :BRIDGE, :PUBLIC, :LEASES
end
st.show(pool, options)
end
# TBD move this method to VirtualNetwork.rb (OCA)
def self.type_to_str(type)
if type=="0"
return "Ranged"
elsif type=="1"
return "Fixed"
if top
table.top(pool, options)
else
table.show(pool, options)
end
end
end

View File

@ -44,15 +44,15 @@ cmd=CommandParser::CmdParser.new(ARGV) do
########################################################################
# Formatters for arguments
########################################################################
set :format, :vmid, OneVMHelper.oneid_desc do |arg|
set :format, :vmid, OneVMHelper.to_id_desc do |arg|
helper.to_id(arg)
end
set :format, :vmid_list, OneVMHelper.oneid_list_desc do |arg|
set :format, :vmid_list, OneVMHelper.list_to_id_desc do |arg|
helper.list_to_id(arg)
end
set :format, :filterflag, OneVMHelper.filter_flag_desc do |arg|
set :format, :filterflag, OneVMHelper.filterflag_to_i_desc do |arg|
helper.filterflag_to_i(arg)
end
@ -92,7 +92,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
end
command :list, 'Lists Virtual Machine in the pool', [:filterflag, nil],
:options=>CLIHelper::OPTIONS<<OpenNebulaHelper::XML do
:options=>CLIHelper::OPTIONS+OpenNebulaHelper::OPTIONS do
helper.list_pool(options)
end
@ -168,5 +168,8 @@ cmd=CommandParser::CmdParser.new(ARGV) do
end
end
# TBD command :top
command :top, 'Tops Virtual Machine in the pool', [:filterflag, nil],
:options=>CLIHelper::OPTIONS+OpenNebulaHelper::OPTIONS do
helper.list_pool(options, true)
end
end

View File

@ -44,15 +44,15 @@ cmd=CommandParser::CmdParser.new(ARGV) do
########################################################################
# Formatters for arguments
########################################################################
set :format, :vnetid, OneVNetHelper.oneid_desc do |arg|
set :format, :vnetid, OneVNetHelper.to_id_desc do |arg|
helper.to_id(arg)
end
set :format, :vnetid_list, OneVNetHelper.oneid_list_desc do |arg|
set :format, :vnetid_list, OneVNetHelper.list_to_id_desc do |arg|
helper.list_to_id(arg)
end
set :format, :filterflag, OneVNetHelper.filter_flag_desc do |arg|
set :format, :filterflag, OneVNetHelper.filterflag_to_i_desc do |arg|
helper.filterflag_to_i(arg)
end
@ -60,45 +60,45 @@ cmd=CommandParser::CmdParser.new(ARGV) do
# Commands
########################################################################
command :create, 'Create a new Virtual Network', :file do
helper.create_resource(args.shift, options)
helper.create_resource(args[0], options)
end
command :show, 'Gets info from a Virtual Network', :oneid,
command :show, 'Gets info from a Virtual Network', :vnetid,
:options=>OpenNebulaHelper::XML do
helper.show_resource(args.shift,options)
helper.show_resource(args[0],options)
end
command :list, 'Lists Virtual Networks in the pool', [:filterflag, nil],
:options=>CLIHelper::OPTIONS<<OpenNebulaHelper::XML do
:options=>CLIHelper::OPTIONS+OpenNebulaHelper::OPTIONS do
helper.list_pool(options)
end
command :publish, 'Publishes a Virtual Network', [:range,:vnetid_list] do
helper.perform_actions(args[0],nil,options,"published") do |vn|
helper.perform_actions(args[0],options,"published") do |vn|
vn.publish
end
end
command :unpublish, 'Unpublishes a Virtual Network', [:range,:vnetid_list] do
helper.perform_actions(args[0],nil,options,"unpublished") do |vn|
helper.perform_actions(args[0],options,"unpublished") do |vn|
vn.unpublish
end
end
command :addleases, 'Adds a lease to the Virtual Network', :oneid, :text, [:text, nil] do
helper.perform_action(args.shift,args,options,"lease added") do |vn|
vn.addleases(args[0], args[1])
command :addleases, 'Adds a lease to the Virtual Network', :vnetid, :text, [:text, nil] do
helper.perform_action(args[0],options,"lease added") do |vn|
vn.addleases(args[1], args[2])
end
end
command :rmleases, 'Removes a lease from the Virtual Network', :oneid, :text do
helper.perform_action(args.shift,args,options,"lease removed") do |vn|
vn.rmleases(args[0])
command :rmleases, 'Removes a lease from the Virtual Network', :vnetid, :text do
helper.perform_action(args[0],options,"lease removed") do |vn|
vn.rmleases(args[1])
end
end
command :delete, 'Removes a Virtual Network', [:range, :vnetid_list] do
helper.perform_actions(args[0],nil,options,"deleted") do |vn|
helper.perform_actions(args[0],options,"deleted") do |vn|
vn.delete
end
end