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

Feature #1435: Add new command 'batchquota' to oneuser & onegroup.

This commit is contained in:
Carlos Martín 2012-09-03 18:18:04 +02:00
parent 6d07ed6e9a
commit 8b00256ddd
3 changed files with 108 additions and 0 deletions

View File

@ -152,6 +152,72 @@ class OneQuotaHelper
str
end
# Retrieves a clean quota template, without any existing resource
# information
# @param path [String] path to the new contents. If nil a editor will be
# used
# @return [String] contents of the new quotas
def self.get_batch_quota(path)
str = ""
if path.nil?
require 'tempfile'
tmp = Tempfile.new('one-cli')
path = tmp.path
tmp << HELP_QUOTA << "\n"
tmp.close
editor_path = ENV["EDITOR"] ? ENV["EDITOR"] : EDITOR_PATH
system("#{editor_path} #{path}")
unless $?.exitstatus == 0
puts "Editor not defined"
exit -1
end
str = File.read(path)
File.unlink(path)
else
str = File.read(path)
end
str
end
# Edits the quota template of a resource, adding the quotas set in str
# @param resource [PoolElement] to get the current info from
# @param str [String] quota template, created by get_batch_quota()
# @return [String, OpenNebula::Error] merged contents of the new quotas on
# success, Error if the user info could not be retrieved
def self.merge_quota(resource, str)
rc = resource.info
if OpenNebula.is_error?(rc)
return rc
end
# Instead of parsing the existing quotas, and deleting the ones that
# conflict with the batch quota string, the new quotas are placed at
# the end of the template sent to opennebula. This relies on the core
# reading them in order and replacing the quotas with each new
# appearance
tmp_str = ""
tmp_str << resource.template_like_str("DATASTORE_QUOTA") << "\n"
tmp_str << resource.template_like_str("VM_QUOTA") << "\n"
tmp_str << resource.template_like_str("NETWORK_QUOTA") << "\n"
tmp_str << resource.template_like_str("IMAGE_QUOTA") << "\n"
tmp_str << str
return tmp_str
end
# Outputs formated quota information to stdout
# @param qh [Hash] with the quotas for a given resource
#

View File

@ -113,4 +113,24 @@ cmd=CommandParser::CmdParser.new(ARGV) do
end
end
end
batchquota_desc = <<-EOT.unindent
Sets the quota limits in batch for various groups. If a path is not
provided the editor will be launched to create new quotas.
EOT
command :batchquota, batchquota_desc, [:range, :groupid_list], [:file, nil] do
batch_str = OneQuotaHelper.get_batch_quota(args[1])
helper.perform_actions(args[0], options, "modified") do |group|
str = OneQuotaHelper.merge_quota(group, batch_str)
if OpenNebula.is_error?(str)
str
else
rc = group.set_quota(str)
rc
end
end
end
end

View File

@ -217,6 +217,28 @@ cmd=CommandParser::CmdParser.new(ARGV) do
end
end
batchquota_desc = <<-EOT.unindent
Sets the quota limits in batch for various users. If a path is not
provided the editor will be launched to create new quotas.
EOT
command :batchquota, batchquota_desc, [:range, :userid_list], [:file, nil] do
helper = OneUserHelper.new
batch_str = OneQuotaHelper.get_batch_quota(args[1])
helper.perform_actions(args[0], options, "modified") do |user|
str = OneQuotaHelper.merge_quota(user, batch_str)
if OpenNebula.is_error?(str)
str
else
rc = user.set_quota(str)
rc
end
end
end
login_desc = <<-EOT.unindent
Creates the Login token for authentication
Examples: