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/41] 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/41] 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/41] 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/41] 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/41] 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/41] 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/41] 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/41] 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/41] 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/41] 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/41] 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/41] 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/41] 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/41] 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/41] 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 bf5a3a76e435cb08dfe953d9364d8f70fa2dc550 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Tue, 27 Nov 2012 17:40:01 +0100 Subject: [PATCH 16/41] feature #1570: add user, password and endpoint to oneacct --- src/cli/one_helper/oneacct_helper.rb | 8 ++++---- src/cli/oneacct | 13 +++++++++---- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/cli/one_helper/oneacct_helper.rb b/src/cli/one_helper/oneacct_helper.rb index f07d766fdb..ffd08c048f 100644 --- a/src/cli/one_helper/oneacct_helper.rb +++ b/src/cli/one_helper/oneacct_helper.rb @@ -33,10 +33,10 @@ class AcctHelper < OpenNebulaHelper::OneHelper :format => String # TODO Time } - USER = { - :name => "user", + USERFILTER = { + :name => "userfilter", :short => "-u user", - :large => "--user user" , + :large => "--userfilter user" , :description => "User name or id to filter the results", :format => String, :proc => lambda { |o, options| @@ -95,7 +95,7 @@ class AcctHelper < OpenNebulaHelper::OneHelper :description => "Split the output in a table for each VM" } - ACCT_OPTIONS = [START_TIME, END_TIME, USER, GROUP, HOST, XPATH, XML, JSON, SPLIT] + ACCT_OPTIONS = [START_TIME, END_TIME, USERFILTER, GROUP, HOST, XPATH, XML, JSON, SPLIT] ACCT_TABLE = CLIHelper::ShowTable.new("oneacct.yaml", nil) do diff --git a/src/cli/oneacct b/src/cli/oneacct index 97a269c029..178a39db7e 100755 --- a/src/cli/oneacct +++ b/src/cli/oneacct @@ -37,12 +37,17 @@ cmd = CommandParser::CmdParser.new(ARGV) do description "" version OpenNebulaHelper::ONE_VERSION - client = OpenNebulaHelper::OneHelper.get_client({}) + helper=OpenNebulaHelper::OneHelper.new - option AcctHelper::ACCT_OPTIONS + CommandParser::OPTIONS + before_proc do + helper.set_client(options) + end + + option AcctHelper::ACCT_OPTIONS + CommandParser::OPTIONS + + OpenNebulaHelper::CLIENT_OPTIONS main do - filter_flag = (options[:user] || VirtualMachinePool::INFO_ALL) + filter_flag = (options[:userfilter] || VirtualMachinePool::INFO_ALL) start_time = options[:start] ? options[:start].to_i : -1 end_time = options[:end] ? options[:end].to_i : -1 @@ -54,7 +59,7 @@ cmd = CommandParser::CmdParser.new(ARGV) do :xpath => options[:xpath] } - pool = OpenNebula::VirtualMachinePool.new(client) + pool = OpenNebula::VirtualMachinePool.new(helper.client) if options[:json] || options[:xml] xml_str = pool.accounting_xml(filter_flag, common_opts) 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 17/41] 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 18/41] 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 19/41] 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 20/41] 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 21/41] 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 2ba0f8203ccf22110fcc6f816ff79b3f3412134a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Thu, 29 Nov 2012 11:24:56 +0100 Subject: [PATCH 22/41] Bug #1668: Fix xml parser for empty xml elements --- src/template/Template.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/template/Template.cc b/src/template/Template.cc index e461a4afbe..4905639714 100644 --- a/src/template/Template.cc +++ b/src/template/Template.cc @@ -538,7 +538,7 @@ Attribute * Template::single_xml_att(const xmlNode * node) Attribute * attr = 0; xmlNode * child = node->children; - if( child->next == 0 && child != 0 && + if( child != 0 && child->next == 0 && (child->type == XML_TEXT_NODE || child->type == XML_CDATA_SECTION_NODE)) { 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 23/41] 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 24/41] 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 25/41] 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 26/41] 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 27/41] 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 28/41] 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 29/41] 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 30/41] 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); From d22567fe23fd8e7e25fed2f1474e7020c6375bd9 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 30 Nov 2012 03:17:49 +0100 Subject: [PATCH 31/41] bug-1669: Wrong condition checks in some files. Makes some string treatment case-senitive, as opposed with the convenction in OpenNebula (strings are *not* case sensitive) --- src/scheduler/include/SchedulerPolicy.h | 2 +- src/vmm/LibVirtDriverKVM.cc | 16 +++++----------- src/vmm/LibVirtDriverVMware.cc | 5 +---- src/vmm/XenDriver.cc | 5 +---- 4 files changed, 8 insertions(+), 20 deletions(-) diff --git a/src/scheduler/include/SchedulerPolicy.h b/src/scheduler/include/SchedulerPolicy.h index ae12854c0c..b62efb3149 100644 --- a/src/scheduler/include/SchedulerPolicy.h +++ b/src/scheduler/include/SchedulerPolicy.h @@ -43,7 +43,7 @@ public: policy(vm); - if(priority.empty()!=true) + if(!priority.empty()) { sw.max = fabs(*max_element( priority.begin(), diff --git a/src/vmm/LibVirtDriverKVM.cc b/src/vmm/LibVirtDriverKVM.cc index c3b10c4e67..2cec09ad83 100644 --- a/src/vmm/LibVirtDriverKVM.cc +++ b/src/vmm/LibVirtDriverKVM.cc @@ -334,10 +334,7 @@ int LibVirtDriver::deployment_description_kvm( // ---- Disk type and source for the image ---- - if (type.empty() == false) - { - transform(type.begin(),type.end(),type.begin(),(int(*)(int))toupper); - } + transform(type.begin(),type.end(),type.begin(),(int(*)(int))toupper); if ( type == "BLOCK" ) { @@ -548,13 +545,10 @@ int LibVirtDriver::deployment_description_kvm( passwd = graphics->vector_value("PASSWD"); keymap = graphics->vector_value("KEYMAP"); - if (type.empty() != false) - { - transform(type.begin(), - type.end(), - type.begin(), - (int(*)(int))tolower); - } + transform(type.begin(), + type.end(), + type.begin(), + (int(*)(int))tolower); if ( type == "vnc" || type == "spice" ) { diff --git a/src/vmm/LibVirtDriverVMware.cc b/src/vmm/LibVirtDriverVMware.cc index 2319eafe14..483db3f357 100644 --- a/src/vmm/LibVirtDriverVMware.cc +++ b/src/vmm/LibVirtDriverVMware.cc @@ -214,10 +214,7 @@ int LibVirtDriver::deployment_description_vmware( } } - if (type.empty() == false) - { - transform(type.begin(),type.end(),type.begin(),(int(*)(int))toupper); - } + transform(type.begin(),type.end(),type.begin(),(int(*)(int))toupper); // ---- Disk type and source for the image ---- diff --git a/src/vmm/XenDriver.cc b/src/vmm/XenDriver.cc index 9be40d3453..7b89e0ebfb 100644 --- a/src/vmm/XenDriver.cc +++ b/src/vmm/XenDriver.cc @@ -248,10 +248,7 @@ int XenDriver::deployment_description( goto error_disk; } - if (type.empty() == false) - { - transform(type.begin(),type.end(),type.begin(),(int(*)(int))toupper); - } + transform(type.begin(),type.end(),type.begin(),(int(*)(int))toupper); mode = "w"; From 5d54095119d0ec652edbf71e0ba3967c62734f4b Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Tue, 4 Dec 2012 13:35:45 +0100 Subject: [PATCH 32/41] Bug #1673: Add support for 64-bit net_tx/net_rx counters. Contributed by Simon Boulet. --- include/ObjectXML.h | 24 +++++++++ include/VirtualMachine.h | 10 ++-- src/vmm/VirtualMachineManagerDriver.cc | 4 +- src/xml/ObjectXML.cc | 68 ++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 7 deletions(-) diff --git a/include/ObjectXML.h b/include/ObjectXML.h index 704ce89bfa..a018cc3325 100644 --- a/include/ObjectXML.h +++ b/include/ObjectXML.h @@ -94,6 +94,30 @@ public: int xpath(unsigned int& value, const char * xpath_expr, const unsigned int& def); + /** + * Gets and sets a xpath attribute, if the attribute is not found a default + * is used + * @param value to set + * @param xpath_expr of the xml element + * @param def default value if the element is not found + * + * @return -1 if default was set + */ + int xpath(long long& value, const char * xpath_expr, + const long long& def); + + /** + * Gets and sets a xpath attribute, if the attribute is not found a default + * is used + * @param value to set + * @param xpath_expr of the xml element + * @param def default value if the element is not found + * + * @return -1 if default was set + */ + int xpath(unsigned long long& value, const char * xpath_expr, + const unsigned long long& def); + /** * Gets and sets a xpath attribute, if the attribute is not found a default * is used diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index 9b8ef879f8..2ec9df391b 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -173,13 +173,13 @@ public: * @param _memory Kilobytes used by the VM (total) * @param _cpu used by the VM (rate) * @param _net_tx transmitted bytes (total) - * @param _net_tx received bytes (total) + * @param _net_rx received bytes (total) */ void update_info( const int _memory, const int _cpu, - const int _net_tx, - const int _net_rx) + const long long _net_tx, + const long long _net_rx) { if (_memory != -1) { @@ -1009,12 +1009,12 @@ private: /** * Network usage, transmitted bytes */ - int net_tx; + long long net_tx; /** * Network usage, received bytes */ - int net_rx; + long long net_rx; /** * History record, for the current host diff --git a/src/vmm/VirtualMachineManagerDriver.cc b/src/vmm/VirtualMachineManagerDriver.cc index fa25eb3551..89cfd80b2d 100644 --- a/src/vmm/VirtualMachineManagerDriver.cc +++ b/src/vmm/VirtualMachineManagerDriver.cc @@ -386,8 +386,8 @@ void VirtualMachineManagerDriver::protocol( int cpu = -1; int memory = -1; - int net_tx = -1; - int net_rx = -1; + long long net_tx = -1; + long long net_rx = -1; char state = '-'; string monitor_str = is.str(); diff --git a/src/xml/ObjectXML.cc b/src/xml/ObjectXML.cc index 0f7d719ea6..df1dcaeff1 100644 --- a/src/xml/ObjectXML.cc +++ b/src/xml/ObjectXML.cc @@ -222,6 +222,74 @@ int ObjectXML::xpath(unsigned int& value, const char * xpath_expr, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +int ObjectXML::xpath(long long& value, const char * xpath_expr, + const long long& def) +{ + vector values; + int rc = 0; + + values = (*this)[xpath_expr]; + + if (values.empty() == true) + { + value = def; + rc = -1; + } + else + { + istringstream iss; + + iss.str(values[0]); + + iss >> dec >> value; + + if (iss.fail() == true) + { + value = def; + rc = -1; + } + } + + return rc; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int ObjectXML::xpath(unsigned long long& value, const char * xpath_expr, + const unsigned long long& def) +{ + vector values; + int rc = 0; + + values = (*this)[xpath_expr]; + + if (values.empty() == true) + { + value = def; + rc = -1; + } + else + { + istringstream iss; + + iss.str(values[0]); + + iss >> dec >> value; + + if (iss.fail() == true) + { + value = def; + rc = -1; + } + } + + return rc; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + int ObjectXML::xpath(time_t& value, const char * xpath_expr, const time_t& def) { int int_val; From bd07ce0fa80d417bf9448fee8e28421f3d442fa9 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Tue, 4 Dec 2012 15:34:57 +0100 Subject: [PATCH 33/41] Add gem requirements to readme file --- README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/README.md b/README.md index 1e3841533a..1261b2301c 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,37 @@ required gems. You have more information at: http://opennebula.org/documentation:documentation:compile +If you want to install them manually here are the list of required rubygems: + +* OpenNebula and clients (plus cloud interfaces) + * sqlite3 + * json + * sequel + * mysql + * net-ldap + * amazon-ec2 + * rack + * sinatra + * thin + * uuidtools + * curb + * nokogiri + +* OpenNebula zones (client and server) + * json + * sequel + * sqlite3 + * mysql + * nokogiri + +* Sunstone server + * json + * rack + * sinatra + * thin + * sequel + * nokogiri + ### OPTIONAL PACKAGES These packages are not needed to run or build OpenNebula. They improve the From 332b6bf0ffbf24f1f78ba7011abfa15d49366568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Tue, 4 Dec 2012 16:35:45 +0100 Subject: [PATCH 34/41] Feature #1611: Migrator creates new system table, and changes the quota limits from '0' to '-1' --- src/onedb/3.8.1_to_3.9.80.rb | 121 +++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/src/onedb/3.8.1_to_3.9.80.rb b/src/onedb/3.8.1_to_3.9.80.rb index 4db25fb799..77555022b4 100644 --- a/src/onedb/3.8.1_to_3.9.80.rb +++ b/src/onedb/3.8.1_to_3.9.80.rb @@ -112,6 +112,87 @@ module Migrator @db.run("DROP TABLE image_pool") @db.run("ALTER TABLE image_pool_new RENAME TO image_pool") + + ######################################################################## + # Feature #1611: Default quotas + ######################################################################## + + @db.run("CREATE TABLE IF NOT EXISTS system_attributes (name VARCHAR(128) PRIMARY KEY, body TEXT)") + @db.run("INSERT INTO system_attributes VALUES('DEFAULT_GROUP_QUOTAS','');") + @db.run("INSERT INTO system_attributes VALUES('DEFAULT_USER_QUOTAS','');") + + + @db.run "ALTER TABLE user_pool RENAME TO old_user_pool;" + @db.run "CREATE TABLE user_pool (oid INTEGER PRIMARY KEY, name VARCHAR(128), body TEXT, uid INTEGER, gid INTEGER, owner_u INTEGER, group_u INTEGER, other_u INTEGER, UNIQUE(name));" + + # oneadmin does not have quotas + @db.fetch("SELECT * FROM old_user_pool WHERE oid=0") do |row| + @db[:user_pool].insert( + :oid => row[:oid], + :name => row[:name], + :body => row[:body], + :uid => row[:oid], + :gid => row[:gid], + :owner_u => row[:owner_u], + :group_u => row[:group_u], + :other_u => row[:other_u]) + end + + @db.fetch("SELECT * FROM old_user_pool WHERE oid>0") do |row| + doc = Document.new(row[:body]) + + set_default_quotas(doc) + + @db[:user_pool].insert( + :oid => row[:oid], + :name => row[:name], + :body => doc.root.to_s, + :uid => row[:oid], + :gid => row[:gid], + :owner_u => row[:owner_u], + :group_u => row[:group_u], + :other_u => row[:other_u]) + end + + @db.run "DROP TABLE old_user_pool;" + + + @db.run "ALTER TABLE group_pool RENAME TO old_group_pool;" + @db.run "CREATE TABLE group_pool (oid INTEGER PRIMARY KEY, name VARCHAR(128), body TEXT, uid INTEGER, gid INTEGER, owner_u INTEGER, group_u INTEGER, other_u INTEGER, UNIQUE(name));" + + + # oneadmin group does not have quotas + @db.fetch("SELECT * FROM old_group_pool WHERE oid=0") do |row| + @db[:group_pool].insert( + :oid => row[:oid], + :name => row[:name], + :body => row[:body], + :uid => row[:oid], + :gid => row[:gid], + :owner_u => row[:owner_u], + :group_u => row[:group_u], + :other_u => row[:other_u]) + end + + @db.fetch("SELECT * FROM old_group_pool WHERE oid>0") do |row| + doc = Document.new(row[:body]) + + set_default_quotas(doc) + + @db[:group_pool].insert( + :oid => row[:oid], + :name => row[:name], + :body => doc.root.to_s, + :uid => row[:oid], + :gid => row[:gid], + :owner_u => row[:owner_u], + :group_u => row[:group_u], + :other_u => row[:other_u]) + end + + @db.run "DROP TABLE old_group_pool;" + + ######################################################################## # # Banner for the new /var/lib/one/vms directory @@ -130,4 +211,44 @@ module Migrator return true end + + + def set_default_quotas(doc) + + # VM quotas + + doc.root.each_element("VM_QUOTA/VM/CPU") do |e| + e.text = "-1" if e.text.to_f == 0 + end + + doc.root.each_element("VM_QUOTA/VM/MEMORY") do |e| + e.text = "-1" if e.text.to_i == 0 + end + + doc.root.each_element("VM_QUOTA/VM/VMS") do |e| + e.text = "-1" if e.text.to_i == 0 + end + + # VNet quotas + + doc.root.each_element("NETWORK_QUOTA/NETWORK/LEASES") do |e| + e.text = "-1" if e.text.to_i == 0 + end + + # Image quotas + + doc.root.each_element("IMAGE_QUOTA/IMAGE/RVMS") do |e| + e.text = "-1" if e.text.to_i == 0 + end + + # Datastore quotas + + doc.root.each_element("DATASTORE_QUOTA/DATASTORE/IMAGES") do |e| + e.text = "-1" if e.text.to_i == 0 + end + + doc.root.each_element("DATASTORE_QUOTA/DATASTORE/SIZE") do |e| + e.text = "-1" if e.text.to_i == 0 + end + end end From ff926f17d5d9ba417197fad1ad2287ae3aeb0c07 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Tue, 4 Dec 2012 23:19:08 +0100 Subject: [PATCH 35/41] feature #1617: New image types: KERNEL, RAMDISK and CONTEXT to store plain files for different uses. --- include/Image.h | 8 +- include/VirtualMachine.h | 3 + src/cli/oneimage | 6 +- src/image/Image.cc | 69 +++++++++------- src/image/ImageManagerActions.cc | 57 +++++++++---- src/image/ImagePool.cc | 31 ++++--- .../org/opennebula/client/image/Image.java | 4 +- src/oca/ruby/OpenNebula/Image.rb | 6 +- src/rm/RequestManagerImage.cc | 80 ++++++++++++++----- src/rm/RequestManagerVirtualMachine.cc | 16 +++- src/vm/VirtualMachine.cc | 30 ++++++- src/vm/vm_file_var_syntax.cc | 26 +++--- src/vm/vm_file_var_syntax.h | 12 +-- src/vm/vm_file_var_syntax.y | 8 -- 14 files changed, 239 insertions(+), 117 deletions(-) diff --git a/include/Image.h b/include/Image.h index 48ee9c670a..4cc2e0d967 100644 --- a/include/Image.h +++ b/include/Image.h @@ -38,7 +38,9 @@ public: OS = 0, /** < Base OS image */ CDROM = 1, /** < An ISO9660 image */ DATABLOCK = 2, /** < User persistent data device */ - DATAFILE = 3 /** < Context, kernels and initrd files */ + KERNEL = 3, /** < Context, kernels and initrd files */ + RAMDISK = 4, /** < Context, kernels and initrd files */ + CONTEXT = 5 /** < Context, kernels and initrd files */ }; /** @@ -53,7 +55,9 @@ public: case OS: return "OS" ; break; case CDROM: return "CDROM" ; break; case DATABLOCK: return "DATABLOCK" ; break; - case DATAFILE: return "FILE" ; break; + case KERNEL: return "KERNEL" ; break; + case RAMDISK: return "RAMDISK" ; break; + case CONTEXT: return "CONTEXT" ; break; default: return ""; } }; diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index 2ec9df391b..e23debde4b 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -20,6 +20,7 @@ #include "VirtualMachineTemplate.h" #include "PoolSQL.h" #include "History.h" +#include "Image.h" #include "Log.h" #include "NebulaLog.h" @@ -1137,11 +1138,13 @@ private: * INITRD files. * @param os attribute of the VM template * @param base_name of the attribute "KERNEL", or "INITRD" + * @param base_type of the image attribute KERNEL, RAMDISK * @param error_str Returns the error reason, if any * @return 0 on success */ int set_os_file(VectorAttribute * os, const string& base_name, + Image::ImageType base_type, string& error_str); /** * Parse the "OS" attribute of the template by substituting diff --git a/src/cli/oneimage b/src/cli/oneimage index dfc0735f3f..e57b1dd413 100755 --- a/src/cli/oneimage +++ b/src/cli/oneimage @@ -76,12 +76,12 @@ cmd=CommandParser::CmdParser.new(ARGV) do helper.filterflag_to_i(arg) end - format(:type, 'Image type: OS, DATABLOCK or CDROM') do |arg| + format(:type, "Image type: #{Image::IMAGE_TYPES.join(", ")}") do |arg| type=arg.strip.upcase - if %w{OS DATABLOCK CDROM}.include? type + if Image::IMAGE_TYPES.include? type [0, type] else - [1, "Image type not supported. Must be OS, DATABLOCK or CDROM."] + [1, "Image type not supported. Must be #{Image::IMAGE_TYPES.join(", ")}."] end end diff --git a/src/image/Image.cc b/src/image/Image.cc index 72f355d660..0548a02797 100644 --- a/src/image/Image.cc +++ b/src/image/Image.cc @@ -128,37 +128,34 @@ int Image::insert(SqlDB *db, string& error_str) goto error_type; } - // ------------ PERSISTENT -------------------- + // ------------ PERSISTENT & PREFIX -------------------- erase_template_attribute("PERSISTENT", persistent_attr); - if ( type != DATAFILE ) + switch (type) { - TO_UPPER(persistent_attr); + case OS: + case DATABLOCK: + case CDROM: + TO_UPPER(persistent_attr); + persistent_img = (persistent_attr == "YES"); - persistent_img = (persistent_attr == "YES"); - } - else // Files are always non-persistent - { - persistent_img = false; - } + get_template_attribute("DEV_PREFIX", dev_prefix); - // ------------ PREFIX -------------------- - - if ( type != DATAFILE ) - { - get_template_attribute("DEV_PREFIX", dev_prefix); - - if( dev_prefix.empty() ) - { - SingleAttribute * dev_att = new SingleAttribute("DEV_PREFIX", + if( dev_prefix.empty() ) + { + SingleAttribute * dev_att = new SingleAttribute("DEV_PREFIX", ImagePool::default_dev_prefix()); - obj_template->set(dev_att); - } - } - else // Do not set dev_prefix for files - { - erase_template_attribute("DEV_PREFIX", dev_prefix); + obj_template->set(dev_att); + } + break; + + case KERNEL: // Files are always non-persistent with no dev_prefix + case RAMDISK: + case CONTEXT: + persistent_img = false; + erase_template_attribute("DEV_PREFIX", dev_prefix); + break; } // ------------ SIZE -------------------- @@ -586,9 +583,17 @@ int Image::set_type(string& _type) { type = DATABLOCK; } - else if ( _type == "FILE" ) + else if ( _type == "KERNEL" ) { - type = DATAFILE; + type = KERNEL; + } + else if ( _type == "RAMDISK" ) + { + type = RAMDISK; + } + else if ( _type == "CONTEXT" ) + { + type = CONTEXT; } else { @@ -651,9 +656,17 @@ Image::ImageType Image::str_to_type(string& str_type) { it = DATABLOCK; } - else if ( str_type == "FILE" ) + else if ( str_type == "KERNEL" ) { - it = DATAFILE; + it = KERNEL; + } + else if ( str_type == "RAMDISK" ) + { + it = RAMDISK; + } + else if ( str_type == "CONTEXT" ) + { + it = CONTEXT; } return it; diff --git a/src/image/ImageManagerActions.cc b/src/image/ImageManagerActions.cc index c50cd9267e..dfcc911403 100644 --- a/src/image/ImageManagerActions.cc +++ b/src/image/ImageManagerActions.cc @@ -88,15 +88,24 @@ int ImageManager::acquire_image(int vm_id, Image *img, string& error) { int rc = 0; - if ( img->get_type() == Image::DATAFILE ) + switch(img->get_type()) { - ostringstream oss; + case Image::OS: + case Image::DATABLOCK: + case Image::CDROM: + break; - oss << "Image " << img->get_oid() << " (" << img->get_name() << ") " - << "of type FILE cannot be used as DISK."; - error = oss.str(); + case Image::KERNEL: + case Image::RAMDISK: + case Image::CONTEXT: + ostringstream oss; - return -1; + oss << "Image " << img->get_oid() << " (" << img->get_name() << ") " + << "of type " << Image::type_to_str(img->get_type()) + << " cannot be used as DISK."; + + error = oss.str(); + return -1; } switch (img->get_state()) @@ -160,12 +169,20 @@ void ImageManager::release_image(int vm_id, int iid, bool failed) return; } - if ( img->get_type() == Image::DATAFILE ) + switch(img->get_type()) { - NebulaLog::log("ImM", Log::ERROR, "Trying to release a FILE image"); + case Image::OS: + case Image::DATABLOCK: + case Image::CDROM: + break; - img->unlock(); - return; + case Image::KERNEL: + case Image::RAMDISK: + case Image::CONTEXT: + NebulaLog::log("ImM", Log::ERROR, "Trying to release a KERNEL, " + "RAMDISK or CONTEXT image"); + img->unlock(); + return; } switch (img->get_state()) @@ -257,12 +274,20 @@ void ImageManager::release_cloning_image(int iid, int clone_img_id) return; } - if ( img->get_type() == Image::DATAFILE ) + switch(img->get_type()) { - NebulaLog::log("ImM", Log::ERROR, "Trying to release a cloning FILE image"); + case Image::OS: + case Image::DATABLOCK: + case Image::CDROM: + break; - img->unlock(); - return; + case Image::KERNEL: + case Image::RAMDISK: + case Image::CONTEXT: + NebulaLog::log("ImM", Log::ERROR, "Trying to release a cloning " + "KERNEL, RAMDISK or CONTEXT image"); + img->unlock(); + return; } switch (img->get_state()) @@ -709,7 +734,9 @@ int ImageManager::stat_image(Template* img_tmpl, { case Image::OS: case Image::CDROM: - case Image::DATAFILE: + case Image::KERNEL: + case Image::RAMDISK: + case Image::CONTEXT: img_tmpl->get("SOURCE", res); if (!res.empty()) diff --git a/src/image/ImagePool.cc b/src/image/ImagePool.cc index 04abb2c592..86d111bc23 100644 --- a/src/image/ImagePool.cc +++ b/src/image/ImagePool.cc @@ -105,16 +105,25 @@ int ImagePool::allocate ( img->get_template_attribute("TYPE", type); - if ( ds_type == Datastore::FILE_DS && - img->str_to_type(type) != Image::DATAFILE ) + switch (img->str_to_type(type)) { - goto error_types_missmatch_file; - } + case Image::OS: + case Image::DATABLOCK: + case Image::CDROM: + if ( ds_type != Datastore::IMAGE_DS ) + { + goto error_types_missmatch_file; + } + break; - if ( ds_type == Datastore::IMAGE_DS && - img->str_to_type(type) == Image::DATAFILE ) - { - goto error_types_missmatch_image; + case Image::KERNEL: + case Image::RAMDISK: + case Image::CONTEXT: + if ( ds_type != Datastore::FILE_DS ) + { + goto error_types_missmatch_image; + } + break; } img_aux = get(name,uid,false); @@ -197,11 +206,13 @@ error_name_length: goto error_common; error_types_missmatch_file: - oss << "Only IMAGES of type FILE can be registered in a FILE_DS datastore"; + oss << "Only IMAGES of type KERNEL, RAMDISK and CONTEXT can be registered" + " in a FILE_DS datastore"; goto error_common; error_types_missmatch_image: - oss << "IMAGES of type FILE cannot be registered in a IMAGE_DS datastore"; + oss << "IMAGES of type KERNEL, RAMDISK and CONTEXT cannot be registered" + " in a IMAGE_DS datastore"; goto error_common; error_duplicated: diff --git a/src/oca/java/src/org/opennebula/client/image/Image.java b/src/oca/java/src/org/opennebula/client/image/Image.java index f6ebf9993f..62d8185790 100644 --- a/src/oca/java/src/org/opennebula/client/image/Image.java +++ b/src/oca/java/src/org/opennebula/client/image/Image.java @@ -47,10 +47,10 @@ public class Image extends PoolElement {"init", "rdy", "used", "disa", "lock", "err", "clon", "dele", "used"}; private static final String[] IMAGE_TYPES = - {"OS", "CDROM", "DATABLOCK", "FILE"}; + {"OS", "CDROM", "DATABLOCK", "KERNEL", "RAMDISK", "CONTEXT"}; private static final String[] SHORT_IMAGE_TYPES = - {"OS", "CD", "DB", "FL"}; + {"OS", "CD", "DB", "KL", "RD", "CX"}; /** * Creates a new Image representation. diff --git a/src/oca/ruby/OpenNebula/Image.rb b/src/oca/ruby/OpenNebula/Image.rb index 6975a29f1e..e8224871f8 100644 --- a/src/oca/ruby/OpenNebula/Image.rb +++ b/src/oca/ruby/OpenNebula/Image.rb @@ -52,13 +52,15 @@ module OpenNebula "USED_PERS" => "used" } - IMAGE_TYPES=%w{OS CDROM DATABLOCK FILE} + IMAGE_TYPES=%w{OS CDROM DATABLOCK KERNEL RAMDISK CONTEXT} SHORT_IMAGE_TYPES={ "OS" => "OS", "CDROM" => "CD", "DATABLOCK" => "DB", - "FILE" => "FL" + "KERNEL" => "KL", + "RAMDISK" => "RD", + "CONTEXT" => "CX" } # Creates an Image description with just its identifier diff --git a/src/rm/RequestManagerImage.cc b/src/rm/RequestManagerImage.cc index b6caf756a3..3d32958eb6 100644 --- a/src/rm/RequestManagerImage.cc +++ b/src/rm/RequestManagerImage.cc @@ -87,12 +87,20 @@ void ImagePersistent::request_execute(xmlrpc_c::paramList const& paramList, return; } - if ( image->get_type() == Image::DATAFILE ) + switch (image->get_type()) { - failure_response(ACTION, - request_error("FILE images must be non-persistent",""), att); + case Image::OS: + case Image::DATABLOCK: + case Image::CDROM: + break; - image->unlock(); + case Image::KERNEL: + case Image::RAMDISK: + case Image::CONTEXT: + failure_response(ACTION, + request_error("KERNEL, RAMDISK and CONTEXT files must be " + "non-persistent",""), att); + image->unlock(); return; } @@ -132,6 +140,8 @@ void ImageChangeType::request_execute(xmlrpc_c::paramList const& paramList, string type = xmlrpc_c::value_string(paramList.getString(2)); int rc; + Image::ImageType itype; + Image * image; string err_msg; @@ -151,15 +161,43 @@ void ImageChangeType::request_execute(xmlrpc_c::paramList const& paramList, return; } - if ( image->get_type() == Image::DATAFILE ) - { - failure_response(ACTION, - request_error("Only FILE images can be stored in a FILE_DS" - " datastore.",""), - att); + itype = Image::str_to_type(type); - image->unlock(); - return; + switch (image->get_type()) + { + case Image::OS: + case Image::DATABLOCK: + case Image::CDROM: + if ((itype != Image::OS) && + (itype != Image::DATABLOCK)&& + (itype != Image::CDROM) ) + { + failure_response(ACTION, + request_error("Cannot change image type to an incompatible" + " type for the current datastore.",""), + att); + + image->unlock(); + return; + } + break; + + case Image::KERNEL: + case Image::RAMDISK: + case Image::CONTEXT: + if ((itype != Image::KERNEL) && + (itype != Image::RAMDISK)&& + (itype != Image::CONTEXT) ) + { + failure_response(ACTION, + request_error("Cannot change image type to an incompatible" + " type for the current datastore.",""), + att); + + image->unlock(); + return; + } + break; } rc = image->set_type(type); @@ -220,14 +258,20 @@ void ImageClone::request_execute( return; } - if ( img->get_type() == Image::DATAFILE ) + switch (img->get_type()) { - failure_response(ACTION, - allocate_error("Image of type FILE cannot be clonned"), - att); - - img->unlock(); + case Image::OS: + case Image::DATABLOCK: + case Image::CDROM: + break; + case Image::KERNEL: + case Image::RAMDISK: + case Image::CONTEXT: + failure_response(ACTION, + allocate_error("KERNEL, RAMDISK and CONTEXT files cannot be " + "clonned."), att); + img->unlock(); return; } diff --git a/src/rm/RequestManagerVirtualMachine.cc b/src/rm/RequestManagerVirtualMachine.cc index a30d61be8c..620aa432bd 100644 --- a/src/rm/RequestManagerVirtualMachine.cc +++ b/src/rm/RequestManagerVirtualMachine.cc @@ -641,11 +641,19 @@ void VirtualMachineSaveDisk::request_execute(xmlrpc_c::paramList const& paramLis return; } - if ( type == Image::DATAFILE ) + switch (type) { - failure_response(INTERNAL, - request_error("Cannot save_as image of type FILE", ""), - att); + case Image::OS: + case Image::DATABLOCK: + case Image::CDROM: + break; + + case Image::KERNEL: + case Image::RAMDISK: + case Image::CONTEXT: + failure_response(INTERNAL, + request_error("Cannot save_as image of type FILE", ""), + att); return; } diff --git a/src/vm/VirtualMachine.cc b/src/vm/VirtualMachine.cc index 13a0ef0e71..e770a97259 100644 --- a/src/vm/VirtualMachine.cc +++ b/src/vm/VirtualMachine.cc @@ -390,6 +390,7 @@ error_common: int VirtualMachine::set_os_file(VectorAttribute * os, const string& base_name, + Image::ImageType base_type, string& error_str) { vector img_ids; @@ -398,6 +399,8 @@ int VirtualMachine::set_os_file(VectorAttribute * os, ImagePool * ipool = nd.get_ipool(); Image * img = 0; + Image::ImageType type; + DatastorePool * ds_pool = nd.get_dspool(); Datastore * ds; int ds_id; @@ -437,6 +440,7 @@ int VirtualMachine::set_os_file(VectorAttribute * os, } ds_id = img->get_ds_id(); + type = img->get_type(); os->remove(base_name); @@ -446,6 +450,18 @@ int VirtualMachine::set_os_file(VectorAttribute * os, img->unlock(); + if ( type != base_type ) + { + ostringstream oss; + + oss << base_name << " needs an image of type " + << Image::type_to_str(base_type) << " and not " + << Image::type_to_str(type); + + error_str = oss.str(); + return -1; + } + ds = ds_pool->get(ds_id, true); if ( ds == 0 ) @@ -496,14 +512,14 @@ int VirtualMachine::parse_os(string& error_str) return -1; } - rc = set_os_file(os, "KERNEL", error_str); + rc = set_os_file(os, "KERNEL", Image::KERNEL, error_str); if ( rc != 0 ) { return -1; } - rc = set_os_file(os, "INITRD", error_str); + rc = set_os_file(os, "INITRD", Image::RAMDISK, error_str); if ( rc != 0 ) { @@ -608,6 +624,8 @@ int VirtualMachine::parse_context(string& error_str) ImagePool * ipool = nd.get_ipool(); Image * img = 0; + Image::ImageType type; + for ( it=img_ids.begin() ; it < img_ids.end(); it++ ) { img = ipool->get(*it, true); @@ -615,8 +633,16 @@ int VirtualMachine::parse_context(string& error_str) if ( img != 0 ) { oss_parsed << img->get_source() << " "; + type = img->get_type(); img->unlock(); + + if (type != Image::CONTEXT) + { + error_str = "Only images of type CONTEXT can be used in" + " FILE_DS attribute."; + return -1; + } } } } diff --git a/src/vm/vm_file_var_syntax.cc b/src/vm/vm_file_var_syntax.cc index 81b2869063..8eaed65093 100644 --- a/src/vm/vm_file_var_syntax.cc +++ b/src/vm/vm_file_var_syntax.cc @@ -196,14 +196,6 @@ int get_image_path(VirtualMachine * vm, img->get_permissions(perm); - if ( img->get_type() != Image::DATAFILE ) - { - error_str = "FILE variables must use images of type FILE."; - img->unlock(); - - return -1; - } - img->unlock(); AuthRequest ar(vm->get_uid(), vm->get_gid()); @@ -226,7 +218,7 @@ int get_image_path(VirtualMachine * vm, /* Line 360 of yacc.c */ -#line 230 "vm_file_var_syntax.cc" +#line 222 "vm_file_var_syntax.cc" # ifndef YY_NULL # if defined __cplusplus && 201103L <= __cplusplus @@ -279,7 +271,7 @@ extern int vm_file_var__debug; typedef union YYSTYPE { /* Line 376 of yacc.c */ -#line 178 "vm_file_var_syntax.y" +#line 170 "vm_file_var_syntax.y" char * val_str; int val_int; @@ -287,7 +279,7 @@ typedef union YYSTYPE /* Line 376 of yacc.c */ -#line 291 "vm_file_var_syntax.cc" +#line 283 "vm_file_var_syntax.cc" } YYSTYPE; # define YYSTYPE_IS_TRIVIAL 1 # define yystype YYSTYPE /* obsolescent; will be withdrawn */ @@ -327,7 +319,7 @@ int vm_file_var__parse (); /* Copy the second part of user declarations. */ /* Line 379 of yacc.c */ -#line 331 "vm_file_var_syntax.cc" +#line 323 "vm_file_var_syntax.cc" #ifdef short # undef short @@ -618,7 +610,7 @@ static const yytype_int8 yyrhs[] = /* YYRLINE[YYN] -- source line where rule number YYN was defined. */ static const yytype_uint8 yyrline[] = { - 0, 202, 202, 203, 207, 225 + 0, 194, 194, 195, 199, 217 }; #endif @@ -1626,7 +1618,7 @@ yyreduce: { case 4: /* Line 1778 of yacc.c */ -#line 208 "vm_file_var_syntax.y" +#line 200 "vm_file_var_syntax.y" { string file((yyvsp[(1) - (7)].val_str)); string var1((yyvsp[(3) - (7)].val_str)); @@ -1648,7 +1640,7 @@ yyreduce: case 5: /* Line 1778 of yacc.c */ -#line 226 "vm_file_var_syntax.y" +#line 218 "vm_file_var_syntax.y" { string file((yyvsp[(1) - (11)].val_str)); string var1((yyvsp[(3) - (11)].val_str)); @@ -1673,7 +1665,7 @@ yyreduce: /* Line 1778 of yacc.c */ -#line 1677 "vm_file_var_syntax.cc" +#line 1669 "vm_file_var_syntax.cc" default: break; } /* User semantic actions sometimes alter yychar, and that requires @@ -1912,7 +1904,7 @@ yyreturn: /* Line 2041 of yacc.c */ -#line 247 "vm_file_var_syntax.y" +#line 239 "vm_file_var_syntax.y" extern "C" void vm_file_var__error( diff --git a/src/vm/vm_file_var_syntax.h b/src/vm/vm_file_var_syntax.h index b638e005f3..a40d587583 100644 --- a/src/vm/vm_file_var_syntax.h +++ b/src/vm/vm_file_var_syntax.h @@ -1,19 +1,19 @@ /* A Bison parser, made by GNU Bison 2.6.5. */ /* Bison interface for Yacc-like parsers in C - + Copyright (C) 1984, 1989-1990, 2000-2012 Free Software Foundation, Inc. - + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program. If not, see . */ @@ -26,7 +26,7 @@ special exception, which will cause the skeleton and the resulting Bison output files to be licensed under the GNU General Public License without this special exception. - + This special exception was added by the Free Software Foundation in version 2.2 of Bison. */ @@ -63,7 +63,7 @@ extern int vm_file_var__debug; typedef union YYSTYPE { /* Line 2042 of yacc.c */ -#line 178 "vm_file_var_syntax.y" +#line 170 "vm_file_var_syntax.y" char * val_str; int val_int; diff --git a/src/vm/vm_file_var_syntax.y b/src/vm/vm_file_var_syntax.y index 4d9bd17a9d..89280c5241 100644 --- a/src/vm/vm_file_var_syntax.y +++ b/src/vm/vm_file_var_syntax.y @@ -138,14 +138,6 @@ int get_image_path(VirtualMachine * vm, img->get_permissions(perm); - if ( img->get_type() != Image::DATAFILE ) - { - error_str = "FILE variables must use images of type FILE."; - img->unlock(); - - return -1; - } - img->unlock(); AuthRequest ar(vm->get_uid(), vm->get_gid()); From 9144d9682a6793f97dca37c40097a6e46d256a90 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 5 Dec 2012 11:45:06 +0100 Subject: [PATCH 36/41] Bug #1679 : Fix order of pre/post migrate arguments --- src/tm/TransferManager.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tm/TransferManager.cc b/src/tm/TransferManager.cc index 78408eee6e..712ded9e8c 100644 --- a/src/tm/TransferManager.cc +++ b/src/tm/TransferManager.cc @@ -1553,8 +1553,8 @@ void TransferManager::migrate_transfer_command( xfr << "MIGRATE " //TM action PRE or POST to be completed by VMM driver << vm->get_tm_mad() << " " - << vm->get_hostname() << " " << vm->get_previous_hostname() << " " + << vm->get_hostname() << " " << vm->get_remote_system_dir() << " " << vm->get_oid() << " " << vm->get_ds_id() From 0d96871ca01f7813e8a886c4066eb060fa58fb4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 5 Dec 2012 13:07:09 +0100 Subject: [PATCH 37/41] Feature #1611: Add default quotas methods to Java OCA --- .../src/org/opennebula/client/OneSystem.java | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 src/oca/java/src/org/opennebula/client/OneSystem.java diff --git a/src/oca/java/src/org/opennebula/client/OneSystem.java b/src/oca/java/src/org/opennebula/client/OneSystem.java new file mode 100644 index 0000000000..d40aa83bae --- /dev/null +++ b/src/oca/java/src/org/opennebula/client/OneSystem.java @@ -0,0 +1,140 @@ +/******************************************************************************* + * 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. + ******************************************************************************/ +package org.opennebula.client; + +import java.io.ByteArrayInputStream; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; + +public class OneSystem +{ + protected Client client; + + private static final String USER_QUOTA_INFO = "userquota.info"; + private static final String USER_QUOTA_UPDATE = "userquota.update"; + private static final String GROUP_QUOTA_INFO = "groupquota.info"; + private static final String GROUP_QUOTA_UPDATE = "groupquota.update"; + + + public OneSystem(Client client) + { + this.client = client; + } + + /** + * Gets the default user quota limits + * + * @return the default user quota in case of success, Error otherwise + */ + public OneResponse getUserQuotas() + { + return client.call(USER_QUOTA_INFO); + } + + /** + * Gets the default user quota limits + * + * @return The xml root node in case of success, null otherwise + */ + public Node getUserQuotasXML() + { + OneResponse r = getUserQuotas(); + Node xml = null; + + if (r.isError()) + { + return null; + } + + try + { + DocumentBuilder builder = + DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Document doc = builder.parse( + new ByteArrayInputStream(r.getMessage().getBytes())); + + xml = doc.getDocumentElement(); + } + catch (Exception e) {} + + return xml; + } + + /** + * Sets the default user quota limits + * + * @param quota a template (XML or txt) with the new quota limits + * @return If an error occurs the error message contains the reason. + */ + public OneResponse setUserQuotas(String quota) + { + return client.call(USER_QUOTA_UPDATE, quota); + } + + /** + * Gets the default group quota limits + * + * @return the default group quota in case of success, Error otherwise + */ + public OneResponse getGroupQuotas() + { + return client.call(GROUP_QUOTA_INFO); + } + + /** + * Gets the default group quota limits + * + * @return The xml root node in case of success, null otherwise + */ + public Node getGroupQuotasXML() + { + OneResponse r = getGroupQuotas(); + Node xml = null; + + if (r.isError()) + { + return null; + } + + try + { + DocumentBuilder builder = + DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Document doc = builder.parse( + new ByteArrayInputStream(r.getMessage().getBytes())); + + xml = doc.getDocumentElement(); + } + catch (Exception e) {} + + return xml; + } + + /** + * Sets the default group quota limits + * + * @param quota a template (XML or txt) with the new quota limits + * @return If an error occurs the error message contains the reason. + */ + public OneResponse setGroupQuotas(String quota) + { + return client.call(GROUP_QUOTA_UPDATE, quota); + } +} From 1433a0780af1b03068664f45b22de7d94bce3632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 5 Dec 2012 14:49:00 +0100 Subject: [PATCH 38/41] Feature #1611: Add tests for default quotas in Java OCA --- src/oca/java/test/GroupTest.java | 46 ++++++++++++++++++++++++++------ src/oca/java/test/UserTest.java | 38 +++++++++++++++++++++++--- 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/src/oca/java/test/GroupTest.java b/src/oca/java/test/GroupTest.java index b5a098f8a2..42029b4627 100644 --- a/src/oca/java/test/GroupTest.java +++ b/src/oca/java/test/GroupTest.java @@ -1,12 +1,12 @@ /******************************************************************************* * 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. @@ -15,6 +15,10 @@ ******************************************************************************/ import static org.junit.Assert.assertTrue; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; + import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -22,8 +26,10 @@ import org.junit.BeforeClass; import org.junit.Test; import org.opennebula.client.Client; import org.opennebula.client.OneResponse; +import org.opennebula.client.OneSystem; import org.opennebula.client.group.Group; import org.opennebula.client.group.GroupPool; +import org.w3c.dom.Node; public class GroupTest { @@ -63,7 +69,7 @@ public class GroupTest { res = Group.allocate(client, group_name); - int group_id = res.isError() ? -1 : Integer.parseInt(res.getMessage()); + int group_id = res.isError() ? -1 : Integer.parseInt(res.getMessage()); group = new Group(group_id, client); } @@ -84,7 +90,7 @@ public class GroupTest res = Group.allocate(client, group_name); assertTrue( res.getErrorMessage(), !res.isError() ); - int group_id = res.isError() ? -1 : Integer.parseInt(res.getMessage()); + int group_id = res.isError() ? -1 : Integer.parseInt(res.getMessage()); group = new Group(group_id, client); @@ -130,6 +136,30 @@ public class GroupTest assertTrue( !found ); } + @Test + public void defaultqutoas() + { + OneSystem system = new OneSystem(client); + + res = system.getGroupQuotas(); + assertTrue( res.getErrorMessage(), !res.isError() ); + + res = system.setGroupQuotas("VM = [ VMS = 7, MEMORY = 0, CPU = 3]"); + assertTrue( res.getErrorMessage(), !res.isError() ); + + Node node = system.getGroupQuotasXML(); + XPathFactory factory = XPathFactory.newInstance(); + XPath xpath = factory.newXPath(); + + try + { + assertTrue( xpath.evaluate("VM_QUOTA/VM/VMS", node).equals("7") ); + } catch (XPathExpressionException e) + { + assertTrue(e.getMessage(), false); + } + } + // Commented out, secondary groups do not exist any more /* @Test @@ -185,17 +215,17 @@ public class GroupTest assertTrue( users.get("a").isPartOf( groups.get("b").id() ) ); assertFalse( users.get("a").isPartOf( groups.get("c").id() ) ); assertFalse( users.get("a").isPartOf( groups.get("d").id() ) ); - + assertFalse( users.get("b").isPartOf( groups.get("a").id() ) ); assertTrue( users.get("b").isPartOf( groups.get("b").id() ) ); assertFalse( users.get("b").isPartOf( groups.get("c").id() ) ); assertFalse( users.get("b").isPartOf( groups.get("d").id() ) ); - + assertFalse( users.get("c").isPartOf( groups.get("a").id() ) ); assertTrue( users.get("c").isPartOf( groups.get("b").id() ) ); assertTrue( users.get("c").isPartOf( groups.get("c").id() ) ); assertTrue( users.get("c").isPartOf( groups.get("d").id() ) ); - + assertFalse( users.get("d").isPartOf( groups.get("a").id() ) ); assertTrue( users.get("d").isPartOf( groups.get("b").id() ) ); assertTrue( users.get("d").isPartOf( groups.get("c").id() ) ); diff --git a/src/oca/java/test/UserTest.java b/src/oca/java/test/UserTest.java index 6594de0857..e0ee277e4a 100644 --- a/src/oca/java/test/UserTest.java +++ b/src/oca/java/test/UserTest.java @@ -1,12 +1,12 @@ /******************************************************************************* * 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. @@ -15,6 +15,10 @@ ******************************************************************************/ import static org.junit.Assert.assertTrue; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; + import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -22,8 +26,10 @@ import org.junit.BeforeClass; import org.junit.Test; import org.opennebula.client.Client; import org.opennebula.client.OneResponse; +import org.opennebula.client.OneSystem; import org.opennebula.client.user.User; import org.opennebula.client.user.UserPool; +import org.w3c.dom.Node; public class UserTest { @@ -98,7 +104,7 @@ public class UserTest { res = user.info(); assertTrue( res.getErrorMessage(), !res.isError() ); - + assertTrue( user.id() >= 0 ); assertTrue( user.getName().equals(name) ); } @@ -158,4 +164,28 @@ public class UserTest res = user.info(); assertTrue( res.getErrorMessage(), res.isError() ); } + + @Test + public void defaultqutoas() + { + OneSystem system = new OneSystem(client); + + res = system.getUserQuotas(); + assertTrue( res.getErrorMessage(), !res.isError() ); + + res = system.setUserQuotas("VM = [ VMS = 7, MEMORY = 0, CPU = 3]"); + assertTrue( res.getErrorMessage(), !res.isError() ); + + Node node = system.getUserQuotasXML(); + XPathFactory factory = XPathFactory.newInstance(); + XPath xpath = factory.newXPath(); + + try + { + assertTrue( xpath.evaluate("VM_QUOTA/VM/VMS", node).equals("7") ); + } catch (XPathExpressionException e) + { + assertTrue(e.getMessage(), false); + } + } } From bf8c8c4d68d95b6fa01491096e446cc3f4d1992b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 5 Dec 2012 16:48:56 +0100 Subject: [PATCH 39/41] Feature #1617: Minor edits, typos --- include/Datastore.h | 6 +++--- include/Image.h | 6 +++--- src/image/ImagePool.cc | 2 +- src/rm/RequestManagerImage.cc | 2 +- src/rm/RequestManagerVirtualMachine.cc | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/Datastore.h b/include/Datastore.h index fe134af97e..a4b8d4243d 100644 --- a/include/Datastore.h +++ b/include/Datastore.h @@ -40,7 +40,7 @@ public: }; /** - * Return the string representation of an DatastoreType + * Return the string representation of a DatastoreType * @param ob the type * @return the string */ @@ -56,9 +56,9 @@ public: }; /** - * Return the string representation of an DatastoreType + * Return the string representation of a DatastoreType * @param str_type string representing the DatastoreTypr - * @return the ImageType (defaults to IMAGE_DS) + * @return the DatastoreType (defaults to IMAGE_DS) */ static DatastoreType str_to_type(string& str_type); diff --git a/include/Image.h b/include/Image.h index 4cc2e0d967..d23530fadc 100644 --- a/include/Image.h +++ b/include/Image.h @@ -38,9 +38,9 @@ public: OS = 0, /** < Base OS image */ CDROM = 1, /** < An ISO9660 image */ DATABLOCK = 2, /** < User persistent data device */ - KERNEL = 3, /** < Context, kernels and initrd files */ - RAMDISK = 4, /** < Context, kernels and initrd files */ - CONTEXT = 5 /** < Context, kernels and initrd files */ + KERNEL = 3, /** < Kernel files */ + RAMDISK = 4, /** < Initrd files */ + CONTEXT = 5 /** < Context files */ }; /** diff --git a/src/image/ImagePool.cc b/src/image/ImagePool.cc index 86d111bc23..5473b188d2 100644 --- a/src/image/ImagePool.cc +++ b/src/image/ImagePool.cc @@ -212,7 +212,7 @@ error_types_missmatch_file: error_types_missmatch_image: oss << "IMAGES of type KERNEL, RAMDISK and CONTEXT cannot be registered" - " in a IMAGE_DS datastore"; + " in an IMAGE_DS datastore"; goto error_common; error_duplicated: diff --git a/src/rm/RequestManagerImage.cc b/src/rm/RequestManagerImage.cc index 3d32958eb6..6b0722c0c1 100644 --- a/src/rm/RequestManagerImage.cc +++ b/src/rm/RequestManagerImage.cc @@ -270,7 +270,7 @@ void ImageClone::request_execute( case Image::CONTEXT: failure_response(ACTION, allocate_error("KERNEL, RAMDISK and CONTEXT files cannot be " - "clonned."), att); + "cloned."), att); img->unlock(); return; } diff --git a/src/rm/RequestManagerVirtualMachine.cc b/src/rm/RequestManagerVirtualMachine.cc index 620aa432bd..016354557d 100644 --- a/src/rm/RequestManagerVirtualMachine.cc +++ b/src/rm/RequestManagerVirtualMachine.cc @@ -652,7 +652,7 @@ void VirtualMachineSaveDisk::request_execute(xmlrpc_c::paramList const& paramLis case Image::RAMDISK: case Image::CONTEXT: failure_response(INTERNAL, - request_error("Cannot save_as image of type FILE", ""), + request_error("Cannot save_as image of type " + Image::type_to_str(type), ""), att); return; } From 6951a60c77f123974cfc53cff5d0a01faad3a5bd Mon Sep 17 00:00:00 2001 From: Tino Vazquez Date: Fri, 7 Dec 2012 12:12:23 +0100 Subject: [PATCH 40/41] bug #1684: Require OpenNebula.rb in host_hook (cherry picked from commit 1a8354e427d510780263728fe3a965ae56f6f992) --- share/hooks/host_error.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/share/hooks/host_error.rb b/share/hooks/host_error.rb index fe353bfeed..a7eab104de 100755 --- a/share/hooks/host_error.rb +++ b/share/hooks/host_error.rb @@ -41,8 +41,8 @@ end $: << RUBY_LIB_LOCATION -#require 'OpenNebula' -#include OpenNebula +require 'OpenNebula' +include OpenNebula require 'getoptlong' From 95036ed62f0c6237a88032eb92a1f2b614578fb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 7 Dec 2012 15:08:43 +0100 Subject: [PATCH 41/41] Feature #1617: Add changes to DB migrator --- src/onedb/3.8.1_to_3.9.80.rb | 66 ++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/onedb/3.8.1_to_3.9.80.rb b/src/onedb/3.8.1_to_3.9.80.rb index 77555022b4..6c4f489615 100644 --- a/src/onedb/3.8.1_to_3.9.80.rb +++ b/src/onedb/3.8.1_to_3.9.80.rb @@ -112,6 +112,72 @@ module Migrator @db.run("DROP TABLE image_pool") @db.run("ALTER TABLE image_pool_new RENAME TO image_pool") + ######################################################################## + # Feature #1617 + # New datastore, 2 "files" + # DATASTORE/SYSTEM is now DATASTORE/TYPE + ######################################################################## + + @db.run "ALTER TABLE datastore_pool RENAME TO old_datastore_pool;" + @db.run "CREATE TABLE datastore_pool (oid INTEGER PRIMARY KEY, name VARCHAR(128), body TEXT, uid INTEGER, gid INTEGER, owner_u INTEGER, group_u INTEGER, other_u INTEGER, UNIQUE(name));" + + @db.fetch("SELECT * FROM old_datastore_pool") do |row| + doc = Document.new(row[:body]) + + type = "0" # IMAGE_DS + + system_elem = doc.root.delete_element("SYSTEM") + + if ( !system_elem.nil? && system_elem.text == "1" ) + type = "1" # SYSTEM_DS + end + + doc.root.add_element("TYPE").text = type + + doc.root.each_element("TEMPLATE") do |e| + e.delete_element("SYSTEM") + e.add_element("TYPE").text = type == "0" ? "IMAGE_DS" : "SYSTEM_DS" + end + + @db[:datastore_pool].insert( + :oid => row[:oid], + :name => row[:name], + :body => doc.root.to_s, + :uid => row[:uid], + :gid => row[:gid], + :owner_u => row[:owner_u], + :group_u => row[:group_u], + :other_u => row[:other_u]) + end + + @db.run "DROP TABLE old_datastore_pool;" + + + user_0_name = "oneadmin" + + @db.fetch("SELECT name FROM user_pool WHERE oid=0") do |row| + user_0_name = row[:name] + end + + group_0_name = "oneadmin" + + @db.fetch("SELECT name FROM group_pool WHERE oid=0") do |row| + group_0_name = row[:name] + end + + base_path = "/var/lib/one/datastores/2" + + @db.fetch("SELECT body FROM datastore_pool WHERE oid=0") do |row| + doc = Document.new(row[:body]) + + doc.root.each_element("BASE_PATH") do |e| + base_path = e.text + base_path[-1] = "2" + end + end + + @db.run "INSERT INTO datastore_pool VALUES(2,'files','200#{user_0_name}#{group_0_name}files110100100fsssh#{base_path}20-1',0,0,1,1,1);" + ######################################################################## # Feature #1611: Default quotas