From a3d6025224e65b47484ab0fede42a31fbce7e5ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20S=2E=20Montero?= Date: Fri, 22 May 2009 00:46:52 +0000 Subject: [PATCH] New method to get the pool through the xml-rpc interface. Some work to have XML format and plain text for the objects. git-svn-id: http://svn.opennebula.org/one/trunk@521 3034c82b-c49b-4eb3-8279-a7acafdc01c0 --- include/RequestManager.h | 24 ++++ include/Template.h | 12 +- include/VirtualMachine.h | 40 ++++++- include/VirtualMachinePool.h | 22 ++++ src/rm/RequestManager.cc | 4 + src/rm/RequestManagerPoolInfo.cc | 85 ++++++++++++++ src/rm/SConstruct | 1 + src/template/Template.cc | 43 ++++--- src/vm/VirtualMachine.cc | 186 +++++++++++++++++++++++++++---- 9 files changed, 378 insertions(+), 39 deletions(-) create mode 100644 src/rm/RequestManagerPoolInfo.cc diff --git a/include/RequestManager.h b/include/RequestManager.h index 750e7bea0d..2e744e680c 100644 --- a/include/RequestManager.h +++ b/include/RequestManager.h @@ -271,6 +271,30 @@ private: private: VirtualMachinePool * vmpool; }; + + + /* ---------------------------------------------------------------------- */ + + class VirtualMachinePoolInfo: public xmlrpc_c::method + { + public: + VirtualMachinePoolInfo( + VirtualMachinePool * _vmpool): + vmpool(_vmpool) + { + _signature="A:si"; + _help="Returns the virtual machine pool"; + }; + + ~VirtualMachinePoolInfo(){}; + + void execute( + xmlrpc_c::paramList const& paramList, + xmlrpc_c::value * const retval); + + private: + VirtualMachinePool * vmpool; + }; /* ---------------------------------------------------------------------- */ diff --git a/include/Template.h b/include/Template.h index b8a7483905..478d222e2f 100644 --- a/include/Template.h +++ b/include/Template.h @@ -89,8 +89,16 @@ public: * * The name of the root element is set when the Template object is created * @param xml string that hold the xml template representation + * @return a reference to the generated string + */ + string& to_xml(string& xml) const; + + /** + * Writes the template in a plain text string + * @param str string that hold the template representation + * @return a reference to the generated string */ - void to_xml(string& xml) const; + string& to_str(string& str) const; /** * Sets a new attribute, the attribute MUST BE ALLOCATED IN THE HEAP, and @@ -148,7 +156,7 @@ public: string& name, int& value) const; - friend ostream& operator<<(ostream& os, Template& t); + friend ostream& operator<<(ostream& os, const Template& t); protected: /** diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index 980fe3dc5d..7b5d7e49be 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -29,6 +29,7 @@ using namespace std; extern "C" int vm_select_cb (void * _vm, int num,char ** values, char ** names); +extern "C" int vm_dump_cb (void * _oss, int num,char ** values, char ** names); /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -116,8 +117,24 @@ public: /** * Function to write a Virtual Machine in an output stream */ - friend ostream& operator<<(ostream& os, VirtualMachine& vm); + friend ostream& operator<<(ostream& os, const VirtualMachine& vm); + /** + * Function to print the VirtualMachine object into a string in + * plain text + * @param str the resulting string + * @return a reference to the generated string + */ + string& to_str(string& str) const; + + /** + * Function to print the VirtualMachine object into a string in + * XML format + * @param xml the resulting XML string + * @return a reference to the generated string + */ + string& to_xml(string& xml) const; + // ------------------------------------------------------------------------ // Dynamic Info // ------------------------------------------------------------------------ @@ -702,6 +719,12 @@ private: int num, char ** values, char ** names); + + friend int vm_dump_cb ( + void * _vm, + int num, + char ** values, + char ** names); // ************************************************************************* // Virtual Machine Attributes @@ -820,8 +843,8 @@ private: /** * Function to unmarshall a VM object, an associated classes. * @param num the number of columns read from the DB - * @para names the column names - * @para vaues the column values + * @param names the column names + * @param vaues the column values * @return 0 on success */ int unmarshall(int num, char **names, char ** values); @@ -981,6 +1004,17 @@ protected: */ virtual int update(SqliteDB * db); + /** + * Dumps the contect of a set of VirtualMachine objects in the given stream + * using XML format + * @param db pointer to the db + * @param oss the output stream + * @param where string to filter the VirtualMachine objects + * @return 0 on success + */ + static int dump(SqliteDB * db, ostringstream& oss, const string& +where); + /** * Deletes a VM from the database and all its associated information: * - History records diff --git a/include/VirtualMachinePool.h b/include/VirtualMachinePool.h index 932f35534e..3c6f6e8b0e 100644 --- a/include/VirtualMachinePool.h +++ b/include/VirtualMachinePool.h @@ -131,7 +131,29 @@ public: { VirtualMachine::bootstrap(db); }; + + /** + * Dumps the VM pool in XML format. A filter can be also added to the query + * Also the hostname where the VirtualMachine is running is added to the + * pool + * @param oss the output stream to dump the pool contents + * @param where filter for the objects, defaults to all + * + * @return 0 on success + */ + int dump(ostringstream& oss, const string& where) + { + int rc; + oss << ""; + + rc = VirtualMachine::dump(db,oss,where); + + oss << ""; + + return rc; + } + private: /** * Generate context file to be sourced upon VM booting diff --git a/src/rm/RequestManager.cc b/src/rm/RequestManager.cc index a88ef9d68a..e38c0a6c08 100644 --- a/src/rm/RequestManager.cc +++ b/src/rm/RequestManager.cc @@ -228,6 +228,9 @@ void RequestManager::register_xml_methods() xmlrpc_c::methodPtr vm_info(new RequestManager::VirtualMachineInfo(vmpool)); + + xmlrpc_c::methodPtr vm_pool_info(new + RequestManager::VirtualMachinePoolInfo(vmpool)); xmlrpc_c::methodPtr host_allocate(new RequestManager::HostAllocate(hpool)); @@ -257,6 +260,7 @@ void RequestManager::register_xml_methods() RequestManagerRegistry.addMethod("one.vmaction", vm_action); RequestManagerRegistry.addMethod("one.vmmigrate", vm_migrate); RequestManagerRegistry.addMethod("one.vmget_info", vm_info); + RequestManagerRegistry.addMethod("one.vmget_pool_info", vm_pool_info); /* Host related methods*/ diff --git a/src/rm/RequestManagerPoolInfo.cc b/src/rm/RequestManagerPoolInfo.cc new file mode 100644 index 0000000000..5a7a1f783a --- /dev/null +++ b/src/rm/RequestManagerPoolInfo.cc @@ -0,0 +1,85 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2009, Distributed Systems Architecture Group, Universidad */ +/* Complutense de Madrid (dsa-research.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 "Nebula.h" + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void RequestManager::VirtualMachinePoolInfo::execute( + xmlrpc_c::paramList const& paramList, + xmlrpc_c::value * const retval) +{ + string session; + + // of the vid to retrieve the information for + int vid, rc; + + ostringstream oss; + + /* -- RPC specific vars -- */ + vector arrayData; + xmlrpc_c::value_array * arrayresult; + + Nebula::log("ReM",Log::DEBUG,"VirtualMachinePoolInfo method invoked"); + + // Get the parameters + //TODO the session id to validate with the SessionManager + session = xmlrpc_c::value_string(paramList.getString(0)); + vid = xmlrpc_c::value_int (paramList.getInt(1)); + + // Perform the allocation in the vmpool + rc = VirtualMachinePoolInfo::vmpool->dump(oss,""); + + if ( rc != 0 ) + { + goto error_dump; + } + + // All nice, return the vm info to the client + arrayData.push_back(xmlrpc_c::value_boolean(true)); // SUCCESS + + arrayData.push_back(xmlrpc_c::value_string(oss.str())); + arrayresult = new xmlrpc_c::value_array(arrayData); + // Copy arrayresult into retval mem space + *retval = *arrayresult; + // and get rid of the original + delete arrayresult; + + return; + +error_dump: + oss << "Error getting the pool info"; + goto error_common; + +error_common: + + arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE + arrayData.push_back(xmlrpc_c::value_string(oss.str())); + + Nebula::log("ReM",Log::ERROR,oss); + + xmlrpc_c::value_array arrayresult_error(arrayData); + + *retval = arrayresult_error; + + return; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ diff --git a/src/rm/SConstruct b/src/rm/SConstruct index 0ac1fdf1bf..cbf97555d3 100644 --- a/src/rm/SConstruct +++ b/src/rm/SConstruct @@ -29,6 +29,7 @@ source_files=[ 'RequestManagerDeploy.cc', 'RequestManagerMigrate.cc', 'RequestManagerInfo.cc', + 'RequestManagerPoolInfo.cc', 'RequestManagerHostAllocate.cc', 'RequestManagerHostDelete.cc', 'RequestManagerHostInfo.cc', diff --git a/src/template/Template.cc b/src/template/Template.cc index 5b6394662b..08856aeb3b 100644 --- a/src/template/Template.cc +++ b/src/template/Template.cc @@ -304,7 +304,7 @@ void Template::get( /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -void Template::to_xml(string& xml) const +string& Template::to_xml(string& xml) const { multimap::const_iterator it; ostringstream oss; @@ -324,25 +324,40 @@ void Template::to_xml(string& xml) const oss << ""; xml = oss.str(); + + return xml; +} +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +string& Template::to_str(string& str) const +{ + ostringstream os; + multimap::const_iterator it; + string * s; + + for ( it = attributes.begin(); it!=attributes.end(); it++) + { + s = it->second->marshall(","); + + os << endl << "\t" << it->first << separator << *s; + + delete s; + } + + str = os.str(); + return str; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -ostream& operator << (ostream& os, Template& t) +ostream& operator << (ostream& os, const Template& t) { - multimap::iterator it; - string * s; - - for ( it = t.attributes.begin(); it!=t.attributes.end(); it++) - { - s = it->second->marshall(","); - - os << endl << "\t" << it->first << t.separator << *s; - - delete s; - } - + string str; + + os << t.to_str(str); + return os; } diff --git a/src/vm/VirtualMachine.cc b/src/vm/VirtualMachine.cc index 1c9607b0dc..851fc4ec7f 100644 --- a/src/vm/VirtualMachine.cc +++ b/src/vm/VirtualMachine.cc @@ -295,7 +295,7 @@ int VirtualMachine::insert(SqliteDB * db) oss << oid; value = oss.str(); - attr = new SingleAttribute("VM_ID",value); + attr = new SingleAttribute("VMID",value); vm_template.set(attr); @@ -383,6 +383,102 @@ int VirtualMachine::update(SqliteDB * db) return rc; } + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +extern "C" int vm_dump_cb ( + void * _oss, + int num, + char ** values, + char ** names) +{ + ostringstream * oss; + ostringstream dbg; + + oss = static_cast(_oss); + + if (oss == 0) + { + return -1; + } + + if ((!values[VirtualMachine::OID]) || + (!values[VirtualMachine::UID]) || + (!values[VirtualMachine::LAST_POLL]) || + (!values[VirtualMachine::TEMPLATE_ID]) || + (!values[VirtualMachine::STATE]) || + (!values[VirtualMachine::LCM_STATE]) || + (!values[VirtualMachine::STIME]) || + (!values[VirtualMachine::ETIME]) || + (!values[VirtualMachine::MEMORY]) || + (!values[VirtualMachine::CPU]) || + (!values[VirtualMachine::NET_TX]) || + (!values[VirtualMachine::NET_RX]) || + (num != VirtualMachine::LIMIT+1 )) + { + return -1; + } + + *oss << "" + << "" << atoi(values[VirtualMachine::OID]) << "" + << "" << atoi(values[VirtualMachine::UID]) << "" + << "" + << static_cast(atoi(values[VirtualMachine::LAST_POLL])) + << "" + << "" << atoi(values[VirtualMachine::STATE]) << "" + << "" + << atoi(values[VirtualMachine::LCM_STATE]) + << "" + << "" + << static_cast(atoi(values[VirtualMachine::STIME])) + << "" + << "" + << static_cast(atoi(values[VirtualMachine::ETIME])) + << "" + << "" << atoi(values[VirtualMachine::MEMORY]) << "" + << "" << atoi(values[VirtualMachine::CPU]) << "" + << "" << atoi(values[VirtualMachine::NET_TX]) << "" + << "" << atoi(values[VirtualMachine::NET_RX]) << ""; + + if ( values[VirtualMachine::DEPLOY_ID] != 0 ) + { + *oss << "" << values[VirtualMachine::DEPLOY_ID] + << ""; + } + + if (values[VirtualMachine::LIMIT] != 0) + { + *oss << "" << values[VirtualMachine::LIMIT] << ""; + } + + *oss<< ""; + + return 0; +}; + +int VirtualMachine::dump(SqliteDB * db, ostringstream& oss, const string& where) +{ + int rc; + ostringstream cmd; + + cmd << "SELECT " << VirtualMachine::table << ".*, " + << History::table << ".host_name FROM " << VirtualMachine::table + << " LEFT OUTER JOIN (SELECT vid, host_name, MAX(seq) FROM " + << History::table << " GROUP BY vid) AS " << History::table + << " ON " << VirtualMachine::table << ".oid = " + << History::table << ".vid"; + + if ( !where.empty() ) + { + cmd << " WHERE" << where; + } + + rc = db->exec(cmd,vm_dump_cb,(void *) &oss); + + return rc; +} + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -784,26 +880,76 @@ error_yy: return -1; } -/* ************************************************************************** */ -/* Virtual Machine :: Misc */ -/* ************************************************************************** */ +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ -ostream& operator<<(ostream& os, VirtualMachine& vm) +ostream& operator<<(ostream& os, const VirtualMachine& vm) { - os << "VID : " << vm.oid << endl; - os << "UID : " << vm.uid << endl; - os << "STATE : " << vm.state << endl; - os << "LCM STATE : " << vm.lcm_state << endl; - os << "DEPLOY ID : " << vm.deploy_id << endl; - os << "MEMORY : " << vm.memory << endl; - os << "CPU : " << vm.cpu << endl; - os << "LAST POLL : " << vm.last_poll << endl; - os << "START TIME : " << vm.stime << endl; - os << "STOP TIME : " << vm.etime << endl; - os << "NET TX : " << vm.net_tx << endl; - os << "NET RX : " << vm.net_rx << endl; - - os << "Template" << endl << vm.vm_template << endl; - + string vm_str; + + os << vm.to_str(vm_str); + return os; }; + +/* -------------------------------------------------------------------------- */ + +string& VirtualMachine::to_xml(string& xml) const +{ + string template_xml; + ostringstream oss; + + oss << "" + << "" << oid << "" + << "" << uid << "" + << "" << last_poll << "" + << "" << state << "" + << "" << lcm_state << "" + << "" << stime << "" + << "" << etime << "" + << "" << memory << "" + << "" << cpu << "" + << "" << net_tx << "" + << "" << net_rx << ""; + + if ( !deploy_id.empty() != 0 ) + { + oss << "" << deploy_id << ""; + } + + oss << vm_template.to_xml(template_xml); + oss << ""; + + xml = oss.str(); + + return xml; +} + +/* -------------------------------------------------------------------------- */ + +string& VirtualMachine::to_str(string& str) const +{ + string template_xml; + ostringstream oss; + + oss<< "VID : " << oid << endl + << "UID : " << uid << endl + << "STATE : " << state << endl + << "LCM STATE : " << lcm_state << endl + << "DEPLOY ID : " << deploy_id << endl + << "MEMORY : " << memory << endl + << "CPU : " << cpu << endl + << "LAST POLL : " << last_poll << endl + << "START TIME : " << stime << endl + << "STOP TIME : " << etime << endl + << "NET TX : " << net_tx << endl + << "NET RX : " << net_rx << endl + << "Template" << endl << vm_template << endl; + + str = oss.str(); + + return str; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */