1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-04-01 06:50:25 +03:00

Feature #1112: Ruby OCA and CLI for datastores

This commit is contained in:
Carlos Martín 2012-02-09 18:55:18 +01:00
parent 9ec4b450a4
commit 2531db2d7a
7 changed files with 353 additions and 3 deletions

View File

@ -556,6 +556,7 @@ BIN_FILES="src/nebula/oned \
src/cli/onegroup \
src/cli/onetemplate \
src/cli/oneacl \
src/cli/onedatastore \
src/onedb/onedb \
src/authm_mad/remotes/quota/onequota \
src/mad/utils/tty_expect \
@ -945,6 +946,8 @@ RUBY_OPENNEBULA_LIB_FILES="src/oca/ruby/OpenNebula/Host.rb \
src/oca/ruby/OpenNebula/GroupPool.rb \
src/oca/ruby/OpenNebula/Acl.rb \
src/oca/ruby/OpenNebula/AclPool.rb \
src/oca/ruby/OpenNebula/Datastore.rb \
src/oca/ruby/OpenNebula/DatastorePool.rb \
src/oca/ruby/OpenNebula/XMLUtils.rb"
#-------------------------------------------------------------------------------
@ -1049,7 +1052,8 @@ ONE_CLI_LIB_FILES="src/cli/one_helper/onegroup_helper.rb \
src/cli/one_helper/oneuser_helper.rb \
src/cli/one_helper/onevm_helper.rb \
src/cli/one_helper/onevnet_helper.rb \
src/cli/one_helper/oneacl_helper.rb"
src/cli/one_helper/oneacl_helper.rb \
src/cli/one_helper/onedatastore_helper.rb"
CLI_BIN_FILES="src/cli/onevm \
src/cli/onehost \
@ -1058,7 +1062,8 @@ CLI_BIN_FILES="src/cli/onevm \
src/cli/oneimage \
src/cli/onetemplate \
src/cli/onegroup \
src/cli/oneacl"
src/cli/oneacl \
src/cli/onedatastore"
CLI_CONF_FILES="src/cli/etc/onegroup.yaml \
src/cli/etc/onehost.yaml \
@ -1067,7 +1072,8 @@ CLI_CONF_FILES="src/cli/etc/onegroup.yaml \
src/cli/etc/oneuser.yaml \
src/cli/etc/onevm.yaml \
src/cli/etc/onevnet.yaml \
src/cli/etc/oneacl.yaml"
src/cli/etc/oneacl.yaml \
src/cli/etc/onedatastore.yaml"
ETC_CLIENT_FILES="src/cli/etc/group.default"
@ -1401,6 +1407,7 @@ MAN_FILES="share/man/oneauth.1.gz \
share/man/onetemplate.1.gz \
share/man/onegroup.1.gz \
share/man/onedb.1.gz \
share/man/onedatastore.1.gz \
share/man/econe-describe-images.1.gz \
share/man/econe-describe-instances.1.gz \
share/man/econe-register.1.gz \

View File

@ -0,0 +1,14 @@
---
:ID:
:desc: ONE identifier for the Datastore
:size: 4
:NAME:
:desc: Name of the Datastore
:size: 15
:left: true
:default:
- :ID
- :NAME

View File

@ -0,0 +1,77 @@
# -------------------------------------------------------------------------- #
# Copyright 2002-2012, 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 OneDatastoreHelper < OpenNebulaHelper::OneHelper
def self.rname
"DATASTORE"
end
def self.conf_file
"onedatastore.yaml"
end
def format_pool(options)
config_file = self.class.table_conf
table = CLIHelper::ShowTable.new(config_file, self) do
column :ID, "ONE identifier for the Datastore", :size=>4 do |d|
d["ID"]
end
column :NAME, "Name of the Datastore", :left, :size=>15 do |d|
d["NAME"]
end
default :ID, :NAME
end
table
end
private
def factory(id=nil)
if id
OpenNebula::Datastore.new_with_id(id, @client)
else
xml=OpenNebula::Datastore.build_xml
OpenNebula::Datastore.new(xml, @client)
end
end
def factory_pool(user_flag=-2)
#TBD OpenNebula::UserPool.new(@client, user_flag)
OpenNebula::DatastorePool.new(@client)
end
def format_resource(datastore)
str="%-15s: %-20s"
str_h1="%-80s"
CLIHelper.print_header(str_h1 % "DATASTORE #{datastore['ID']} INFORMATION")
puts str % ["ID", datastore.id.to_s]
puts str % ["NAME", datastore.name]
puts
CLIHelper.print_header(str_h1 % "IMAGES", false)
CLIHelper.print_header("%-15s" % ["ID"])
datastore.user_ids.each do |id|
puts "%-15s" % [id]
end
end
end

98
src/cli/onedatastore Executable file
View File

