From d4d4184cd774a8cc0a4434289ea5d89889bd7660 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Wed, 31 Aug 2011 18:36:33 +0200 Subject: [PATCH] feature #788: Add quota conf file --- src/authm_mad/remotes/quota/quota.conf | 27 +++++++++++++++++++++ src/authm_mad/remotes/quota/quota.rb | 33 +++++++++++++++----------- 2 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 src/authm_mad/remotes/quota/quota.conf diff --git a/src/authm_mad/remotes/quota/quota.conf b/src/authm_mad/remotes/quota/quota.conf new file mode 100644 index 0000000000..04cbc0f8a3 --- /dev/null +++ b/src/authm_mad/remotes/quota/quota.conf @@ -0,0 +1,27 @@ +# -------------------------------------------------------------------------- # +# 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. # +#--------------------------------------------------------------------------- # + +# Database URI +#:db: sqlite:///var/one/onequota.db + +#------------------------------------------------------------------------------- +# Default quotas +#------------------------------------------------------------------------------- +:defaults: + :cpu: nil, + :memory: nil, + :num_vms: nil, + :storage: nil diff --git a/src/authm_mad/remotes/quota/quota.rb b/src/authm_mad/remotes/quota/quota.rb index 9e259e271d..64a8d3ae8e 100644 --- a/src/authm_mad/remotes/quota/quota.rb +++ b/src/authm_mad/remotes/quota/quota.rb @@ -16,17 +16,24 @@ require 'sequel' require 'base64' +require 'yaml' class Quota ########################################################################### # Constants with paths to relevant files and defaults ########################################################################### - if !ENV["ONE_LOCATION"] + ONE_LOCATION=ENV["ONE_LOCATION"] + + if !ONE_LOCATION VAR_LOCATION = "/var/lib/one" + ETC_LOCATION = "/etc/one" else - VAR_LOCATION = ENV["ONE_LOCATION"] + "/var" + VAR_LOCATION = ONE_LOCATION + "/var" + ETC_LOCATION = ONE_LOCATION + "/etc" end + CONF_FILE = ETC_LOCATION + "/quota.conf" + CONF = { :db => "sqlite://#{VAR_LOCATION}/onequota.db", :defaults => { @@ -81,9 +88,11 @@ class Quota ########################################################################### # DB handling ########################################################################### - def initialize(conf={}) - # TBD merge with the conf file - @conf=CONF + def initialize + conf = YAML.load_file(CONF_FILE) + @conf=CONF.merge(conf) {|key,h1,h2| + h1.merge(h2) if h1.instance_of?(Hash) && h2.instance_of?(Hash) + } @client = OpenNebula::Client.new @@ -123,12 +132,7 @@ class Quota # Gets user limits def get(table, uid=nil) if uid - limit=@db[table].filter(:uid => uid).first - if limit - limit - else - @conf[:defaults].merge!(:uid => uid) - end + @db[table].filter(:uid => uid).first else @db[table].all end @@ -151,7 +155,8 @@ class Quota end def get_quota(uid=nil) - get(QUOTA_TABLE, uid) + limit = get(QUOTA_TABLE, uid) + limit ? limit : @conf[:defaults].merge!(:uid => uid) end def delete_quota(uid) @@ -181,7 +186,7 @@ class Quota def check_quotas(user_id, obj, template) info = get_resources(obj, template) - total = get_usage(obj, user_id) + total = get_usage(user_id, obj, true) quota = get_quota(user_id) msg = "" @@ -238,7 +243,7 @@ class Quota usage = get(USAGE_TABLE, user_id) end - usage + usage ? usage : Hash.new end # Retrieve the useful information of the template for the specified