diff --git a/include/RequestManager.h b/include/RequestManager.h index 89daa75b7b..0c873cf8b3 100644 --- a/include/RequestManager.h +++ b/include/RequestManager.h @@ -321,10 +321,12 @@ private: VirtualMachinePool * _vmpool, VirtualNetworkPool * _vnpool, ImagePool * _ipool, + VMTemplatePool * _tpool, UserPool * _upool): vmpool(_vmpool), vnpool(_vnpool), ipool(_ipool), + tpool(_tpool), upool(_upool) { _signature="A:ss"; @@ -340,6 +342,7 @@ private: VirtualMachinePool * vmpool; VirtualNetworkPool * vnpool; ImagePool * ipool; + VMTemplatePool * tpool; UserPool * upool; }; diff --git a/include/Template.h b/include/Template.h index d81cf4b8c2..334fbfffe9 100644 --- a/include/Template.h +++ b/include/Template.h @@ -161,8 +161,10 @@ public: * @param name the attribute name. * @param value the attribute value, an int, 0 if the attribute is not * defined or not Single + * + * @returns True if the Single attribute was found */ - virtual void get( + virtual bool get( string& name, int& value) const; diff --git a/include/VMTemplate.h b/include/VMTemplate.h index 50bf19dca5..1047f7dca2 100644 --- a/include/VMTemplate.h +++ b/include/VMTemplate.h @@ -74,6 +74,11 @@ public: // Template Contents // ------------------------------------------------------------------------ + VirtualMachineTemplate * get_template_contents() const + { + return new VirtualMachineTemplate(*template_contents); + } + /** * Gets the values of a template attribute * @param name of the attribute @@ -178,12 +183,16 @@ private: */ VirtualMachineTemplate* template_contents; - /** * Public scope of the VMTemplate */ int public_template; + /** + * Registration time + */ + time_t regtime; + // ************************************************************************* // DataBase implementation (Private) // ************************************************************************* diff --git a/src/authm/AuthManager.cc b/src/authm/AuthManager.cc index 217eca79b6..67f0b04469 100644 --- a/src/authm/AuthManager.cc +++ b/src/authm/AuthManager.cc @@ -148,7 +148,7 @@ void AuthRequest::add_auth(Object ob, switch (op) { case CREATE: - if ( ob == VM || ob == NET || ob == IMAGE ) + if ( ob == VM || ob == NET || ob == IMAGE || ob == TEMPLATE ) { auth = true; } @@ -159,7 +159,7 @@ void AuthRequest::add_auth(Object ob, break; case USE: - if (ob == NET || ob == IMAGE) + if (ob == NET || ob == IMAGE || ob == TEMPLATE) { auth = (owner == uid) || pub; } diff --git a/src/cli/onetemplate b/src/cli/onetemplate index 627a0f177d..5ba4a25014 100755 --- a/src/cli/onetemplate +++ b/src/cli/onetemplate @@ -70,14 +70,8 @@ ShowTableTemplate={ :proc => lambda {|d,e| if d["PUBLIC"].to_i == 1 then "Yes" else "No" end} }, - :runningvms => { - :name => "#VMS", - :desc => "Number of VMs currently running from this Template", - :size => 5, - :proc => lambda {|d,e| d['RUNNING_VMS'] } - }, - :default => [:id, :user, :name, :regtime, :public, :runningvms] + :default => [:id, :user, :name, :regtime, :public] } @@ -377,8 +371,6 @@ when "show" end puts str % ["PUBLIC", public_str] - puts str % ["RUNNING_VMS", template['RUNNING_VMS']] - puts print_header(str_h1,"TEMPLATE CONTENTS",false) @@ -400,7 +392,7 @@ when "delete" OpenNebula::Template.build_xml(template_id), get_one_client) - result = img_repo.delete(template) + result = template.delete if is_successful?(result) puts "Template correctly deleted" if ops[:verbose] end diff --git a/src/rm/RequestManager.cc b/src/rm/RequestManager.cc index 7defc59b91..9f2c9f64f4 100644 --- a/src/rm/RequestManager.cc +++ b/src/rm/RequestManager.cc @@ -214,7 +214,7 @@ void RequestManager::do_action( void RequestManager::register_xml_methods() { xmlrpc_c::methodPtr vm_allocate(new - RequestManager::VirtualMachineAllocate(vmpool, vnpool, ipool, upool)); + RequestManager::VirtualMachineAllocate(vmpool,vnpool,ipool,tpool,upool)); xmlrpc_c::methodPtr vm_deploy(new RequestManager::VirtualMachineDeploy(vmpool,hpool,upool)); diff --git a/src/rm/RequestManagerAllocate.cc b/src/rm/RequestManagerAllocate.cc index 6ae73f60bc..836b208574 100644 --- a/src/rm/RequestManagerAllocate.cc +++ b/src/rm/RequestManagerAllocate.cc @@ -30,10 +30,11 @@ void RequestManager::VirtualMachineAllocate::execute( string str_template; string error_str; string user_name; + string att_name; const string method_name = "VirtualMachineAllocate"; - int vid, uid; + int vid, uid, tid; int rc; ostringstream oss; @@ -43,6 +44,11 @@ void RequestManager::VirtualMachineAllocate::execute( VirtualMachineTemplate * vm_template; User * user; + VMTemplate * registered_template; + bool using_template_pool; + int template_owner; + bool template_public; + char * error_msg = 0; int num; @@ -77,11 +83,46 @@ void RequestManager::VirtualMachineAllocate::execute( goto error_parse; } + //-------------------------------------------------------------------------- + // Look for a template id + //-------------------------------------------------------------------------- + att_name = "TEMPLATE_ID"; + using_template_pool = vm_template->get(att_name, tid); + + if( using_template_pool ) + { + // Get the registered template + registered_template = VirtualMachineAllocate::tpool->get(tid, true); + + if( registered_template == 0 ) + { + goto error_template_get; + } + + delete vm_template; + + // Use the template contents + vm_template = registered_template->get_template_contents(); + template_owner = registered_template->get_uid(); + template_public = registered_template->isPublic(); + + registered_template->unlock(); + } + if ( uid != 0 ) { AuthRequest ar(uid); string t64; + if( using_template_pool ) + { + ar.add_auth(AuthRequest::TEMPLATE, + tid, + AuthRequest::USE, + template_owner, + template_public); + } + num = vm_template->get("DISK",vectors); for(int i=0; i> value; + return true; } /* -------------------------------------------------------------------------- */ diff --git a/src/vm_template/VMTemplate.cc b/src/vm_template/VMTemplate.cc index 92b1ef540a..313b8fe01e 100644 --- a/src/vm_template/VMTemplate.cc +++ b/src/vm_template/VMTemplate.cc @@ -28,7 +28,8 @@ VMTemplate::VMTemplate(int id, string _user_name, VirtualMachineTemplate * _template_contents): PoolObjectSQL(id,"",_uid,table), - user_name(_user_name) + user_name(_user_name), + regtime(time(0)) { if (_template_contents != 0) { @@ -61,7 +62,7 @@ const char * VMTemplate::db_names = "oid, name, body, uid, public"; const char * VMTemplate::db_bootstrap = "CREATE TABLE IF NOT EXISTS template_pool (oid INTEGER PRIMARY KEY, " - "name VARCHAR(256), body TEXT, uid INTEGER, public INTEGER, UNIQUE(name))"; + "name VARCHAR(256), body TEXT, uid INTEGER, public INTEGER)"; /* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */ @@ -197,6 +198,7 @@ string& VMTemplate::to_xml(string& xml) const << "" << user_name << "" << "" << name << "" << "" << public_template << "" + << "" << regtime << "" << template_contents->to_xml(template_xml) << ""; @@ -222,6 +224,7 @@ int VMTemplate::from_xml(const string& xml) rc += xpath(user_name, "/VMTEMPLATE/USERNAME", "not_found"); rc += xpath(name, "/VMTEMPLATE/NAME", "not_found"); rc += xpath(public_template,"/VMTEMPLATE/PUBLIC", 0); + rc += xpath(regtime, "/VMTEMPLATE/REGTIME", 0); // Get associated classes ObjectXML::get_nodes("/VMTEMPLATE/TEMPLATE", content);