mirror of
https://github.com/OpenNebula/one.git
synced 2024-12-22 13:33:52 +03:00
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
This commit is contained in:
parent
e66508330a
commit
a3d6025224
@ -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;
|
||||
};
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
|
@ -89,8 +89,16 @@ public:
|
||||
* </template>
|
||||
* 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:
|
||||
/**
|
||||
|
@ -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
|
||||
|
@ -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 << "<VM_POOL>";
|
||||
|
||||
rc = VirtualMachine::dump(db,oss,where);
|
||||
|
||||
oss << "</VM_POOL>";
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
private:
|
||||
/**
|
||||
* Generate context file to be sourced upon VM booting
|
||||
|
@ -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*/
|
||||
|
||||
|
85
src/rm/RequestManagerPoolInfo.cc
Normal file
85
src/rm/RequestManagerPoolInfo.cc
Normal file
@ -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;
|
||||
|
||||
// <vid> of the vid to retrieve the information for
|
||||
int vid, rc;
|
||||
|
||||
ostringstream oss;
|
||||
|
||||
/* -- RPC specific vars -- */
|
||||
vector<xmlrpc_c::value> 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;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
@ -29,6 +29,7 @@ source_files=[
|
||||
'RequestManagerDeploy.cc',
|
||||
'RequestManagerMigrate.cc',
|
||||
'RequestManagerInfo.cc',
|
||||
'RequestManagerPoolInfo.cc',
|
||||
'RequestManagerHostAllocate.cc',
|
||||
'RequestManagerHostDelete.cc',
|
||||
'RequestManagerHostInfo.cc',
|
||||
|
@ -304,7 +304,7 @@ void Template::get(
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
void Template::to_xml(string& xml) const
|
||||
string& Template::to_xml(string& xml) const
|
||||
{
|
||||
multimap<string,Attribute *>::const_iterator it;
|
||||
ostringstream oss;
|
||||
@ -324,25 +324,40 @@ void Template::to_xml(string& xml) const
|
||||
oss << "</" << xml_root << ">";
|
||||
|
||||
xml = oss.str();
|
||||
|
||||
return xml;
|
||||
}
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
string& Template::to_str(string& str) const
|
||||
{
|
||||
ostringstream os;
|
||||
multimap<string,Attribute *>::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<string,Attribute *>::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;
|
||||
}
|
||||
|
||||
|
@ -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<ostringstream *>(_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 << "<VM>"
|
||||
<< "<OID>" << atoi(values[VirtualMachine::OID]) << "</OID>"
|
||||
<< "<UID>" << atoi(values[VirtualMachine::UID]) << "</UID>"
|
||||
<< "<LAST_POLL>"
|
||||
<< static_cast<time_t>(atoi(values[VirtualMachine::LAST_POLL]))
|
||||
<< "</LAST_PLOL>"
|
||||
<< "<STATE>" << atoi(values[VirtualMachine::STATE]) << "</STATE>"
|
||||
<< "<LCM_STATE>"
|
||||
<< atoi(values[VirtualMachine::LCM_STATE])
|
||||
<< "</LCM_STATE>"
|
||||
<< "<STIME>"
|
||||
<< static_cast<time_t>(atoi(values[VirtualMachine::STIME]))
|
||||
<< "</STIME>"
|
||||
<< "<ETIME>"
|
||||
<< static_cast<time_t>(atoi(values[VirtualMachine::ETIME]))
|
||||
<< "</ETIME>"
|
||||
<< "<MEMORY>" << atoi(values[VirtualMachine::MEMORY]) << "</MEMORY>"
|
||||
<< "<CPU>" << atoi(values[VirtualMachine::CPU]) << "</CPU>"
|
||||
<< "<NET_TX>" << atoi(values[VirtualMachine::NET_TX]) << "</NET_TX>"
|
||||
<< "<NET_RX>" << atoi(values[VirtualMachine::NET_RX]) << "</NET_RX>";
|
||||
|
||||
if ( values[VirtualMachine::DEPLOY_ID] != 0 )
|
||||
{
|
||||
*oss << "<DEPLOY_ID>" << values[VirtualMachine::DEPLOY_ID]
|
||||
<< "</DEPLOY_ID>";
|
||||
}
|
||||
|
||||
if (values[VirtualMachine::LIMIT] != 0)
|
||||
{
|
||||
*oss << "<HOSTNAME>" << values[VirtualMachine::LIMIT] << "</HOSTNAME>";
|
||||
}
|
||||
|
||||
*oss<< "</VM>";
|
||||
|
||||
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 << "<VM>"
|
||||
<< "<OID>" << oid << "</OID>"
|
||||
<< "<UID>" << uid << "</UID>"
|
||||
<< "<LAST_POLL>" << last_poll << "</LAST_POLL>"
|
||||
<< "<STATE>" << state << "</STATE>"
|
||||
<< "<LCM_STATE>" << lcm_state << "</LCM_STATE>"
|
||||
<< "<STIME>" << stime << "</STIME>"
|
||||
<< "<ETIME>" << etime << "</ETIME>"
|
||||
<< "<MEMORY>" << memory << "</MEMORY>"
|
||||
<< "<CPU>" << cpu << "</CPU>"
|
||||
<< "<NET_TX>" << net_tx << "</NET_TX>"
|
||||
<< "<NET_RX>" << net_rx << "</NET_RX>";
|
||||
|
||||
if ( !deploy_id.empty() != 0 )
|
||||
{
|
||||
oss << "<DEPLOY_ID>" << deploy_id << "</DEPLOY_ID>";
|
||||
}
|
||||
|
||||
oss << vm_template.to_xml(template_xml);
|
||||
oss << "</VM>";
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
Loading…
Reference in New Issue
Block a user