mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-26 06:50:09 +03:00
feature #201 Added Cluster management to OCA
This commit is contained in:
parent
8bcd1010da
commit
5c69971412
@ -285,6 +285,10 @@ def get_user_id(name)
|
||||
get_entity_id(name, OpenNebula::UserPool)
|
||||
end
|
||||
|
||||
def get_cluster_id(name)
|
||||
get_entity_id(name, OpenNebula::ClusterPool)
|
||||
end
|
||||
|
||||
def str_running_time(data)
|
||||
stime=Time.at(data["stime"].to_i)
|
||||
if data["etime"]=="0"
|
||||
|
214
src/cli/onecluster
Executable file
214
src/cli/onecluster
Executable file
@ -0,0 +1,214 @@
|
||||
#!/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'
|
||||
|
||||
|
||||
ShowTableUP={
|
||||
:id => {
|
||||
:name => "ID",
|
||||
:desc => "ONE identifier for cluster",
|
||||
:size => 4,
|
||||
:proc => lambda {|d,e|
|
||||
d.id
|
||||
}
|
||||
},
|
||||
:name => {
|
||||
:name => "NAME",
|
||||
:desc => "Name of the cluster",
|
||||
:size => 8,
|
||||
:proc => lambda {|d,e|
|
||||
d.name
|
||||
}
|
||||
},
|
||||
|
||||
:default => [:id, :name]
|
||||
}
|
||||
|
||||
class UPShow
|
||||
def initialize
|
||||
@clusterpool=OpenNebula::ClusterPool.new(get_one_client)
|
||||
@table=ShowTable.new(ShowTableUP)
|
||||
end
|
||||
|
||||
def header_up_small
|
||||
scr_bold
|
||||
scr_underline
|
||||
print @table.header_str
|
||||
scr_restore
|
||||
puts ""
|
||||
end
|
||||
|
||||
def list_short(options=nil)
|
||||
res=@clusterpool.info
|
||||
if options
|
||||
@table.columns=options[:columns] if options[:columns]
|
||||
end
|
||||
|
||||
if OpenNebula.is_error?(res)
|
||||
result=res
|
||||
else
|
||||
result=res
|
||||
header_up_small
|
||||
|
||||
puts @table.data_str(@clusterpool, options)
|
||||
result
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class OneUPParse < CommandParse
|
||||
|
||||
COMMANDS_HELP=<<-EOT
|
||||
Commands:
|
||||
|
||||
* create (Creates a new user)
|
||||
onecluster create clustername
|
||||
|
||||
* delete (Removes a cluster)
|
||||
onecluster delete <id>
|
||||
|
||||
* list (Lists all the clusters in the pool)
|
||||
onecluster list
|
||||
|
||||
* addhost (Add a host to the cluster)
|
||||
onecluster addhost <host_id> <cluster_id>
|
||||
|
||||
* removehost (Remove a host from the cluster)
|
||||
onecluster removehost <host_id> <cluster_id>
|
||||
|
||||
EOT
|
||||
|
||||
def text_commands
|
||||
COMMANDS_HELP
|
||||
end
|
||||
|
||||
def text_command_name
|
||||
"onecluster"
|
||||
end
|
||||
|
||||
def list_options
|
||||
table=ShowTable.new(ShowTableUP)
|
||||
table.print_help
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
oneup_opts=OneUPParse.new
|
||||
oneup_opts.parse(ARGV)
|
||||
ops=oneup_opts.options
|
||||
|
||||
result=[false, "Unknown error"]
|
||||
|
||||
command=ARGV.shift
|
||||
|
||||
case command
|
||||
when "create"
|
||||
check_parameters("create", 1)
|
||||
cluster=OpenNebula::Cluster.new(OpenNebula::Cluster.build_xml, get_one_client)
|
||||
result=cluster.allocate(ARGV[0])
|
||||
|
||||
if is_successful?(result)
|
||||
puts "ID: " + cluster.id.to_s if ops[:verbose]
|
||||
exit 0
|
||||
end
|
||||
|
||||
when "delete"
|
||||
check_parameters("delete", 1)
|
||||
args=expand_args(ARGV)
|
||||
|
||||
args.each do |param|
|
||||
cluster_id=get_cluster_id(param)
|
||||
cluster=OpenNebula::Cluster.new(
|
||||
OpenNebula::Cluster.build_xml(cluster_id), get_one_client)
|
||||
result=cluster.delete
|
||||
if !OpenNebula.is_error?(result)
|
||||
puts "Cluster deleted" if ops[:verbose]
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
when "addhost"
|
||||
check_parameters("addhost", 2)
|
||||
cluster_id=get_cluster_id(ARGV[-1])
|
||||
cluster=OpenNebula::Cluster.new(
|
||||
OpenNebula::Cluster.build_xml(cluster_id), get_one_client)
|
||||
args=expand_args(ARGV[0..-2])
|
||||
|
||||
args.each do |param|
|
||||
host_id=get_host_id(param)
|
||||
|
||||
result=cluster.add_host(host_id)
|
||||
if is_successful?(result)
|
||||
puts "Added HOST to the Cluster" if ops[:verbose]
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
when "removehost"
|
||||
check_parameters("removehost", 2)
|
||||
cluster_id=get_cluster_id(ARGV[-1])
|
||||
cluster=OpenNebula::Cluster.new(
|
||||
OpenNebula::Cluster.build_xml(cluster_id), get_one_client)
|
||||
args=expand_args(ARGV[0..-2])
|
||||
|
||||
args.each do |param|
|
||||
host_id=get_host_id(param)
|
||||
|
||||
result=cluster.remove_host(host_id)
|
||||
if is_successful?(result)
|
||||
puts "Host removed from the Cluster" if ops[:verbose]
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
when "list"
|
||||
if !ops[:xml]
|
||||
uplist=UPShow.new
|
||||
ops[:columns]=ops[:list] if ops[:list]
|
||||
result=uplist.list_short(ops)
|
||||
else
|
||||
clusterpool=OpenNebula::ClusterPool.new(get_one_client)
|
||||
clusterpool.info
|
||||
puts clusterpool.to_xml(true)
|
||||
end
|
||||
|
||||
else
|
||||
oneup_opts.print_help
|
||||
exit -1
|
||||
end
|
||||
|
||||
if OpenNebula.is_error?(result)
|
||||
puts "Error: " + result.message
|
||||
exit -1
|
||||
end
|
@ -17,6 +17,8 @@ require 'OpenNebula/User'
|
||||
require 'OpenNebula/UserPool'
|
||||
require 'OpenNebula/Host'
|
||||
require 'OpenNebula/HostPool'
|
||||
require 'OpenNebula/Cluster'
|
||||
require 'OpenNebula/ClusterPool'
|
||||
|
||||
module OpenNebula
|
||||
|
||||
|
76
src/oca/ruby/OpenNebula/Cluster.rb
Normal file
76
src/oca/ruby/OpenNebula/Cluster.rb
Normal file
@ -0,0 +1,76 @@
|
||||
require 'OpenNebula/Pool'
|
||||
|
||||
module OpenNebula
|
||||
class Cluster < PoolElement
|
||||
# ---------------------------------------------------------------------
|
||||
# Constants and Class Methods
|
||||
# ---------------------------------------------------------------------
|
||||
CLUSTER_METHODS = {
|
||||
:info => "cluster.info",
|
||||
:allocate => "cluster.allocate",
|
||||
:delete => "cluster.delete",
|
||||
:addhost => "cluster.add",
|
||||
:removehost => "cluster.remove",
|
||||
}
|
||||
|
||||
# Creates a Cluster description with just its identifier
|
||||
# this method should be used to create plain Cluster objects.
|
||||
# +id+ the id of the user
|
||||
#
|
||||
# Example:
|
||||
# cluster = Cluster.new(User.build_xml(3),rpc_client)
|
||||
#
|
||||
def Cluster.build_xml(pe_id=nil)
|
||||
if pe_id
|
||||
user_xml = "<CLUSTER><ID>#{pe_id}</ID></CLUSTER>"
|
||||
else
|
||||
user_xml = "<CLUSTER></CLUSTER>"
|
||||
end
|
||||
|
||||
XMLUtilsElement.initialize_xml(user_xml, 'CLUSTER')
|
||||
end
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
# Class constructor
|
||||
# ---------------------------------------------------------------------
|
||||
def initialize(xml, client)
|
||||
super(xml,client)
|
||||
|
||||
@client = client
|
||||
end
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
# XML-RPC Methods for the User Object
|
||||
# ---------------------------------------------------------------------
|
||||
def info()
|
||||
super(CLUSTER_METHODS[:info], 'CLUSTER')
|
||||
end
|
||||
|
||||
def allocate(clustername)
|
||||
super(CLUSTER_METHODS[:allocate], clustername)
|
||||
end
|
||||
|
||||
def delete()
|
||||
super(CLUSTER_METHODS[:delete])
|
||||
end
|
||||
|
||||
def add_host(host_id)
|
||||
return Error.new('ID not defined') if !@pe_id
|
||||
|
||||
rc = @client.call(CLUSTER_METHODS[:addhost], host_id.to_i, @pe_id)
|
||||
rc = nil if !OpenNebula.is_error?(rc)
|
||||
|
||||
return rc
|
||||
end
|
||||
|
||||
def remove_host(host_id)
|
||||
return Error.new('ID not defined') if !@pe_id
|
||||
|
||||
# rc = @client.call(CLUSTER_METHODS[:removehost], host_id.to_i)
|
||||
rc = @client.call(CLUSTER_METHODS[:removehost], host_id.to_i, @pe_id)
|
||||
rc = nil if !OpenNebula.is_error?(rc)
|
||||
|
||||
return rc
|
||||
end
|
||||
end
|
||||
end
|
35
src/oca/ruby/OpenNebula/ClusterPool.rb
Normal file
35
src/oca/ruby/OpenNebula/ClusterPool.rb
Normal file
@ -0,0 +1,35 @@
|
||||
require 'OpenNebula/Pool'
|
||||
|
||||
module OpenNebula
|
||||
class ClusterPool < Pool
|
||||
# ---------------------------------------------------------------------
|
||||
# Constants and Class attribute accessors
|
||||
# ---------------------------------------------------------------------
|
||||
|
||||
CLUSTER_POOL_METHODS = {
|
||||
:info => "clusterpool.info"
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
# Class constructor & Pool Methods
|
||||
# ---------------------------------------------------------------------
|
||||
|
||||
# +client+ a Client object that represents a XML-RPC connection
|
||||
def initialize(client)
|
||||
super('CLUSTER_POOL','CLUSTER',client)
|
||||
end
|
||||
|
||||
# Factory method to create User objects
|
||||
def factory(element_xml)
|
||||
OpenNebula::Cluster.new(element_xml,@client)
|
||||
end
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
# XML-RPC Methods for the User Object
|
||||
# ---------------------------------------------------------------------
|
||||
|
||||
def info()
|
||||
super(CLUSTER_POOL_METHODS[:info])
|
||||
end
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user