1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-02-22 17:57:46 +03:00

Added defaults values for EC2 drivers, removed the EC2 core driver. Now,

we fully support the XML driver to extend  OpenNebula. Other minor 
changes to improve EC2.



git-svn-id: http://svn.opennebula.org/one/trunk@365 3034c82b-c49b-4eb3-8279-a7acafdc01c0
This commit is contained in:
Rubén S. Montero 2009-02-15 00:24:39 +00:00
parent dd6c5655f4
commit 23cc92c69c
7 changed files with 207 additions and 349 deletions

View File

@ -1,61 +0,0 @@
/* -------------------------------------------------------------------------- */
/* 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. */
/* -------------------------------------------------------------------------- */
#ifndef EC2_DRIVER_H_
#define EC2_DRIVER_H_
#include <map>
#include <string>
#include <sstream>
#include "VirtualMachineManagerDriver.h"
using namespace std;
/**
* EC2 Driver class implements a VM Manager Driver to interface the Amazon
* Cloud EC2.
*/
class EC2Driver : public VirtualMachineManagerDriver
{
public:
EC2Driver(
int userid,
const map<string,string> &attrs,
bool sudo,
VirtualMachinePool * pool):
VirtualMachineManagerDriver(userid, attrs,sudo,pool)
{};
~EC2Driver(){};
private:
/**
* Generates an EC2 deployment file
* @param vm pointer to a virtual machine
* @param file_name to generate the deployment description
* @return 0 on success
*/
int deployment_description(
const VirtualMachine * vm,
const string& file_name) const;
};
/* -------------------------------------------------------------------------- */
#endif /*EC2_DRIVER_H_*/

View File

