diff --git a/include/Host.h b/include/Host.h index a1c4943cae..9d9308b7c4 100644 --- a/include/Host.h +++ b/include/Host.h @@ -293,6 +293,14 @@ public: return host_share.test(cpu,mem,disk); } + /** + * Factory method for host templates + */ + Template * get_new_template() + { + return new HostTemplate; + } + private: // ------------------------------------------------------------------------- diff --git a/include/Image.h b/include/Image.h index e90829691d..a59b6c9fac 100644 --- a/include/Image.h +++ b/include/Image.h @@ -250,6 +250,14 @@ public: */ int disk_attribute(VectorAttribute * disk, int* index, ImageType* img_type); + /** + * Factory method for image templates + */ + Template * get_new_template() + { + return new ImageTemplate; + } + private: // ------------------------------------------------------------------------- diff --git a/include/PoolObjectSQL.h b/include/PoolObjectSQL.h index 488a17157b..f573f77588 100644 --- a/include/PoolObjectSQL.h +++ b/include/PoolObjectSQL.h @@ -265,6 +265,25 @@ public: */ void set_template_error_message(const string& message); + /** + * Factory method for templates, it should be implemented + * by classes that uses templates + * @return a new template + */ + virtual Template * get_new_template() + { + return 0; + } + + /** + * Replace template for this object + * @param tmpl string representation of the template + */ +// int replace_template(const string& tmpl_str, string& error) +// { +// Template * new_template = + +// } protected: /** diff --git a/include/VMTemplate.h b/include/VMTemplate.h index 4955affc84..948d9fb5fc 100644 --- a/include/VMTemplate.h +++ b/include/VMTemplate.h @@ -65,6 +65,14 @@ public: // Template Contents // ------------------------------------------------------------------------ + /** + * Factory method for virtual machine templates + */ + Template * get_new_template() + { + return new VirtualMachineTemplate; + } + /** * Returns a copy of the VirtualMachineTemplate * @return A copy of the VirtualMachineTemplate diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index f8a27e5c67..aa76ae9f7e 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -542,6 +542,14 @@ public: * @return 0 on success. */ int parse_template_attribute(const string& attribute, string& parsed); + + /** + * Factory method for virtual machine templates + */ + Template * get_new_template() + { + return new VirtualMachineTemplate; + } // ------------------------------------------------------------------------ // States diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index 6c1dc92e3c..51ed7fd7dd 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -20,6 +20,7 @@ #include "PoolSQL.h" #include "Leases.h" +#include "VirtualNetworkTemplate.h" #include #include @@ -32,7 +33,6 @@ using namespace std; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -class VirtualNetworkTemplate; /** * The Virtual Network class. It represents a Virtual Network at manages its @@ -75,6 +75,14 @@ public: } } + /** + * Factory method for virtual network templates + */ + Template * get_new_template() + { + return new VirtualNetworkTemplate; + } + /** * Adds Leases to the virtual network (Only implemented for FIXED networks) * @param leases_template template in the form LEASES = [IP=XX, MAC=XX]. diff --git a/src/rm/RequestManagerAction.cc b/src/rm/RequestManagerAction.cc deleted file mode 100644 index dbeb091d30..0000000000 --- a/src/rm/RequestManagerAction.cc +++ /dev/null @@ -1,184 +0,0 @@ -/* -------------------------------------------------------------------------- */ -/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) */ -/* */ -/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ -/* not use this file except in compliance with the License. You may obtain */ -/* a copy of the License at */ -/* */ -/* http://www.apache.org/licenses/LICENSE-2.0 */ -/* */ -/* Unless required by applicable law or agreed to in writing, software */ -/* distributed under the License is distributed on an "AS IS" BASIS, */ -/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -/* See the License for the specific language governing permissions and */ -/* limitations under the License. */ -/* -------------------------------------------------------------------------- */ - -#include "RequestManager.h" -#include "NebulaLog.h" - -#include "Nebula.h" - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -void RequestManager::VirtualMachineAction::execute( - xmlrpc_c::paramList const& paramList, - xmlrpc_c::value * const retval) -{ - string session; - string action; - int vid; - int rc; - - int uid; - - vector arrayData; - xmlrpc_c::value_array * arrayresult; - - Nebula& nd = Nebula::instance(); - DispatchManager * dm = nd.get_dm(); - - VirtualMachine * vm; - - ostringstream oss; - - const string method_name = "VirtualMachineAction"; - - NebulaLog::log("ReM",Log::DEBUG,"VirtualMachineAction invoked"); - - session = xmlrpc_c::value_string(paramList.getString(0)); - action = xmlrpc_c::value_string(paramList.getString(1)); - vid = xmlrpc_c::value_int(paramList.getInt(2)); - - // Get the VM - vm = VirtualMachineAction::vmpool->get(vid,true); - - if ( vm == 0 ) - { - goto error_vm_get; - } - - uid = vm->get_uid(); - - vm->unlock(); - - //Authenticate the user - rc = VirtualMachineAction::upool->authenticate(session); - - if (rc == -1) - { - goto error_authenticate; - } - - //Authorize the operation - if ( rc != 0 ) // rc == 0 means oneadmin - { - AuthRequest ar(rc); - - ar.add_auth(AuthRequest::VM,vid,AuthRequest::MANAGE,uid,false); - - if (UserPool::authorize(ar) == -1) - { - goto error_authorize; - } - } - - if (action == "shutdown") - { - rc = dm->shutdown(vid); - } - else if (action == "hold") - { - rc = dm->hold(vid); - } - else if (action == "release") - { - rc = dm->release(vid); - } - else if (action == "stop") - { - rc = dm->stop(vid); - } - else if (action == "cancel") - { - rc = dm->cancel(vid); - } - else if (action == "suspend") - { - rc = dm->suspend(vid); - } - else if (action == "resume") - { - rc = dm->resume(vid); - } - else if (action == "restart") - { - rc = dm->restart(vid); - } - else if (action == "finalize") - { - rc = dm->finalize(vid); - } - else if (action == "resubmit") - { - rc = dm->resubmit(vid); - } - else - { - rc = -3; - } - - if (rc != 0) - { - goto error_operation; - } - - arrayData.push_back(xmlrpc_c::value_boolean(true)); - arrayresult = new xmlrpc_c::value_array(arrayData); - *retval = *arrayresult; - delete arrayresult; - return; - -error_operation: - if (rc == -1) - { - oss << "Virtual machine does not exist"; - } - else if ( rc == -2 ) - { - oss << "Wrong state to perform action"; - } - else if ( rc == -3 ) - { - oss << "Unknown action"; - } - goto error_common; - -error_vm_get: - oss.str(get_error(method_name, "VM", vid)); - goto error_common; - -error_authenticate: - oss.str(authenticate_error(method_name)); - goto error_common; - -error_authorize: - oss.str(authorization_error(method_name, "MANAGE", "VM", rc, vid)); - goto error_common; - -error_common: - arrayData.push_back(xmlrpc_c::value_boolean(false)); - arrayData.push_back(xmlrpc_c::value_string(oss.str())); - - xmlrpc_c::value_array arrayresult_error(arrayData); - - NebulaLog::log("ReM",Log::ERROR,oss); - - *retval = arrayresult_error; - - return; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ diff --git a/src/rm/RequestManagerMigrate.cc b/src/rm/RequestManagerMigrate.cc deleted file mode 100644 index 9761692b33..0000000000 --- a/src/rm/RequestManagerMigrate.cc +++ /dev/null @@ -1,191 +0,0 @@ -/* -------------------------------------------------------------------------- */ -/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) */ -/* */ -/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ -/* not use this file except in compliance with the License. You may obtain */ -/* a copy of the License at */ -/* */ -/* http://www.apache.org/licenses/LICENSE-2.0 */ -/* */ -/* Unless required by applicable law or agreed to in writing, software */ -/* distributed under the License is distributed on an "AS IS" BASIS, */ -/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -/* See the License for the specific language governing permissions and */ -/* limitations under the License. */ -/* -------------------------------------------------------------------------- */ - -#include "RequestManager.h" -#include "NebulaLog.h" - -#include "Nebula.h" - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -void RequestManager::VirtualMachineMigrate::execute( - xmlrpc_c::paramList const& paramList, - xmlrpc_c::value * const retval) -{ - string session; - int vid; - int hid; - int uid; - int rc; - bool live; - - string hostname; - string vmm_mad; - string tm_mad; - string vmdir; - - VirtualMachine * vm; - Host * host; - - Nebula& nd = Nebula::instance(); - DispatchManager * dm = nd.get_dm(); - - vector arrayData; - xmlrpc_c::value_array * arrayresult; - - ostringstream oss; - - const string method_name = "VirtualMachineMigrate"; - - NebulaLog::log("ReM",Log::DEBUG,"VirtualMachineMigrate invoked"); - - //Parse Arguments - session = xmlrpc_c::value_string(paramList.getString(0)); - vid = xmlrpc_c::value_int(paramList.getInt(1)); - hid = xmlrpc_c::value_int(paramList.getInt(2)); - live = xmlrpc_c::value_boolean(paramList.getBoolean(3)); - - //Get host info to migrate the VM - host = VirtualMachineMigrate::hpool->get(hid,true); - - if ( host == 0 ) - { - goto error_host_get; - } - - hostname = host->get_name(); - vmm_mad = host->get_vmm_mad(); - tm_mad = host->get_tm_mad(); - - nd.get_configuration_attribute("VM_DIR",vmdir); - - host->unlock(); - - //Get the VM and migrate it - vm = VirtualMachineMigrate::vmpool->get(vid,true); - - if ( vm == 0 ) - { - goto error_vm_get; - } - - uid = vm->get_uid(); - - // Only oneadmin or the VM owner can perform operations upon the VM - rc = VirtualMachineMigrate::upool->authenticate(session); - - if ( rc == -1) - { - goto error_authenticate; - } - - //Authorize the operation - if ( rc != 0 ) // rc == 0 means oneadmin - { - AuthRequest ar(rc); - - ar.add_auth(AuthRequest::VM,vid,AuthRequest::MANAGE,uid,false); - ar.add_auth(AuthRequest::HOST,hid,AuthRequest::USE,0,false); - - if (UserPool::authorize(ar) == -1) - { - goto error_authorize; - } - } - - if((vm->get_state() != VirtualMachine::ACTIVE) || - (vm->get_lcm_state() != VirtualMachine::RUNNING) || - (vm->hasPreviousHistory() && vm->get_previous_reason() == History::NONE)) - { - goto error_state; - } - - vm->add_history(hid,hostname,vmdir,vmm_mad,tm_mad); - - rc = VirtualMachineMigrate::vmpool->update_history(vm); - - if ( rc != 0 ) - { - goto error_history; - } - - vmpool->update(vm); //Insert last_seq in the DB - - if ( live == true ) - { - dm->live_migrate(vm); - } - else - { - dm->migrate(vm); - } - - vm->unlock(); - - // Send results to client - arrayData.push_back(xmlrpc_c::value_boolean(true)); - - arrayresult = new xmlrpc_c::value_array(arrayData); - - *retval = *arrayresult; - - delete arrayresult; - - return; - - -error_host_get: - oss.str(get_error(method_name, "HOST", hid)); - goto error_common; - -error_vm_get: - oss.str(get_error(method_name, "VM", vid)); - goto error_common; - -error_authenticate: - oss.str(authenticate_error(method_name)); - goto error_common_lock; - -error_authorize: - oss.str(authorization_error(method_name, "MANAGE", "VM", uid, vid)); - goto error_common_lock; - -error_history: - oss.str(action_error(method_name, "INSERT HISTORY", "VM", vid, rc)); - goto error_common_lock; - -error_state: - oss << action_error(method_name, "MANAGE", "VM", vid, rc) - << ". Reason: VM in wrong state."; - goto error_common_lock; - -error_common_lock: - vm->unlock(); - -error_common: - arrayData.push_back(xmlrpc_c::value_boolean(false)); - arrayData.push_back(xmlrpc_c::value_string(oss.str())); - - xmlrpc_c::value_array arrayresult_error(arrayData); - - *retval = arrayresult_error; - - return; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ diff --git a/src/rm/RequestManagerSaveDisk.cc b/src/rm/RequestManagerSaveDisk.cc deleted file mode 100644 index c4693ac9f5..0000000000 --- a/src/rm/RequestManagerSaveDisk.cc +++ /dev/null @@ -1,287 +0,0 @@ -/* -------------------------------------------------------------------------- */ -/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) */ -/* */ -/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ -/* not use this file except in compliance with the License. You may obtain */ -/* a copy of the License at */ -/* */ -/* http://www.apache.org/licenses/LICENSE-2.0 */ -/* */ -/* Unless required by applicable law or agreed to in writing, software */ -/* distributed under the License is distributed on an "AS IS" BASIS, */ -/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -/* See the License for the specific language governing permissions and */ -/* limitations under the License. */ -/* -------------------------------------------------------------------------- */ - -#include "RequestManager.h" -#include "NebulaLog.h" - -#include "Nebula.h" - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -void RequestManager::VirtualMachineSaveDisk::execute( - xmlrpc_c::paramList const& paramList, - xmlrpc_c::value * const retval) -{ - string session; - - int vm_id; - int disk_id; - int iid; - string img_name; - - int vm_owner; - - int rc; - int uid; - int gid; - string estr; - char * error_char; - string error_str; - - const string method_name = "VirtualMachineSaveDisk"; - - VirtualMachine * vm; - Image * image; - ImageTemplate * img_template; - User * user; - - Image * source_img; - int source_img_id; - bool source_img_persistent = false; - - vector arrayData; - xmlrpc_c::value_array * arrayresult; - - ostringstream oss; - - Nebula& nd = Nebula::instance(); - ImageManager * imagem = nd.get_imagem(); - - NebulaLog::log("ReM",Log::DEBUG,"VirtualMachineSaveDisk invoked"); - - //Parse Arguments - session = xmlrpc_c::value_string(paramList.getString(0)); - vm_id = xmlrpc_c::value_int(paramList.getInt(1)); - disk_id = xmlrpc_c::value_int(paramList.getInt(2)); - img_name = xmlrpc_c::value_string(paramList.getString(3)); - - //------------------------------------------------------------------------- - // Authenticate the user - //------------------------------------------------------------------------- - uid = VirtualMachineSaveDisk::upool->authenticate(session); - - if ( uid == -1 ) - { - goto error_authenticate; - } - - //------------------------------------------------------------------------- - // Check that the image does not exist & prepare the template - //------------------------------------------------------------------------- - image = VirtualMachineSaveDisk::ipool->get(img_name,uid,false); - - if ( image != 0 ) - { - goto error_image_exists; - } - - oss << "NAME= " << img_name << endl; - oss << "PUBLIC = NO " << endl; - oss << "SOURCE = - " << endl; - - img_template = new ImageTemplate; - - img_template->parse(oss.str(),&error_char); - - oss.str(""); - - //-------------------------------------------------------------------------- - // Get the VM - //-------------------------------------------------------------------------- - vm = VirtualMachineSaveDisk::vmpool->get(vm_id,true); - - if ( vm == 0 ) - { - delete img_template; - goto error_vm_get; - } - - vm_owner = vm->get_uid(); - - vm->unlock(); - - //-------------------------------------------------------------------------- - // Authorize the operation - //-------------------------------------------------------------------------- - if ( uid != 0 ) - { - AuthRequest ar(uid); - string t64; - - ar.add_auth(AuthRequest::VM,vm_id,AuthRequest::MANAGE,vm_owner,false); - ar.add_auth(AuthRequest::IMAGE, - img_template->to_xml(t64), - AuthRequest::CREATE, - uid, - false); - - if (UserPool::authorize(ar) == -1) - { - goto error_authorize; - } - } - - //-------------------------------------------------------------------------- - // Get the User Group - //-------------------------------------------------------------------------- - - user = VirtualMachineSaveDisk::upool->get(uid,true); - - if ( user == 0 ) - { - goto error_user_get; - } - - gid = user->get_gid(); - - user->unlock(); - - //-------------------------------------------------------------------------- - // Create the image - //-------------------------------------------------------------------------- - rc = VirtualMachineSaveDisk::ipool->allocate(uid, gid, img_template, - &iid,estr); - - if ( rc < 0 ) - { - goto error_allocate; - } - - oss << "Image " << img_name << " created to store disk."; - - NebulaLog::log("ReM",Log::INFO,oss); - oss.str(""); - - //-------------------------------------------------------------------------- - // Get the VM - //-------------------------------------------------------------------------- - vm = VirtualMachineSaveDisk::vmpool->get(vm_id,true); - - if ( vm == 0 ) - { - goto error_vm_get; - } - - //-------------------------------------------------------------------------- - // Check if the disk has a persistent source image - //-------------------------------------------------------------------------- - oss << "/VM/TEMPLATE/DISK[DISK_ID=" << disk_id << "]/IMAGE_ID"; - rc = vm->xpath(source_img_id, oss.str().c_str(), -1); - oss.str(""); - - if( rc == 0 ) //The disk was created from an Image - { - source_img = VirtualMachineSaveDisk::ipool->get(source_img_id, true); - - if( source_img != 0 ) //The Image still exists - { - source_img_persistent = source_img->isPersistent(); - source_img->unlock(); - - if( source_img_persistent ) - { - goto error_img_persistent; - } - } - } - - //-------------------------------------------------------------------------- - // Store image id to save the disk in the VM template - //-------------------------------------------------------------------------- - rc = vm->save_disk(disk_id, iid, error_str); - - if ( rc == -1 ) - { - goto error_vm_get_disk_id; - } - - VirtualMachineSaveDisk::vmpool->update(vm); - - vm->unlock(); - - //-------------------------------------------------------------------------- - // Send results to client - //-------------------------------------------------------------------------- - arrayData.push_back(xmlrpc_c::value_boolean(true)); - arrayData.push_back(xmlrpc_c::value_int(iid)); - - arrayresult = new xmlrpc_c::value_array(arrayData); - - *retval = *arrayresult; - - delete arrayresult; - - return; - -error_image_exists: - oss << action_error(method_name, "CREATE", "IMAGE", -2, 0); - oss << " Image " << img_name << " already exists in the repository."; - goto error_common; - -error_vm_get: - oss.str(get_error(method_name, "VM", vm_id)); - goto error_common; - -error_img_persistent: - oss << action_error(method_name, "SAVEDISK", "DISK", disk_id, 0); - oss << " Source IMAGE " << source_img_id << " is persistent."; - vm->unlock(); - goto error_common; - -error_vm_get_disk_id: - oss.str(get_error(method_name, "DISK from VM", vm_id)); - oss << " " << error_str; - oss << " Deleting Image " << img_name; - imagem->delete_image(iid); - vm->unlock(); - goto error_common; - -error_authenticate: - oss.str(authenticate_error(method_name)); - goto error_common; - -error_authorize: - oss.str(authorization_error(method_name, "MANAGE", "VM/IMAGE", uid, vm_id)); - delete img_template; - goto error_common; - -error_allocate: - oss << action_error(method_name, "CREATE", "IMAGE", -2, 0); - oss << " " << estr; - goto error_common; - -error_user_get: - oss.str(get_error(method_name, "USER", uid)); - delete img_template; - goto error_common; - -error_common: - arrayData.push_back(xmlrpc_c::value_boolean(false)); - arrayData.push_back(xmlrpc_c::value_string(oss.str())); - - NebulaLog::log("ReM",Log::ERROR,oss); - - xmlrpc_c::value_array arrayresult_error(arrayData); - - *retval = arrayresult_error; - - return; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ -