mirror of
https://github.com/OpenNebula/one.git
synced 2026-11-19 03:12:55 +09:00
370 lines
9.3 KiB
Ruby
Executable File
370 lines
9.3 KiB
Ruby
Executable File
#!/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 <template>
|
||
|
||
template is a file name where the Image description is located
|
||
|
||
* update (Modifies an image attribute)
|
||
oneimage update <image_id> <attribute_name> <attribute_value>
|
||
|
||
* list (Shows Images in the pool)
|
||
oneimage list <filter_flag>
|
||
|
||
where filter_flag can be
|
||
a, all --> all the known Images (admin users)
|
||
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 (admin users)
|
||
user --> Images of the user identified by the username (admin users)
|
||
|
||
* 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
|
||
|
||
oneimage_opts=OnevmParse.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)
|
||
image=OpenNebula::Image.new(
|
||
OpenNebula::Image.build_xml, get_one_client)
|
||
begin
|
||
template=File.read(ARGV[0])
|
||
result=image.allocate(template)
|
||
rescue
|
||
result=OpenNebula::Error.new("Can not read template: #{ARGV[0]}")
|
||
end
|
||
if is_successful?(result)
|
||
puts "ID: " + image.id.to_s if ops[:verbose]
|
||
exit 0
|
||
end
|
||
|
||
when "update" # TODO finish this
|
||
check_parameters("deploy", 2)
|
||
host_id=get_host_id(ARGV[-1])
|
||
args=expand_args(ARGV[0..-2])
|
||
|
||
args.each do |param|
|
||
vm_id=get_vm_id(param)
|
||
|
||
vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
|
||
|
||
result=vm.deploy(host_id)
|
||
if is_successful?(result)
|
||
puts "Deploying VM" if ops[:verbose]
|
||
else
|
||
break
|
||
end
|
||
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 "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)
|
||
image.info
|
||
|
||
if !ops[:xml]
|
||
str="%-15s: %-20s"
|
||
str_h1="%-80s"
|
||
|
||
print_header(str_h1, "IMAGE #{image[:id]} INFORMATION", true)
|
||
|
||
puts str % ["ID", image[:id]]
|
||
puts str % ["USER", image[:user]]
|
||
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]
|
||
puts str % ["PUBLIC", image[:public]]
|
||
|
||
puts str % ["STATE", image.state_str]
|
||
puts str % ["STATE", image[:runningvms]]
|
||
|
||
puts
|
||
|
||
print_header(str_h1,"IMAGE TEMPLATE",false)
|
||
|
||
puts image.template_str
|
||
else
|
||
puts image.to_xml
|
||
end
|
||
end
|
||
else
|
||
oneimage_opts.print_help
|
||
exit -1
|
||
end
|
||
|
||
when "delete"
|
||
check_parameters("delete", 1)
|
||
args=expand_args(ARGV)
|
||
|
||
args.each do |param|
|
||
image_id=get_image_id(param)
|
||
|
||
vm=OpenNebula::Image.new_with_id(image_id, get_one_client)
|
||
|
||
result=image.finalize
|
||
if is_successful?(result)
|
||
puts "Image correctly deleted" if ops[:verbose]
|
||
else
|
||
break
|
||
end
|
||
end
|
||
|
||
if OpenNebula.is_error?(result)
|
||
puts "Error: " + result.message
|
||
exit -1
|
||
end
|
||
|
||
|