@ -10,9 +10,9 @@
# VM_POLLING_INTERVAL: Time in seconds between virtual machine monitorization
#
# VM_DIR: Remote path to store the VM images, it should be shared between all
# the cluster nodes to perform live migrations. This variable is the default
# for all the hosts in the cluster.
#
# the cluster nodes to perform live migrations. This variable is the default
# for all the hosts in the cluster.
#
# PORT: Port where oned will listen for xmlrpc calls.
#
# DEBUG_LEVEL: 0 = ERROR, 1 = WARNING, 2 = INFO, 3 = DEBUG
@ -33,8 +33,8 @@ DEBUG_LEVEL=3
#-------------------------------------------------------------------------------
# NETWORK_SIZE: Here you can define the default size for the virtual networks
#
# MAC_PREFIX: Default MAC prefix to be used to create the auto-generated MAC
# addresses is defined here (this can be overrided by the Virtual Network
# MAC_PREFIX: Default MAC prefix to be used to create the auto-generated MAC
# addresses is defined here (this can be overrided by the Virtual Network
# template)
#-------------------------------------------------------------------------------
@ -45,20 +45,20 @@ MAC_PREFIX = "00:03"
#-------------------------------------------------------------------------------
# Information Driver Configuration
#-------------------------------------------------------------------------------
# You can add more information managers with different configurations but make
# You can add more information managers with different configurations but make
# sure it has different names.
#
# name : name for this information manager
#
# executable: path of the information driver executable, can be an
# executable: path of the information driver executable, can be an
# absolute path or relative to $ONE_LOCATION/lib/mads (or
# /usr/lib/one/mads/ if OpenNebula was installed in /)
# /usr/lib/one/mads/ if OpenNebula was installed in /)
#
# arguments : for the driver executable, usually a probe configuration file,
# can be an absolute path or relative to $ONE_LOCATION/etc (or
# /etc/one/ if OpenNebula was installed in /)
#
# default : default values and configuration parameters for the driver, can
# default : default values and configuration parameters for the driver, can
# be an absolute path or relative to $ONE_LOCATION/etc (or
# /etc/one/ if OpenNebula was installed in /)
#-------------------------------------------------------------------------------
@ -92,22 +92,22 @@ IM_MAD = [
#-------------------------------------------------------------------------------
# Virtualization Driver Configuration
#-------------------------------------------------------------------------------
# You can add more virtualization managers with different configurations but
# You can add more virtualization managers with different configurations but
# make sure it has different names.
#
# name : name of the virtual machine manager driver
#
# executable: path of the virtualization driver executable, can be an
# executable: path of the virtualization driver executable, can be an
# absolute path or relative to $ONE_LOCATION/lib/mads (or
# /usr/lib/one/mads/ if OpenNebula was installed in /)
# /usr/lib/one/mads/ if OpenNebula was installed in /)
#
# arguments : for the driver executable
#
# default : default values and configuration parameters for the driver, can
# default : default values and configuration parameters for the driver, can
# be an absolute path or relative to $ONE_LOCATION/etc (or
# /etc/one/ if OpenNebula was installed in /)
#
# type : driver type, supported drivers: xen, kvm, ec2
# type : driver type, supported drivers: xen, kvm, xml
#-------------------------------------------------------------------------------
VM_MAD = [
@ -128,31 +128,34 @@ VM_MAD = [
#-------------------------------------------------------------------------------
# EC2 Virtualization Driver Manager sample configuration
# arguments: default values for the EC2 driver, can be an absolute path or
# relative to $ONE_LOCATION/etc (or /etc/one/ if OpenNebula was
# installed in /).
#-------------------------------------------------------------------------------
# VM_MAD = [
# name = "vmm_ec2",
# executable = "one_vmm_ec2",
# default = "vmm_ec2/vmm_ec2.conf",
# type = "ec2" ]
# arguments = "vmm_ec2/vmm_ec2.conf",
# type = "xml" ]
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Transfer Manager Driver Configuration
#-------------------------------------------------------------------------------
# You can add more transfer managers with different configurations but make
# You can add more transfer managers with different configurations but make
# sure it has different names.
# name : name for this transfer driver
#
# executable: path of the transfer driver executable, can be an
# executable: path of the transfer driver executable, can be an
# absolute path or relative to $ONE_LOCATION/lib/mads (or
# /usr/lib/one/mads/ if OpenNebula was installed in /)
# /usr/lib/one/mads/ if OpenNebula was installed in /)
#
# arguments : for the driver executable, usually a commands configuration file
# , can be an absolute path or relative to $ONE_LOCATION/etc (or
# /etc/one/ if OpenNebula was installed in /)
#
# default : default values and configuration parameters for the driver, can
# default : default values and configuration parameters for the driver, can
# be an absolute path or relative to $ONE_LOCATION/etc (or
# /etc/one/ if OpenNebula was installed in /)
#-------------------------------------------------------------------------------

View File

@ -1,158 +0,0 @@
/* -------------------------------------------------------------------------- */
/* 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 "EC2Driver.h"
#include <sstream>
#include <fstream>
int EC2Driver::deployment_description(
const VirtualMachine * vm,
const string& file_name) const
{
ofstream file;
vector<const Attribute *> attrs;
const VectorAttribute * ec2;
string aminame = "";
string keypair = "";
string elasticip = "";
string a_ports = "";
string itype = "";
// -------------------------------------------------------------------------
file.open(file_name.c_str(), ios::out);
if (file.fail() == true)
{
goto error_file;
}
// -------------------------------------------------------------------------
if ( vm->get_template_attribute("EC2",attrs) == 0 )
{
goto error_ec2;
}
ec2 = static_cast<const VectorAttribute *>(attrs[0]);
// ------------------------------------------------------------------------
// AMI - Amazon Machine Image
// ------------------------------------------------------------------------
aminame = ec2->vector_value("AMI");
if (aminame.empty())
{
goto error_aminame;
}
file << "aminame=" << aminame << "\n" << endl;
// ------------------------------------------------------------------------
// KEY PAIR - IdRsa key pair
// ------------------------------------------------------------------------
keypair = ec2->vector_value("KEYPAIR");
if (keypair.empty())
{
get_default("EC2","KEYPAIR",keypair);
if ( keypair.empty() )
{
goto error_keypair;
}
}
file << "keypair=" << keypair << "\n" << endl;
// ------------------------------------------------------------------------
// ELASTIC IP ADDRESS
// ------------------------------------------------------------------------
elasticip = ec2->vector_value("ELASTICIP");
if (!elasticip.empty())
{
file << "elasticip=" << elasticip << "\n" << endl;
}
// ------------------------------------------------------------------------
// AUTHORIZED PORTS
// ------------------------------------------------------------------------
a_ports = ec2->vector_value("AUTHORIZEDPORTS");
if (a_ports.empty())
{
get_default("EC2","AUTHORIZEDPORTS",a_ports);
}
if (!a_ports.empty())
{
file << "authorizedports=" << a_ports << "\n" << endl;
}
// ------------------------------------------------------------------------
// INSTANCE TYPE
// ------------------------------------------------------------------------
itype = ec2->vector_value("INSTANCETYPE");
if (itype.empty())
{
get_default("EC2","INSTANCETYPE",itype);
if ( itype.empty() )
{
goto error_itype;
}
}
file << "instancetype=" << itype << "\n" << endl;
file.close();
return 0;
error_file:
vm->log("VMM", Log::ERROR, "Could not open EC2 deployment file.");
return -1;
error_ec2:
vm->log("VMM", Log::ERROR, "No EC2 attributes found.");
file.close();
return -1;
error_aminame:
vm->log("VMM", Log::ERROR, "No AMI name attribute defined.");
file.close();
return -1;
error_keypair:
vm->log("VMM", Log::ERROR, "No keypair defined and no default provided.");
file.close();
return -1;
error_itype:
vm->log("VMM", Log::ERROR, "No AMI type defined and no default provided.");
file.close();
return -1;
}

View File

@ -27,7 +27,6 @@ source_files=[
'VirtualMachineManagerDriver.cc',
'XenDriver.cc',
'LibVirtDriver.cc',
'EC2Driver.cc',
'XMLDriver.cc'
]

View File

@ -20,7 +20,6 @@
#include "XenDriver.h"
#include "XMLDriver.h"
#include "LibVirtDriver.h"
#include "EC2Driver.h"
#include <time.h>
/* ************************************************************************** */
@ -31,14 +30,14 @@ VirtualMachineManager::VirtualMachineManager(
VirtualMachinePool * _vmpool,
HostPool * _hpool,
time_t _timer_period,
time_t _poll_period,
time_t _poll_period,
vector<const Attribute*>& _mads):
MadManager(_mads),
vmpool(_vmpool),
hpool(_hpool),
timer_period(_timer_period),
poll_period(_poll_period)
{
poll_period(_poll_period)
{
am.addListener(this);
};
@ -58,11 +57,11 @@ extern "C" void * vmm_action_loop(void *arg)
vmm = static_cast<VirtualMachineManager *>(arg);
Nebula::log("VMM",Log::INFO,"Virtual Machine Manager started.");
vmm->am.loop(vmm->timer_period,0);
Nebula::log("VMM",Log::INFO,"Virtual Machine Manager stopped.");
return 0;
}
@ -81,7 +80,7 @@ int VirtualMachineManager::start()
}
Nebula::log("VMM",Log::INFO,"Starting Virtual Machine Manager...");
pthread_attr_init (&pattr);
pthread_attr_setdetachstate (&pattr, PTHREAD_CREATE_JOINABLE);
@ -130,7 +129,7 @@ void VirtualMachineManager::trigger(Actions action, int _vid)
case POLL:
aname = "POLL";
break;
case FINALIZE:
aname = ACTION_FINALIZE;
break;
@ -150,7 +149,7 @@ void VirtualMachineManager::do_action(const string &action, void * arg)
{
int vid;
ostringstream os;
if ( arg == 0)
{
if ( action != ACTION_TIMER && action != ACTION_FINALIZE )
@ -190,7 +189,7 @@ void VirtualMachineManager::do_action(const string &action, void * arg)
else if (action == "POLL")
{
poll_action(vid);
}
}
else if (action == ACTION_TIMER)
{
timer_action();
@ -198,15 +197,15 @@ void VirtualMachineManager::do_action(const string &action, void * arg)
else if (action == ACTION_FINALIZE)
{
Nebula::log("VMM",Log::INFO,"Stopping Virtual Machine Manager...");
MadManager::stop();
}
else
{
ostringstream oss;
oss << "Unknown action name: " << action;
Nebula::log("VMM", Log::ERROR, oss);
Nebula::log("VMM", Log::ERROR, oss);
}
}
@ -224,7 +223,7 @@ void VirtualMachineManager::deploy_action(int vid)
// Get the VM from the pool
vm = vmpool->get(vid,true);
if (vm == 0)
if (vm == 0)
{
return;
}
@ -233,10 +232,10 @@ void VirtualMachineManager::deploy_action(int vid)
{
goto error_history;
}
// Get the driver for this VM
vmd = get(vm->get_uid(),vm->get_vmm_mad());
if ( vmd == 0 )
{
goto error_driver;
@ -245,7 +244,7 @@ void VirtualMachineManager::deploy_action(int vid)
//Generate VM description file
os.str("");
os << "Generating deployment file: " << vm->get_deployment_file();
vm->log("VMM", Log::INFO, os);
rc = vmd->deployment_description(vm,vm->get_deployment_file());
@ -259,9 +258,9 @@ void VirtualMachineManager::deploy_action(int vid)
vmd->deploy(vid,vm->get_hostname(),vm->get_remote_deployment_file());
vm->unlock();
return;
error_history:
os.str("");
os << "deploy_action, VM has no history";
@ -275,16 +274,16 @@ error_driver:
error_file:
os.str("");
os << "deploy_action, error generating deployment file: " << vm->get_deployment_file();
goto error_common;
goto error_common;
error_common:
Nebula &ne = Nebula::instance();
LifeCycleManager * lcm = ne.get_lcm();
lcm->trigger(LifeCycleManager::DEPLOY_FAILURE, vid);
vm->log("VMM", Log::ERROR, os);
vm->unlock();
return;
}
@ -299,11 +298,11 @@ void VirtualMachineManager::save_action(
const VirtualMachineManagerDriver * vmd;
string hostname;
ostringstream os;
// Get the VM from the pool
vm = vmpool->get(vid,true);
if (vm == 0)
if (vm == 0)
{
return;
}
@ -328,7 +327,7 @@ void VirtualMachineManager::save_action(
{
goto error_previous_history;
}
hostname = vm->get_previous_hostname();
}
else
@ -359,15 +358,15 @@ error_driver:
error_previous_history:
os.str("");
os << "save_action, VM has no previous history";
error_common:
Nebula &ne = Nebula::instance();
LifeCycleManager * lcm = ne.get_lcm();
lcm->trigger(LifeCycleManager::SAVE_FAILURE, vid);
vm->log("VMM", Log::ERROR, os);
vm->unlock();
return;
}
@ -381,11 +380,11 @@ void VirtualMachineManager::shutdown_action(
VirtualMachine * vm;
const VirtualMachineManagerDriver * vmd;
ostringstream os;
// Get the VM from the pool
vm = vmpool->get(vid,true);
if (vm == 0)
if (vm == 0)
{
return;
}
@ -394,7 +393,7 @@ void VirtualMachineManager::shutdown_action(
{
goto error_history;
}
// Get the driver for this VM
vmd = get(vm->get_uid(),vm->get_vmm_mad());
@ -408,7 +407,7 @@ void VirtualMachineManager::shutdown_action(
vm->unlock();
return;
error_history:
os.str("");
os << "shutdown_action, VM has no history";
@ -424,9 +423,9 @@ error_common:
LifeCycleManager * lcm = ne.get_lcm();
lcm->trigger(LifeCycleManager::SHUTDOWN_FAILURE, vid);
vm->log("VMM", Log::ERROR, os);
vm->unlock();
return;
}
@ -440,11 +439,11 @@ void VirtualMachineManager::cancel_action(
VirtualMachine * vm;
const VirtualMachineManagerDriver * vmd;
ostringstream os;
// Get the VM from the pool
vm = vmpool->get(vid,true);
if (vm == 0)
if (vm == 0)
{
return;
}
@ -453,7 +452,7 @@ void VirtualMachineManager::cancel_action(
{
goto error_history;
}
// Get the driver for this VM
vmd = get(vm->get_uid(),vm->get_vmm_mad());
@ -464,7 +463,7 @@ void VirtualMachineManager::cancel_action(
// Invoke driver method
vmd->cancel(vid,vm->get_hostname(),vm->get_deploy_id());
vm->unlock();
return;
@ -483,9 +482,9 @@ error_common:
LifeCycleManager * lcm = ne.get_lcm();
lcm->trigger(LifeCycleManager::CANCEL_FAILURE, vid);
vm->log("VMM", Log::ERROR, os);
vm->unlock();
return;
}
@ -503,7 +502,7 @@ void VirtualMachineManager::migrate_action(
// Get the VM from the pool
vm = vmpool->get(vid,true);
if (vm == 0)
if (vm == 0)
{
return;
}
@ -512,7 +511,7 @@ void VirtualMachineManager::migrate_action(
{
goto error_history;
}
// Get the driver for this VM
vmd = get(vm->get_uid(),vm->get_vmm_mad());
@ -533,7 +532,7 @@ void VirtualMachineManager::migrate_action(
vm->get_hostname());
vm->unlock();
return;
error_history:
@ -550,17 +549,17 @@ error_previous_history:
os.str("");
os << "migrate_action, error VM has no previous history";
goto error_common;
error_common:
Nebula &ne = Nebula::instance();
LifeCycleManager * lcm = ne.get_lcm();
lcm->trigger(LifeCycleManager::DEPLOY_FAILURE, vid);
vm->log("VMM", Log::ERROR, os);
vm->unlock();
return;
return;
}
/* -------------------------------------------------------------------------- */
@ -572,11 +571,11 @@ void VirtualMachineManager::restore_action(
VirtualMachine * vm;
const VirtualMachineManagerDriver * vmd;
ostringstream os;
// Get the VM from the pool
vm = vmpool->get(vid,true);
if (vm == 0)
if (vm == 0)
{
return;
}
@ -585,7 +584,7 @@ void VirtualMachineManager::restore_action(
{
goto error_history;
}
// Get the driver for this VM
vmd = get(vm->get_uid(),vm->get_vmm_mad());
@ -596,10 +595,10 @@ void VirtualMachineManager::restore_action(
// Invoke driver method
vmd->restore(vid,vm->get_hostname(),vm->get_checkpoint_file());
vm->unlock();
vm->unlock();
return;
error_history:
os.str("");
os << "restore_action, VM has no history";
@ -615,11 +614,11 @@ error_common:
LifeCycleManager * lcm = ne.get_lcm();
lcm->trigger(LifeCycleManager::DEPLOY_FAILURE, vid);
vm->log("VMM", Log::ERROR, os);
vm->unlock();
return;
return;
}
/* -------------------------------------------------------------------------- */
@ -631,11 +630,11 @@ void VirtualMachineManager::poll_action(
VirtualMachine * vm;
const VirtualMachineManagerDriver * vmd;
ostringstream os;
// Get the VM from the pool
vm = vmpool->get(vid,true);
if (vm == 0)
if (vm == 0)
{
return;
}
@ -644,7 +643,7 @@ void VirtualMachineManager::poll_action(
{
goto error_history;
}
// Get the driver for this VM
vmd = get(vm->get_uid(),vm->get_vmm_mad());
@ -655,7 +654,7 @@ void VirtualMachineManager::poll_action(
// Invoke driver method
vmd->poll(vid,vm->get_hostname(),vm->get_deploy_id());
vm->unlock();
return;
@ -674,9 +673,9 @@ error_common:
LifeCycleManager * lcm = ne.get_lcm();
lcm->trigger(LifeCycleManager::CANCEL_FAILURE, vid);
vm->log("VMM", Log::ERROR, os);
vm->unlock();
return;
}
@ -687,7 +686,7 @@ error_common:
void VirtualMachineManager::timer_action()
{
static int mark = 0;
VirtualMachine * vm;
vector<int> oids;
vector<int>::iterator it;
@ -721,19 +720,19 @@ void VirtualMachineManager::timer_action()
if ( vm == 0 || (!vm->hasHistory()))
{
os.str("");
os << "Monitoring VM " << *it << " but it has no history.";
os << "Monitoring VM " << *it << " but it has no history.";
Nebula::log("VMM", Log::ERROR, os);
continue;
}
if ( (thetime - vm->get_last_poll()) >= poll_period )
{
os.str("");
os << "Monitoring VM " << *it << ".";
Nebula::log("VMM", Log::INFO, os);
vm->set_last_poll(thetime);
vmd = get(vm->get_uid(),vm->get_vmm_mad());
@ -745,7 +744,7 @@ void VirtualMachineManager::timer_action()
}
vmd->poll(*it,vm->get_hostname(),vm->get_deploy_id());
vmpool->update(vm);
}
@ -766,36 +765,32 @@ void VirtualMachineManager::load_mads(int uid)
string name;
string type;
VirtualMachineManagerDriver * vmm_driver = 0;
oss << "Loading Virtual Machine Manager drivers.";
Nebula::log("VMM",Log::INFO,oss);
for(i=0,oss.str("");i<mad_conf.size();i++,oss.str(""),vmm_driver=0)
{
vattr = static_cast<const VectorAttribute *>(mad_conf[i]);
name = vattr->vector_value("NAME");
type = vattr->vector_value("TYPE");
transform (type.begin(),type.end(),type.begin(),(int(*)(int))toupper);
oss << "\tLoading driver: " << name << " (" << type << ")";
oss << "\tLoading driver: " << name << " (" << type << ")";
Nebula::log("VMM", Log::INFO, oss);
if ( type == "XEN" )
{
{
vmm_driver = new XenDriver(uid, vattr->value(),(uid != 0),vmpool);
}
else if ( type == "KVM" )
{
vmm_driver = new LibVirtDriver(uid, vattr->value(),(uid != 0),vmpool,"kvm");
}
else if ( type == "EC2" )
{
vmm_driver = new EC2Driver(uid, vattr->value(),(uid != 0),vmpool);
}
else if ( type == "XML" )
{
vmm_driver = new XMLDriver(uid, vattr->value(),(uid != 0),vmpool);
@ -804,21 +799,21 @@ void VirtualMachineManager::load_mads(int uid)
{
oss.str("");
oss << "\tUnknown driver type: " << type;
Nebula::log("VMM",Log::ERROR,oss);
continue;
}
rc = add(vmm_driver);
if ( rc == 0 )
{
oss.str("");
oss.str("");
oss << "\tDriver " << name << " loaded.";
Nebula::log("VMM",Log::INFO,oss);
}
}
}

View File

@ -16,6 +16,10 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# Set up the environment for the driver #
# ---------------------------------------------------------------------------- #
EC2_LOCATION = ENV["EC2_HOME"]
if !EC2_LOCATION
@ -23,12 +27,14 @@ if !EC2_LOCATION
exit(-1)
end
ONE_LOCATION=ENV["ONE_LOCATION"]
ONE_LOCATION = ENV["ONE_LOCATION"]
if !ONE_LOCATION
RUBY_LIB_LOCATION="/usr/lib/one/ruby"
RUBY_LIB_LOCATION = "/usr/lib/one/ruby"
ETC_LOCATION = "/etc/one/"
else
RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
RUBY_LIB_LOCATION = ONE_LOCATION + "/lib/ruby"
ETC_LOCATION = ONE_LOCATION + "/etc/"
end
$: << RUBY_LIB_LOCATION
@ -38,8 +44,15 @@ require "VirtualMachineDriver"
require "CommandManager"
require "rexml/document"
# ---------------------------------------------------------------------------- #
# The main class for the EC2 driver #
# ---------------------------------------------------------------------------- #
class EC2Driver < VirtualMachineDriver
# ------------------------------------------------------------------------ #
# EC2 commands constants #
# ------------------------------------------------------------------------ #
EC2 = {
:run => "#{EC2_LOCATION}/bin/ec2-run-instances",
:terminate => "#{EC2_LOCATION}/bin/ec2-terminate-instances",
@ -48,10 +61,33 @@ class EC2Driver < VirtualMachineDriver
:authorize => "#{EC2_LOCATION}bin/ec2-authorize"
}
def initialize
# ------------------------------------------------------------------------ #
# EC2 constructor, loads defaults for the EC2Driver #
# ------------------------------------------------------------------------ #
def initialize(ec2_conf = nil)
super(15,true)
@defaults = Hash.new
if ec2_conf && File.exists?(ec2_conf)
fd = File.new(ec2_conf)
xml = REXML::Document.new fd
return if !xml || !xml.root
fd.close()
ec2 = xml.root.elements["EC2"]
return if !ec2
@defaults["KEYPAIR"] = ec2_value(ec2,"KEYPAIR")
@defaults["AUTHORIZEDPORTS"] = ec2_value(ec2,"AUTHORIZEDPORTS")
@defaults["INSTANCETYPE"] = ec2_value(ec2,"INSTANCETYPE")
end
end
# ------------------------------------------------------------------------ #
# DEPLOY action, also sets ports and ip if needed #
# ------------------------------------------------------------------------ #
def deploy(id, host, remote_dfile, not_used)
local_dfile = get_local_deployment_file(remote_dfile)
@ -80,7 +116,16 @@ class EC2Driver < VirtualMachineDriver
ports = ec2_value(ec2,"AUTHORIZEDPORTS")
type = ec2_value(ec2,"INSTANCETYPE")
deploy_cmd = "#{EC2[:run]} #{ami} -k #{keypair} -t #{type}"
if !ami
send_message(ACTION[:deploy],RESULT[:failure],id,
"Can not find AMI in deployment file #{local_dfile}")
return
end
deploy_cmd = "#{EC2[:run]} #{ami}"
deploy_cmd << " -k #{keypair}" if keypair
deploy_cmd << " -t #{type}" if keypair
deploy_exe = LocalCommand.run(deploy_cmd, log_method(id))
if deploy_exe.code != 0
@ -109,14 +154,23 @@ class EC2Driver < VirtualMachineDriver
send_message(ACTION[:deploy],RESULT[:success],id,deploy_id)
end
# ------------------------------------------------------------------------ #
# Shutdown a EC2 instance #
# ------------------------------------------------------------------------ #
def shutdown(id, host, deploy_id, not_used)
ec2_terminate(ACTION[:shutdown], id, deploy_id)
end
# ------------------------------------------------------------------------ #
# Cancel a EC2 instance #
# ------------------------------------------------------------------------ #
def cancel(id, host, deploy_id, not_used)
ec2_terminate(ACTION[:cancel], id, deploy_id)
end
# ------------------------------------------------------------------------ #
# Get info (IP, and state) for a EC2 instance #
# ------------------------------------------------------------------------ #
def poll(id, host, deploy_id, not_used)
info = "#{POLL_ATTRIBUTE[:usedmemory]}=0 " \
@ -140,13 +194,14 @@ class EC2Driver < VirtualMachineDriver
monitor_data = $1.split(/\s+/)
case monitor_data[3]
when "pending","running"
when "pending"
info << " #{POLL_ATTRIBUTE[:state]}=#{VM_STATE[:active]}"
when "running"
info << " #{POLL_ATTRIBUTE[:state]}=#{VM_STATE[:active]}" \
" IP=#{monitor_data[1]}"
when "shutting-down","terminated"
info << " #{POLL_ATTRIBUTE[:state]}=#{VM_STATE[:deleted]}"
end
info << " IP=#{monitor_data[1]}"
end
send_message(ACTION[:poll], RESULT[:success], id, info)
@ -170,11 +225,25 @@ private
def ec2_value(xml,name)
value = nil
element = xml.elements[name]
value = element.text.strip if element
value = element.text.strip if element && element.text
if !value
value = @defaults[name]
end
return value
end
end
ec2_driver = EC2Driver.new
# ---------------------------------------------------------------------------- #
# EC2Driver Main program
# ---------------------------------------------------------------------------- #
ec2_conf = ARGV[0]
if ec2_conf
ec2_conf = ETC_LOCATION + ec2_conf if ec2_conf[0] != ?/
end
ec2_driver = EC2Driver.new(ec2_conf)
ec2_driver.start_driver

View File

@ -1,10 +1,21 @@
# Default configuration attributes for the KVM driver
# (all domains will use these values as defaults)
# Valid atributes are:
# - ec2[keypair,authorizedports,instancetype]
#Example:
# EC2 = [ keypair = "gsg-keypair",
# authorizedports = 22,
# instancetype = "m1.small"]
<!--
Default configuration attributes for the EC2 driver
(all domains will use these values as defaults)
Valid atributes are:
- ec2[keypair,authorizedports,instancetype]
Use XML syntax to specify defaults, note elements are UPCASE
Example:
<VM>
<EC2>
<KEYPAIR>gsg-keypair</KEYPAIR>
<AUTHORIZEDPORTS>22</AUTHORIZEDPORTS>
<INSTANCETYPE>m1.small</INSTANCETYPE>
</EC2>
</VM>
-->
EC2 = [ instancetype="m1.small" ]
<VM>
<EC2>
<INSTANCETYPE>m1.small</INSTANCETYPE>
</EC2>
</VM>