@ -0,0 +1,98 @@
#!/usr/bin/env ruby
# -------------------------------------------------------------------------- #
# Copyright 2002-2012, 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
$: << RUBY_LIB_LOCATION+"/cli"
require 'command_parser'
require 'one_helper/onedatastore_helper'
cmd=CommandParser::CmdParser.new(ARGV) do
usage "`onedatastore` <command> [<args>] [<options>]"
version OpenNebulaHelper::ONE_VERSION
helper = OneDatastoreHelper.new
########################################################################
# Global Options
########################################################################
set :option, CommandParser::OPTIONS
list_options = CLIHelper::OPTIONS
list_options << OpenNebulaHelper::XML
list_options << OpenNebulaHelper::NUMERIC
########################################################################
# Formatters for arguments
########################################################################
set :format, :datastoreid, OneDatastoreHelper.to_id_desc do |arg|
helper.to_id(arg)
end
set :format, :datastoreid_list, OneDatastoreHelper.list_to_id_desc do |arg|
helper.list_to_id(arg)
end
########################################################################
# Commands
########################################################################
create_desc = <<-EOT.unindent
Creates a new Datastore
EOT
command :create, create_desc, :name do
helper.create_resource(options) do |datastore|
datastore.allocate(args[0])
end
end
delete_desc = <<-EOT.unindent
Deletes the given Datastore
EOT
command :delete, delete_desc, [:range, :datastoreid_list] do
helper.perform_actions(args[0],options,"deleted") do |obj|
obj.delete
end
end
list_desc = <<-EOT.unindent
Lists Datastores in the pool
EOT
command :list, list_desc, :options=>list_options do
helper.list_pool(options)
end
show_desc = <<-EOT.unindent
Shows information for the given Datastore
EOT
command :show, show_desc, :datastoreid, :options=>OpenNebulaHelper::XML do
helper.show_resource(args[0],options)
end
end

View File

@ -42,6 +42,8 @@ require 'OpenNebula/Group'
require 'OpenNebula/GroupPool'
require 'OpenNebula/Acl'
require 'OpenNebula/AclPool'
require 'OpenNebula/Datastore'
require 'OpenNebula/DatastorePool'
module OpenNebula

View File

@ -0,0 +1,99 @@
# -------------------------------------------------------------------------- #
# Copyright 2002-2012, 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 'OpenNebula/Pool'
module OpenNebula
class Datastore < PoolElement
#######################################################################
# Constants and Class Methods
#######################################################################
DATASTORE_METHODS = {
:info => "datastore.info",
:allocate => "datastore.allocate",
:delete => "datastore.delete"
}
# Creates a Datastore description with just its identifier
# this method should be used to create plain Datastore objects.
# +id+ the id of the user
#
# Example:
# datastore = Datastore.new(Datastore.build_xml(3),rpc_client)
#
def Datastore.build_xml(pe_id=nil)
if pe_id
datastore_xml = "<DATASTORE><ID>#{pe_id}</ID></DATASTORE>"
else
datastore_xml = "<DATASTORE></DATASTORE>"
end
XMLElement.build_xml(datastore_xml,'DATASTORE')
end
# Class constructor
def initialize(xml, client)
super(xml,client)
end
#######################################################################
# XML-RPC Methods for the Datastore Object
#######################################################################
# Retrieves the information of the given Datastore.
def info()
super(DATASTORE_METHODS[:info], 'DATASTORE')
end
# Allocates a new Datastore in OpenNebula
#
# +datastorename+ A string containing the name of the Datastore.
def allocate(datastorename)
super(DATASTORE_METHODS[:allocate], datastorename)
end
# Deletes the Datastore
def delete()
super(DATASTORE_METHODS[:delete])
end
# ---------------------------------------------------------------------
# Helpers to get information
# ---------------------------------------------------------------------
# Returns whether or not the image with id 'id' is part of this datastore
def contains(id)
#This doesn't work in ruby 1.8.5
#return self["DATASTORE/ID[.=#{uid}]"] != nil
id_array = retrieve_elements('DATASTORE/ID')
return id_array != nil && id_array.include?(uid.to_s)
end
# Returns an array with the numeric image ids
def user_ids
array = Array.new
self.each("DATASTORE/ID") do |id|
array << id.text.to_i
end
return array
end
end
end

View File

@ -0,0 +1,53 @@
# -------------------------------------------------------------------------- #
# Copyright 2002-2012, 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 'OpenNebula/Pool'
module OpenNebula
class DatastorePool < Pool
#######################################################################
# Constants and Class attribute accessors
#######################################################################
DATASTORE_POOL_METHODS = {
:info => "datastorepool.info"
}
#######################################################################
# Class constructor & Pool Methods
#######################################################################
# +client+ a Client object that represents a XML-RPC connection
def initialize(client)
super('DATASTORE_POOL','DATASTORE',client)
end
# Factory method to create User objects
def factory(element_xml)
OpenNebula::Group.new(element_xml,@client)
end
#######################################################################
# XML-RPC Methods for the User Object
#######################################################################
# Retrieves all the Groups in the pool.
def info()
super(DATASTORE_POOL_METHODS[:info])
end
end
end