1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-23 17:33:56 +03:00

feature #661: Update oneimage command

This commit is contained in:
Daniel Molina 2011-06-10 12:25:53 +02:00
parent fd9de4704b
commit 2ea0742882
2 changed files with 218 additions and 477 deletions

View File

@ -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

View File

@ -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 <template>
template is a file name where the Image description is located
* addattr (Add a new image attribute)
oneimage addattr <image_id> <attribute_name> <attribute_value>
* update (Modifies an image attribute)
oneimage update <image_id> <attribute_name> <attribute_value>
* rmattr (Deletes an Image attribute)
oneimage rmattr <image_id> <attribute_name>
* enable (Enabled an Image)
oneimage enable <image_id>
* disable (Disabled an Image)
oneimage disable <image_id>
* publish (Publish an Image)
oneimage publish <image_id>
* unpublish (Unpublish an Image)
oneimage unpublish <image_id>
* persistent (Makes an Image persistent)
oneimage persistent <image_id>
* nonpersistent (Makes an Image non persistent)
oneimage nonpersistent <image_id>
* list (Shows Images in the pool)
oneimage list <filter_flag>
where filter_flag can be
a, all --> all the known Images
m, mine --> the Images belonging to the user in ONE_AUTH
and all the Public Images
uid --> Images of the user identified by this uid
user --> Images of the user identified by the username
* top (Lists Images continuously)
oneimage top
* show (Gets information about an specific Image)
oneimage show <image_id>
* delete (Deletes an already deployed Image)
oneimage delete <image_id>
EOT
def text_commands
COMMANDS_HELP
end
def text_command_name
"oneimage"
end
def list_options
table=ShowTable.new(ShowTableImage)
table.print_help
end
end
def get_user_flags
ops=Hash.new
if ARGV[0]
case ARGV[0]
when "a", "all"
ops[:filter_user]="-2"
when "m", "mine"
ops[:filter_user]="-1"
else
if !ARGV[0].match(/^[0123456789]+$/)
ops[:filter_user]="-2"
ops[:filter_flag]=ARGV[0]
else
ops[:filter_user]=ARGV[0]
end
end
else
ops[:filter_user]="-2"
end
ops
end
def get_template(template_path)
begin
template = File.read(ARGV[0])
rescue
result = OpenNebula::Error.new("Can not read template: #{ARGV[0]}")
end
if !is_successful?(result)
puts result.message
exit -1
end
return template
end
oneimage_opts=OneImageParse.new
oneimage_opts.parse(ARGV)
ops=oneimage_opts.options
result=[false, "Unknown error"]
command=ARGV.shift
case command
when "register", "create", "add"
check_parameters("register", 1)
template = get_template(ARGV[0])
image = OpenNebula::Image.new(OpenNebula::Image.build_xml, get_one_client)
result = image.allocate(template)
if is_successful?(result)
puts "ID: " + image.id.to_s if ops[:verbose]
end
when "update", "addattr"
check_parameters("update", 3)
image_id=get_image_id(ARGV[0])
image=OpenNebula::Image.new_with_id(image_id, get_one_client)
result=image.update(ARGV[1],ARGV[2])
if is_successful?(result)
puts "Modified image" if ops[:verbose]
end
when "rmattr"
check_parameters("rmattr", 2)
image_id=get_image_id(ARGV[0])
image=OpenNebula::Image.new_with_id(image_id, get_one_client)
result=image.remove_attr(ARGV[1])
if is_successful?(result)
puts "Removed image atrribute" if ops[:verbose]
end
when "enable"
check_parameters("enable", 1)
image_id=get_image_id(ARGV[0])
image=OpenNebula::Image.new_with_id(image_id, get_one_client)
result=image.enable
if is_successful?(result)
puts "Image enabled" if ops[:verbose]
end
when "disable"
check_parameters("disable", 1)
image_id=get_image_id(ARGV[0])
image=OpenNebula::Image.new_with_id(image_id, get_one_client)
result=image.disable
if is_successful?(result)
puts "Image disabled" if ops[:verbose]
end
when "publish"
check_parameters("publish", 1)
image_id=get_image_id(ARGV[0])
image=OpenNebula::Image.new_with_id(image_id, get_one_client)
result=image.publish
if is_successful?(result)
puts "Image published" if ops[:verbose]
end
when "unpublish"
check_parameters("unpublish", 1)
image_id=get_image_id(ARGV[0])
image=OpenNebula::Image.new_with_id(image_id, get_one_client)
result=image.unpublish
if is_successful?(result)
puts "Image unpublished" if ops[:verbose]
end
when "persistent"
check_parameters("publish", 1)
image_id=get_image_id(ARGV[0])
image=OpenNebula::Image.new_with_id(image_id, get_one_client)
result=image.persistent
if is_successful?(result)
puts "Image made persistent" if ops[:verbose]
end
when "nonpersistent"
check_parameters("nonpersistent", 1)
image_id=get_image_id(ARGV[0])
image=OpenNebula::Image.new_with_id(image_id, get_one_client)
result=image.nonpersistent
if is_successful?(result)
puts "Image made nonpersistent" if ops[:verbose]
end
when "list"
ops.merge!(get_user_flags)
if !ops[:xml]
imagelist=ImageShow.new(get_one_client, ops[:filter_user].to_i)
ops[:columns]=ops[:list] if ops[:list]
result=imagelist.list_short(ops)
else
imagepool=OpenNebula::ImagePool.new(get_one_client,
ops[:filter_user].to_i)
imagepool.info
puts imagepool.to_xml
end
when "top"
ops.merge!(get_user_flags)
imagelist=ImageShow.new(get_one_client, ops[:filter_user].to_i)
ops[:columns]=ops[:list] if ops[:list]
result=imagelist.top(ops)
when "show"
check_parameters("get_info", 1)
args=expand_args(ARGV)
args.each do |param|
image_id=get_image_id(param)
image=OpenNebula::Image.new_with_id(image_id, get_one_client)
result = image.info
if is_successful?(result)
if !ops[:xml]
str="%-15s: %-20s"
str_h1="%-80s"
print_header(str_h1, "IMAGE #{image[:id]} INFORMATION", true)
puts str % ["ID", image.id.to_s]
puts str % ["NAME", image.name]
puts str % ["TYPE", image.type_str]
value=image['REGTIME'].to_i
if value==0
value='-'
else
value=Time.at(value).strftime("%m/%d %H:%M:%S")
end
puts str % ["REGISTER TIME", value]
if image['PUBLIC'].to_i == 1
public_str = "Yes"
else
public_str = "No"
end
puts str % ["PUBLIC", public_str]
persistent_str=(image["PERSISTENT"].to_i==1 ? "Yes" : "No")
puts str % ["PERSISTENT", persistent_str]
puts str % ["SOURCE", image['SOURCE']]
puts str % ["STATE", image.short_state_str]
puts str % ["RUNNING_VMS", image['RUNNING_VMS']]
puts
print_header(str_h1,"IMAGE TEMPLATE",false)
puts image.template_str
else
puts image.to_xml
end
end
end
when "delete"
check_parameters("delete", 1)
args=expand_args(ARGV)
args.each do |param|
image_id = get_image_id(param)
image = OpenNebula::Image.new(
OpenNebula::Image.build_xml(image_id),
get_one_client)
result = image.delete
if is_successful?(result)
puts "Image correctly deleted" if ops[:verbose]
end
end
else
oneimage_opts.print_help
exit -1
end
if OpenNebula.is_error?(result)
puts "Error: " + result.message
exit -1
end