From c04f35d5ed6680a42dd6a58de264a770a210fe4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Mon, 19 Nov 2012 15:19:09 +0100 Subject: [PATCH 01/28] Feature #1611: Start working in support for default User quotas --- include/Nebula.h | 28 +++++++++++++++++-- include/Quota.h | 21 ++++++++++---- include/QuotaDatastore.h | 13 ++++++++- include/QuotaImage.h | 11 ++++++++ include/QuotaNetwork.h | 11 ++++++++ include/QuotaVirtualMachine.h | 14 +++++++++- include/Quotas.h | 52 +++++++++++++++++++++++++++++++++++ src/um/Quota.cc | 15 +++++++++- src/um/QuotaDatastore.cc | 12 ++++++++ src/um/QuotaImage.cc | 12 ++++++++ src/um/QuotaNetwork.cc | 12 ++++++++ src/um/QuotaVirtualMachine.cc | 12 ++++++++ 12 files changed, 201 insertions(+), 12 deletions(-) diff --git a/include/Nebula.h b/include/Nebula.h index 85131be7ea..7bd3ac2557 100644 --- a/include/Nebula.h +++ b/include/Nebula.h @@ -42,6 +42,8 @@ #include "AclManager.h" #include "ImageManager.h" +#include "Quotas.h" + #include "Callbackable.h" class Nebula : public Callbackable @@ -311,13 +313,27 @@ public: return nebula_configuration->to_xml(xml); }; + const Quotas& get_default_user_quota() + { + return default_user_quota; + }; + private: // ----------------------------------------------------------------------- //Constructors and = are private to only access the class through instance // ----------------------------------------------------------------------- - Nebula():nebula_configuration(0),db(0),vmpool(0),hpool(0),vnpool(0), + Nebula():nebula_configuration(0), + default_user_quota("/USER/DATASTORE_QUOTA", + "/USER/NETWORK_QUOTA", + "/USER/IMAGE_QUOTA", + "/USER/VM_QUOTA"), + default_group_quota("/GROUP/DATASTORE_QUOTA", + "/GROUP/NETWORK_QUOTA", + "/GROUP/IMAGE_QUOTA", + "/GROUP/VM_QUOTA"), + db(0),vmpool(0),hpool(0),vnpool(0), upool(0),ipool(0),gpool(0),tpool(0),dspool(0),clpool(0),docpool(0), lcm(0),vmm(0),im(0),tm(0),dm(0),rm(0),hm(0),authm(0),aclm(0),imagem(0) { @@ -467,8 +483,6 @@ private: } }; - Nebula(Nebula const&){}; - Nebula& operator=(Nebula const&){return *this;}; // --------------------------------------------------------------- @@ -494,6 +508,14 @@ private: OpenNebulaTemplate * nebula_configuration; + // --------------------------------------------------------------- + // Default quotas + // --------------------------------------------------------------- + + Quotas default_user_quota; + Quotas default_group_quota; + + // --------------------------------------------------------------- // Nebula Pools // --------------------------------------------------------------- diff --git a/include/Quota.h b/include/Quota.h index 1a677d27a0..2d7aed1112 100644 --- a/include/Quota.h +++ b/include/Quota.h @@ -61,6 +61,17 @@ public: return template_name; } + /** + * Gets a quota identified by its ID. + * @param id of the quota + * @param va The quota, if it is found + * @return 0 on success, -1 if not found + */ + virtual int get_quota(const string& id, VectorAttribute **va) + { + map::iterator it; + return get_quota(id, va, it); + } protected: @@ -122,16 +133,14 @@ protected: map& usage_req); /** - * Gets a quota identified by its ID. + * Gets the default quota identified by its ID. + * * @param id of the quota * @param va The quota, if it is found + * * @return 0 on success, -1 if not found */ - virtual int get_quota(const string& id, VectorAttribute **va) - { - map::iterator it; - return get_quota(id, va, it); - } + virtual int get_default_quota(const string& id, VectorAttribute **va) = 0; /** * Gets a quota identified by its ID. diff --git a/include/QuotaDatastore.h b/include/QuotaDatastore.h index 02964e141c..e70925479c 100644 --- a/include/QuotaDatastore.h +++ b/include/QuotaDatastore.h @@ -60,9 +60,20 @@ public: void del(Template* tmpl); protected: + + /** + * Gets the default quota identified by its ID. + * + * @param id of the quota + * @param va The quota, if it is found + * + * @return 0 on success, -1 if not found + */ + int get_default_quota(const string& id, VectorAttribute **va); + static const char * DS_METRICS[]; static const int NUM_DS_METRICS; }; -#endif /*QUOTA_DATASTORE_H_*/ \ No newline at end of file +#endif /*QUOTA_DATASTORE_H_*/ diff --git a/include/QuotaImage.h b/include/QuotaImage.h index 6d75c62648..cab4a93ed8 100644 --- a/include/QuotaImage.h +++ b/include/QuotaImage.h @@ -58,6 +58,17 @@ public: void del(Template* tmpl); protected: + + /** + * Gets the default quota identified by its ID. + * + * @param id of the quota + * @param va The quota, if it is found + * + * @return 0 on success, -1 if not found + */ + int get_default_quota(const string& id, VectorAttribute **va); + static const char * IMAGE_METRICS[]; static const int NUM_IMAGE_METRICS; diff --git a/include/QuotaNetwork.h b/include/QuotaNetwork.h index defb73e482..082b908aeb 100644 --- a/include/QuotaNetwork.h +++ b/include/QuotaNetwork.h @@ -58,6 +58,17 @@ public: void del(Template* tmpl); protected: + + /** + * Gets the default quota identified by its ID. + * + * @param id of the quota + * @param va The quota, if it is found + * + * @return 0 on success, -1 if not found + */ + int get_default_quota(const string& id, VectorAttribute **va); + static const char * NET_METRICS[]; static const int NUM_NET_METRICS; diff --git a/include/QuotaVirtualMachine.h b/include/QuotaVirtualMachine.h index d600fde363..a1368c6673 100644 --- a/include/QuotaVirtualMachine.h +++ b/include/QuotaVirtualMachine.h @@ -60,7 +60,6 @@ public: */ void del(Template* tmpl); -protected: /** * Gets a quota, overrides base to not to use ID. * @param id of the quota, ignored @@ -70,6 +69,9 @@ protected: */ int get_quota(const string& id, VectorAttribute **va); +protected: + + /** * Gets a quota, overrides base to not to use ID. * @@ -88,6 +90,16 @@ protected: return get_quota(id, va); } + /** + * Gets the default quota identified by its ID. + * + * @param id of the quota + * @param va The quota, if it is found + * + * @return 0 on success, -1 if not found + */ + int get_default_quota(const string& id, VectorAttribute **va); + static const char * VM_METRICS[]; static const int NUM_VM_METRICS; diff --git a/include/Quotas.h b/include/Quotas.h index c090f1b415..16cd6032b2 100644 --- a/include/Quotas.h +++ b/include/Quotas.h @@ -80,6 +80,19 @@ public: datastore_quota.del(tmpl); } + /** + * Gets a Datastore quota identified by its ID. + * + * @param id of the quota + * @param va The quota, if it is found + * + * @return 0 on success, -1 if not found + */ + int ds_get(const string& id, VectorAttribute **va) + { + return datastore_quota.get_quota(id, va); + } + /** * Check Virtual Machine quotas (network, image and compute), it updates * usage counters if quotas are not exceeded. @@ -103,6 +116,45 @@ public: image_quota.del(tmpl); } + /** + * Gets a VM quota identified by its ID. + * + * @param id of the quota + * @param va The quota, if it is found + * + * @return 0 on success, -1 if not found + */ + int vm_get(const string& id, VectorAttribute **va) + { + return vm_quota.get_quota(id, va); + } + + /** + * Gets a Network quota identified by its ID. + * + * @param id of the quota + * @param va The quota, if it is found + * + * @return 0 on success, -1 if not found + */ + int network_get(const string& id, VectorAttribute **va) + { + return network_quota.get_quota(id, va); + } + + /** + * Gets an Image quota identified by its ID. + * + * @param id of the quota + * @param va The quota, if it is found + * + * @return 0 on success, -1 if not found + */ + int image_get(const string& id, VectorAttribute **va) + { + return image_quota.get_quota(id, va); + } + /** * Check quota, it updates usage counters if quotas are not exceeded. * @param type the quota to work with diff --git a/src/um/Quota.cc b/src/um/Quota.cc index a411123cc6..c821521078 100644 --- a/src/um/Quota.cc +++ b/src/um/Quota.cc @@ -155,6 +155,7 @@ bool Quota::check_quota(const string& qid, string& error) { VectorAttribute * q; + VectorAttribute * default_q; map::iterator it; bool check; @@ -171,6 +172,11 @@ bool Quota::check_quota(const string& qid, return false; } + if ( get_default_quota(qid, &default_q) == -1 ) + { + default_q = 0; + } + // ------------------------------------------------------------------------- // Quota does not exist, create a new one // ------------------------------------------------------------------------- @@ -229,6 +235,11 @@ bool Quota::check_quota(const string& qid, q->vector_value(metrics[i], limit); q->vector_value(metrics_used.c_str(), usage); + if ( limit == -1 && default_q != 0 ) + { + default_q->vector_value(metrics[i], limit); + } + check = ( limit == 0 ) || ( ( usage + it->second ) <= limit ); if ( !check ) @@ -365,7 +376,9 @@ int Quota::update_limits(VectorAttribute * quota, const VectorAttribute * va) { limit = va->vector_value_str(metrics[i], limit_i); - if ( limit_i < 0 ) //No quota, NaN or negative + //No quota, NaN or negative. -1 is allowed, it means default limit + if ( limit_i < -1 || + ( limit_i == -1 && limit == "" )) { return -1; } diff --git a/src/um/QuotaDatastore.cc b/src/um/QuotaDatastore.cc index 9e6a925a3b..fa2f131656 100644 --- a/src/um/QuotaDatastore.cc +++ b/src/um/QuotaDatastore.cc @@ -15,6 +15,8 @@ /* -------------------------------------------------------------------------- */ #include "QuotaDatastore.h" +#include "Quotas.h" +#include "Nebula.h" /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -83,3 +85,13 @@ void QuotaDatastore::del(Template * tmpl) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ + +int QuotaDatastore::get_default_quota(const string& id, VectorAttribute **va) +{ + // TODO: We need to know if this is a user or group quota + Quotas default_quotas = Nebula::instance().get_default_user_quota(); + return default_quotas.ds_get(id, va); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ diff --git a/src/um/QuotaImage.cc b/src/um/QuotaImage.cc index 766fc74083..ee78395027 100644 --- a/src/um/QuotaImage.cc +++ b/src/um/QuotaImage.cc @@ -15,6 +15,8 @@ /* -------------------------------------------------------------------------- */ #include "QuotaImage.h" +#include "Quotas.h" +#include "Nebula.h" /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -98,3 +100,13 @@ void QuotaImage::del(Template * tmpl) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ + +int QuotaImage::get_default_quota(const string& id, VectorAttribute **va) +{ + // TODO: We need to know if this is a user or group quota + Quotas default_quotas = Nebula::instance().get_default_user_quota(); + return default_quotas.image_get(id, va); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ diff --git a/src/um/QuotaNetwork.cc b/src/um/QuotaNetwork.cc index 8c08192df4..d560d062b8 100644 --- a/src/um/QuotaNetwork.cc +++ b/src/um/QuotaNetwork.cc @@ -15,6 +15,8 @@ /* -------------------------------------------------------------------------- */ #include "QuotaNetwork.h" +#include "Quotas.h" +#include "Nebula.h" /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -98,3 +100,13 @@ void QuotaNetwork::del(Template * tmpl) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ + +int QuotaNetwork::get_default_quota(const string& id, VectorAttribute **va) +{ + // TODO: We need to know if this is a user or group quota + Quotas default_quotas = Nebula::instance().get_default_user_quota(); + return default_quotas.network_get(id, va); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ diff --git a/src/um/QuotaVirtualMachine.cc b/src/um/QuotaVirtualMachine.cc index 9a76539605..4a8ccd8875 100644 --- a/src/um/QuotaVirtualMachine.cc +++ b/src/um/QuotaVirtualMachine.cc @@ -15,6 +15,8 @@ /* -------------------------------------------------------------------------- */ #include "QuotaVirtualMachine.h" +#include "Quotas.h" +#include "Nebula.h" /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -103,3 +105,13 @@ void QuotaVirtualMachine::del(Template * tmpl) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ + +int QuotaVirtualMachine::get_default_quota(const string& id, VectorAttribute **va) +{ + // TODO: We need to know if this is a user or group quota + Quotas default_quotas = Nebula::instance().get_default_user_quota(); + return default_quotas.vm_get(id, va); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ From 8ed9fa884aaa74d64e8d516633ea9971bfb5863e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Mon, 19 Nov 2012 16:38:39 +0100 Subject: [PATCH 02/28] Feagture #1611: Make the default quotas work for both user and group --- include/Nebula.h | 5 +++++ include/Quota.h | 14 ++++++++++++-- include/QuotaDatastore.h | 8 ++++++-- include/QuotaImage.h | 8 ++++++-- include/QuotaNetwork.h | 8 ++++++-- include/QuotaVirtualMachine.h | 6 ++++-- include/Quotas.h | 30 +++++------------------------- src/rm/Request.cc | 8 ++++++-- src/um/Quota.cc | 3 ++- src/um/QuotaDatastore.cc | 14 +++++++------- src/um/QuotaImage.cc | 9 +++------ src/um/QuotaNetwork.cc | 12 ++++++------ src/um/QuotaVirtualMachine.cc | 11 +++++------ src/um/Quotas.cc | 19 +++++++++++-------- 14 files changed, 84 insertions(+), 71 deletions(-) diff --git a/include/Nebula.h b/include/Nebula.h index 7bd3ac2557..d1f5560a87 100644 --- a/include/Nebula.h +++ b/include/Nebula.h @@ -318,6 +318,11 @@ public: return default_user_quota; }; + const Quotas& get_default_group_quota() + { + return default_group_quota; + }; + private: // ----------------------------------------------------------------------- diff --git a/include/Quota.h b/include/Quota.h index 2d7aed1112..bc22e911dc 100644 --- a/include/Quota.h +++ b/include/Quota.h @@ -19,6 +19,9 @@ #include "Template.h" +// Forward declaration to avoid include cycle +class Quotas; + /** * Base class for resource quotas, it provides basic storage and management of * the quotas. Each resource MUST inherit from it to implement check and @@ -41,10 +44,11 @@ public: * Check if the resource allocation will exceed the quota limits. If not * the usage counters are updated * @param tmpl template for the resource + * @param default_quotas Quotas that contain the default limits * @param error string * @return true if the operation can be performed */ - virtual bool check(Template* tmpl, string& error) = 0; + virtual bool check(Template* tmpl, Quotas& default_quotas, string& error) = 0; /** * Decrement usage counters when deallocating image @@ -118,10 +122,13 @@ protected: * request does not exceed quota limits * @param qid id that identifies the quota, to be used by get_quota * @param usage_req usage for each metric + * @param default_quotas Quotas that contain the default limits + * @param error string describing the error * @return true if the request does not exceed current limits */ bool check_quota(const string& qid, map& usage_req, + Quotas& default_quotas, string& error); /** @@ -136,11 +143,14 @@ protected: * Gets the default quota identified by its ID. * * @param id of the quota + * @param default_quotas Quotas that contain the default limits * @param va The quota, if it is found * * @return 0 on success, -1 if not found */ - virtual int get_default_quota(const string& id, VectorAttribute **va) = 0; + virtual int get_default_quota(const string& id, + Quotas& default_quotas, + VectorAttribute **va) = 0; /** * Gets a quota identified by its ID. diff --git a/include/QuotaDatastore.h b/include/QuotaDatastore.h index e70925479c..fcd5a7f7ab 100644 --- a/include/QuotaDatastore.h +++ b/include/QuotaDatastore.h @@ -48,10 +48,11 @@ public: * Check if the resource allocation will exceed the quota limits. If not * the usage counters are updated * @param tmpl template for the resource + * @param default_quotas Quotas that contain the default limits * @param error string * @return true if the operation can be performed */ - bool check(Template* tmpl, string& error); + bool check(Template* tmpl, Quotas& default_quotas, string& error); /** * Decrement usage counters when deallocating image @@ -65,11 +66,14 @@ protected: * Gets the default quota identified by its ID. * * @param id of the quota + * @param default_quotas Quotas that contain the default limits * @param va The quota, if it is found * * @return 0 on success, -1 if not found */ - int get_default_quota(const string& id, VectorAttribute **va); + int get_default_quota(const string& id, + Quotas& default_quotas, + VectorAttribute **va); static const char * DS_METRICS[]; diff --git a/include/QuotaImage.h b/include/QuotaImage.h index cab4a93ed8..39b87ce6ac 100644 --- a/include/QuotaImage.h +++ b/include/QuotaImage.h @@ -46,10 +46,11 @@ public: * Check if the resource allocation will exceed the quota limits. If not * the usage counters are updated * @param tmpl template for the resource + * @param default_quotas Quotas that contain the default limits * @param error string * @return true if the operation can be performed */ - bool check(Template* tmpl, string& error); + bool check(Template* tmpl, Quotas& default_quotas, string& error); /** * Decrement usage counters when deallocating image @@ -63,11 +64,14 @@ protected: * Gets the default quota identified by its ID. * * @param id of the quota + * @param default_quotas Quotas that contain the default limits * @param va The quota, if it is found * * @return 0 on success, -1 if not found */ - int get_default_quota(const string& id, VectorAttribute **va); + int get_default_quota(const string& id, + Quotas& default_quotas, + VectorAttribute **va); static const char * IMAGE_METRICS[]; diff --git a/include/QuotaNetwork.h b/include/QuotaNetwork.h index 082b908aeb..afa92e5874 100644 --- a/include/QuotaNetwork.h +++ b/include/QuotaNetwork.h @@ -46,10 +46,11 @@ public: * Check if the resource allocation will exceed the quota limits. If not * the usage counters are updated * @param tmpl template for the resource + * @param default_quotas Quotas that contain the default limits * @param error string * @return true if the operation can be performed */ - bool check(Template* tmpl, string& error); + bool check(Template* tmpl, Quotas& default_quotas, string& error); /** * Decrement usage counters when deallocating image @@ -63,11 +64,14 @@ protected: * Gets the default quota identified by its ID. * * @param id of the quota + * @param default_quotas Quotas that contain the default limits * @param va The quota, if it is found * * @return 0 on success, -1 if not found */ - int get_default_quota(const string& id, VectorAttribute **va); + int get_default_quota(const string& id, + Quotas& default_quotas, + VectorAttribute **va); static const char * NET_METRICS[]; diff --git a/include/QuotaVirtualMachine.h b/include/QuotaVirtualMachine.h index a1368c6673..fa085c0421 100644 --- a/include/QuotaVirtualMachine.h +++ b/include/QuotaVirtualMachine.h @@ -49,10 +49,11 @@ public: * Check if the resource allocation will exceed the quota limits. If not * the usage counters are updated * @param tmpl template for the resource + * @param default_quotas Quotas that contain the default limits * @param error string * @return true if the operation can be performed */ - bool check(Template* tmpl, string& error); + bool check(Template* tmpl, Quotas& default_quotas, string& error); /** * Decrement usage counters when deallocating image @@ -94,11 +95,12 @@ protected: * Gets the default quota identified by its ID. * * @param id of the quota + * @param default_quotas Quotas that contain the default limits * @param va The quota, if it is found * * @return 0 on success, -1 if not found */ - int get_default_quota(const string& id, VectorAttribute **va); + int get_default_quota(const string& id, Quotas& default_quotas, VectorAttribute **va); static const char * VM_METRICS[]; diff --git a/include/Quotas.h b/include/Quotas.h index 16cd6032b2..1097ea9306 100644 --- a/include/Quotas.h +++ b/include/Quotas.h @@ -59,18 +59,6 @@ public: */ int set(Template *tmpl, string& error); - /** - * Check Datastore quotas, it updates usage counters if quotas are not - * exceeded. - * @param tmpl template for the image - * @param reason string describing the error - * @return true if image can be allocated, false otherwise - */ - bool ds_check(Template * tmpl, string& reason) - { - return datastore_quota.check(tmpl, reason); - } - /** * Delete usage from quota counters. * @param tmpl template for the image, with usage @@ -93,18 +81,6 @@ public: return datastore_quota.get_quota(id, va); } - /** - * Check Virtual Machine quotas (network, image and compute), it updates - * usage counters if quotas are not exceeded. - * @param tmpl template for the VirtualMachine - * @param error_str string describing the error - * @return true if VM can be allocated, false otherwise - */ - bool vm_check(Template * tmpl, string& error_str) - { - return quota_check(VIRTUALMACHINE, tmpl, error_str); - } - /** * Delete VM related usage (network, image and compute) from quota counters. * @param tmpl template for the image, with usage @@ -159,10 +135,14 @@ public: * Check quota, it updates usage counters if quotas are not exceeded. * @param type the quota to work with * @param tmpl template for the VirtualMachine + * @param default_quotas Quotas that contain the default limits * @param error_str string describing the error * @return true if VM can be allocated, false otherwise */ - bool quota_check(QuotaType type, Template *tmpl, string& error_str); + bool quota_check(QuotaType type, + Template *tmpl, + Quotas& default_quotas, + string& error_str); /** * Delete usage from the given quota counters. diff --git a/src/rm/Request.cc b/src/rm/Request.cc index 5600c497b6..21c3624c65 100644 --- a/src/rm/Request.cc +++ b/src/rm/Request.cc @@ -278,7 +278,9 @@ bool Request::user_quota_authorization (Template * tmpl, return false; } - rc = user->quota.quota_check(qtype, tmpl, error_str); + Quotas default_user_quotas = nd.get_default_user_quota(); + + rc = user->quota.quota_check(qtype, tmpl, default_user_quotas, error_str); if (rc == true) { @@ -320,7 +322,9 @@ bool Request::group_quota_authorization (Template * tmpl, return false; } - rc = group->quota.quota_check(qtype, tmpl, error_str); + Quotas default_group_quotas = nd.get_default_group_quota(); + + rc = group->quota.quota_check(qtype, tmpl, default_group_quotas, error_str); if (rc == true) { diff --git a/src/um/Quota.cc b/src/um/Quota.cc index c821521078..326bd5c47e 100644 --- a/src/um/Quota.cc +++ b/src/um/Quota.cc @@ -152,6 +152,7 @@ error_limits: bool Quota::check_quota(const string& qid, map& usage_req, + Quotas& default_quotas, string& error) { VectorAttribute * q; @@ -172,7 +173,7 @@ bool Quota::check_quota(const string& qid, return false; } - if ( get_default_quota(qid, &default_q) == -1 ) + if ( get_default_quota(qid, default_quotas, &default_q) == -1 ) { default_q = 0; } diff --git a/src/um/QuotaDatastore.cc b/src/um/QuotaDatastore.cc index fa2f131656..feabd98c00 100644 --- a/src/um/QuotaDatastore.cc +++ b/src/um/QuotaDatastore.cc @@ -16,7 +16,6 @@ #include "QuotaDatastore.h" #include "Quotas.h" -#include "Nebula.h" /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -28,7 +27,7 @@ const int QuotaDatastore::NUM_DS_METRICS = 2; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -bool QuotaDatastore::check(Template * tmpl, string& error) +bool QuotaDatastore::check(Template * tmpl, Quotas& default_quotas, string& error) { map ds_request; @@ -51,8 +50,8 @@ bool QuotaDatastore::check(Template * tmpl, string& error) ds_request.insert(make_pair("IMAGES",1)); ds_request.insert(make_pair("SIZE", size)); - - return check_quota(ds_id, ds_request, error); + + return check_quota(ds_id, ds_request, default_quotas, error); } /* -------------------------------------------------------------------------- */ @@ -86,10 +85,11 @@ void QuotaDatastore::del(Template * tmpl) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int QuotaDatastore::get_default_quota(const string& id, VectorAttribute **va) +int QuotaDatastore::get_default_quota( + const string& id, + Quotas& default_quotas, + VectorAttribute **va) { - // TODO: We need to know if this is a user or group quota - Quotas default_quotas = Nebula::instance().get_default_user_quota(); return default_quotas.ds_get(id, va); } diff --git a/src/um/QuotaImage.cc b/src/um/QuotaImage.cc index ee78395027..0b6774524f 100644 --- a/src/um/QuotaImage.cc +++ b/src/um/QuotaImage.cc @@ -16,7 +16,6 @@ #include "QuotaImage.h" #include "Quotas.h" -#include "Nebula.h" /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -28,7 +27,7 @@ const int QuotaImage::NUM_IMAGE_METRICS = 1; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -bool QuotaImage::check(Template * tmpl, string& error) +bool QuotaImage::check(Template * tmpl, Quotas& default_quotas, string& error) { vector disks; VectorAttribute * disk; @@ -55,7 +54,7 @@ bool QuotaImage::check(Template * tmpl, string& error) if ( !image_id.empty() ) { - if ( !check_quota(image_id, image_request, error) ) + if ( !check_quota(image_id, image_request, default_quotas, error) ) { return false; } @@ -101,10 +100,8 @@ void QuotaImage::del(Template * tmpl) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int QuotaImage::get_default_quota(const string& id, VectorAttribute **va) +int QuotaImage::get_default_quota(const string& id, Quotas& default_quotas, VectorAttribute **va) { - // TODO: We need to know if this is a user or group quota - Quotas default_quotas = Nebula::instance().get_default_user_quota(); return default_quotas.image_get(id, va); } diff --git a/src/um/QuotaNetwork.cc b/src/um/QuotaNetwork.cc index d560d062b8..97dd50b1b1 100644 --- a/src/um/QuotaNetwork.cc +++ b/src/um/QuotaNetwork.cc @@ -16,7 +16,6 @@ #include "QuotaNetwork.h" #include "Quotas.h" -#include "Nebula.h" /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -28,7 +27,7 @@ const int QuotaNetwork::NUM_NET_METRICS = 1; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -bool QuotaNetwork::check(Template * tmpl, string& error) +bool QuotaNetwork::check(Template * tmpl, Quotas& default_quotas, string& error) { vector nics; VectorAttribute * nic; @@ -55,7 +54,7 @@ bool QuotaNetwork::check(Template * tmpl, string& error) if ( !net_id.empty() ) { - if ( !check_quota(net_id, net_request, error) ) + if ( !check_quota(net_id, net_request, default_quotas, error) ) { return false; } @@ -101,10 +100,11 @@ void QuotaNetwork::del(Template * tmpl) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int QuotaNetwork::get_default_quota(const string& id, VectorAttribute **va) +int QuotaNetwork::get_default_quota( + const string& id, + Quotas& default_quotas, + VectorAttribute **va) { - // TODO: We need to know if this is a user or group quota - Quotas default_quotas = Nebula::instance().get_default_user_quota(); return default_quotas.network_get(id, va); } diff --git a/src/um/QuotaVirtualMachine.cc b/src/um/QuotaVirtualMachine.cc index 4a8ccd8875..14c0335aee 100644 --- a/src/um/QuotaVirtualMachine.cc +++ b/src/um/QuotaVirtualMachine.cc @@ -16,7 +16,6 @@ #include "QuotaVirtualMachine.h" #include "Quotas.h" -#include "Nebula.h" /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -50,7 +49,9 @@ int QuotaVirtualMachine::get_quota(const string& id, VectorAttribute **va) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -bool QuotaVirtualMachine::check(Template * tmpl, string& error) +bool QuotaVirtualMachine::check(Template * tmpl, + Quotas& default_quotas, + string& error) { map vm_request; @@ -73,7 +74,7 @@ bool QuotaVirtualMachine::check(Template * tmpl, string& error) vm_request.insert(make_pair("MEMORY", memory)); vm_request.insert(make_pair("CPU", cpu)); - return check_quota("", vm_request, error); + return check_quota("", vm_request, default_quotas, error); } /* -------------------------------------------------------------------------- */ @@ -106,10 +107,8 @@ void QuotaVirtualMachine::del(Template * tmpl) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int QuotaVirtualMachine::get_default_quota(const string& id, VectorAttribute **va) +int QuotaVirtualMachine::get_default_quota(const string& id, Quotas& default_quotas, VectorAttribute **va) { - // TODO: We need to know if this is a user or group quota - Quotas default_quotas = Nebula::instance().get_default_user_quota(); return default_quotas.vm_get(id, va); } diff --git a/src/um/Quotas.cc b/src/um/Quotas.cc index b1a2b817c4..baa46e0bf2 100644 --- a/src/um/Quotas.cc +++ b/src/um/Quotas.cc @@ -173,35 +173,38 @@ void Quotas::quota_del(QuotaType type, Template *tmpl) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -bool Quotas::quota_check(QuotaType type, Template *tmpl, string& error_str) +bool Quotas::quota_check(QuotaType type, + Template *tmpl, + Quotas &default_quotas, + string &error_str) { switch (type) { case DATASTORE: - return datastore_quota.check(tmpl, error_str); + return datastore_quota.check(tmpl, default_quotas, error_str); case NETWORK: - return network_quota.check(tmpl, error_str); + return network_quota.check(tmpl, default_quotas, error_str); case IMAGE: - return image_quota.check(tmpl, error_str); + return image_quota.check(tmpl, default_quotas, error_str); case VM: - return vm_quota.check(tmpl, error_str); + return vm_quota.check(tmpl, default_quotas, error_str); case VIRTUALMACHINE: - if ( network_quota.check(tmpl, error_str) == false ) + if ( network_quota.check(tmpl, default_quotas, error_str) == false ) { return false; } - if ( vm_quota.check(tmpl, error_str) == false ) + if ( vm_quota.check(tmpl, default_quotas, error_str) == false ) { network_quota.del(tmpl); return false; } - if ( image_quota.check(tmpl, error_str) == false ) + if ( image_quota.check(tmpl, default_quotas, error_str) == false ) { network_quota.del(tmpl); vm_quota.del(tmpl); From 1fde5eca2e5ea02543e4b45628e3573a21cdf0fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 23 Nov 2012 16:03:10 +0100 Subject: [PATCH 03/28] Feature #1611: Add xml-rpc methods to retrieve and update the default user & group quotas --- include/Nebula.h | 10 ++++ include/RequestManagerSystem.h | 101 +++++++++++++++++++++++++++++++++ src/rm/RequestManager.cc | 14 ++++- src/rm/RequestManagerSystem.cc | 98 ++++++++++++++++++++++++++++++++ 4 files changed, 221 insertions(+), 2 deletions(-) diff --git a/include/Nebula.h b/include/Nebula.h index d1f5560a87..4038c6df7a 100644 --- a/include/Nebula.h +++ b/include/Nebula.h @@ -318,11 +318,21 @@ public: return default_user_quota; }; + int set_default_user_quota(Template *tmpl, string& error) + { + return default_user_quota.set(tmpl, error); + }; + const Quotas& get_default_group_quota() { return default_group_quota; }; + int set_default_group_quota(Template *tmpl, string& error) + { + return default_group_quota.set(tmpl, error); + }; + private: // ----------------------------------------------------------------------- diff --git a/include/RequestManagerSystem.h b/include/RequestManagerSystem.h index be66eaed25..cbd0696d57 100644 --- a/include/RequestManagerSystem.h +++ b/include/RequestManagerSystem.h @@ -78,6 +78,107 @@ public: RequestAttributes& att); }; +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +class UserQuotaInfo : public RequestManagerSystem +{ +public: + UserQuotaInfo(): + RequestManagerSystem("UserQuotaInfo", + "Returns the default user quota limits", + "A:s") + { + auth_op = AuthRequest::ADMIN; + }; + + ~UserQuotaInfo(){}; + + void request_execute(xmlrpc_c::paramList const& _paramList, + RequestAttributes& att); +}; + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +class GroupQuotaInfo : public RequestManagerSystem +{ +public: + GroupQuotaInfo(): + RequestManagerSystem("GroupQuotaInfo", + "Returns the default group quota limits", + "A:s") + { + auth_op = AuthRequest::ADMIN; + }; + + ~GroupQuotaInfo(){}; + + void request_execute(xmlrpc_c::paramList const& _paramList, + RequestAttributes& att); +}; + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +class QuotaUpdate : public RequestManagerSystem +{ +public: + QuotaUpdate(const string& method_name, + const string& help): + RequestManagerSystem(method_name, + help, + "A:ss") + { + auth_op = AuthRequest::ADMIN; + }; + + ~QuotaUpdate(){}; + + void request_execute(xmlrpc_c::paramList const& _paramList, + RequestAttributes& att); + + virtual int set_default_quota(Template *tmpl, string& error) = 0; + + virtual string& get_default_quota(string& xml) = 0; +}; + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +class UserQuotaUpdate : public QuotaUpdate +{ +public: + UserQuotaUpdate(): + QuotaUpdate("UserQuotaUpdate", + "Updates the default user quota limits") + { + auth_op = AuthRequest::ADMIN; + }; + + int set_default_quota(Template *tmpl, string& error); + + string& get_default_quota(string& xml); +}; + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +class GroupQuotaUpdate : public QuotaUpdate +{ +public: + GroupQuotaUpdate(): + QuotaUpdate("GroupQuotaUpdate", + "Updates the default group quota limits") + { + auth_op = AuthRequest::ADMIN; + }; + + int set_default_quota(Template *tmpl, string& error); + + string& get_default_quota(string& xml); +}; + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ diff --git a/src/rm/RequestManager.cc b/src/rm/RequestManager.cc index 5e9e0311b1..1ba7f4b8bf 100644 --- a/src/rm/RequestManager.cc +++ b/src/rm/RequestManager.cc @@ -368,6 +368,12 @@ void RequestManager::register_xml_methods() xmlrpc_c::methodPtr system_version(new SystemVersion()); xmlrpc_c::methodPtr system_config(new SystemConfig()); + xmlrpc_c::methodPtr user_get_default_quota(new UserQuotaInfo()); + xmlrpc_c::methodPtr user_set_default_quota(new UserQuotaUpdate()); + + xmlrpc_c::methodPtr group_get_default_quota(new GroupQuotaInfo()); + xmlrpc_c::methodPtr group_set_default_quota(new GroupQuotaUpdate()); + /* VM related methods */ RequestManagerRegistry.addMethod("one.vm.deploy", vm_deploy); RequestManagerRegistry.addMethod("one.vm.action", vm_action); @@ -416,6 +422,9 @@ void RequestManager::register_xml_methods() RequestManagerRegistry.addMethod("one.grouppool.info", grouppool_info); + RequestManagerRegistry.addMethod("one.groupquota.info", group_get_default_quota); + RequestManagerRegistry.addMethod("one.groupquota.update", group_set_default_quota); + /* Network related methods*/ RequestManagerRegistry.addMethod("one.vn.addleases", vn_addleases); RequestManagerRegistry.addMethod("one.vn.rmleases", vn_rmleases); @@ -441,6 +450,9 @@ void RequestManager::register_xml_methods() RequestManagerRegistry.addMethod("one.user.quota", user_set_quota); RequestManagerRegistry.addMethod("one.userpool.info", userpool_info); + + RequestManagerRegistry.addMethod("one.userquota.info", user_get_default_quota); + RequestManagerRegistry.addMethod("one.userquota.update", user_set_default_quota); /* Image related methods*/ RequestManagerRegistry.addMethod("one.image.persistent", image_persistent); @@ -500,8 +512,6 @@ void RequestManager::register_xml_methods() /* System related methods */ RequestManagerRegistry.addMethod("one.system.version", system_version); RequestManagerRegistry.addMethod("one.system.config", system_config); - - }; /* -------------------------------------------------------------------------- */ diff --git a/src/rm/RequestManagerSystem.cc b/src/rm/RequestManagerSystem.cc index df3f39204c..fd35a7f131 100644 --- a/src/rm/RequestManagerSystem.cc +++ b/src/rm/RequestManagerSystem.cc @@ -56,3 +56,101 @@ void SystemConfig::request_execute(xmlrpc_c::paramList const& paramList, /* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */ + +void UserQuotaInfo::request_execute(xmlrpc_c::paramList const& paramList, + RequestAttributes& att) +{ + string xml; + success_response(Nebula::instance().get_default_user_quota().to_xml(xml), att); + + return; +} + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +void GroupQuotaInfo::request_execute(xmlrpc_c::paramList const& paramList, + RequestAttributes& att) +{ + string xml; + success_response(Nebula::instance().get_default_group_quota().to_xml(xml), att); + + return; +} + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +void QuotaUpdate::request_execute(xmlrpc_c::paramList const& paramList, + RequestAttributes& att) +{ + string quota_str = xmlrpc_c::value_string(paramList.getString(1)); + string error_str; + Template quota_tmpl; + string xml; + + int rc; + + if ( att.gid != GroupPool::ONEADMIN_ID ) + { + failure_response(AUTHORIZATION, + "The default quotas can only be updated by users in the oneadmin group", + att); + return; + } + + rc = quota_tmpl.parse_str_or_xml(quota_str, error_str); + + if ( rc != 0 ) + { + failure_response(ACTION, request_error(error_str,""), att); + return; + } + + rc = set_default_quota("a_tmpl, error_str); + + if ( rc != 0 ) + { + failure_response(ACTION, request_error(error_str,""), att); + return; + } + + success_response(get_default_quota(xml), att); + + return; +} + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +int UserQuotaUpdate::set_default_quota(Template *tmpl, string& error) +{ + return Nebula::instance().set_default_user_quota(tmpl, error); +} + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +string& UserQuotaUpdate::get_default_quota(string& xml) +{ + return Nebula::instance().get_default_user_quota().to_xml(xml); +} + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +int GroupQuotaUpdate::set_default_quota(Template *tmpl, string& error) +{ + return Nebula::instance().set_default_group_quota(tmpl, error); +} + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +string& GroupQuotaUpdate::get_default_quota(string& xml) +{ + return Nebula::instance().get_default_group_quota().to_xml(xml); +} + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ From 38f2a8dab4345b135e54d8ea3536c5a345fda354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 23 Nov 2012 18:24:50 +0100 Subject: [PATCH 04/28] Feature #1611: Add a top level xml element to the default quota info methods --- include/RequestManagerSystem.h | 6 ++--- src/rm/RequestManagerSystem.cc | 43 +++++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/include/RequestManagerSystem.h b/include/RequestManagerSystem.h index cbd0696d57..22d3c17d5d 100644 --- a/include/RequestManagerSystem.h +++ b/include/RequestManagerSystem.h @@ -140,7 +140,7 @@ public: virtual int set_default_quota(Template *tmpl, string& error) = 0; - virtual string& get_default_quota(string& xml) = 0; + virtual string get_default_quota() = 0; }; /* ------------------------------------------------------------------------- */ @@ -158,7 +158,7 @@ public: int set_default_quota(Template *tmpl, string& error); - string& get_default_quota(string& xml); + string get_default_quota(); }; /* ------------------------------------------------------------------------- */ @@ -176,7 +176,7 @@ public: int set_default_quota(Template *tmpl, string& error); - string& get_default_quota(string& xml); + string get_default_quota(); }; /* -------------------------------------------------------------------------- */ diff --git a/src/rm/RequestManagerSystem.cc b/src/rm/RequestManagerSystem.cc index fd35a7f131..2d7701d23f 100644 --- a/src/rm/RequestManagerSystem.cc +++ b/src/rm/RequestManagerSystem.cc @@ -61,7 +61,14 @@ void UserQuotaInfo::request_execute(xmlrpc_c::paramList const& paramList, RequestAttributes& att) { string xml; - success_response(Nebula::instance().get_default_user_quota().to_xml(xml), att); + + ostringstream oss; + + oss << "" + << Nebula::instance().get_default_user_quota().to_xml(xml) + << ""; + + success_response(oss.str(), att); return; } @@ -73,7 +80,14 @@ void GroupQuotaInfo::request_execute(xmlrpc_c::paramList const& paramList, RequestAttributes& att) { string xml; - success_response(Nebula::instance().get_default_group_quota().to_xml(xml), att); + + ostringstream oss; + + oss << "" + << Nebula::instance().get_default_group_quota().to_xml(xml) + << ""; + + success_response(oss.str(), att); return; } @@ -87,7 +101,6 @@ void QuotaUpdate::request_execute(xmlrpc_c::paramList const& paramList, string quota_str = xmlrpc_c::value_string(paramList.getString(1)); string error_str; Template quota_tmpl; - string xml; int rc; @@ -115,7 +128,7 @@ void QuotaUpdate::request_execute(xmlrpc_c::paramList const& paramList, return; } - success_response(get_default_quota(xml), att); + success_response(get_default_quota(), att); return; } @@ -131,9 +144,16 @@ int UserQuotaUpdate::set_default_quota(Template *tmpl, string& error) /* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */ -string& UserQuotaUpdate::get_default_quota(string& xml) +string UserQuotaUpdate::get_default_quota() { - return Nebula::instance().get_default_user_quota().to_xml(xml); + string xml; + ostringstream oss; + + oss << "" + << Nebula::instance().get_default_user_quota().to_xml(xml) + << ""; + + return oss.str(); } /* ------------------------------------------------------------------------- */ @@ -147,9 +167,16 @@ int GroupQuotaUpdate::set_default_quota(Template *tmpl, string& error) /* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */ -string& GroupQuotaUpdate::get_default_quota(string& xml) +string GroupQuotaUpdate::get_default_quota() { - return Nebula::instance().get_default_group_quota().to_xml(xml); + string xml; + ostringstream oss; + + oss << "" + << Nebula::instance().get_default_group_quota().to_xml(xml) + << ""; + + return oss.str(); } /* ------------------------------------------------------------------------- */ From 2fd07e84562d3f7222c16deccaf364a4c5ba4910 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 23 Nov 2012 18:27:09 +0100 Subject: [PATCH 05/28] Feature #1611: Add default quota info and update methods to ruby oca --- src/oca/ruby/OpenNebula/GroupPool.rb | 25 ++++++++++++++++++++++--- src/oca/ruby/OpenNebula/Pool.rb | 15 +++++++++++++++ src/oca/ruby/OpenNebula/UserPool.rb | 23 +++++++++++++++++++++-- 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/src/oca/ruby/OpenNebula/GroupPool.rb b/src/oca/ruby/OpenNebula/GroupPool.rb index 20ebdeed08..782243e69c 100644 --- a/src/oca/ruby/OpenNebula/GroupPool.rb +++ b/src/oca/ruby/OpenNebula/GroupPool.rb @@ -25,7 +25,9 @@ module OpenNebula GROUP_POOL_METHODS = { - :info => "grouppool.info" + :info => "grouppool.info", + :quotainfo => "groupquota.info", + :quotaupdate => "groupquota.update" } ####################################################################### @@ -37,18 +39,35 @@ module OpenNebula super('GROUP_POOL','GROUP',client) end - # Factory method to create User objects + # Factory method to create Group objects def factory(element_xml) OpenNebula::Group.new(element_xml,@client) end ####################################################################### - # XML-RPC Methods for the User Object + # XML-RPC Methods for the GroupPool Object ####################################################################### # Retrieves all the Groups in the pool. def info() super(GROUP_POOL_METHODS[:info]) end + + # Gets the default group quota limits + # + # @return [String, OpenNebula::Error] the default group quota in case + # of success, Error otherwise + def get_quota() + return @client.call(GROUP_POOL_METHODS[:quotainfo]) + end + + # Sets the default group quota limits + # @param quota [String] a template (XML or txt) with the new quota limits + # + # @return [nil, OpenNebula::Error] nil in case of success, Error + # otherwise + def set_quota(quota) + return call(GROUP_POOL_METHODS[:quotaupdate], quota) + end end end diff --git a/src/oca/ruby/OpenNebula/Pool.rb b/src/oca/ruby/OpenNebula/Pool.rb index e917f7bb32..3f68d6204e 100644 --- a/src/oca/ruby/OpenNebula/Pool.rb +++ b/src/oca/ruby/OpenNebula/Pool.rb @@ -48,6 +48,21 @@ module OpenNebula # Common XML-RPC Methods for all the Pool Types ####################################################################### + # Common client call wrapper. Returns nil instead of the response + # if it is successful + # + # @param [String] xml_method xml-rpc method + # @param [Array] args any arguments for the xml-rpc method + # + # @return [nil, OpenNebula::Error] nil in case of success, Error + # otherwise + def call(xml_method, *args) + rc = @client.call(xml_method, *args) + rc = nil if !OpenNebula.is_error?(rc) + + return rc + end + #Gets the pool without any filter. Host, Group and User Pools # xml_method:: _String_ the name of the XML-RPC method def info(xml_method) diff --git a/src/oca/ruby/OpenNebula/UserPool.rb b/src/oca/ruby/OpenNebula/UserPool.rb index b4d9b733a6..85cf907b93 100644 --- a/src/oca/ruby/OpenNebula/UserPool.rb +++ b/src/oca/ruby/OpenNebula/UserPool.rb @@ -24,7 +24,9 @@ module OpenNebula ####################################################################### USER_POOL_METHODS = { - :info => "userpool.info" + :info => "userpool.info", + :quotainfo => "userquota.info", + :quotaupdate => "userquota.update" } ####################################################################### @@ -42,12 +44,29 @@ module OpenNebula end ####################################################################### - # XML-RPC Methods for the User Object + # XML-RPC Methods for the UserPool Object ####################################################################### # Retrieves all the Users in the pool. def info() super(USER_POOL_METHODS[:info]) end + + # Gets the default user quota limits + # + # @return [String, OpenNebula::Error] the default user quota in case + # of success, Error otherwise + def get_quota() + return @client.call(USER_POOL_METHODS[:quotainfo]) + end + + # Sets the default user quota limits + # @param quota [String] a template (XML or txt) with the new quota limits + # + # @return [nil, OpenNebula::Error] nil in case of success, Error + # otherwise + def set_quota(quota) + return call(USER_POOL_METHODS[:quotaupdate], quota) + end end end From a3b30e7bf37555c70a482a1222b21248548bfd74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 23 Nov 2012 18:28:09 +0100 Subject: [PATCH 06/28] Feature #1611: Show the default quotas limit in oneuser/group list --- src/cli/one_helper/onegroup_helper.rb | 38 ++++++++++++++++++++++++--- src/cli/one_helper/oneuser_helper.rb | 37 +++++++++++++++++++++++--- src/oca/ruby/OpenNebula/XMLUtils.rb | 18 ++++++++----- 3 files changed, 81 insertions(+), 12 deletions(-) diff --git a/src/cli/one_helper/onegroup_helper.rb b/src/cli/one_helper/onegroup_helper.rb index 1737f250a0..3f1fc3e1de 100644 --- a/src/cli/one_helper/onegroup_helper.rb +++ b/src/cli/one_helper/onegroup_helper.rb @@ -48,6 +48,16 @@ class OneGroupHelper < OpenNebulaHelper::OneHelper def format_pool(options) config_file = self.class.table_conf + gpool = GroupPool.new(@client) + default_xml_str = gpool.get_quota() + + # TODO: check error +# if OpenNebula::is_error?(default_xml_str) +# end + + default_user_quotas = XMLElement.new + default_user_quotas.initialize_xml(default_xml_str, 'DEFAULT_GROUP_QUOTAS') + table = CLIHelper::ShowTable.new(config_file, self) do column :ID, "ONE identifier for the Group", :size=>4 do |d| d["ID"] @@ -67,7 +77,14 @@ class OneGroupHelper < OpenNebulaHelper::OneHelper column :VMS , "Number of VMS", :size=>9 do |d| if d.has_key?('VM_QUOTA') and d['VM_QUOTA'].has_key?('VM') - "%3d / %3d" % [d['VM_QUOTA']['VM']["VMS_USED"], d['VM_QUOTA']['VM']["VMS"]] + limit = d['VM_QUOTA']['VM']["VMS"] + + if limit == "-1" + limit = default_user_quotas['VM_QUOTA/VM/VMS'] + limit = "0" if limit.nil? || limit == "" + end + + "%3d / %3d" % [d['VM_QUOTA']['VM']["VMS_USED"], limit] else "-" end @@ -75,8 +92,16 @@ class OneGroupHelper < OpenNebulaHelper::OneHelper column :MEMORY, "Total memory allocated to user VMs", :size=>17 do |d| if d.has_key?('VM_QUOTA') and d['VM_QUOTA'].has_key?('VM') + limit = d['VM_QUOTA']['VM']["MEMORY"] + + if limit == "-1" + limit = default_user_quotas['VM_QUOTA/VM/MEMORY'] + limit = "0" if limit.nil? || limit == "" + end + + d['VM_QUOTA']['VM']['MEMORY_USED'] "%7s / %7s" % [OpenNebulaHelper.unit_to_str(d['VM_QUOTA']['VM']["MEMORY_USED"].to_i,{},"M"), - OpenNebulaHelper.unit_to_str(d['VM_QUOTA']['VM']["MEMORY"].to_i,{},"M")] + OpenNebulaHelper.unit_to_str(limit.to_i,{},"M")] else "-" end @@ -84,7 +109,14 @@ class OneGroupHelper < OpenNebulaHelper::OneHelper column :CPU, "Total CPU allocated to user VMs", :size=>11 do |d| if d.has_key?('VM_QUOTA') and d['VM_QUOTA'].has_key?('VM') - "%4.0f / %4.0f" % [d['VM_QUOTA']['VM']["CPU_USED"], d['VM_QUOTA']['VM']["CPU"]] + limit = d['VM_QUOTA']['VM']["CPU"] + + if limit == "-1" + limit = default_user_quotas['VM_QUOTA/VM/CPU'] + limit = "0" if limit.nil? || limit == "" + end + + "%4.0f / %4.0f" % [d['VM_QUOTA']['VM']["CPU_USED"], limit] else "-" end diff --git a/src/cli/one_helper/oneuser_helper.rb b/src/cli/one_helper/oneuser_helper.rb index 70a5f38aaf..7f879d3bd7 100644 --- a/src/cli/one_helper/oneuser_helper.rb +++ b/src/cli/one_helper/oneuser_helper.rb @@ -147,6 +147,16 @@ class OneUserHelper < OpenNebulaHelper::OneHelper def format_pool(options) config_file = self.class.table_conf + upool = UserPool.new(@client) + default_xml_str = upool.get_quota() + + # TODO: check error +# if OpenNebula::is_error?(default_xml_str) +# end + + default_user_quotas = XMLElement.new + default_user_quotas.initialize_xml(default_xml_str, 'DEFAULT_USER_QUOTAS') + table = CLIHelper::ShowTable.new(config_file, self) do column :ID, "ONE identifier for the User", :size=>4 do |d| d["ID"] @@ -166,7 +176,14 @@ class OneUserHelper < OpenNebulaHelper::OneHelper column :VMS , "Number of VMS", :size=>9 do |d| if d.has_key?('VM_QUOTA') and d['VM_QUOTA'].has_key?('VM') - "%3d / %3d" % [d['VM_QUOTA']['VM']["VMS_USED"], d['VM_QUOTA']['VM']["VMS"]] + limit = d['VM_QUOTA']['VM']["VMS"] + + if limit == "-1" + limit = default_user_quotas['VM_QUOTA/VM/VMS'] + limit = "0" if limit.nil? || limit == "" + end + + "%3d / %3d" % [d['VM_QUOTA']['VM']["VMS_USED"], limit] else "-" end @@ -174,9 +191,16 @@ class OneUserHelper < OpenNebulaHelper::OneHelper column :MEMORY, "Total memory allocated to user VMs", :size=>17 do |d| if d.has_key?('VM_QUOTA') and d['VM_QUOTA'].has_key?('VM') + limit = d['VM_QUOTA']['VM']["MEMORY"] + + if limit == "-1" + limit = default_user_quotas['VM_QUOTA/VM/MEMORY'] + limit = "0" if limit.nil? || limit == "" + end + d['VM_QUOTA']['VM']['MEMORY_USED'] "%7s / %7s" % [OpenNebulaHelper.unit_to_str(d['VM_QUOTA']['VM']["MEMORY_USED"].to_i,{},"M"), - OpenNebulaHelper.unit_to_str(d['VM_QUOTA']['VM']["MEMORY"].to_i,{},"M")] + OpenNebulaHelper.unit_to_str(limit.to_i,{},"M")] else "-" end @@ -184,7 +208,14 @@ class OneUserHelper < OpenNebulaHelper::OneHelper column :CPU, "Total CPU allocated to user VMs", :size=>11 do |d| if d.has_key?('VM_QUOTA') and d['VM_QUOTA'].has_key?('VM') - "%4.0f / %4.0f" % [d['VM_QUOTA']['VM']["CPU_USED"], d['VM_QUOTA']['VM']["CPU"]] + limit = d['VM_QUOTA']['VM']["CPU"] + + if limit == "-1" + limit = default_user_quotas['VM_QUOTA/VM/CPU'] + limit = "0" if limit.nil? || limit == "" + end + + "%4.0f / %4.0f" % [d['VM_QUOTA']['VM']["CPU_USED"], limit] else "-" end diff --git a/src/oca/ruby/OpenNebula/XMLUtils.rb b/src/oca/ruby/OpenNebula/XMLUtils.rb index 13100fafdf..9a244312c0 100644 --- a/src/oca/ruby/OpenNebula/XMLUtils.rb +++ b/src/oca/ruby/OpenNebula/XMLUtils.rb @@ -79,12 +79,18 @@ module OpenNebula return doc end - # Extract an element from the XML description of the PoolElement. - # key::_String_ The name of the element - # [return] _String_ the value of the element - # Examples: - # ['VID'] # gets VM id - # ['HISTORY/HOSTNAME'] # get the hostname from the history + + # Extract a text element from the XML description of the PoolElement. + # + # @param [String] key Xpath expression + # + # @return [String, nil] If a text element is found, the element's + # text value. Otherwise, an empty string or nil, depending + # on the backend + # + # @example + # vm['VID'] # gets VM id + # vm['HISTORY/HOSTNAME'] # get the hostname from the history def [](key) if NOKOGIRI element=@xml.xpath(key.to_s) From b42fe133e530b422a4b05218593d3c130602c9cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 23 Nov 2012 18:48:09 +0100 Subject: [PATCH 07/28] Feature #1611: New (provisional?) method oneuser defaultquota --- src/cli/one_helper/onequota_helper.rb | 66 +++++++++++++++++++++++++-- src/cli/oneuser | 23 ++++++++++ 2 files changed, 85 insertions(+), 4 deletions(-) diff --git a/src/cli/one_helper/onequota_helper.rb b/src/cli/one_helper/onequota_helper.rb index f448389586..960e5f2631 100644 --- a/src/cli/one_helper/onequota_helper.rb +++ b/src/cli/one_helper/onequota_helper.rb @@ -96,12 +96,16 @@ class OneQuotaHelper # ] # # IMAGE = [ - # ID = - # RVMS = + # ID = + # RVMS = # ] # - # In any quota 0 means unlimited. The usage counters "*_USED" are - # shown for information purposes and will NOT be modified. + # In any quota: + # -1 means use the default limit ('defaultquota' command) + # 0 means unlimited. + # + # The usage counters "*_USED" are shown for information + # purposes and will NOT be modified. #----------------------------------------------------------------------- EOT @@ -152,6 +156,60 @@ class OneQuotaHelper str end + # Edits the default quota template of a pool + # @param [Pool] resource to get the current info from + # @param [String] root_elem the root element's name + # @param [String] path to the new contents. If nil a editor will be + # used + # @return [String] contents of the new quotas + def self.set_defaultquota(resource, root_elem, path) + str = "" + + if path.nil? + require 'tempfile' + + tmp = Tempfile.new('one-cli') + path = tmp.path + + + + default_xml_str = resource.get_quota() + + if OpenNebula::is_error?(default_xml_str) + puts default_xml_str.message + exit -1 + end + + default_quotas = XMLElement.new + default_quotas.initialize_xml(default_xml_str, root_elem) + + + tmp << HELP_QUOTA + tmp << default_quotas.template_like_str("DATASTORE_QUOTA") << "\n" + tmp << default_quotas.template_like_str("VM_QUOTA") << "\n" + tmp << default_quotas.template_like_str("NETWORK_QUOTA") << "\n" + tmp << default_quotas.template_like_str("IMAGE_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 + # Retrieves a clean quota template, without any existing resource # information # @param path [String] path to the new contents. If nil a editor will be diff --git a/src/cli/oneuser b/src/cli/oneuser index 12600e0412..840c20eda2 100755 --- a/src/cli/oneuser +++ b/src/cli/oneuser @@ -240,6 +240,29 @@ cmd=CommandParser::CmdParser.new(ARGV) do end end + defaultquota_desc = <<-EOT.unindent + Sets the default quota limits for the users. If a path is not provided + the editor will be launched to modify the current default quotas. + EOT + + command :defaultquota, defaultquota_desc, [:file, nil] do + + # TODO: Do not create a new Client here +# user_pool = factory_pool() + user_pool = UserPool.new(Client.new) + + str = OneQuotaHelper.set_defaultquota(user_pool, 'DEFAULT_USER_QUOTAS', args[1]) + + rc = user_pool.set_quota(str) + + if OpenNebula.is_error?(rc) + puts rc.message + exit -1 + end + + exit 0 + end + login_desc = <<-EOT.unindent Creates the Login token for authentication Examples: From edfed5e53137c0f4f66d185f00a00d3113976925 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Mon, 26 Nov 2012 12:48:10 +0100 Subject: [PATCH 08/28] Feature #1611: Create new class DefaultQuotas, and disable the limit '-1' for def. quotas --- include/DefaultQuotas.h | 49 +++++++++++++++++++++++++++++++++++++++++ include/Nebula.h | 6 ++--- include/Quota.h | 14 +++++++++--- include/Quotas.h | 18 +++++++++++++-- src/um/Quota.cc | 28 ++++++++++++++--------- src/um/Quotas.cc | 11 +++++---- 6 files changed, 101 insertions(+), 25 deletions(-) create mode 100644 include/DefaultQuotas.h diff --git a/include/DefaultQuotas.h b/include/DefaultQuotas.h new file mode 100644 index 0000000000..209df83b3c --- /dev/null +++ b/include/DefaultQuotas.h @@ -0,0 +1,49 @@ +/* -------------------------------------------------------------------------- */ +/* 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. */ +/* -------------------------------------------------------------------------- */ + +#ifndef DEFAULT_QUOTAS_H_ +#define DEFAULT_QUOTAS_H_ + +#include "Quotas.h" + +class DefaultQuotas : public Quotas +{ +public: + DefaultQuotas(const char * _ds_xpath, + const char * _net_xpath, + const char * _img_xpath, + const char * _vm_xpath): + Quotas(_ds_xpath, _net_xpath, _img_xpath, _vm_xpath) + {}; + + ~DefaultQuotas(){}; + + /** + * Set the quotas + * @param tmpl contains the user quota limits + * @param error describes error when setting the quotas + * + * @return 0 on success, -1 otherwise + */ + int set(Template *tmpl, string& error) + { + return Quotas::set(tmpl, false, error); + }; + +private: +}; + +#endif /*DEFAULT_QUOTAS_H_*/ diff --git a/include/Nebula.h b/include/Nebula.h index 4038c6df7a..ea0580c668 100644 --- a/include/Nebula.h +++ b/include/Nebula.h @@ -42,7 +42,7 @@ #include "AclManager.h" #include "ImageManager.h" -#include "Quotas.h" +#include "DefaultQuotas.h" #include "Callbackable.h" @@ -527,8 +527,8 @@ private: // Default quotas // --------------------------------------------------------------- - Quotas default_user_quota; - Quotas default_group_quota; + DefaultQuotas default_user_quota; + DefaultQuotas default_group_quota; // --------------------------------------------------------------- diff --git a/include/Quota.h b/include/Quota.h index bc22e911dc..756fe903d1 100644 --- a/include/Quota.h +++ b/include/Quota.h @@ -35,10 +35,12 @@ public: /** * Set the quotas. If the quota previously exists its limit is updated. * @param quota_str the quota template in ASCII or XML formats + * @param default_allowed whether or not the limit -1 is allowed * @param error describe the error in case of error + * * @return 0 on success -1 otherwise */ - int set(vector * quotas, string& error); + int set(vector * quotas, bool default_allowed, string& error); /** * Check if the resource allocation will exceed the quota limits. If not @@ -178,9 +180,11 @@ private: * Creates an empty quota based on the given attribute. The attribute va * contains the limits for the quota. * @param va limits for the new quota if 0 limits will be 0 + * @param default_allowed whether or not the limit -1 is allowed + * * @return a new attribute representing the quota */ - VectorAttribute * new_quota(VectorAttribute* va); + VectorAttribute * new_quota(VectorAttribute* va, bool default_allowed); /** * Adds a new quota, it also updates an internal index for fast accessing @@ -204,9 +208,13 @@ private: * Sets new limit values for the quota * @param quota to be updated * @param va attribute with the new limits + * @param default_allowed whether or not the limit -1 is allowed + * * @return 0 on success or -1 if wrong limits */ - int update_limits(VectorAttribute* quota, const VectorAttribute* va); + int update_limits(VectorAttribute* quota, + const VectorAttribute* va, + bool default_allowed); /** * Extract the limits for the defined quota metrics from a given attribute diff --git a/include/Quotas.h b/include/Quotas.h index 1097ea9306..e819e5681c 100644 --- a/include/Quotas.h +++ b/include/Quotas.h @@ -37,7 +37,7 @@ public: vm_xpath(_vm_xpath) {}; - virtual ~Quotas(){}; + ~Quotas(){}; /** * Different quota types @@ -57,7 +57,10 @@ public: * * @return 0 on success, -1 otherwise */ - int set(Template *tmpl, string& error); + virtual int set(Template *tmpl, string& error) + { + return set(tmpl, true, error); + }; /** * Delete usage from quota counters. @@ -199,6 +202,17 @@ public: */ static void quota_del(QuotaType type, int uid, int gid, Template * tmpl); +protected: + /** + * Set the quotas + * @param tmpl contains the user quota limits + * @param default_allowed whether or not the limit -1 is allowed + * @param error describes error when setting the quotas + * + * @return 0 on success, -1 otherwise + */ + int set(Template *tmpl, bool default_allowed, string& error); + private: //-------------------------------------------------------------------------- // Usage Counters and Quotas diff --git a/src/um/Quota.cc b/src/um/Quota.cc index 326bd5c47e..cbf524ebd5 100644 --- a/src/um/Quota.cc +++ b/src/um/Quota.cc @@ -80,7 +80,7 @@ void Quota::add_to_quota(VectorAttribute * attr, const string& va_name, float nu /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int Quota::set(vector * new_quotas, string& error) +int Quota::set(vector * new_quotas, bool default_allowed, string& error) { vector::iterator it; @@ -108,7 +108,7 @@ int Quota::set(vector * new_quotas, string& error) { VectorAttribute * nq; - if ((nq = new_quota(iq)) == 0) + if ((nq = new_quota(iq, default_allowed)) == 0) { goto error_limits; } @@ -117,7 +117,7 @@ int Quota::set(vector * new_quotas, string& error) } else { - if (update_limits(tq, iq) != 0) + if (update_limits(tq, iq, default_allowed) != 0) { goto error_limits; } @@ -368,8 +368,11 @@ void Quota::cleanup_quota(const string& qid) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int Quota::update_limits(VectorAttribute * quota, const VectorAttribute * va) -{ +int Quota::update_limits( + VectorAttribute * quota, + const VectorAttribute * va, + bool default_allowed) +{ string limit; float limit_i; @@ -377,9 +380,10 @@ int Quota::update_limits(VectorAttribute * quota, const VectorAttribute * va) { limit = va->vector_value_str(metrics[i], limit_i); - //No quota, NaN or negative. -1 is allowed, it means default limit - if ( limit_i < -1 || - ( limit_i == -1 && limit == "" )) + //No quota, NaN or negative + if ((default_allowed && + ( limit_i < -1 || ( limit_i == -1 && limit == "" ))) || + (!default_allowed && limit_i < 0) ) { return -1; } @@ -395,7 +399,7 @@ int Quota::update_limits(VectorAttribute * quota, const VectorAttribute * va) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -VectorAttribute * Quota::new_quota(VectorAttribute * va) +VectorAttribute * Quota::new_quota(VectorAttribute * va, bool default_allowed) { map limits; @@ -409,8 +413,10 @@ VectorAttribute * Quota::new_quota(VectorAttribute * va) metrics_used += "_USED"; limit = va->vector_value_str(metrics[i], limit_i); - - if ( limit_i < 0 ) //No quota, NaN or negative + + //No quota, NaN or negative + if ( (default_allowed && limit_i < -1) || + (!default_allowed && limit_i < 0) ) { limit = "0"; } diff --git a/src/um/Quotas.cc b/src/um/Quotas.cc index baa46e0bf2..58124cb33f 100644 --- a/src/um/Quotas.cc +++ b/src/um/Quotas.cc @@ -19,14 +19,13 @@ #include "ObjectXML.h" - -int Quotas::set(Template *tmpl, string& error) +int Quotas::set(Template *tmpl, bool default_allowed, string& error) { vector vquotas; if ( tmpl->get(datastore_quota.get_quota_name(), vquotas) > 0 ) { - if ( datastore_quota.set(&vquotas, error) != 0 ) + if ( datastore_quota.set(&vquotas, default_allowed, error) != 0 ) { return -1; } @@ -36,7 +35,7 @@ int Quotas::set(Template *tmpl, string& error) if ( tmpl->get(network_quota.get_quota_name(), vquotas) > 0 ) { - if ( network_quota.set(&vquotas, error) != 0 ) + if ( network_quota.set(&vquotas, default_allowed, error) != 0 ) { return -1; } @@ -46,7 +45,7 @@ int Quotas::set(Template *tmpl, string& error) if ( tmpl->get(image_quota.get_quota_name(), vquotas) > 0 ) { - if ( image_quota.set(&vquotas, error) != 0 ) + if ( image_quota.set(&vquotas, default_allowed, error) != 0 ) { return -1; } @@ -56,7 +55,7 @@ int Quotas::set(Template *tmpl, string& error) if ( tmpl->get(vm_quota.get_quota_name(), vquotas) > 0 ) { - if ( vm_quota.set(&vquotas, error) != 0 ) + if ( vm_quota.set(&vquotas, default_allowed, error) != 0 ) { return -1; } From 08f8806de9f285b04f77f57599055232ec513761 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Mon, 26 Nov 2012 16:12:05 +0100 Subject: [PATCH 09/28] Feature #1611: DefaultQuotas methods to & from xml --- include/DefaultQuotas.h | 28 ++++++++-- include/Nebula.h | 22 ++++---- include/Quotas.h | 1 - include/RequestManagerSystem.h | 7 +-- src/rm/RequestManagerSystem.cc | 42 +++------------ src/um/DefaultQuotas.cc | 93 ++++++++++++++++++++++++++++++++++ src/um/SConstruct | 3 +- 7 files changed, 142 insertions(+), 54 deletions(-) create mode 100644 src/um/DefaultQuotas.cc diff --git a/include/DefaultQuotas.h b/include/DefaultQuotas.h index 209df83b3c..892f23415c 100644 --- a/include/DefaultQuotas.h +++ b/include/DefaultQuotas.h @@ -22,11 +22,14 @@ class DefaultQuotas : public Quotas { public: - DefaultQuotas(const char * _ds_xpath, - const char * _net_xpath, - const char * _img_xpath, - const char * _vm_xpath): - Quotas(_ds_xpath, _net_xpath, _img_xpath, _vm_xpath) + DefaultQuotas( + const char * _root_elem, + const char * _ds_xpath, + const char * _net_xpath, + const char * _img_xpath, + const char * _vm_xpath): + Quotas(_ds_xpath, _net_xpath, _img_xpath, _vm_xpath), + root_elem(_root_elem) {}; ~DefaultQuotas(){}; @@ -43,7 +46,22 @@ public: return Quotas::set(tmpl, false, error); }; + /** + * Generates a string representation of the quotas in XML format + * @param xml the string to store the XML + * @return the same xml string to use it in << compounds + */ + string& to_xml(string& xml) const; + + /** + * Builds quota object from an ObjectXML + * @param xml The xml-formatted string + * @return 0 if success + */ + int from_xml(const string& xml); + private: + const char * root_elem; }; #endif /*DEFAULT_QUOTAS_H_*/ diff --git a/include/Nebula.h b/include/Nebula.h index ea0580c668..39430cc5d5 100644 --- a/include/Nebula.h +++ b/include/Nebula.h @@ -313,7 +313,7 @@ public: return nebula_configuration->to_xml(xml); }; - const Quotas& get_default_user_quota() + const DefaultQuotas& get_default_user_quota() { return default_user_quota; }; @@ -323,7 +323,7 @@ public: return default_user_quota.set(tmpl, error); }; - const Quotas& get_default_group_quota() + const DefaultQuotas& get_default_group_quota() { return default_group_quota; }; @@ -340,14 +340,16 @@ private: // ----------------------------------------------------------------------- Nebula():nebula_configuration(0), - default_user_quota("/USER/DATASTORE_QUOTA", - "/USER/NETWORK_QUOTA", - "/USER/IMAGE_QUOTA", - "/USER/VM_QUOTA"), - default_group_quota("/GROUP/DATASTORE_QUOTA", - "/GROUP/NETWORK_QUOTA", - "/GROUP/IMAGE_QUOTA", - "/GROUP/VM_QUOTA"), + default_user_quota( "DEFAULT_USER_QUOTAS", + "/DEFAULT_USER_QUOTAS/DATASTORE_QUOTA", + "/DEFAULT_USER_QUOTAS/NETWORK_QUOTA", + "/DEFAULT_USER_QUOTAS/IMAGE_QUOTA", + "/DEFAULT_USER_QUOTAS/VM_QUOTA"), + default_group_quota("DEFAULT_GROUP_QUOTAS", + "/DEFAULT_GROUP_QUOTAS/DATASTORE_QUOTA", + "/DEFAULT_GROUP_QUOTAS/NETWORK_QUOTA", + "/DEFAULT_GROUP_QUOTAS/IMAGE_QUOTA", + "/DEFAULT_GROUP_QUOTAS/VM_QUOTA"), db(0),vmpool(0),hpool(0),vnpool(0), upool(0),ipool(0),gpool(0),tpool(0),dspool(0),clpool(0),docpool(0), lcm(0),vmm(0),im(0),tm(0),dm(0),rm(0),hm(0),authm(0),aclm(0),imagem(0) diff --git a/include/Quotas.h b/include/Quotas.h index e819e5681c..5e2344171f 100644 --- a/include/Quotas.h +++ b/include/Quotas.h @@ -213,7 +213,6 @@ protected: */ int set(Template *tmpl, bool default_allowed, string& error); -private: //-------------------------------------------------------------------------- // Usage Counters and Quotas //-------------------------------------------------------------------------- diff --git a/include/RequestManagerSystem.h b/include/RequestManagerSystem.h index 22d3c17d5d..4e436b7d4e 100644 --- a/include/RequestManagerSystem.h +++ b/include/RequestManagerSystem.h @@ -18,6 +18,7 @@ #define REQUEST_MANAGER_SYSTEM_H #include "Request.h" +#include "DefaultQuotas.h" using namespace std; @@ -140,7 +141,7 @@ public: virtual int set_default_quota(Template *tmpl, string& error) = 0; - virtual string get_default_quota() = 0; + const virtual DefaultQuotas* get_default_quota() = 0; }; /* ------------------------------------------------------------------------- */ @@ -158,7 +159,7 @@ public: int set_default_quota(Template *tmpl, string& error); - string get_default_quota(); + const DefaultQuotas* get_default_quota(); }; /* ------------------------------------------------------------------------- */ @@ -176,7 +177,7 @@ public: int set_default_quota(Template *tmpl, string& error); - string get_default_quota(); + const DefaultQuotas* get_default_quota(); }; /* -------------------------------------------------------------------------- */ diff --git a/src/rm/RequestManagerSystem.cc b/src/rm/RequestManagerSystem.cc index 2d7701d23f..f020f4e84a 100644 --- a/src/rm/RequestManagerSystem.cc +++ b/src/rm/RequestManagerSystem.cc @@ -62,13 +62,7 @@ void UserQuotaInfo::request_execute(xmlrpc_c::paramList const& paramList, { string xml; - ostringstream oss; - - oss << "" - << Nebula::instance().get_default_user_quota().to_xml(xml) - << ""; - - success_response(oss.str(), att); + success_response(Nebula::instance().get_default_user_quota().to_xml(xml), att); return; } @@ -81,13 +75,7 @@ void GroupQuotaInfo::request_execute(xmlrpc_c::paramList const& paramList, { string xml; - ostringstream oss; - - oss << "" - << Nebula::instance().get_default_group_quota().to_xml(xml) - << ""; - - success_response(oss.str(), att); + success_response(Nebula::instance().get_default_group_quota().to_xml(xml), att); return; } @@ -99,7 +87,7 @@ void QuotaUpdate::request_execute(xmlrpc_c::paramList const& paramList, RequestAttributes& att) { string quota_str = xmlrpc_c::value_string(paramList.getString(1)); - string error_str; + string error_str, xml; Template quota_tmpl; int rc; @@ -128,7 +116,7 @@ void QuotaUpdate::request_execute(xmlrpc_c::paramList const& paramList, return; } - success_response(get_default_quota(), att); + success_response(get_default_quota()->to_xml(xml), att); return; } @@ -144,16 +132,9 @@ int UserQuotaUpdate::set_default_quota(Template *tmpl, string& error) /* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */ -string UserQuotaUpdate::get_default_quota() +const DefaultQuotas * UserQuotaUpdate::get_default_quota() { - string xml; - ostringstream oss; - - oss << "" - << Nebula::instance().get_default_user_quota().to_xml(xml) - << ""; - - return oss.str(); + return &Nebula::instance().get_default_user_quota(); } /* ------------------------------------------------------------------------- */ @@ -167,16 +148,9 @@ int GroupQuotaUpdate::set_default_quota(Template *tmpl, string& error) /* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */ -string GroupQuotaUpdate::get_default_quota() +const DefaultQuotas * GroupQuotaUpdate::get_default_quota() { - string xml; - ostringstream oss; - - oss << "" - << Nebula::instance().get_default_group_quota().to_xml(xml) - << ""; - - return oss.str(); + return &Nebula::instance().get_default_group_quota(); } /* ------------------------------------------------------------------------- */ diff --git a/src/um/DefaultQuotas.cc b/src/um/DefaultQuotas.cc new file mode 100644 index 0000000000..fc8877dc24 --- /dev/null +++ b/src/um/DefaultQuotas.cc @@ -0,0 +1,93 @@ +/* -------------------------------------------------------------------------- */ +/* 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. */ +/* -------------------------------------------------------------------------- */ + +#include "DefaultQuotas.h" +#include "ObjectXML.h" + +string& DefaultQuotas::to_xml(string& xml) const +{ + ostringstream oss; + + string ds_quota_xml; + string net_quota_xml; + string vm_quota_xml; + string image_quota_xml; + + oss << "<" << root_elem << ">" + << datastore_quota.to_xml(ds_quota_xml) + << network_quota.to_xml(net_quota_xml) + << vm_quota.to_xml(vm_quota_xml) + << image_quota.to_xml(image_quota_xml) + << ""; + + xml = oss.str(); + + return xml; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int DefaultQuotas::from_xml(const string& xml) +{ + vector content; + int rc = 0; + + ObjectXML *object_xml = new ObjectXML(xml); + + object_xml->get_nodes(ds_xpath, content); + + if (!content.empty()) + { + rc += datastore_quota.from_xml_node(content[0]); + } + + object_xml->free_nodes(content); + content.clear(); + + object_xml->get_nodes(net_xpath, content); + + if (!content.empty()) + { + rc += network_quota.from_xml_node(content[0]); + } + + object_xml->free_nodes(content); + content.clear(); + + object_xml->get_nodes(vm_xpath, content); + + if (!content.empty()) + { + rc += vm_quota.from_xml_node(content[0]); + } + + object_xml->free_nodes(content); + content.clear(); + + object_xml->get_nodes(img_xpath, content); + + if (!content.empty()) + { + rc += image_quota.from_xml_node(content[0]); + } + + object_xml->free_nodes(content); + + delete object_xml; + + return rc; +} diff --git a/src/um/SConstruct b/src/um/SConstruct index 0c8ea406dd..122767aff1 100644 --- a/src/um/SConstruct +++ b/src/um/SConstruct @@ -29,7 +29,8 @@ source_files=[ 'QuotaNetwork.cc', 'QuotaVirtualMachine.cc', 'QuotaImage.cc', - 'Quotas.cc' + 'Quotas.cc', + 'DefaultQuotas.cc' ] # Build library From c35887f3c3d8c36ab431cb038bd4564e3d4957a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Mon, 26 Nov 2012 18:14:12 +0100 Subject: [PATCH 10/28] Feature #1611: Add persistence to default quotas, in a new DB table --- include/DefaultQuotas.h | 92 +++++++++++++++++++++++++++++++-- include/Nebula.h | 20 +++++++- src/nebula/Nebula.cc | 9 ++++ src/um/DefaultQuotas.cc | 109 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 223 insertions(+), 7 deletions(-) diff --git a/include/DefaultQuotas.h b/include/DefaultQuotas.h index 892f23415c..22a81b875e 100644 --- a/include/DefaultQuotas.h +++ b/include/DefaultQuotas.h @@ -18,8 +18,10 @@ #define DEFAULT_QUOTAS_H_ #include "Quotas.h" +#include "SqlDB.h" +#include "ObjectSQL.h" -class DefaultQuotas : public Quotas +class DefaultQuotas : public Quotas, ObjectSQL { public: DefaultQuotas( @@ -54,14 +56,94 @@ public: string& to_xml(string& xml) const; /** - * Builds quota object from an ObjectXML - * @param xml The xml-formatted string - * @return 0 if success + * Writes the quotas in the database. + * @param db pointer to the db + * @return 0 on success */ - int from_xml(const string& xml); + int insert(SqlDB *db, string& error_str) + { + return insert_replace(db, false, error_str); + }; + + /** + * Writes/updates the quotas data fields in the database. + * @param db pointer to the db + * @return 0 on success + */ + int update(SqlDB *db) + { + string error_str; + return insert_replace(db, true, error_str); + }; + + /** + * Reads the Quotas from the database. + * @param db pointer to the db + * @return 0 on success + */ + int select(SqlDB *db); + + /** + * Bootstraps the database table(s) associated to the default quotas + * @return 0 on success + */ + static int bootstrap(SqlDB * db); private: const char * root_elem; + + // ************************************************************************* + // DataBase implementation + // ************************************************************************* + + static const char * db_names; + + static const char * db_bootstrap; + + static const char * table; + + /** + * Execute an INSERT or REPLACE Sql query. + * @param db The SQL DB + * @param replace Execute an INSERT or a REPLACE + * @param error_str Returns the error reason, if any + * @return 0 one success + */ + int insert_replace(SqlDB *db, bool replace, string& error_str); + + /** + * Callback function to unmarshall a PoolObjectSQL + * @param num the number of columns read from the DB + * @param names the column names + * @param vaues the column values + * @return 0 on success + */ + int select_cb(void *nil, int num, char **values, char **names) + { + if ( (!values[0]) || (num != 1) ) + { + return -1; + } + + return from_xml(values[0]); + }; + + /** + * Builds quota object from an ObjectXML + * @param xml The xml-formatted string + * @return 0 on success + */ + int from_xml(const string& xml); + + /** + * Removes the Quotas from the database. + * @param db pointer to the db + * @return 0 on success + */ + int drop(SqlDB * db) + { + return -1; // Not supported + }; }; #endif /*DEFAULT_QUOTAS_H_*/ diff --git a/include/Nebula.h b/include/Nebula.h index 39430cc5d5..ef30c0e32a 100644 --- a/include/Nebula.h +++ b/include/Nebula.h @@ -320,7 +320,15 @@ public: int set_default_user_quota(Template *tmpl, string& error) { - return default_user_quota.set(tmpl, error); + int rc; + rc = default_user_quota.set(tmpl, error); + + if ( rc == 0 ) + { + rc = default_user_quota.update(db); + } + + return rc; }; const DefaultQuotas& get_default_group_quota() @@ -330,7 +338,15 @@ public: int set_default_group_quota(Template *tmpl, string& error) { - return default_group_quota.set(tmpl, error); + int rc; + rc = default_group_quota.set(tmpl, error); + + if ( rc == 0 ) + { + rc = default_group_quota.update(db); + } + + return rc; }; private: diff --git a/src/nebula/Nebula.cc b/src/nebula/Nebula.cc index d7b5513d56..e6a6eaed9a 100644 --- a/src/nebula/Nebula.cc +++ b/src/nebula/Nebula.cc @@ -245,6 +245,8 @@ void Nebula::start() rc += ClusterPool::bootstrap(db); rc += DocumentPool::bootstrap(db); + rc += DefaultQuotas::bootstrap(db); + // Create the versioning table only if bootstrap went well if ( rc == 0 ) { @@ -340,6 +342,9 @@ void Nebula::start() tpool = new VMTemplatePool(db); dspool = new DatastorePool(db); + + default_user_quota.select(db); + default_group_quota.select(db); } catch (exception&) { @@ -681,6 +686,7 @@ int Nebula::bootstrap() { int rc; ostringstream oss; + string error; oss << "CREATE TABLE pool_control (tablename VARCHAR(32) PRIMARY KEY, " "last_oid BIGINT UNSIGNED)"; @@ -700,6 +706,9 @@ int Nebula::bootstrap() rc += db->exec(oss); + rc += default_user_quota.insert(db, error); + rc += default_group_quota.insert(db, error); + return rc; } diff --git a/src/um/DefaultQuotas.cc b/src/um/DefaultQuotas.cc index fc8877dc24..139e53e9aa 100644 --- a/src/um/DefaultQuotas.cc +++ b/src/um/DefaultQuotas.cc @@ -17,6 +17,15 @@ #include "DefaultQuotas.h" #include "ObjectXML.h" +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +const char * DefaultQuotas::table = "system_attributes"; +const char * DefaultQuotas::db_names = "name, body"; + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + string& DefaultQuotas::to_xml(string& xml) const { ostringstream oss; @@ -91,3 +100,103 @@ int DefaultQuotas::from_xml(const string& xml) return rc; } + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int DefaultQuotas::select(SqlDB *db) +{ + ostringstream oss; + + int rc; + + set_callback( + static_cast(&DefaultQuotas::select_cb)); + + oss << "SELECT body FROM " << table << " WHERE name = '" << root_elem << "'"; + + rc = db->exec(oss, this); + + unset_callback(); + + if (rc != 0) + { + return -1; + } + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int DefaultQuotas::insert_replace(SqlDB *db, bool replace, string& error_str) +{ + ostringstream oss; + + int rc; + string xml_body; + + char * sql_xml; + + sql_xml = db->escape_str(to_xml(xml_body).c_str()); + + if ( sql_xml == 0 ) + { + goto error_body; + } +/* + if ( validate_xml(sql_xml) != 0 ) + { + goto error_xml; + } +*/ + if ( replace ) + { + oss << "REPLACE"; + } + else + { + oss << "INSERT"; + } + + // Construct the SQL statement to Insert or Replace + + oss <<" INTO "<exec(oss); + + db->free_str(sql_xml); + + return rc; +/* +error_xml: + db->free_str(sql_xml); + + error_str = "Error transforming the Quotas to XML."; + + goto error_common; +*/ +error_body: + goto error_generic; + +error_generic: + error_str = "Error inserting Quotas in DB."; +error_common: + return -1; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int DefaultQuotas::bootstrap(SqlDB * db) +{ + ostringstream oss; + + oss << "CREATE TABLE IF NOT EXISTS system_attributes (" + "name VARCHAR(128) PRIMARY KEY, body TEXT)"; + + return db->exec(oss); +} From 534e793402362967e5c01d480f6d6b573d74829e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Mon, 26 Nov 2012 18:30:54 +0100 Subject: [PATCH 11/28] Feature #1611: When a new usage quota is created, the limit is set to -1 (use default) --- src/um/Quota.cc | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/um/Quota.cc b/src/um/Quota.cc index cbf524ebd5..179768640d 100644 --- a/src/um/Quota.cc +++ b/src/um/Quota.cc @@ -187,34 +187,22 @@ bool Quota::check_quota(const string& qid, for (int i=0; i < num_metrics; i++) { - ostringstream usage_req_str; - string metrics_used = metrics[i]; + string metrics_used = metrics[i]; metrics_used += "_USED"; - it = usage_req.find(metrics[i]); - - if (it == usage_req.end()) - { - usage_req_str << "0"; - } - else - { - usage_req_str << it->second; - } - - values.insert(make_pair(metrics[i], "0")); - values.insert(make_pair(metrics_used, usage_req_str.str())); + values.insert(make_pair(metrics[i], "-1")); + values.insert(make_pair(metrics_used, "0")); } - + if (!qid.empty()) { values.insert(make_pair("ID", qid)); } - add(new VectorAttribute(template_name, values)); + q = new VectorAttribute(template_name, values); - return true; + add(q); } // ------------------------------------------------------------------------- @@ -236,9 +224,16 @@ bool Quota::check_quota(const string& qid, q->vector_value(metrics[i], limit); q->vector_value(metrics_used.c_str(), usage); - if ( limit == -1 && default_q != 0 ) + if ( limit == -1 ) { - default_q->vector_value(metrics[i], limit); + if ( default_q != 0 ) + { + default_q->vector_value(metrics[i], limit); + } + else + { + limit = 0; + } } check = ( limit == 0 ) || ( ( usage + it->second ) <= limit ); From cb1361074bfc5eeb8ac5d273a1b289b18d7d7555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Tue, 27 Nov 2012 12:42:46 +0100 Subject: [PATCH 12/28] Feature #1611: Fix quota cleanup --- src/um/Quota.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/um/Quota.cc b/src/um/Quota.cc index 179768640d..40c4d6b59e 100644 --- a/src/um/Quota.cc +++ b/src/um/Quota.cc @@ -323,8 +323,8 @@ void Quota::cleanup_quota(const string& qid) VectorAttribute * q; map::iterator q_it; - float limit, limit_tmp; - float usage, usage_tmp; + float usage, usage_tmp, limit_tmp; + bool no_limit; if ( get_quota(qid, &q, q_it) == -1) { @@ -336,8 +336,8 @@ void Quota::cleanup_quota(const string& qid) return; } - limit = 0; - usage = 0; + no_limit = true; + usage = 0; for (int i=0; i < num_metrics; i++) { @@ -348,11 +348,11 @@ void Quota::cleanup_quota(const string& qid) q->vector_value(metrics[i], limit_tmp); q->vector_value(metrics_used.c_str(), usage_tmp); - limit += limit_tmp; + no_limit = (no_limit && (limit_tmp <= 0) ); // Unlimited or default limit usage += usage_tmp; } - if ( limit == 0 && usage == 0 ) + if ( usage == 0 && no_limit ) { delete static_cast(q_it->second); From 3cbcc77b9db29d9e3d927fdbb11b022e10890069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Tue, 27 Nov 2012 12:48:18 +0100 Subject: [PATCH 13/28] Revert "Feature #1611: Add default quota info and update methods to ruby oca" This reverts commit 2fd07e84562d3f7222c16deccaf364a4c5ba4910. --- src/oca/ruby/OpenNebula/GroupPool.rb | 25 +++---------------------- src/oca/ruby/OpenNebula/Pool.rb | 15 --------------- src/oca/ruby/OpenNebula/UserPool.rb | 23 ++--------------------- 3 files changed, 5 insertions(+), 58 deletions(-) diff --git a/src/oca/ruby/OpenNebula/GroupPool.rb b/src/oca/ruby/OpenNebula/GroupPool.rb index 782243e69c..20ebdeed08 100644 --- a/src/oca/ruby/OpenNebula/GroupPool.rb +++ b/src/oca/ruby/OpenNebula/GroupPool.rb @@ -25,9 +25,7 @@ module OpenNebula GROUP_POOL_METHODS = { - :info => "grouppool.info", - :quotainfo => "groupquota.info", - :quotaupdate => "groupquota.update" + :info => "grouppool.info" } ####################################################################### @@ -39,35 +37,18 @@ module OpenNebula super('GROUP_POOL','GROUP',client) end - # Factory method to create Group objects + # Factory method to create User objects def factory(element_xml) OpenNebula::Group.new(element_xml,@client) end ####################################################################### - # XML-RPC Methods for the GroupPool Object + # XML-RPC Methods for the User Object ####################################################################### # Retrieves all the Groups in the pool. def info() super(GROUP_POOL_METHODS[:info]) end - - # Gets the default group quota limits - # - # @return [String, OpenNebula::Error] the default group quota in case - # of success, Error otherwise - def get_quota() - return @client.call(GROUP_POOL_METHODS[:quotainfo]) - end - - # Sets the default group quota limits - # @param quota [String] a template (XML or txt) with the new quota limits - # - # @return [nil, OpenNebula::Error] nil in case of success, Error - # otherwise - def set_quota(quota) - return call(GROUP_POOL_METHODS[:quotaupdate], quota) - end end end diff --git a/src/oca/ruby/OpenNebula/Pool.rb b/src/oca/ruby/OpenNebula/Pool.rb index 3f68d6204e..e917f7bb32 100644 --- a/src/oca/ruby/OpenNebula/Pool.rb +++ b/src/oca/ruby/OpenNebula/Pool.rb @@ -48,21 +48,6 @@ module OpenNebula # Common XML-RPC Methods for all the Pool Types ####################################################################### - # Common client call wrapper. Returns nil instead of the response - # if it is successful - # - # @param [String] xml_method xml-rpc method - # @param [Array] args any arguments for the xml-rpc method - # - # @return [nil, OpenNebula::Error] nil in case of success, Error - # otherwise - def call(xml_method, *args) - rc = @client.call(xml_method, *args) - rc = nil if !OpenNebula.is_error?(rc) - - return rc - end - #Gets the pool without any filter. Host, Group and User Pools # xml_method:: _String_ the name of the XML-RPC method def info(xml_method) diff --git a/src/oca/ruby/OpenNebula/UserPool.rb b/src/oca/ruby/OpenNebula/UserPool.rb index 85cf907b93..b4d9b733a6 100644 --- a/src/oca/ruby/OpenNebula/UserPool.rb +++ b/src/oca/ruby/OpenNebula/UserPool.rb @@ -24,9 +24,7 @@ module OpenNebula ####################################################################### USER_POOL_METHODS = { - :info => "userpool.info", - :quotainfo => "userquota.info", - :quotaupdate => "userquota.update" + :info => "userpool.info" } ####################################################################### @@ -44,29 +42,12 @@ module OpenNebula end ####################################################################### - # XML-RPC Methods for the UserPool Object + # XML-RPC Methods for the User Object ####################################################################### # Retrieves all the Users in the pool. def info() super(USER_POOL_METHODS[:info]) end - - # Gets the default user quota limits - # - # @return [String, OpenNebula::Error] the default user quota in case - # of success, Error otherwise - def get_quota() - return @client.call(USER_POOL_METHODS[:quotainfo]) - end - - # Sets the default user quota limits - # @param quota [String] a template (XML or txt) with the new quota limits - # - # @return [nil, OpenNebula::Error] nil in case of success, Error - # otherwise - def set_quota(quota) - return call(USER_POOL_METHODS[:quotaupdate], quota) - end end end From f3ca471b60c14fec7b3b5d75647cc97929fcc296 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Tue, 27 Nov 2012 15:29:37 +0100 Subject: [PATCH 14/28] Feature #1611: Move default quotas ruby oca methods to new class System --- install.sh | 3 +- src/cli/one_helper/onegroup_helper.rb | 13 ++-- src/cli/one_helper/onequota_helper.rb | 28 +++----- src/cli/one_helper/oneuser_helper.rb | 15 ++-- src/cli/oneuser | 16 +++-- src/oca/ruby/OpenNebula.rb | 1 + src/oca/ruby/OpenNebula/System.rb | 99 +++++++++++++++++++++++++++ 7 files changed, 132 insertions(+), 43 deletions(-) create mode 100644 src/oca/ruby/OpenNebula/System.rb diff --git a/install.sh b/install.sh index 4d16a24a9f..e316e55b94 100755 --- a/install.sh +++ b/install.sh @@ -1108,7 +1108,8 @@ RUBY_OPENNEBULA_LIB_FILES="src/oca/ruby/OpenNebula/Host.rb \ src/oca/ruby/OpenNebula/DatastorePool.rb \ src/oca/ruby/OpenNebula/Cluster.rb \ src/oca/ruby/OpenNebula/ClusterPool.rb \ - src/oca/ruby/OpenNebula/XMLUtils.rb" + src/oca/ruby/OpenNebula/XMLUtils.rb \ + src/oca/ruby/OpenNebula/System.rb" #------------------------------------------------------------------------------- # Common Cloud Files diff --git a/src/cli/one_helper/onegroup_helper.rb b/src/cli/one_helper/onegroup_helper.rb index 3f1fc3e1de..cb1d0c7452 100644 --- a/src/cli/one_helper/onegroup_helper.rb +++ b/src/cli/one_helper/onegroup_helper.rb @@ -48,16 +48,13 @@ class OneGroupHelper < OpenNebulaHelper::OneHelper def format_pool(options) config_file = self.class.table_conf - gpool = GroupPool.new(@client) - default_xml_str = gpool.get_quota() + system = System.new(@client) + default_quotas = system.get_group_quotas() # TODO: check error # if OpenNebula::is_error?(default_xml_str) # end - default_user_quotas = XMLElement.new - default_user_quotas.initialize_xml(default_xml_str, 'DEFAULT_GROUP_QUOTAS') - table = CLIHelper::ShowTable.new(config_file, self) do column :ID, "ONE identifier for the Group", :size=>4 do |d| d["ID"] @@ -80,7 +77,7 @@ class OneGroupHelper < OpenNebulaHelper::OneHelper limit = d['VM_QUOTA']['VM']["VMS"] if limit == "-1" - limit = default_user_quotas['VM_QUOTA/VM/VMS'] + limit = default_quotas['VM_QUOTA/VM/VMS'] limit = "0" if limit.nil? || limit == "" end @@ -95,7 +92,7 @@ class OneGroupHelper < OpenNebulaHelper::OneHelper limit = d['VM_QUOTA']['VM']["MEMORY"] if limit == "-1" - limit = default_user_quotas['VM_QUOTA/VM/MEMORY'] + limit = default_quotas['VM_QUOTA/VM/MEMORY'] limit = "0" if limit.nil? || limit == "" end @@ -112,7 +109,7 @@ class OneGroupHelper < OpenNebulaHelper::OneHelper limit = d['VM_QUOTA']['VM']["CPU"] if limit == "-1" - limit = default_user_quotas['VM_QUOTA/VM/CPU'] + limit = default_quotas['VM_QUOTA/VM/CPU'] limit = "0" if limit.nil? || limit == "" end diff --git a/src/cli/one_helper/onequota_helper.rb b/src/cli/one_helper/onequota_helper.rb index 960e5f2631..ca1d71b1e9 100644 --- a/src/cli/one_helper/onequota_helper.rb +++ b/src/cli/one_helper/onequota_helper.rb @@ -156,13 +156,14 @@ class OneQuotaHelper str end - # Edits the default quota template of a pool - # @param [Pool] resource to get the current info from - # @param [String] root_elem the root element's name - # @param [String] path to the new contents. If nil a editor will be - # used - # @return [String] contents of the new quotas - def self.set_defaultquota(resource, root_elem, path) + # Edits the default quota template of a pool + # + # @param [XMLElement] default_quotas to get the current info from + # @param [String] path to the new contents. If nil a editor will be + # used + # + # @return [String] contents of the new quotas + def self.set_defaultquota(default_quotas, path) str = "" if path.nil? @@ -171,19 +172,6 @@ class OneQuotaHelper tmp = Tempfile.new('one-cli') path = tmp.path - - - default_xml_str = resource.get_quota() - - if OpenNebula::is_error?(default_xml_str) - puts default_xml_str.message - exit -1 - end - - default_quotas = XMLElement.new - default_quotas.initialize_xml(default_xml_str, root_elem) - - tmp << HELP_QUOTA tmp << default_quotas.template_like_str("DATASTORE_QUOTA") << "\n" tmp << default_quotas.template_like_str("VM_QUOTA") << "\n" diff --git a/src/cli/one_helper/oneuser_helper.rb b/src/cli/one_helper/oneuser_helper.rb index 7f879d3bd7..c8e2513ff8 100644 --- a/src/cli/one_helper/oneuser_helper.rb +++ b/src/cli/one_helper/oneuser_helper.rb @@ -147,16 +147,13 @@ class OneUserHelper < OpenNebulaHelper::OneHelper def format_pool(options) config_file = self.class.table_conf - upool = UserPool.new(@client) - default_xml_str = upool.get_quota() + system = System.new(@client) + default_quotas = system.get_user_quotas() # TODO: check error -# if OpenNebula::is_error?(default_xml_str) +# if OpenNebula::is_error?(default_quotas) # end - default_user_quotas = XMLElement.new - default_user_quotas.initialize_xml(default_xml_str, 'DEFAULT_USER_QUOTAS') - table = CLIHelper::ShowTable.new(config_file, self) do column :ID, "ONE identifier for the User", :size=>4 do |d| d["ID"] @@ -179,7 +176,7 @@ class OneUserHelper < OpenNebulaHelper::OneHelper limit = d['VM_QUOTA']['VM']["VMS"] if limit == "-1" - limit = default_user_quotas['VM_QUOTA/VM/VMS'] + limit = default_quotas['VM_QUOTA/VM/VMS'] limit = "0" if limit.nil? || limit == "" end @@ -194,7 +191,7 @@ class OneUserHelper < OpenNebulaHelper::OneHelper limit = d['VM_QUOTA']['VM']["MEMORY"] if limit == "-1" - limit = default_user_quotas['VM_QUOTA/VM/MEMORY'] + limit = default_quotas['VM_QUOTA/VM/MEMORY'] limit = "0" if limit.nil? || limit == "" end @@ -211,7 +208,7 @@ class OneUserHelper < OpenNebulaHelper::OneHelper limit = d['VM_QUOTA']['VM']["CPU"] if limit == "-1" - limit = default_user_quotas['VM_QUOTA/VM/CPU'] + limit = default_quotas['VM_QUOTA/VM/CPU'] limit = "0" if limit.nil? || limit == "" end diff --git a/src/cli/oneuser b/src/cli/oneuser index 840c20eda2..f3746383d8 100755 --- a/src/cli/oneuser +++ b/src/cli/oneuser @@ -248,16 +248,22 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :defaultquota, defaultquota_desc, [:file, nil] do # TODO: Do not create a new Client here -# user_pool = factory_pool() - user_pool = UserPool.new(Client.new) + system = System.new(Client.new) - str = OneQuotaHelper.set_defaultquota(user_pool, 'DEFAULT_USER_QUOTAS', args[1]) + default_quotas = system.get_user_quotas() - rc = user_pool.set_quota(str) + if OpenNebula.is_error?(default_quotas) + puts default_quotas.message + exit(-1) + end + + str = OneQuotaHelper.set_defaultquota(default_quotas, args[1]) + + rc = system.set_user_quotas(str) if OpenNebula.is_error?(rc) puts rc.message - exit -1 + exit(-1) end exit 0 diff --git a/src/oca/ruby/OpenNebula.rb b/src/oca/ruby/OpenNebula.rb index 6dbb239e2b..a2e7f4f8ca 100644 --- a/src/oca/ruby/OpenNebula.rb +++ b/src/oca/ruby/OpenNebula.rb @@ -48,6 +48,7 @@ require 'OpenNebula/Cluster' require 'OpenNebula/ClusterPool' require 'OpenNebula/Document' require 'OpenNebula/DocumentPool' +require 'OpenNebula/System' module OpenNebula diff --git a/src/oca/ruby/OpenNebula/System.rb b/src/oca/ruby/OpenNebula/System.rb new file mode 100644 index 0000000000..d7e0b1aa78 --- /dev/null +++ b/src/oca/ruby/OpenNebula/System.rb @@ -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 System + ####################################################################### + # Constants and Class attribute accessors + ####################################################################### + + SYSTEM_METHODS = { + :userquotainfo => "userquota.info", + :userquotaupdate => "userquota.update", + :groupquotainfo => "groupquota.info", + :groupquotaupdate => "groupquota.update" + } + + ####################################################################### + # Class constructor + ####################################################################### + + # Constructor + # @param [Client] client that represents a XML-RPC connection + def initialize(client) + @client = client + end + + ####################################################################### + # XML-RPC Methods + ####################################################################### + + # Gets the default user quota limits + # + # @return [XMLElement, OpenNebula::Error] the default user quota in case + # of success, Error otherwise + def get_user_quotas() + rc = @client.call(SYSTEM_METHODS[:userquotainfo]) + + if OpenNebula.is_error?(rc) + return rc + end + + default_quotas = XMLElement.new + default_quotas.initialize_xml(rc, 'DEFAULT_USER_QUOTAS') + + return default_quotas + end + + # Sets the default user quota limits + # @param quota [String] a template (XML or txt) with the new quota limits + # + # @return [nil, OpenNebula::Error] nil in case of success, Error + # otherwise + def set_user_quotas(quota) + return @client.call(SYSTEM_METHODS[:userquotaupdate], quota) + end + + # Gets the default group quota limits + # + # @return [XMLElement, OpenNebula::Error] the default group quota in case + # of success, Error otherwise + def get_group_quotas() + rc = @client.call(SYSTEM_METHODS[:groupquotainfo]) + + if OpenNebula.is_error?(rc) + return rc + end + + default_quotas = XMLElement.new + default_quotas.initialize_xml(rc, 'DEFAULT_GROUP_QUOTAS') + + return default_quotas + end + + # Sets the default group quota limits + # @param quota [String] a template (XML or txt) with the new quota limits + # + # @return [nil, OpenNebula::Error] nil in case of success, Error + # otherwise + def set_group_quotas(quota) + return @client.call(SYSTEM_METHODS[:groupquotaupdate], quota) + end + end +end From 7021d81bd0e60dc3599f20c5133ad4b8c141c6da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Tue, 27 Nov 2012 16:00:41 +0100 Subject: [PATCH 15/28] Feature #1611: Merge default quota and individual quota update methods --- src/cli/one_helper/onequota_helper.rb | 53 +-------------------------- src/cli/onegroup | 36 ++++++++++++++++++ src/cli/oneuser | 13 +++++-- 3 files changed, 47 insertions(+), 55 deletions(-) diff --git a/src/cli/one_helper/onequota_helper.rb b/src/cli/one_helper/onequota_helper.rb index ca1d71b1e9..61974bf76c 100644 --- a/src/cli/one_helper/onequota_helper.rb +++ b/src/cli/one_helper/onequota_helper.rb @@ -110,8 +110,8 @@ class OneQuotaHelper EOT # Edits the quota template of a resource - # @param resource [PoolElement] to get the current info from - # @param path [String] path to the new contents. If nil a editor will be + # @param [XMLElement] resource to get the current info from + # @param [String] path to the new contents. If nil a editor will be # used # @return [String] contents of the new quotas def self.set_quota(resource, path) @@ -123,13 +123,6 @@ class OneQuotaHelper tmp = Tempfile.new('one-cli') path = tmp.path - rc = resource.info - - if OpenNebula.is_error?(rc) - puts rc.message - exit -1 - end - tmp << HELP_QUOTA tmp << resource.template_like_str("DATASTORE_QUOTA") << "\n" tmp << resource.template_like_str("VM_QUOTA") << "\n" @@ -156,48 +149,6 @@ class OneQuotaHelper str end - # Edits the default quota template of a pool - # - # @param [XMLElement] default_quotas to get the current info from - # @param [String] path to the new contents. If nil a editor will be - # used - # - # @return [String] contents of the new quotas - def self.set_defaultquota(default_quotas, path) - str = "" - - if path.nil? - require 'tempfile' - - tmp = Tempfile.new('one-cli') - path = tmp.path - - tmp << HELP_QUOTA - tmp << default_quotas.template_like_str("DATASTORE_QUOTA") << "\n" - tmp << default_quotas.template_like_str("VM_QUOTA") << "\n" - tmp << default_quotas.template_like_str("NETWORK_QUOTA") << "\n" - tmp << default_quotas.template_like_str("IMAGE_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 - # Retrieves a clean quota template, without any existing resource # information # @param path [String] path to the new contents. If nil a editor will be diff --git a/src/cli/onegroup b/src/cli/onegroup index bb191bc84d..1b04d31136 100755 --- a/src/cli/onegroup +++ b/src/cli/onegroup @@ -105,6 +105,13 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :quota, quota_desc, :groupid, [:file, nil] do helper.perform_action(args[0], options, "modified") do |group| + rc = group.info + + if OpenNebula.is_error?(rc) + puts rc.message + exit -1 + end + str = OneQuotaHelper.set_quota(group, args[1]) rc = group.set_quota(str) @@ -134,4 +141,33 @@ cmd=CommandParser::CmdParser.new(ARGV) do end end end + + + defaultquota_desc = <<-EOT.unindent + Sets the default quota limits for the groups. If a path is not provided + the editor will be launched to modify the current default quotas. + EOT + + command :defaultquota, defaultquota_desc, [:file, nil] do + # TODO: Do not create a new Client here + system = System.new(Client.new) + + default_quotas = system.get_group_quotas() + + if OpenNebula.is_error?(default_quotas) + puts default_quotas.message + exit(-1) + end + + str = OneQuotaHelper.set_quota(default_quotas, args[1]) + + rc = system.set_group_quotas(str) + + if OpenNebula.is_error?(rc) + puts rc.message + exit(-1) + end + + exit 0 + end end diff --git a/src/cli/oneuser b/src/cli/oneuser index f3746383d8..89cf1185b3 100755 --- a/src/cli/oneuser +++ b/src/cli/oneuser @@ -207,6 +207,13 @@ cmd=CommandParser::CmdParser.new(ARGV) do helper = OneUserHelper.new helper.perform_action(args[0], options, "modified") do |user| + rc = user.info + + if OpenNebula.is_error?(rc) + puts rc.message + exit -1 + end + str = OneQuotaHelper.set_quota(user, args[1]) rc = user.set_quota(str) @@ -214,7 +221,6 @@ cmd=CommandParser::CmdParser.new(ARGV) do puts rc.message exit -1 end - end end @@ -246,7 +252,6 @@ cmd=CommandParser::CmdParser.new(ARGV) do EOT command :defaultquota, defaultquota_desc, [:file, nil] do - # TODO: Do not create a new Client here system = System.new(Client.new) @@ -257,10 +262,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do exit(-1) end - str = OneQuotaHelper.set_defaultquota(default_quotas, args[1]) + str = OneQuotaHelper.set_quota(default_quotas, args[1]) rc = system.set_user_quotas(str) - + if OpenNebula.is_error?(rc) puts rc.message exit(-1) From f4a70487c7a10640726145b9902116770ad33fa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 28 Nov 2012 14:51:52 +0100 Subject: [PATCH 16/28] Feature #1611: Show default quota limits in oneuser/group show --- src/cli/one_helper/onegroup_helper.rb | 14 +- src/cli/one_helper/onequota_helper.rb | 183 +++++++++++++++++--------- src/cli/one_helper/oneuser_helper.rb | 14 +- 3 files changed, 150 insertions(+), 61 deletions(-) diff --git a/src/cli/one_helper/onegroup_helper.rb b/src/cli/one_helper/onegroup_helper.rb index cb1d0c7452..b7252618df 100644 --- a/src/cli/one_helper/onegroup_helper.rb +++ b/src/cli/one_helper/onegroup_helper.rb @@ -158,6 +158,18 @@ class OneGroupHelper < OpenNebulaHelper::OneHelper group_hash = group.to_hash - OneQuotaHelper.format_quota(group_hash['GROUP']) + system = System.new(@client) + default_quotas = system.get_group_quotas() + + if OpenNebula::is_error?(default_quotas) + # TODO: cannot do + # return -1, default_quotas.message + # because OneHelper::show_resource ignores this method's output + + default_quotas = nil + end + + helper = OneQuotaHelper.new + helper.format_quota(group_hash['GROUP'], default_quotas) end end diff --git a/src/cli/one_helper/onequota_helper.rb b/src/cli/one_helper/onequota_helper.rb index 61974bf76c..5925c0d6da 100644 --- a/src/cli/one_helper/onequota_helper.rb +++ b/src/cli/one_helper/onequota_helper.rb @@ -20,60 +20,6 @@ class OneQuotaHelper EDITOR_PATH='/usr/bin/vi' - #--------------------------------------------------------------------------- - # Tables to format user quotas - #--------------------------------------------------------------------------- - TABLE_DS = CLIHelper::ShowTable.new(nil, self) do - column :"DATASTORE ID", "", :size=>12 do |d| - d["ID"] if !d.nil? - end - - column :"IMAGES", "", :right, :size=>20 do |d| - "%8d / %8d" % [d["IMAGES_USED"], d["IMAGES"]] if !d.nil? - end - - column :"SIZE", "", :right, :size=>19 do |d| - "%8s / %8s" % [OpenNebulaHelper.unit_to_str(d["SIZE_USED"].to_i,{},"M"), - OpenNebulaHelper.unit_to_str(d["SIZE"].to_i,{},"M")] if !d.nil? - end - end - - TABLE_NET = CLIHelper::ShowTable.new(nil, self) do - column :"NETWORK ID", "", :size=>12 do |d| - d["ID"] if !d.nil? - end - - column :"LEASES", "", :right, :size=>20 do |d| - "%8d / %8d" % [d["LEASES_USED"], d["LEASES"]] if !d.nil? - end - end - - TABLE_VM = CLIHelper::ShowTable.new(nil, self) do - - column :"NUMBER OF VMS", "", :right, :size=>20 do |d| - "%8d / %8d" % [d["VMS_USED"], d["VMS"]] if !d.nil? - end - - column :"MEMORY", "", :right, :size=>20 do |d| - "%8s / %8s" % [OpenNebulaHelper.unit_to_str(d["MEMORY_USED"].to_i,{},"M"), - OpenNebulaHelper.unit_to_str(d["MEMORY"].to_i,{},"M")] if !d.nil? - end - - column :"CPU", "", :right, :size=>20 do |d| - "%8.2f / %8.2f" % [d["CPU_USED"], d["CPU"]] if !d.nil? - end - end - - TABLE_IMG = CLIHelper::ShowTable.new(nil, self) do - column :"IMAGE ID", "", :size=>12 do |d| - d["ID"] if !d.nil? - end - - column :"RUNNING VMS", "", :right, :size=>20 do |d| - "%8d / %8d" % [d["RVMS_USED"], d["RVMS"]] if !d.nil? - end - end - HELP_QUOTA = <<-EOT.unindent #----------------------------------------------------------------------- # Supported quota limits: @@ -217,8 +163,9 @@ class OneQuotaHelper # Outputs formated quota information to stdout # @param qh [Hash] with the quotas for a given resource + # @param default_quotas_hash [XMLElement] with the default quota limits # - def self.format_quota(qh) + def format_quota(qh, default_quotas) str_h1="%-80s" puts @@ -227,27 +174,145 @@ class OneQuotaHelper puts + @default_quotas = default_quotas + vm_quotas = [qh['VM_QUOTA']['VM']].flatten + if !vm_quotas[0].nil? - TABLE_VM.show(vm_quotas, {}) + CLIHelper::ShowTable.new(nil, self) do + column :"NUMBER OF VMS", "", :right, :size=>20 do |d| + if !d.nil? + elem = 'VMS' + limit = d[elem] + limit = helper.get_default_limit( + limit, "VM_QUOTA/VM/#{elem}") + + "%8d / %8d" % [d["VMS_USED"], limit] + end + end + + column :"MEMORY", "", :right, :size=>20 do |d| + if !d.nil? + elem = 'MEMORY' + limit = d[elem] + limit = helper.get_default_limit( + limit, "VM_QUOTA/VM/#{elem}") + + "%8s / %8s" % [ + OpenNebulaHelper.unit_to_str(d["MEMORY_USED"].to_i,{},"M"), + OpenNebulaHelper.unit_to_str(limit.to_i,{},"M") + ] + end + end + + column :"CPU", "", :right, :size=>20 do |d| + if !d.nil? + elem = 'CPU' + limit = d[elem] + limit = helper.get_default_limit( + limit, "VM_QUOTA/VM/#{elem}") + + "%8.2f / %8.2f" % [d["CPU_USED"], limit] + end + end + end.show(vm_quotas, {}) + puts end ds_quotas = [qh['DATASTORE_QUOTA']['DATASTORE']].flatten + if !ds_quotas[0].nil? - TABLE_DS.show(ds_quotas, {}) + CLIHelper::ShowTable.new(nil, self) do + column :"DATASTORE ID", "", :size=>12 do |d| + d["ID"] if !d.nil? + end + + column :"IMAGES", "", :right, :size=>20 do |d| + if !d.nil? + elem = 'IMAGES' + limit = d[elem] + limit = helper.get_default_limit( + limit, "DATASTORE_QUOTA/DATASTORE[ID=#{d['ID']}]/#{elem}") + + "%8d / %8d" % [d["IMAGES_USED"], limit] + end + end + + column :"SIZE", "", :right, :size=>19 do |d| + if !d.nil? + elem = 'SIZE' + limit = d[elem] + limit = helper.get_default_limit( + limit, "DATASTORE_QUOTA/DATASTORE[ID=#{d['ID']}]/#{elem}") + + "%8s / %8s" % [ + OpenNebulaHelper.unit_to_str(d["SIZE_USED"].to_i,{},"M"), + OpenNebulaHelper.unit_to_str(limit.to_i,{},"M") + ] + end + end + end.show(ds_quotas, {}) + puts end net_quotas = [qh['NETWORK_QUOTA']['NETWORK']].flatten + if !net_quotas[0].nil? - TABLE_NET.show(net_quotas, {}) + CLIHelper::ShowTable.new(nil, self) do + column :"NETWORK ID", "", :size=>12 do |d| + d["ID"] if !d.nil? + end + + column :"LEASES", "", :right, :size=>20 do |d| + if !d.nil? + elem = 'LEASES' + limit = d[elem] + limit = helper.get_default_limit( + limit, "NETWORK_QUOTA/NETWORK[ID=#{d['ID']}]/#{elem}") + + "%8d / %8d" % [d["LEASES_USED"], limit] + end + end + end.show(net_quotas, {}) + puts end image_quotas = [qh['IMAGE_QUOTA']['IMAGE']].flatten + if !image_quotas[0].nil? - TABLE_IMG.show(image_quotas, {}) + CLIHelper::ShowTable.new(nil, self) do + column :"IMAGE ID", "", :size=>12 do |d| + d["ID"] if !d.nil? + end + + column :"RUNNING VMS", "", :right, :size=>20 do |d| + if !d.nil? + elem = 'RVMS' + limit = d[elem] + limit = helper.get_default_limit( + limit, "IMAGE_QUOTA/IMAGE[ID=#{d['ID']}]/RVMS") + + "%8d / %8d" % [d["RVMS_USED"], limit] + end + end + end.show(image_quotas, {}) end end + + def get_default_limit(limit, xpath) + if limit == "-1" + if !@default_quotas.nil? + limit = @default_quotas[xpath] + + limit = "0" if limit.nil? || limit == "" + else + limit = "0" + end + end + + return limit + end end diff --git a/src/cli/one_helper/oneuser_helper.rb b/src/cli/one_helper/oneuser_helper.rb index c8e2513ff8..565674691e 100644 --- a/src/cli/one_helper/oneuser_helper.rb +++ b/src/cli/one_helper/oneuser_helper.rb @@ -265,6 +265,18 @@ class OneUserHelper < OpenNebulaHelper::OneHelper user_hash = user.to_hash - OneQuotaHelper.format_quota(user_hash['USER']) + system = System.new(@client) + default_quotas = system.get_user_quotas() + + if OpenNebula::is_error?(default_quotas) + # TODO: cannot do + # return -1, default_quotas.message + # because OneHelper::show_resource ignores this method's output + + default_quotas = nil + end + + helper = OneQuotaHelper.new + helper.format_quota(user_hash['USER'], default_quotas) end end From 9195c37f598b01f640ab654fd8f487329f978673 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 28 Nov 2012 16:28:41 +0100 Subject: [PATCH 17/28] Feature #1611: Add a simple exception message in the rare cases when a pool can be retrieved, but the def. quotas can't --- src/cli/one_helper/onegroup_helper.rb | 24 ++++++++++-------------- src/cli/one_helper/oneuser_helper.rb | 24 ++++++++++-------------- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/src/cli/one_helper/onegroup_helper.rb b/src/cli/one_helper/onegroup_helper.rb index b7252618df..7bbcaa44dc 100644 --- a/src/cli/one_helper/onegroup_helper.rb +++ b/src/cli/one_helper/onegroup_helper.rb @@ -51,9 +51,9 @@ class OneGroupHelper < OpenNebulaHelper::OneHelper system = System.new(@client) default_quotas = system.get_group_quotas() - # TODO: check error -# if OpenNebula::is_error?(default_xml_str) -# end + if OpenNebula::is_error?(default_quotas) + raise "Error retrieving the default group quotas: #{default_quotas.message}" + end table = CLIHelper::ShowTable.new(config_file, self) do column :ID, "ONE identifier for the Group", :size=>4 do |d| @@ -142,6 +142,13 @@ class OneGroupHelper < OpenNebulaHelper::OneHelper end def format_resource(group) + system = System.new(@client) + default_quotas = system.get_group_quotas() + + if OpenNebula::is_error?(default_quotas) + raise "Error retrieving the default group quotas: #{default_quotas.message}" + end + str="%-15s: %-20s" str_h1="%-80s" @@ -158,17 +165,6 @@ class OneGroupHelper < OpenNebulaHelper::OneHelper group_hash = group.to_hash - system = System.new(@client) - default_quotas = system.get_group_quotas() - - if OpenNebula::is_error?(default_quotas) - # TODO: cannot do - # return -1, default_quotas.message - # because OneHelper::show_resource ignores this method's output - - default_quotas = nil - end - helper = OneQuotaHelper.new helper.format_quota(group_hash['GROUP'], default_quotas) end diff --git a/src/cli/one_helper/oneuser_helper.rb b/src/cli/one_helper/oneuser_helper.rb index 565674691e..8a04444ada 100644 --- a/src/cli/one_helper/oneuser_helper.rb +++ b/src/cli/one_helper/oneuser_helper.rb @@ -150,9 +150,9 @@ class OneUserHelper < OpenNebulaHelper::OneHelper system = System.new(@client) default_quotas = system.get_user_quotas() - # TODO: check error -# if OpenNebula::is_error?(default_quotas) -# end + if OpenNebula::is_error?(default_quotas) + raise "Error retrieving the default user quotas: #{default_quotas.message}" + end table = CLIHelper::ShowTable.new(config_file, self) do column :ID, "ONE identifier for the User", :size=>4 do |d| @@ -245,6 +245,13 @@ class OneUserHelper < OpenNebulaHelper::OneHelper end def format_resource(user) + system = System.new(@client) + default_quotas = system.get_user_quotas() + + if OpenNebula::is_error?(default_quotas) + raise "Error retrieving the default user quotas: #{default_quotas.message}" + end + str="%-15s: %-20s" str_h1="%-80s" @@ -265,17 +272,6 @@ class OneUserHelper < OpenNebulaHelper::OneHelper user_hash = user.to_hash - system = System.new(@client) - default_quotas = system.get_user_quotas() - - if OpenNebula::is_error?(default_quotas) - # TODO: cannot do - # return -1, default_quotas.message - # because OneHelper::show_resource ignores this method's output - - default_quotas = nil - end - helper = OneQuotaHelper.new helper.format_quota(user_hash['USER'], default_quotas) end From d2a88b0376a406d89e343cba3dbb9dbedb8ac556 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 28 Nov 2012 16:42:07 +0100 Subject: [PATCH 18/28] Feature #1611: Use the helper client in oneuser/group defaultquota method --- src/cli/onegroup | 3 +-- src/cli/oneuser | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/cli/onegroup b/src/cli/onegroup index 5426d0e5ef..5052a66484 100755 --- a/src/cli/onegroup +++ b/src/cli/onegroup @@ -153,8 +153,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do EOT command :defaultquota, defaultquota_desc, [:file, nil] do - # TODO: Do not create a new Client here - system = System.new(Client.new) + system = System.new(OneGroupHelper.get_client(options)) default_quotas = system.get_group_quotas() diff --git a/src/cli/oneuser b/src/cli/oneuser index 7b47e99938..59fa39cfef 100755 --- a/src/cli/oneuser +++ b/src/cli/oneuser @@ -249,8 +249,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do EOT command :defaultquota, defaultquota_desc, [:file, nil] do - # TODO: Do not create a new Client here - system = System.new(Client.new) + system = System.new(OneUserHelper.get_client(options)) default_quotas = system.get_user_quotas() From 87f307c1dc60cf5f46466d7220962a7fa5287b51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 28 Nov 2012 18:20:11 +0100 Subject: [PATCH 19/28] Feature #1611: Fix bug in onegroup/user defaultquota, file argument was ignored --- src/cli/onegroup | 2 +- src/cli/oneuser | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cli/onegroup b/src/cli/onegroup index 5052a66484..660bb7cebb 100755 --- a/src/cli/onegroup +++ b/src/cli/onegroup @@ -162,7 +162,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do exit(-1) end - str = OneQuotaHelper.set_quota(default_quotas, args[1]) + str = OneQuotaHelper.set_quota(default_quotas, args[0]) rc = system.set_group_quotas(str) diff --git a/src/cli/oneuser b/src/cli/oneuser index 59fa39cfef..357cfa4887 100755 --- a/src/cli/oneuser +++ b/src/cli/oneuser @@ -258,7 +258,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do exit(-1) end - str = OneQuotaHelper.set_quota(default_quotas, args[1]) + str = OneQuotaHelper.set_quota(default_quotas, args[0]) rc = system.set_user_quotas(str) From 84f5153122ce72f63cd92fcf686a15416151ebc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 28 Nov 2012 18:20:44 +0100 Subject: [PATCH 20/28] Feature #1611: Fix bug in Quota::cleanup_quota. A limit of 0 now is not the default one, which means that the limit was set manually and should not be automatically cleaned --- src/um/Quota.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/um/Quota.cc b/src/um/Quota.cc index 40c4d6b59e..74a2b95739 100644 --- a/src/um/Quota.cc +++ b/src/um/Quota.cc @@ -324,7 +324,7 @@ void Quota::cleanup_quota(const string& qid) map::iterator q_it; float usage, usage_tmp, limit_tmp; - bool no_limit; + bool default_limit; if ( get_quota(qid, &q, q_it) == -1) { @@ -336,8 +336,8 @@ void Quota::cleanup_quota(const string& qid) return; } - no_limit = true; - usage = 0; + default_limit = true; + usage = 0; for (int i=0; i < num_metrics; i++) { @@ -348,11 +348,11 @@ void Quota::cleanup_quota(const string& qid) q->vector_value(metrics[i], limit_tmp); q->vector_value(metrics_used.c_str(), usage_tmp); - no_limit = (no_limit && (limit_tmp <= 0) ); // Unlimited or default limit + default_limit = (default_limit && (limit_tmp == -1) ); usage += usage_tmp; } - if ( usage == 0 && no_limit ) + if ( usage == 0 && default_limit ) { delete static_cast(q_it->second); From fba200499bc924c7530418f58877994178c850cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Thu, 29 Nov 2012 12:49:29 +0100 Subject: [PATCH 21/28] Feature #1611: Instead of changing negative limits to 0, report the error --- include/Quota.h | 2 +- src/um/Quota.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/Quota.h b/include/Quota.h index 756fe903d1..bf00b18049 100644 --- a/include/Quota.h +++ b/include/Quota.h @@ -182,7 +182,7 @@ private: * @param va limits for the new quota if 0 limits will be 0 * @param default_allowed whether or not the limit -1 is allowed * - * @return a new attribute representing the quota + * @return a new attribute representing the quota, 0 on error */ VectorAttribute * new_quota(VectorAttribute* va, bool default_allowed); diff --git a/src/um/Quota.cc b/src/um/Quota.cc index 74a2b95739..5ba6d55d83 100644 --- a/src/um/Quota.cc +++ b/src/um/Quota.cc @@ -413,7 +413,7 @@ VectorAttribute * Quota::new_quota(VectorAttribute * va, bool default_allowed) if ( (default_allowed && limit_i < -1) || (!default_allowed && limit_i < 0) ) { - limit = "0"; + return 0; } limits.insert(make_pair(metrics[i], limit)); From 9d3fb4297091b9e832ac71f1a7cb893979a1e2c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Thu, 29 Nov 2012 17:26:21 +0100 Subject: [PATCH 22/28] Feature #1611: Better handling of default vs non-default quotas --- include/DefaultQuotas.h | 19 ++++++------------- include/Quota.h | 28 ++++++++++++++++++++-------- include/Quotas.h | 25 ++++++------------------- src/um/Quota.cc | 32 ++++++++++++++++++++------------ src/um/Quotas.cc | 10 +++++----- 5 files changed, 57 insertions(+), 57 deletions(-) diff --git a/include/DefaultQuotas.h b/include/DefaultQuotas.h index 22a81b875e..e41658c1c9 100644 --- a/include/DefaultQuotas.h +++ b/include/DefaultQuotas.h @@ -32,22 +32,15 @@ public: const char * _vm_xpath): Quotas(_ds_xpath, _net_xpath, _img_xpath, _vm_xpath), root_elem(_root_elem) - {}; + { + datastore_quota.make_default(); + network_quota.make_default(); + image_quota.make_default(); + vm_quota.make_default(); + }; ~DefaultQuotas(){}; - /** - * Set the quotas - * @param tmpl contains the user quota limits - * @param error describes error when setting the quotas - * - * @return 0 on success, -1 otherwise - */ - int set(Template *tmpl, string& error) - { - return Quotas::set(tmpl, false, error); - }; - /** * Generates a string representation of the quotas in XML format * @param xml the string to store the XML diff --git a/include/Quota.h b/include/Quota.h index bf00b18049..1f6106c798 100644 --- a/include/Quota.h +++ b/include/Quota.h @@ -35,12 +35,11 @@ public: /** * Set the quotas. If the quota previously exists its limit is updated. * @param quota_str the quota template in ASCII or XML formats - * @param default_allowed whether or not the limit -1 is allowed * @param error describe the error in case of error * * @return 0 on success -1 otherwise */ - int set(vector * quotas, bool default_allowed, string& error); + int set(vector * quotas, string& error); /** * Check if the resource allocation will exceed the quota limits. If not @@ -79,6 +78,15 @@ public: return get_quota(id, va, it); } + /** + * Sets this Quota as a Default Quota. Default quotas do not have usage, + * and can't have a limit of -1 + */ + void make_default() + { + is_default = true; + }; + protected: Quota(const char * quota_name, @@ -88,7 +96,8 @@ protected: : Template(false, '=', quota_name), template_name(_template_name), metrics(_metrics), - num_metrics(_num_metrics){}; + num_metrics(_num_metrics), + is_default(false){}; virtual ~Quota(){}; @@ -175,16 +184,21 @@ protected: */ void cleanup_quota(const string& qid); + /** + * Whether or not this is a default quota. Default quotas do not have usage, + * and can't have a limit of -1 + */ + bool is_default; + private: /** * Creates an empty quota based on the given attribute. The attribute va * contains the limits for the quota. * @param va limits for the new quota if 0 limits will be 0 - * @param default_allowed whether or not the limit -1 is allowed * * @return a new attribute representing the quota, 0 on error */ - VectorAttribute * new_quota(VectorAttribute* va, bool default_allowed); + VectorAttribute * new_quota(VectorAttribute* va); /** * Adds a new quota, it also updates an internal index for fast accessing @@ -208,13 +222,11 @@ private: * Sets new limit values for the quota * @param quota to be updated * @param va attribute with the new limits - * @param default_allowed whether or not the limit -1 is allowed * * @return 0 on success or -1 if wrong limits */ int update_limits(VectorAttribute* quota, - const VectorAttribute* va, - bool default_allowed); + const VectorAttribute* va); /** * Extract the limits for the defined quota metrics from a given attribute diff --git a/include/Quotas.h b/include/Quotas.h index 5e2344171f..810629302f 100644 --- a/include/Quotas.h +++ b/include/Quotas.h @@ -30,11 +30,11 @@ public: Quotas(const char * _ds_xpath, const char * _net_xpath, const char * _img_xpath, - const char * _vm_xpath): - ds_xpath(_ds_xpath), - net_xpath(_net_xpath), - img_xpath(_img_xpath), - vm_xpath(_vm_xpath) + const char * _vm_xpath): + ds_xpath(_ds_xpath), + net_xpath(_net_xpath), + img_xpath(_img_xpath), + vm_xpath(_vm_xpath) {}; ~Quotas(){}; @@ -57,10 +57,7 @@ public: * * @return 0 on success, -1 otherwise */ - virtual int set(Template *tmpl, string& error) - { - return set(tmpl, true, error); - }; + int set(Template *tmpl, string& error); /** * Delete usage from quota counters. @@ -203,16 +200,6 @@ public: static void quota_del(QuotaType type, int uid, int gid, Template * tmpl); protected: - /** - * Set the quotas - * @param tmpl contains the user quota limits - * @param default_allowed whether or not the limit -1 is allowed - * @param error describes error when setting the quotas - * - * @return 0 on success, -1 otherwise - */ - int set(Template *tmpl, bool default_allowed, string& error); - //-------------------------------------------------------------------------- // Usage Counters and Quotas //-------------------------------------------------------------------------- diff --git a/src/um/Quota.cc b/src/um/Quota.cc index 5ba6d55d83..bd36507a39 100644 --- a/src/um/Quota.cc +++ b/src/um/Quota.cc @@ -80,7 +80,7 @@ void Quota::add_to_quota(VectorAttribute * attr, const string& va_name, float nu /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int Quota::set(vector * new_quotas, bool default_allowed, string& error) +int Quota::set(vector * new_quotas, string& error) { vector::iterator it; @@ -108,7 +108,7 @@ int Quota::set(vector * new_quotas, bool default_allowed, string& er { VectorAttribute * nq; - if ((nq = new_quota(iq, default_allowed)) == 0) + if ((nq = new_quota(iq)) == 0) { goto error_limits; } @@ -117,7 +117,7 @@ int Quota::set(vector * new_quotas, bool default_allowed, string& er } else { - if (update_limits(tq, iq, default_allowed) != 0) + if (update_limits(tq, iq) != 0) { goto error_limits; } @@ -323,7 +323,7 @@ void Quota::cleanup_quota(const string& qid) VectorAttribute * q; map::iterator q_it; - float usage, usage_tmp, limit_tmp; + float usage, usage_tmp, limit_tmp, implicit_limit; bool default_limit; if ( get_quota(qid, &q, q_it) == -1) @@ -336,6 +336,15 @@ void Quota::cleanup_quota(const string& qid) return; } + if ( is_default ) + { + implicit_limit = 0; + } + else + { + implicit_limit = -1; + } + default_limit = true; usage = 0; @@ -348,7 +357,7 @@ void Quota::cleanup_quota(const string& qid) q->vector_value(metrics[i], limit_tmp); q->vector_value(metrics_used.c_str(), usage_tmp); - default_limit = (default_limit && (limit_tmp == -1) ); + default_limit = (default_limit && limit_tmp == implicit_limit ); usage += usage_tmp; } @@ -365,8 +374,7 @@ void Quota::cleanup_quota(const string& qid) int Quota::update_limits( VectorAttribute * quota, - const VectorAttribute * va, - bool default_allowed) + const VectorAttribute * va) { string limit; float limit_i; @@ -376,9 +384,9 @@ int Quota::update_limits( limit = va->vector_value_str(metrics[i], limit_i); //No quota, NaN or negative - if ((default_allowed && + if ((!is_default && ( limit_i < -1 || ( limit_i == -1 && limit == "" ))) || - (!default_allowed && limit_i < 0) ) + (is_default && limit_i < 0) ) { return -1; } @@ -394,7 +402,7 @@ int Quota::update_limits( /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -VectorAttribute * Quota::new_quota(VectorAttribute * va, bool default_allowed) +VectorAttribute * Quota::new_quota(VectorAttribute * va) { map limits; @@ -410,8 +418,8 @@ VectorAttribute * Quota::new_quota(VectorAttribute * va, bool default_allowed) limit = va->vector_value_str(metrics[i], limit_i); //No quota, NaN or negative - if ( (default_allowed && limit_i < -1) || - (!default_allowed && limit_i < 0) ) + if ( (!is_default && limit_i < -1) || + (is_default && limit_i < 0) ) { return 0; } diff --git a/src/um/Quotas.cc b/src/um/Quotas.cc index 58124cb33f..325c419e99 100644 --- a/src/um/Quotas.cc +++ b/src/um/Quotas.cc @@ -19,13 +19,13 @@ #include "ObjectXML.h" -int Quotas::set(Template *tmpl, bool default_allowed, string& error) +int Quotas::set(Template *tmpl, string& error) { vector vquotas; if ( tmpl->get(datastore_quota.get_quota_name(), vquotas) > 0 ) { - if ( datastore_quota.set(&vquotas, default_allowed, error) != 0 ) + if ( datastore_quota.set(&vquotas, error) != 0 ) { return -1; } @@ -35,7 +35,7 @@ int Quotas::set(Template *tmpl, bool default_allowed, string& error) if ( tmpl->get(network_quota.get_quota_name(), vquotas) > 0 ) { - if ( network_quota.set(&vquotas, default_allowed, error) != 0 ) + if ( network_quota.set(&vquotas, error) != 0 ) { return -1; } @@ -45,7 +45,7 @@ int Quotas::set(Template *tmpl, bool default_allowed, string& error) if ( tmpl->get(image_quota.get_quota_name(), vquotas) > 0 ) { - if ( image_quota.set(&vquotas, default_allowed, error) != 0 ) + if ( image_quota.set(&vquotas, error) != 0 ) { return -1; } @@ -55,7 +55,7 @@ int Quotas::set(Template *tmpl, bool default_allowed, string& error) if ( tmpl->get(vm_quota.get_quota_name(), vquotas) > 0 ) { - if ( vm_quota.set(&vquotas, default_allowed, error) != 0 ) + if ( vm_quota.set(&vquotas, error) != 0 ) { return -1; } From d680315fce7b3ee4b611f2ea1c548a4a977ed535 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Thu, 29 Nov 2012 17:36:10 +0100 Subject: [PATCH 23/28] Feature #1611: Clean DefaultQuotas to/from xml methods --- src/um/DefaultQuotas.cc | 52 +++-------------------------------------- 1 file changed, 3 insertions(+), 49 deletions(-) diff --git a/src/um/DefaultQuotas.cc b/src/um/DefaultQuotas.cc index 139e53e9aa..82d96f016f 100644 --- a/src/um/DefaultQuotas.cc +++ b/src/um/DefaultQuotas.cc @@ -30,16 +30,10 @@ string& DefaultQuotas::to_xml(string& xml) const { ostringstream oss; - string ds_quota_xml; - string net_quota_xml; - string vm_quota_xml; - string image_quota_xml; + string aux_xml; oss << "<" << root_elem << ">" - << datastore_quota.to_xml(ds_quota_xml) - << network_quota.to_xml(net_quota_xml) - << vm_quota.to_xml(vm_quota_xml) - << image_quota.to_xml(image_quota_xml) + << Quotas::to_xml(aux_xml) << ""; xml = oss.str(); @@ -52,49 +46,9 @@ string& DefaultQuotas::to_xml(string& xml) const int DefaultQuotas::from_xml(const string& xml) { - vector content; - int rc = 0; - ObjectXML *object_xml = new ObjectXML(xml); - object_xml->get_nodes(ds_xpath, content); - - if (!content.empty()) - { - rc += datastore_quota.from_xml_node(content[0]); - } - - object_xml->free_nodes(content); - content.clear(); - - object_xml->get_nodes(net_xpath, content); - - if (!content.empty()) - { - rc += network_quota.from_xml_node(content[0]); - } - - object_xml->free_nodes(content); - content.clear(); - - object_xml->get_nodes(vm_xpath, content); - - if (!content.empty()) - { - rc += vm_quota.from_xml_node(content[0]); - } - - object_xml->free_nodes(content); - content.clear(); - - object_xml->get_nodes(img_xpath, content); - - if (!content.empty()) - { - rc += image_quota.from_xml_node(content[0]); - } - - object_xml->free_nodes(content); + int rc = Quotas::from_xml(object_xml); delete object_xml; From 0aa1b5577f68a9bb058d58ba1bd1fefa12a8356a Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 29 Nov 2012 23:28:49 +0100 Subject: [PATCH 24/28] feature-1611: Validate DefaultQuota XML and remove unused label --- src/um/DefaultQuotas.cc | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/um/DefaultQuotas.cc b/src/um/DefaultQuotas.cc index 82d96f016f..993f999108 100644 --- a/src/um/DefaultQuotas.cc +++ b/src/um/DefaultQuotas.cc @@ -99,12 +99,12 @@ int DefaultQuotas::insert_replace(SqlDB *db, bool replace, string& error_str) { goto error_body; } -/* - if ( validate_xml(sql_xml) != 0 ) + + if ( ObjectXML::validate_xml(sql_xml) != 0 ) { goto error_xml; } -*/ + if ( replace ) { oss << "REPLACE"; @@ -125,20 +125,15 @@ int DefaultQuotas::insert_replace(SqlDB *db, bool replace, string& error_str) db->free_str(sql_xml); return rc; -/* + error_xml: db->free_str(sql_xml); error_str = "Error transforming the Quotas to XML."; + return -1; - goto error_common; -*/ error_body: - goto error_generic; - -error_generic: error_str = "Error inserting Quotas in DB."; -error_common: return -1; } From 2d25550e6feebd61a03e42f98871ede3694bb6be Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 29 Nov 2012 23:29:28 +0100 Subject: [PATCH 25/28] feature #1617: Add default to disk_attribute --- src/image/Image.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/image/Image.cc b/src/image/Image.cc index 4ab57a7cde..72f355d660 100644 --- a/src/image/Image.cc +++ b/src/image/Image.cc @@ -545,6 +545,9 @@ int Image::disk_attribute( VectorAttribute * disk, disk_attr_type = "CDROM"; disk->replace("READONLY","YES"); break; + + default: //Other file types should not be never a DISK + break; } disk->replace("TYPE",disk_attr_type); From a7acfca25ecba3fdea270087abbe0d1571e71a92 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 29 Nov 2012 23:53:34 +0100 Subject: [PATCH 26/28] feature #1611: Safer virtual destructor for Quotas --- include/Quotas.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/include/Quotas.h b/include/Quotas.h index 810629302f..2280362744 100644 --- a/include/Quotas.h +++ b/include/Quotas.h @@ -37,7 +37,7 @@ public: vm_xpath(_vm_xpath) {}; - ~Quotas(){}; + virtual ~Quotas(){}; /** * Different quota types @@ -160,7 +160,7 @@ public: /** * Builds quota object from an ObjectXML - * @param object_xml pointer to the ObjectXML + * @param object_xml pointer to the ObjectXML * @return 0 if success */ int from_xml(ObjectXML * object_xml); @@ -201,27 +201,27 @@ public: protected: //-------------------------------------------------------------------------- - // Usage Counters and Quotas + // Usage Counters and Quotas //-------------------------------------------------------------------------- /** - * Datastore Quotas - */ + * Datastore Quotas + */ QuotaDatastore datastore_quota; /** - * Network Quotas + * Network Quotas */ QuotaNetwork network_quota; /** - * Image Quotas - */ + * Image Quotas + */ QuotaImage image_quota; /** - * Virtual Machine Quotas - */ + * Virtual Machine Quotas + */ QuotaVirtualMachine vm_quota; //-------------------------------------------------------------------------- From e4a601dde8365ee1bdf81533f242436f7b8f5d10 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 30 Nov 2012 00:16:52 +0100 Subject: [PATCH 27/28] feature #1611: Default attribute for quotas can only be set at initialization --- include/DefaultQuotas.h | 16 ++++++------ include/Quota.h | 48 +++++++++++++++-------------------- include/QuotaDatastore.h | 16 +++++++----- include/QuotaImage.h | 16 +++++++----- include/QuotaNetwork.h | 16 +++++++----- include/QuotaVirtualMachine.h | 22 +++++++++------- include/Quotas.h | 32 ++++++++++++++++++++--- src/um/DefaultQuotas.cc | 13 +++------- src/um/QuotaImage.cc | 9 ++++--- src/um/QuotaNetwork.cc | 2 +- src/um/QuotaVirtualMachine.cc | 9 ++++--- 11 files changed, 112 insertions(+), 87 deletions(-) diff --git a/include/DefaultQuotas.h b/include/DefaultQuotas.h index e41658c1c9..dafdba97ce 100644 --- a/include/DefaultQuotas.h +++ b/include/DefaultQuotas.h @@ -30,14 +30,9 @@ public: const char * _net_xpath, const char * _img_xpath, const char * _vm_xpath): - Quotas(_ds_xpath, _net_xpath, _img_xpath, _vm_xpath), + Quotas(_ds_xpath, _net_xpath, _img_xpath, _vm_xpath, true), root_elem(_root_elem) - { - datastore_quota.make_default(); - network_quota.make_default(); - image_quota.make_default(); - vm_quota.make_default(); - }; + {}; ~DefaultQuotas(){}; @@ -80,7 +75,12 @@ public: * Bootstraps the database table(s) associated to the default quotas * @return 0 on success */ - static int bootstrap(SqlDB * db); + static int bootstrap(SqlDB * db) + { + ostringstream oss(DefaultQuotas::db_bootstrap); + + return db->exec(oss); + } private: const char * root_elem; diff --git a/include/Quota.h b/include/Quota.h index 1f6106c798..ac7a74b1cf 100644 --- a/include/Quota.h +++ b/include/Quota.h @@ -24,14 +24,14 @@ class Quotas; /** * Base class for resource quotas, it provides basic storage and management of - * the quotas. Each resource MUST inherit from it to implement check and + * the quotas. Each resource MUST inherit from it to implement check and * update methods. Quotas are stored in a template form, each class store the * limits and usage in a resource specific format. */ class Quota: public Template { public: - + /** * Set the quotas. If the quota previously exists its limit is updated. * @param quota_str the quota template in ASCII or XML formats @@ -42,11 +42,11 @@ public: int set(vector * quotas, string& error); /** - * Check if the resource allocation will exceed the quota limits. If not + * Check if the resource allocation will exceed the quota limits. If not * the usage counters are updated * @param tmpl template for the resource * @param default_quotas Quotas that contain the default limits - * @param error string + * @param error string * @return true if the operation can be performed */ virtual bool check(Template* tmpl, Quotas& default_quotas, string& error) = 0; @@ -78,31 +78,23 @@ public: return get_quota(id, va, it); } - /** - * Sets this Quota as a Default Quota. Default quotas do not have usage, - * and can't have a limit of -1 - */ - void make_default() - { - is_default = true; - }; - protected: Quota(const char * quota_name, const char * _template_name, const char ** _metrics, - int _num_metrics) + int _num_metrics, + bool _is_default) : Template(false, '=', quota_name), template_name(_template_name), metrics(_metrics), num_metrics(_num_metrics), - is_default(false){}; - + is_default(_is_default){}; + virtual ~Quota(){}; /** - * Generic Quota Names + * Generic Quota Names * * template_name = [ * ID = "ID to identify the resource", @@ -128,8 +120,14 @@ protected: */ int num_metrics; - /** - * Check a given quota for an usage request and update counters if the + /** + * Whether or not this is a default quota. Default quotas do not have usage, + * and can't have a limit of -1 + */ + bool is_default; + + /** + * Check a given quota for an usage request and update counters if the * request does not exceed quota limits * @param qid id that identifies the quota, to be used by get_quota * @param usage_req usage for each metric @@ -137,7 +135,7 @@ protected: * @param error string describing the error * @return true if the request does not exceed current limits */ - bool check_quota(const string& qid, + bool check_quota(const string& qid, map& usage_req, Quotas& default_quotas, string& error); @@ -147,7 +145,7 @@ protected: * @param qid id that identifies the quota, to be used by get_quota * @param usage_req usage for each metric */ - void del_quota(const string& qid, + void del_quota(const string& qid, map& usage_req); /** @@ -184,12 +182,6 @@ protected: */ void cleanup_quota(const string& qid); - /** - * Whether or not this is a default quota. Default quotas do not have usage, - * and can't have a limit of -1 - */ - bool is_default; - private: /** * Creates an empty quota based on the given attribute. The attribute va @@ -218,7 +210,7 @@ private: */ void add_to_quota(VectorAttribute * attr, const string& va_name, float num); - /** + /** * Sets new limit values for the quota * @param quota to be updated * @param va attribute with the new limits diff --git a/include/QuotaDatastore.h b/include/QuotaDatastore.h index fcd5a7f7ab..b37fe53c68 100644 --- a/include/QuotaDatastore.h +++ b/include/QuotaDatastore.h @@ -35,21 +35,23 @@ class QuotaDatastore : public Quota { public: - - QuotaDatastore():Quota("DATASTORE_QUOTA", - "DATASTORE", - DS_METRICS, - NUM_DS_METRICS) + + QuotaDatastore(bool is_default): + Quota("DATASTORE_QUOTA", + "DATASTORE", + DS_METRICS, + NUM_DS_METRICS, + is_default) {}; ~QuotaDatastore(){}; /** - * Check if the resource allocation will exceed the quota limits. If not + * Check if the resource allocation will exceed the quota limits. If not * the usage counters are updated * @param tmpl template for the resource * @param default_quotas Quotas that contain the default limits - * @param error string + * @param error string * @return true if the operation can be performed */ bool check(Template* tmpl, Quotas& default_quotas, string& error); diff --git a/include/QuotaImage.h b/include/QuotaImage.h index 39b87ce6ac..57a8c8e759 100644 --- a/include/QuotaImage.h +++ b/include/QuotaImage.h @@ -33,21 +33,23 @@ class QuotaImage : public Quota { public: - - QuotaImage():Quota("IMAGE_QUOTA", - "IMAGE", - IMAGE_METRICS, - NUM_IMAGE_METRICS) + + QuotaImage(bool is_default): + Quota("IMAGE_QUOTA", + "IMAGE", + IMAGE_METRICS, + NUM_IMAGE_METRICS, + is_default) {}; ~QuotaImage(){}; /** - * Check if the resource allocation will exceed the quota limits. If not + * Check if the resource allocation will exceed the quota limits. If not * the usage counters are updated * @param tmpl template for the resource * @param default_quotas Quotas that contain the default limits - * @param error string + * @param error string * @return true if the operation can be performed */ bool check(Template* tmpl, Quotas& default_quotas, string& error); diff --git a/include/QuotaNetwork.h b/include/QuotaNetwork.h index afa92e5874..2bb4d6d6d1 100644 --- a/include/QuotaNetwork.h +++ b/include/QuotaNetwork.h @@ -33,21 +33,23 @@ class QuotaNetwork : public Quota { public: - - QuotaNetwork():Quota("NETWORK_QUOTA", - "NETWORK", - NET_METRICS, - NUM_NET_METRICS) + + QuotaNetwork(bool is_default): + Quota("NETWORK_QUOTA", + "NETWORK", + NET_METRICS, + NUM_NET_METRICS, + is_default) {}; ~QuotaNetwork(){}; /** - * Check if the resource allocation will exceed the quota limits. If not + * Check if the resource allocation will exceed the quota limits. If not * the usage counters are updated * @param tmpl template for the resource * @param default_quotas Quotas that contain the default limits - * @param error string + * @param error string * @return true if the operation can be performed */ bool check(Template* tmpl, Quotas& default_quotas, string& error); diff --git a/include/QuotaVirtualMachine.h b/include/QuotaVirtualMachine.h index fa085c0421..3d33eb9f10 100644 --- a/include/QuotaVirtualMachine.h +++ b/include/QuotaVirtualMachine.h @@ -36,21 +36,23 @@ class QuotaVirtualMachine : public Quota { public: - - QuotaVirtualMachine():Quota("VM_QUOTA", - "VM", - VM_METRICS, - NUM_VM_METRICS) + + QuotaVirtualMachine(bool is_default): + Quota("VM_QUOTA", + "VM", + VM_METRICS, + NUM_VM_METRICS, + is_default) {}; ~QuotaVirtualMachine(){}; /** - * Check if the resource allocation will exceed the quota limits. If not + * Check if the resource allocation will exceed the quota limits. If not * the usage counters are updated * @param tmpl template for the resource * @param default_quotas Quotas that contain the default limits - * @param error string + * @param error string * @return true if the operation can be performed */ bool check(Template* tmpl, Quotas& default_quotas, string& error); @@ -72,7 +74,6 @@ public: protected: - /** * Gets a quota, overrides base to not to use ID. * @@ -100,7 +101,10 @@ protected: * * @return 0 on success, -1 if not found */ - int get_default_quota(const string& id, Quotas& default_quotas, VectorAttribute **va); + int get_default_quota( + const string& id, + Quotas& default_quotas, + VectorAttribute **va); static const char * VM_METRICS[]; diff --git a/include/Quotas.h b/include/Quotas.h index 2280362744..fb6392540f 100644 --- a/include/Quotas.h +++ b/include/Quotas.h @@ -31,10 +31,14 @@ public: const char * _net_xpath, const char * _img_xpath, const char * _vm_xpath): - ds_xpath(_ds_xpath), - net_xpath(_net_xpath), - img_xpath(_img_xpath), - vm_xpath(_vm_xpath) + datastore_quota(false), + network_quota(false), + image_quota(false), + vm_quota(false), + ds_xpath(_ds_xpath), + net_xpath(_net_xpath), + img_xpath(_img_xpath), + vm_xpath(_vm_xpath) {}; virtual ~Quotas(){}; @@ -200,6 +204,26 @@ public: static void quota_del(QuotaType type, int uid, int gid, Template * tmpl); protected: + /** + * This is an specialized constructor only for derived Quotas classes. + * It allows to set the defaultness attribute + */ + Quotas(const char * _ds_xpath, + const char * _net_xpath, + const char * _img_xpath, + const char * _vm_xpath, + bool is_deafult): + datastore_quota(is_deafult), + network_quota(is_deafult), + image_quota(is_deafult), + vm_quota(is_deafult), + ds_xpath(_ds_xpath), + net_xpath(_net_xpath), + img_xpath(_img_xpath), + vm_xpath(_vm_xpath) + {}; + +private: //-------------------------------------------------------------------------- // Usage Counters and Quotas //-------------------------------------------------------------------------- diff --git a/src/um/DefaultQuotas.cc b/src/um/DefaultQuotas.cc index 993f999108..e88e6a83eb 100644 --- a/src/um/DefaultQuotas.cc +++ b/src/um/DefaultQuotas.cc @@ -23,6 +23,9 @@ const char * DefaultQuotas::table = "system_attributes"; const char * DefaultQuotas::db_names = "name, body"; +const char * DefaultQuotas::db_bootstrap = "CREATE TABLE IF NOT EXISTS" + " system_attributes (name VARCHAR(128) PRIMARY KEY, body TEXT)"; + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -139,13 +142,3 @@ error_body: /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ - -int DefaultQuotas::bootstrap(SqlDB * db) -{ - ostringstream oss; - - oss << "CREATE TABLE IF NOT EXISTS system_attributes (" - "name VARCHAR(128) PRIMARY KEY, body TEXT)"; - - return db->exec(oss); -} diff --git a/src/um/QuotaImage.cc b/src/um/QuotaImage.cc index 0b6774524f..dd3f1e563a 100644 --- a/src/um/QuotaImage.cc +++ b/src/um/QuotaImage.cc @@ -51,7 +51,7 @@ bool QuotaImage::check(Template * tmpl, Quotas& default_quotas, string& error) } image_id = disk->vector_value("IMAGE_ID"); - + if ( !image_id.empty() ) { if ( !check_quota(image_id, image_request, default_quotas, error) ) @@ -92,7 +92,7 @@ void QuotaImage::del(Template * tmpl) } image_id = disk->vector_value("IMAGE_ID"); - + del_quota(image_id, image_request); } } @@ -100,7 +100,10 @@ void QuotaImage::del(Template * tmpl) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int QuotaImage::get_default_quota(const string& id, Quotas& default_quotas, VectorAttribute **va) +int QuotaImage::get_default_quota( + const string& id, + Quotas& default_quotas, + VectorAttribute **va) { return default_quotas.image_get(id, va); } diff --git a/src/um/QuotaNetwork.cc b/src/um/QuotaNetwork.cc index 97dd50b1b1..b1ca09b45d 100644 --- a/src/um/QuotaNetwork.cc +++ b/src/um/QuotaNetwork.cc @@ -92,7 +92,7 @@ void QuotaNetwork::del(Template * tmpl) } net_id = nic->vector_value("NETWORK_ID"); - + del_quota(net_id, net_request); } } diff --git a/src/um/QuotaVirtualMachine.cc b/src/um/QuotaVirtualMachine.cc index 14c0335aee..34378c41dc 100644 --- a/src/um/QuotaVirtualMachine.cc +++ b/src/um/QuotaVirtualMachine.cc @@ -73,7 +73,7 @@ bool QuotaVirtualMachine::check(Template * tmpl, vm_request.insert(make_pair("VMS",1)); vm_request.insert(make_pair("MEMORY", memory)); vm_request.insert(make_pair("CPU", cpu)); - + return check_quota("", vm_request, default_quotas, error); } @@ -100,14 +100,17 @@ void QuotaVirtualMachine::del(Template * tmpl) vm_request.insert(make_pair("VMS",1)); vm_request.insert(make_pair("MEMORY", memory)); vm_request.insert(make_pair("CPU", cpu)); - + del_quota("", vm_request); } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int QuotaVirtualMachine::get_default_quota(const string& id, Quotas& default_quotas, VectorAttribute **va) +int QuotaVirtualMachine::get_default_quota( + const string& id, + Quotas& default_quotas, + VectorAttribute **va) { return default_quotas.vm_get(id, va); } From d6091c439ea56debed5895cb3453e24f13ef96f1 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 30 Nov 2012 02:59:26 +0100 Subject: [PATCH 28/28] feature #1611: Abstracts system properties and system DB management --- include/DefaultQuotas.h | 75 +-------- include/Nebula.h | 100 +++++++---- include/SystemDB.h | 147 +++++++++++++++++ src/nebula/Nebula.cc | 355 ++++++++++++++++++++++++++++------------ src/nebula/SConstruct | 1 + src/nebula/SystemDB.cc | 252 ++++++++++++++++++++++++++++ src/um/DefaultQuotas.cc | 92 +++-------- src/um/Quota.cc | 49 +++--- 8 files changed, 762 insertions(+), 309 deletions(-) create mode 100644 include/SystemDB.h create mode 100644 src/nebula/SystemDB.cc diff --git a/include/DefaultQuotas.h b/include/DefaultQuotas.h index dafdba97ce..9a90f4e239 100644 --- a/include/DefaultQuotas.h +++ b/include/DefaultQuotas.h @@ -21,7 +21,7 @@ #include "SqlDB.h" #include "ObjectSQL.h" -class DefaultQuotas : public Quotas, ObjectSQL +class DefaultQuotas : public Quotas { public: DefaultQuotas( @@ -48,95 +48,34 @@ public: * @param db pointer to the db * @return 0 on success */ - int insert(SqlDB *db, string& error_str) - { - return insert_replace(db, false, error_str); - }; + int insert(); /** * Writes/updates the quotas data fields in the database. * @param db pointer to the db * @return 0 on success */ - int update(SqlDB *db) - { - string error_str; - return insert_replace(db, true, error_str); - }; + int update(); /** * Reads the Quotas from the database. * @param db pointer to the db * @return 0 on success */ - int select(SqlDB *db); - - /** - * Bootstraps the database table(s) associated to the default quotas - * @return 0 on success - */ - static int bootstrap(SqlDB * db) - { - ostringstream oss(DefaultQuotas::db_bootstrap); - - return db->exec(oss); - } + int select(); private: + /** + * Name for the default quota attribute + */ const char * root_elem; - // ************************************************************************* - // DataBase implementation - // ************************************************************************* - - static const char * db_names; - - static const char * db_bootstrap; - - static const char * table; - - /** - * Execute an INSERT or REPLACE Sql query. - * @param db The SQL DB - * @param replace Execute an INSERT or a REPLACE - * @param error_str Returns the error reason, if any - * @return 0 one success - */ - int insert_replace(SqlDB *db, bool replace, string& error_str); - - /** - * Callback function to unmarshall a PoolObjectSQL - * @param num the number of columns read from the DB - * @param names the column names - * @param vaues the column values - * @return 0 on success - */ - int select_cb(void *nil, int num, char **values, char **names) - { - if ( (!values[0]) || (num != 1) ) - { - return -1; - } - - return from_xml(values[0]); - }; - /** * Builds quota object from an ObjectXML * @param xml The xml-formatted string * @return 0 on success */ int from_xml(const string& xml); - - /** - * Removes the Quotas from the database. - * @param db pointer to the db - * @return 0 on success - */ - int drop(SqlDB * db) - { - return -1; // Not supported - }; }; #endif /*DEFAULT_QUOTAS_H_*/ diff --git a/include/Nebula.h b/include/Nebula.h index ef30c0e32a..6cd0963af5 100644 --- a/include/Nebula.h +++ b/include/Nebula.h @@ -18,6 +18,7 @@ #define NEBULA_H_ #include "SqlDB.h" +#include "SystemDB.h" #include "NebulaTemplate.h" @@ -46,7 +47,13 @@ #include "Callbackable.h" -class Nebula : public Callbackable + +/** + * This is the main class for the OpenNebula daemon oned. It stores references + * to the main modules and data pools. It also includes functions to bootstrap + * the system and start all its components. + */ +class Nebula { public: @@ -325,7 +332,7 @@ public: if ( rc == 0 ) { - rc = default_user_quota.update(db); + rc = default_user_quota.update(); } return rc; @@ -343,12 +350,52 @@ public: if ( rc == 0 ) { - rc = default_group_quota.update(db); + rc = default_group_quota.update(); } return rc; }; + // ----------------------------------------------------------------------- + // System attributes + // ----------------------------------------------------------------------- + /** + * Reads a System attribute from the DB + * @param attr_name name of the attribute + * @param cb Callback that will receive the attribute in XML + * @return 0 on success + */ + int select_sys_attribute(const string& attr_name, string& attr_xml) + { + return system_db->select_sys_attribute(attr_name, attr_xml); + }; + + /** + * Writes a system attribute in the database. + * @param db pointer to the db + * @return 0 on success + */ + int insert_sys_attribute( + const string& attr_name, + const string& xml_attr, + string& error_str) + { + return system_db->insert_sys_attribute(attr_name, xml_attr, error_str); + }; + + /** + * Updates the system attribute in the database. + * @param db pointer to the db + * @return 0 on success + */ + int update_sys_attribute( + const string& attr_name, + const string& xml_attr, + string& error_str) + { + return system_db->update_sys_attribute(attr_name, xml_attr, error_str); + }; + private: // ----------------------------------------------------------------------- @@ -366,9 +413,10 @@ private: "/DEFAULT_GROUP_QUOTAS/NETWORK_QUOTA", "/DEFAULT_GROUP_QUOTAS/IMAGE_QUOTA", "/DEFAULT_GROUP_QUOTAS/VM_QUOTA"), - db(0),vmpool(0),hpool(0),vnpool(0), - upool(0),ipool(0),gpool(0),tpool(0),dspool(0),clpool(0),docpool(0), - lcm(0),vmm(0),im(0),tm(0),dm(0),rm(0),hm(0),authm(0),aclm(0),imagem(0) + system_db(0), db(0), vmpool(0), hpool(0), vnpool(0), + upool(0), ipool(0), gpool(0), tpool(0), dspool(0), clpool(0), + docpool(0), lcm(0), vmm(0), im(0), tm(0), dm(0), rm(0), hm(0), authm(0), + aclm(0), imagem(0) { const char * nl = getenv("ONE_LOCATION"); @@ -514,6 +562,11 @@ private: { delete db; } + + if ( system_db != 0 ) + { + delete system_db; + } }; Nebula& operator=(Nebula const&){return *this;}; @@ -539,7 +592,7 @@ private: // Configuration // --------------------------------------------------------------- - OpenNebulaTemplate * nebula_configuration; + OpenNebulaTemplate * nebula_configuration; // --------------------------------------------------------------- // Default quotas @@ -548,6 +601,11 @@ private: DefaultQuotas default_user_quota; DefaultQuotas default_group_quota; + // --------------------------------------------------------------- + // The system database + // --------------------------------------------------------------- + + SystemDB * system_db; // --------------------------------------------------------------- // Nebula Pools @@ -585,34 +643,6 @@ private: // --------------------------------------------------------------- friend void nebula_signal_handler (int sig); - - /** - * Bootstraps the database control tables - * - * @return 0 on success - */ - int bootstrap(); - - /** - * Callback function for the check_db_version method. Stores the read - * version in loaded_db_version - * @param _loaded_db_version returned columns - * @param num the number of columns read from the DB - * @param names the column names - * @param vaues the column values - * @return 0 on success - */ - int select_cb(void *_loaded_db_version, int num, char **values, - char **names); - - /** - * Reads the current DB version. - * - * @return 0 on success, - * -1 if there is a version mismatch, - * -2 if the DB needs a bootstrap - */ - int check_db_version(); }; #endif /*NEBULA_H_*/ diff --git a/include/SystemDB.h b/include/SystemDB.h new file mode 100644 index 0000000000..597868c9b1 --- /dev/null +++ b/include/SystemDB.h @@ -0,0 +1,147 @@ +/* -------------------------------------------------------------------------- */ +/* 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. */ +/* -------------------------------------------------------------------------- */ + +#ifndef SYSTEM_DB_H_ +#define SYSTEM_DB_H_ + +#include "SqlDB.h" +#include "Callbackable.h" + +class Nebula; + +/** + * This class represents the OpenNebula core system data tables: + * - pool_control + * - db_versioning + * - system attributes + */ +class SystemDB: public Callbackable +{ +public: + /** + * Reads a System attribute from the DB + * @param attr_name name of the attribute + * @param attr_xml the attribute in a XML document + * @return 0 on success + */ + int select_sys_attribute(const string& attr_name, string& attr_zml); + + /** + * Writes a system attribute in the database. + * @param db pointer to the db + * @return 0 on success + */ + int insert_sys_attribute( + const string& attr_name, + const string& xml_attr, + string& error_str) + { + return insert_replace(attr_name, xml_attr, false, error_str); + }; + + /** + * Updates the system attribute in the database. + * @param db pointer to the db + * @return 0 on success + */ + int update_sys_attribute( + const string& attr_name, + const string& xml_attr, + string& error_str) + { + return insert_replace(attr_name, xml_attr, true, error_str); + }; + +private: + friend class Nebula; //Only for Nebula class + + SystemDB(SqlDB *_db):db(_db){}; + + ~SystemDB(){}; + + // Pool control table + static const char * pc_names; + + static const char * pc_bootstrap; + + static const char * pc_table; + + // DB versioning table + static const char * ver_names; + + static const char * ver_bootstrap; + + static const char * ver_table; + + // System attributes table + static const char * sys_names; + + static const char * sys_bootstrap; + + static const char * sys_table; + + // Pointer to the db database + SqlDB *db; + + /** + * Execute an INSERT or REPLACE Sql query for a system attribute. + * @param db The SQL DB + * @param replace Execute an INSERT or a REPLACE + * @param error_str Returns the error reason, if any + * @return 0 one success + */ + int insert_replace(const string& attr_name, + const string& xml_attr, + bool replace, + string& error_str); + /** + * Bootstraps the database control tables + * + * @return 0 on success + */ + int bootstrap(); + + /** + * Callback function for the check_db_version method. Stores the read + * version in loaded_db_version + * @param _loaded_db_version returned columns + * @param num the number of columns read from the DB + * @param names the column names + * @param vaues the column values + * @return 0 on success + */ + int select_cb(void *_loaded_db_version, + int num, + char **values, + char **names); + /** + * Callback function for selecting system attributes + */ + int select_attr_cb(void *_attr_xml, + int num, + char **values, + char **names); + /** + * Reads the current DB version. + * + * @return 0 on success, + * -1 if there is a version mismatch, + * -2 if the DB needs a bootstrap + */ + int check_db_version(); +}; + +#endif //SYSTEM_DB_H diff --git a/src/nebula/Nebula.cc b/src/nebula/Nebula.cc index e6a6eaed9a..de2e0645cd 100644 --- a/src/nebula/Nebula.cc +++ b/src/nebula/Nebula.cc @@ -33,6 +33,238 @@ using namespace std; + +// Pool control table +const char * SystemDB::pc_table = "pool_control"; +const char * SystemDB::pc_names = "tablename, last_oid"; + +const char * SystemDB::pc_bootstrap = "CREATE TABLE pool_control " + "(tablename VARCHAR(32) PRIMARY KEY, last_oid BIGINT UNSIGNED)"; + + +// DB versioning table +const char * SystemDB::ver_table = "db_versioning"; +const char * SystemDB::ver_names = "oid, version, timestamp, comment"; + +const char * SystemDB::ver_bootstrap = "CREATE TABLE db_versioning " + "(oid INTEGER PRIMARY KEY, version VARCHAR(256), timestamp INTEGER, " + "comment VARCHAR(256))"; + +// System attributes table +const char * SystemDB::sys_table = "system_attributes"; +const char * SystemDB::sys_names = "name, body"; + +const char * SystemDB::sys_bootstrap = "CREATE TABLE IF NOT EXISTS" + " system_attributes (name VARCHAR(128) PRIMARY KEY, body TEXT)"; + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int SystemDB::bootstrap() +{ + int rc; + ostringstream oss; + + // ------------------------------------------------------------------------ + // pool control, tracks the last ID's assigned to objects + // ------------------------------------------------------------------------ + oss.str(pc_bootstrap); + rc = db->exec(oss); + + // ------------------------------------------------------------------------ + // db versioning, version of OpenNebula. Insert this version number + // ------------------------------------------------------------------------ + oss.str(ver_bootstrap); + rc += db->exec(oss); + + oss.str(""); + oss << "INSERT INTO " << ver_table << " (" << ver_names << ") " + << "VALUES (0, '" << Nebula::db_version() << "', " << time(0) + << ", '" << Nebula::version() << " daemon bootstrap')"; + + rc += db->exec(oss); + + // ------------------------------------------------------------------------ + // system , version of OpenNebula. Insert this version number + // ------------------------------------------------------------------------ + oss.str(sys_bootstrap); + rc += db->exec(oss); + + return rc; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int SystemDB::select_cb(void *_loaded_db_version, int num, char **values, + char **names) +{ + istringstream iss; + string * loaded_db_version; + + loaded_db_version = static_cast(_loaded_db_version); + + if ( (values[0]) && (num == 1) ) + { + *loaded_db_version = values[0]; + } + + return 0; +}; + +/* -------------------------------------------------------------------------- */ + +int SystemDB::check_db_version() +{ + int rc; + ostringstream oss; + + string loaded_db_version = ""; + + // Try to read latest version + set_callback( static_cast(&SystemDB::select_cb), + static_cast(&loaded_db_version) ); + + oss << "SELECT version FROM " << ver_table + << " WHERE oid=(SELECT MAX(oid) FROM " << ver_table << ")"; + + db->exec(oss, this); + + oss.str(""); + unset_callback(); + + if( loaded_db_version == "" ) + { + // Table user_pool is present for all OpenNebula versions, and it + // always contains at least the oneadmin user. + oss << "SELECT MAX(oid) FROM user_pool"; + rc = db->exec(oss); + + oss.str(""); + + if( rc != 0 ) // Database needs bootstrap + { + return -2; + } + } + + if( Nebula::db_version() != loaded_db_version ) + { + oss << "Database version mismatch. " + << "Installed " << Nebula::version() << " uses DB version '" + << Nebula::db_version() << "', and existing DB version is '" + << loaded_db_version << "'."; + + NebulaLog::log("ONE",Log::ERROR,oss); + return -1; + } + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int SystemDB::insert_replace( + const string& attr_name, + const string& xml_attr, + bool replace, + string& err) +{ + ostringstream oss; + + int rc; + char * sql_xml; + + sql_xml = db->escape_str(xml_attr.c_str()); + + if ( sql_xml == 0 ) + { + goto error_body; + } + + if ( ObjectXML::validate_xml(sql_xml) != 0 ) + { + goto error_xml; + } + + if ( replace ) + { + oss << "REPLACE"; + } + else + { + oss << "INSERT"; + } + + // Construct the SQL statement to Insert or Replace + + oss <<" INTO "<< sys_table << + " ("<< sys_names <<") VALUES (" + << "'" << attr_name << "'," + << "'" << sql_xml << "')"; + + rc = db->exec(oss); + + db->free_str(sql_xml); + + return rc; + +error_xml: + db->free_str(sql_xml); + +error_body: + err = "Error inserting system attribute in DB."; + return -1; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int SystemDB::select_attr_cb(void * _xml_attr, + int num, + char ** values, + char ** names) +{ + istringstream iss; + string * xml_attr; + + xml_attr = static_cast(_xml_attr); + + if ( (values[0]) && (num == 1) ) + { + *xml_attr = values[0]; + } + + return 0; +}; + +/* -------------------------------------------------------------------------- */ + +int SystemDB::select_sys_attribute(const string& attr_name, string& attr_xml) +{ + ostringstream oss; + + int rc; + + set_callback(static_cast(&SystemDB::select_attr_cb), + static_cast(&attr_xml) ); + + oss << "SELECT body FROM " << sys_table << " WHERE name = '" + << attr_name << "'"; + + rc = db->exec(oss, this); + + unset_callback(); + + if (rc != 0) + { + return -1; + } + + return 0; +} + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -219,8 +451,15 @@ void Nebula::start() } } + // --------------------------------------------------------------------- + // Prepare the SystemDB and check versions + // --------------------------------------------------------------------- + NebulaLog::log("ONE",Log::INFO,"Checking database version."); - rc = check_db_version(); + + system_db = new SystemDB(db); + + rc = system_db->check_db_version(); if( rc == -1 ) { @@ -245,14 +484,16 @@ void Nebula::start() rc += ClusterPool::bootstrap(db); rc += DocumentPool::bootstrap(db); - rc += DefaultQuotas::bootstrap(db); - - // Create the versioning table only if bootstrap went well + // Create the system tables only if bootstrap went well if ( rc == 0 ) { - rc += bootstrap(); + rc += system_db->bootstrap(); } + // Insert default system attributes + rc += default_user_quota.insert(); + rc += default_group_quota.insert(); + if ( rc != 0 ) { throw runtime_error("Error bootstrapping database."); @@ -343,8 +584,8 @@ void Nebula::start() dspool = new DatastorePool(db); - default_user_quota.select(db); - default_group_quota.select(db); + default_user_quota.select(); + default_group_quota.select(); } catch (exception&) { @@ -679,103 +920,3 @@ void Nebula::start() NebulaLog::log("ONE", Log::INFO, "All modules finalized, exiting.\n"); } -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int Nebula::bootstrap() -{ - int rc; - ostringstream oss; - string error; - - oss << "CREATE TABLE pool_control (tablename VARCHAR(32) PRIMARY KEY, " - "last_oid BIGINT UNSIGNED)"; - - rc = db->exec(oss); - - oss.str(""); - oss << "CREATE TABLE db_versioning (oid INTEGER PRIMARY KEY, " - "version VARCHAR(256), timestamp INTEGER, comment VARCHAR(256))"; - - rc += db->exec(oss); - - oss.str(""); - oss << "INSERT INTO db_versioning (oid, version, timestamp, comment) " - << "VALUES (0, '" << db_version() << "', " << time(0) - << ", '" << version() << " daemon bootstrap')"; - - rc += db->exec(oss); - - rc += default_user_quota.insert(db, error); - rc += default_group_quota.insert(db, error); - - return rc; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int Nebula::check_db_version() -{ - int rc; - ostringstream oss; - - - string loaded_db_version = ""; - - // Try to read latest version - set_callback( static_cast(&Nebula::select_cb), - static_cast(&loaded_db_version) ); - - oss << "SELECT version FROM db_versioning " - << "WHERE oid=(SELECT MAX(oid) FROM db_versioning)"; - - db->exec(oss, this); - - oss.str(""); - unset_callback(); - - if( loaded_db_version == "" ) - { - // Table user_pool is present for all OpenNebula versions, and it - // always contains at least the oneadmin user. - oss << "SELECT MAX(oid) FROM user_pool"; - rc = db->exec(oss); - - oss.str(""); - - if( rc != 0 ) // Database needs bootstrap - { - return -2; - } - } - - if( db_version() != loaded_db_version ) - { - oss << "Database version mismatch. " - << "Installed " << version() << " uses DB version '" << db_version() - << "', and existing DB version is '" - << loaded_db_version << "'."; - - NebulaLog::log("ONE",Log::ERROR,oss); - return -1; - } - - return 0; -} - -int Nebula::select_cb(void *_loaded_db_version, int num, char **values, - char **names) -{ - istringstream iss; - string * loaded_db_version; - - loaded_db_version = static_cast(_loaded_db_version); - - if ( (values[0]) && (num == 1) ) - { - *loaded_db_version = values[0]; - } - - return 0; -}; diff --git a/src/nebula/SConstruct b/src/nebula/SConstruct index ceafbc245e..2483ba74d2 100644 --- a/src/nebula/SConstruct +++ b/src/nebula/SConstruct @@ -23,6 +23,7 @@ lib_name='nebula_core' # Sources to generate the library source_files=[ + 'SystemDB.cc', 'Nebula.cc', 'NebulaTemplate.cc', ] diff --git a/src/nebula/SystemDB.cc b/src/nebula/SystemDB.cc new file mode 100644 index 0000000000..444033a917 --- /dev/null +++ b/src/nebula/SystemDB.cc @@ -0,0 +1,252 @@ +/* -------------------------------------------------------------------------- */ +/* 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. */ +/* -------------------------------------------------------------------------- */ + +#include "Nebula.h" +#include "SystemDB.h" + +using namespace std; + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +// Pool control table +const char * SystemDB::pc_table = "pool_control"; +const char * SystemDB::pc_names = "tablename, last_oid"; + +const char * SystemDB::pc_bootstrap = "CREATE TABLE pool_control " + "(tablename VARCHAR(32) PRIMARY KEY, last_oid BIGINT UNSIGNED)"; + + +// DB versioning table +const char * SystemDB::ver_table = "db_versioning"; +const char * SystemDB::ver_names = "oid, version, timestamp, comment"; + +const char * SystemDB::ver_bootstrap = "CREATE TABLE db_versioning " + "(oid INTEGER PRIMARY KEY, version VARCHAR(256), timestamp INTEGER, " + "comment VARCHAR(256))"; + +// System attributes table +const char * SystemDB::sys_table = "system_attributes"; +const char * SystemDB::sys_names = "name, body"; + +const char * SystemDB::sys_bootstrap = "CREATE TABLE IF NOT EXISTS" + " system_attributes (name VARCHAR(128) PRIMARY KEY, body TEXT)"; + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int SystemDB::bootstrap() +{ + int rc; + ostringstream oss; + + // ------------------------------------------------------------------------ + // pool control, tracks the last ID's assigned to objects + // ------------------------------------------------------------------------ + oss.str(pc_bootstrap); + rc = db->exec(oss); + + // ------------------------------------------------------------------------ + // db versioning, version of OpenNebula. Insert this version number + // ------------------------------------------------------------------------ + oss.str(ver_bootstrap); + rc += db->exec(oss); + + oss.str(""); + oss << "INSERT INTO " << ver_table << " (" << ver_names << ") " + << "VALUES (0, '" << Nebula::db_version() << "', " << time(0) + << ", '" << Nebula::version() << " daemon bootstrap')"; + + rc += db->exec(oss); + + // ------------------------------------------------------------------------ + // system , version of OpenNebula. Insert this version number + // ------------------------------------------------------------------------ + oss.str(sys_bootstrap); + rc += db->exec(oss); + + return rc; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int SystemDB::select_cb(void *_loaded_db_version, int num, char **values, + char **names) +{ + istringstream iss; + string * loaded_db_version; + + loaded_db_version = static_cast(_loaded_db_version); + + if ( (values[0]) && (num == 1) ) + { + *loaded_db_version = values[0]; + } + + return 0; +}; + +/* -------------------------------------------------------------------------- */ + +int SystemDB::check_db_version() +{ + int rc; + ostringstream oss; + + string loaded_db_version = ""; + + // Try to read latest version + set_callback( static_cast(&SystemDB::select_cb), + static_cast(&loaded_db_version) ); + + oss << "SELECT version FROM " << ver_table + << " WHERE oid=(SELECT MAX(oid) FROM " << ver_table << ")"; + + db->exec(oss, this); + + oss.str(""); + unset_callback(); + + if( loaded_db_version == "" ) + { + // Table user_pool is present for all OpenNebula versions, and it + // always contains at least the oneadmin user. + oss << "SELECT MAX(oid) FROM user_pool"; + rc = db->exec(oss); + + oss.str(""); + + if( rc != 0 ) // Database needs bootstrap + { + return -2; + } + } + + if( Nebula::db_version() != loaded_db_version ) + { + oss << "Database version mismatch. " + << "Installed " << Nebula::version() << " uses DB version '" + << Nebula::db_version() << "', and existing DB version is '" + << loaded_db_version << "'."; + + NebulaLog::log("ONE",Log::ERROR,oss); + return -1; + } + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int SystemDB::insert_replace( + const string& attr_name, + const string& xml_attr, + bool replace, + string& err) +{ + ostringstream oss; + + int rc; + char * sql_xml; + + sql_xml = db->escape_str(xml_attr.c_str()); + + if ( sql_xml == 0 ) + { + goto error_body; + } + + if ( ObjectXML::validate_xml(sql_xml) != 0 ) + { + goto error_xml; + } + + if ( replace ) + { + oss << "REPLACE"; + } + else + { + oss << "INSERT"; + } + + // Construct the SQL statement to Insert or Replace + + oss <<" INTO "<< sys_table << " ("<< sys_names <<") VALUES (" + << "'" << attr_name << "'," << "'" << sql_xml << "')"; + + rc = db->exec(oss); + + db->free_str(sql_xml); + + return rc; + +error_xml: + db->free_str(sql_xml); + +error_body: + err = "Error inserting system attribute in DB."; + return -1; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int SystemDB::select_attr_cb(void * _xml_attr, + int num, + char ** values, + char ** names) +{ + istringstream iss; + string * xml_attr; + + xml_attr = static_cast(_xml_attr); + + if ( (values[0]) && (num == 1) ) + { + *xml_attr = values[0]; + } + + return 0; +}; + +/* -------------------------------------------------------------------------- */ + +int SystemDB::select_sys_attribute(const string& attr_name, string& attr_xml) +{ + ostringstream oss; + + int rc; + + set_callback(static_cast(&SystemDB::select_attr_cb), + static_cast(&attr_xml) ); + + oss << "SELECT body FROM " << sys_table << " WHERE name = '" + << attr_name << "'"; + + rc = db->exec(oss, this); + + unset_callback(); + + if (rc != 0) + { + return -1; + } + + return 0; +} diff --git a/src/um/DefaultQuotas.cc b/src/um/DefaultQuotas.cc index e88e6a83eb..7fa4d5a7f9 100644 --- a/src/um/DefaultQuotas.cc +++ b/src/um/DefaultQuotas.cc @@ -17,14 +17,7 @@ #include "DefaultQuotas.h" #include "ObjectXML.h" -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -const char * DefaultQuotas::table = "system_attributes"; -const char * DefaultQuotas::db_names = "name, body"; - -const char * DefaultQuotas::db_bootstrap = "CREATE TABLE IF NOT EXISTS" - " system_attributes (name VARCHAR(128) PRIMARY KEY, body TEXT)"; +#include "Nebula.h" /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -61,84 +54,39 @@ int DefaultQuotas::from_xml(const string& xml) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int DefaultQuotas::select(SqlDB *db) +int DefaultQuotas::select() { - ostringstream oss; + string xml_body; + Nebula& nd = Nebula::instance(); - int rc; - - set_callback( - static_cast(&DefaultQuotas::select_cb)); - - oss << "SELECT body FROM " << table << " WHERE name = '" << root_elem << "'"; - - rc = db->exec(oss, this); - - unset_callback(); - - if (rc != 0) + if ( nd.select_sys_attribute(root_elem, xml_body) != 0 ) { return -1; } - return 0; + return from_xml(xml_body); } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int DefaultQuotas::insert_replace(SqlDB *db, bool replace, string& error_str) +int DefaultQuotas::insert() { - ostringstream oss; + string xml_body; + string error; + Nebula& nd = Nebula::instance(); - int rc; - string xml_body; - - char * sql_xml; - - sql_xml = db->escape_str(to_xml(xml_body).c_str()); - - if ( sql_xml == 0 ) - { - goto error_body; - } - - if ( ObjectXML::validate_xml(sql_xml) != 0 ) - { - goto error_xml; - } - - if ( replace ) - { - oss << "REPLACE"; - } - else - { - oss << "INSERT"; - } - - // Construct the SQL statement to Insert or Replace - - oss <<" INTO "<
exec(oss); - - db->free_str(sql_xml); - - return rc; - -error_xml: - db->free_str(sql_xml); - - error_str = "Error transforming the Quotas to XML."; - return -1; - -error_body: - error_str = "Error inserting Quotas in DB."; - return -1; + return nd.insert_sys_attribute(root_elem, to_xml(xml_body), error); } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ + +int DefaultQuotas::update() +{ + string xml_body; + string error; + Nebula& nd = Nebula::instance(); + + return nd.update_sys_attribute(root_elem, to_xml(xml_body), error); +} diff --git a/src/um/Quota.cc b/src/um/Quota.cc index bd36507a39..9080df65be 100644 --- a/src/um/Quota.cc +++ b/src/um/Quota.cc @@ -120,12 +120,12 @@ int Quota::set(vector * new_quotas, string& error) if (update_limits(tq, iq) != 0) { goto error_limits; - } + } } cleanup_quota(id); } - + return 0; error_limits: @@ -141,16 +141,16 @@ error_limits: delete quota_str; } - + error = oss.str(); - return -1; + return -1; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -bool Quota::check_quota(const string& qid, +bool Quota::check_quota(const string& qid, map& usage_req, Quotas& default_quotas, string& error) @@ -184,7 +184,7 @@ bool Quota::check_quota(const string& qid, if ( q == 0 ) { map values; - + for (int i=0; i < num_metrics; i++) { string metrics_used = metrics[i]; @@ -211,7 +211,7 @@ bool Quota::check_quota(const string& qid, for (int i=0; i < num_metrics; i++) { string metrics_used = metrics[i]; - + metrics_used += "_USED"; it = usage_req.find(metrics[i]); @@ -245,7 +245,7 @@ bool Quota::check_quota(const string& qid, oss << "limit of " << limit << " reached for " << metrics[i] << " quota in " << template_name; - if ( !qid.empty() ) + if ( !qid.empty() ) { oss << " with ID: " << qid; } @@ -262,7 +262,7 @@ bool Quota::check_quota(const string& qid, for (int i=0; i < num_metrics; i++) { string metrics_used = metrics[i]; - + metrics_used += "_USED"; it = usage_req.find(metrics[i]); @@ -294,12 +294,12 @@ void Quota::del_quota(const string& qid, map& usage_req) if ( q == 0 ) { return; - } + } for (int i=0; i < num_metrics; i++) { string metrics_used = metrics[i]; - + metrics_used += "_USED"; it = usage_req.find(metrics[i]); @@ -323,8 +323,7 @@ void Quota::cleanup_quota(const string& qid) VectorAttribute * q; map::iterator q_it; - float usage, usage_tmp, limit_tmp, implicit_limit; - bool default_limit; + float usage, limit, implicit_limit; if ( get_quota(qid, &q, q_it) == -1) { @@ -345,28 +344,24 @@ void Quota::cleanup_quota(const string& qid) implicit_limit = -1; } - default_limit = true; - usage = 0; - for (int i=0; i < num_metrics; i++) { string metrics_used = metrics[i]; metrics_used += "_USED"; - q->vector_value(metrics[i], limit_tmp); - q->vector_value(metrics_used.c_str(), usage_tmp); + q->vector_value(metrics[i], limit); + q->vector_value(metrics_used.c_str(), usage); - default_limit = (default_limit && limit_tmp == implicit_limit ); - usage += usage_tmp; + if ( usage != 0 || limit != implicit_limit ) + { + return; + } } - if ( usage == 0 && default_limit ) - { - delete static_cast(q_it->second); + delete static_cast(q_it->second); - attributes.erase(q_it); - } + attributes.erase(q_it); } /* -------------------------------------------------------------------------- */ @@ -395,7 +390,7 @@ int Quota::update_limits( quota->replace(metrics[i], limit); } } - + return 0; } @@ -412,7 +407,7 @@ VectorAttribute * Quota::new_quota(VectorAttribute * va) for (int i=0; i < num_metrics; i++) { string metrics_used = metrics[i]; - + metrics_used += "_USED"; limit = va->vector_value_str(metrics[i], limit_i);