diff --git a/include/Attribute.h b/include/Attribute.h index 23ad9af95d..42f06d8a57 100644 --- a/include/Attribute.h +++ b/include/Attribute.h @@ -355,6 +355,14 @@ public: */ void replace(const map& attr); + /** + * The attributes from vattr will be copied to this vector + * @param attr Vector attribute to merge + * @param replace True to replace existing values, false to copy values + * only if they don't exist in this vector attribute + */ + void merge(VectorAttribute* vattr, bool replace); + /** * Replace the value of the given vector attribute */ diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index e4bdd94441..f8cefde36e 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -1266,28 +1266,34 @@ public: // ------------------------------------------------------------------------ /** - * Collects information about VM DISKS - * @param max_disk_id of the VM - */ - void get_nic_info(int& max_nic_id); - - /** - * Generates a NIC attribute to be attached to the VM. - * @param tmpl Template containing a single NIC vector attribute. - * @param max_nic_id Max NIC/NIC_ID of the VM - * @param uid of the VM owner - * @param network_id returns the id of the acquired network - * @param error_str describes the error + * Gets info about the new NIC to attach * - * @return a new VectorAttribute with the DISK (should be freed if not + * @param tmpl Template containing a single NIC vector attribute. + * @param max_nic_id Returns the max NIC_ID of the VM + * @param error_str error reason, if any + * @return a new VectorAttribute with the NIC (should be freed if not * added to the template), 0 in case of error */ - static VectorAttribute * set_up_attach_nic( - int vm_id, + VectorAttribute * get_attach_nic_info( VirtualMachineTemplate * tmpl, + int& max_nic_id, + string& error_str); + + /** + * Setups the new NIC attribute to be attached to the VM. + * + * @param vm_id Id of the VM where this nic will be attached + * @param new_nic New NIC vector attribute, obtained from get_attach_nic_info + * @param max_nic_id Max NIC/NIC_ID of the VM + * @param uid of the VM owner + * @param error_str error reason, if any + * @return 0 on success, -1 otherwise + */ + static int set_up_attach_nic( + int vm_id, + VectorAttribute * new_nic, int max_nic_id, int uid, - int& network_id, string& error_str); /** @@ -1596,6 +1602,21 @@ private: */ int parse_os(string& error_str); + /** + * Attributes not allowed in NIC_DEFAULT to avoid authorization bypass and + * inconsistencies for NIC_DEFAULTS + */ + static const char * NO_NIC_DEFAULTS[]; + + static const int NUM_NO_NIC_DEFAULTS; + + /** + * Parse the "NIC_DEFAULT" attribute + * @param error_str Returns the error reason, if any + * @return 0 on success + */ + int parse_defaults(string& error_str); + /** * Parse the "CONTEXT" attribute of the template by substituting * $VARIABLE, $VARIABLE[ATTR] and $VARIABLE[ATTR, ATTR = VALUE] @@ -1644,6 +1665,13 @@ private: */ string& to_xml_extended(string& xml, int n_history) const; + /** + * Merges NIC_DEFAULT with the given NIC + * @param nic NIC to process + */ + void merge_nic_defaults(VectorAttribute* nic); + + protected: //************************************************************************** diff --git a/install.sh b/install.sh index aad2af1f6b..0ffd972103 100755 --- a/install.sh +++ b/install.sh @@ -250,12 +250,16 @@ VAR_DIRS="$VAR_LOCATION/remotes \ $VAR_LOCATION/remotes/im/xen4-probes.d \ $VAR_LOCATION/remotes/im/vmware.d \ $VAR_LOCATION/remotes/im/ec2.d \ + $VAR_LOCATION/remotes/im/sl.d \ + $VAR_LOCATION/remotes/im/az.d \ $VAR_LOCATION/remotes/vmm \ $VAR_LOCATION/remotes/vmm/kvm \ $VAR_LOCATION/remotes/vmm/xen3 \ $VAR_LOCATION/remotes/vmm/xen4 \ $VAR_LOCATION/remotes/vmm/vmware \ $VAR_LOCATION/remotes/vmm/ec2 \ + $VAR_LOCATION/remotes/vmm/sl \ + $VAR_LOCATION/remotes/vmm/az \ $VAR_LOCATION/remotes/vnm \ $VAR_LOCATION/remotes/vnm/802.1Q \ $VAR_LOCATION/remotes/vnm/dummy \ @@ -412,6 +416,8 @@ INSTALL_FILES=( IM_PROBES_XEN4_PROBES_FILES:$VAR_LOCATION/remotes/im/xen4-probes.d IM_PROBES_VMWARE_FILES:$VAR_LOCATION/remotes/im/vmware.d IM_PROBES_EC2_FILES:$VAR_LOCATION/remotes/im/ec2.d + IM_PROBES_SL_FILES:$VAR_LOCATION/remotes/im/sl.d + IM_PROBES_AZ_FILES:$VAR_LOCATION/remotes/im/az.d IM_PROBES_VERSION:$VAR_LOCATION/remotes AUTH_SSH_FILES:$VAR_LOCATION/remotes/auth/ssh AUTH_X509_FILES:$VAR_LOCATION/remotes/auth/x509 @@ -425,6 +431,8 @@ INSTALL_FILES=( VMM_EXEC_XEN4_SCRIPTS:$VAR_LOCATION/remotes/vmm/xen4 VMM_EXEC_VMWARE_SCRIPTS:$VAR_LOCATION/remotes/vmm/vmware VMM_EXEC_EC2_SCRIPTS:$VAR_LOCATION/remotes/vmm/ec2 + VMM_EXEC_SL_SCRIPTS:$VAR_LOCATION/remotes/vmm/sl + VMM_EXEC_AZ_SCRIPTS:$VAR_LOCATION/remotes/vmm/az TM_FILES:$VAR_LOCATION/remotes/tm TM_SHARED_FILES:$VAR_LOCATION/remotes/tm/shared TM_FS_LVM_FILES:$VAR_LOCATION/remotes/tm/fs_lvm @@ -576,6 +584,8 @@ INSTALL_ETC_FILES=( ETC_FILES:$ETC_LOCATION VMWARE_ETC_FILES:$ETC_LOCATION EC2_ETC_FILES:$ETC_LOCATION + SL_ETC_FILES:$ETC_LOCATION + AZ_ETC_FILES:$ETC_LOCATION VMM_EXEC_ETC_FILES:$ETC_LOCATION/vmm_exec HM_ETC_FILES:$ETC_LOCATION/hm AUTH_ETC_FILES:$ETC_LOCATION/auth @@ -788,6 +798,51 @@ VMM_EXEC_EC2_SCRIPTS="src/vmm_mad/remotes/ec2/cancel \ src/vmm_mad/remotes/ec2/shutdown \ src/vmm_mad/remotes/ec2/ec2_driver.rb" +#------------------------------------------------------------------------------ +# VMM Driver SoftLayer scripts, to be installed under $REMOTES_LOCATION/vmm/sl +#------------------------------------------------------------------------------ + +VMM_EXEC_SL_SCRIPTS="src/vmm_mad/remotes/sl/cancel \ + src/vmm_mad/remotes/sl/attach_disk \ + src/vmm_mad/remotes/sl/detach_disk \ + src/vmm_mad/remotes/sl/attach_nic \ + src/vmm_mad/remotes/sl/detach_nic \ + src/vmm_mad/remotes/sl/snapshot_create \ + src/vmm_mad/remotes/sl/snapshot_revert \ + src/vmm_mad/remotes/sl/snapshot_delete \ + src/vmm_mad/remotes/sl/deploy \ + src/vmm_mad/remotes/sl/migrate \ + src/vmm_mad/remotes/sl/restore \ + src/vmm_mad/remotes/sl/reboot \ + src/vmm_mad/remotes/sl/reset \ + src/vmm_mad/remotes/sl/save \ + src/vmm_mad/remotes/sl/poll \ + src/vmm_mad/remotes/sl/shutdown \ + src/vmm_mad/remotes/sl/sl_driver.rb" + +#------------------------------------------------------------------------------ +# VMM Driver Azure scripts, to be installed under $REMOTES_LOCATION/vmm/az +#------------------------------------------------------------------------------ + +VMM_EXEC_AZ_SCRIPTS="src/vmm_mad/remotes/az/cancel \ + src/vmm_mad/remotes/az/attach_disk \ + src/vmm_mad/remotes/az/detach_disk \ + src/vmm_mad/remotes/az/attach_nic \ + src/vmm_mad/remotes/az/detach_nic \ + src/vmm_mad/remotes/az/snapshot_create \ + src/vmm_mad/remotes/az/snapshot_revert \ + src/vmm_mad/remotes/az/snapshot_delete \ + src/vmm_mad/remotes/az/deploy \ + src/vmm_mad/remotes/az/migrate \ + src/vmm_mad/remotes/az/restore \ + src/vmm_mad/remotes/az/reboot \ + src/vmm_mad/remotes/az/reset \ + src/vmm_mad/remotes/az/save \ + src/vmm_mad/remotes/az/poll \ + src/vmm_mad/remotes/az/shutdown \ + src/vmm_mad/remotes/az/az_driver.rb" +>>>>>>> feature-2989 + #------------------------------------------------------------------------------- # Information Manager Probes, to be installed under $REMOTES_LOCATION/im #------------------------------------------------------------------------------- @@ -835,6 +890,9 @@ IM_PROBES_VMWARE_FILES="src/im_mad/remotes/vmware.d/vmware.rb" IM_PROBES_EC2_FILES="src/im_mad/remotes/ec2.d/poll" +IM_PROBES_SL_FILES="src/im_mad/remotes/sl.d/poll" +IM_PROBES_AZ_FILES="src/im_mad/remotes/az.d/poll" + IM_PROBES_VERSION="src/im_mad/remotes/VERSION" #------------------------------------------------------------------------------- @@ -1124,6 +1182,13 @@ VMWARE_ETC_FILES="src/vmm_mad/remotes/vmware/vmwarerc" EC2_ETC_FILES="src/vmm_mad/remotes/ec2/ec2_driver.conf \ src/vmm_mad/remotes/ec2/ec2_driver.default" +SL_ETC_FILES="src/vmm_mad/remotes/sl/sl_driver.conf \ + src/vmm_mad/remotes/sl/sl_driver.default" + +AZ_ETC_FILES="src/vmm_mad/remotes/az/az_driver.conf \ + src/vmm_mad/remotes/az/az_driver.default" + + #------------------------------------------------------------------------------- # Virtualization drivers config. files, to be installed under $ETC_LOCATION # - ssh, $ETC_LOCATION/vmm_exec diff --git a/share/etc/oned.conf b/share/etc/oned.conf index 1bbed45b6b..07292e29a1 100644 --- a/share/etc/oned.conf +++ b/share/etc/oned.conf @@ -312,6 +312,8 @@ IM_MAD = [ #------------------------------------------------------------------------------- # EC2 Information Driver Manager Configuration +# -r number of retries when monitoring a host +# -t number of threads, i.e. number of hosts monitored at the same time #------------------------------------------------------------------------------- #IM_MAD = [ # name = "ec2", @@ -319,6 +321,28 @@ IM_MAD = [ # arguments = "-c -t 1 -r 0 ec2" ] #------------------------------------------------------------------------------- +#------------------------------------------------------------------------------- +# SoftLayer Information Driver Manager Configuration +# -r number of retries when monitoring a host +# -t number of threads, i.e. number of hosts monitored at the same time +#------------------------------------------------------------------------------- +#IM_MAD = [ +# name = "sl", +# executable = "one_im_sh", +# arguments = "-c -t 1 -r 0 sl" ] +#------------------------------------------------------------------------------- + +#------------------------------------------------------------------------------- +# Azure Information Driver Manager Configuration +# -r number of retries when monitoring a host +# -t number of threads, i.e. number of hosts monitored at the same time +#------------------------------------------------------------------------------- +#IM_MAD = [ +# name = "az", +# executable = "one_im_sh", +# arguments = "-c -t 1 -r 0 az" ] +#------------------------------------------------------------------------------- + #------------------------------------------------------------------------------- # Dummy Information Driver Manager Configuration #------------------------------------------------------------------------------- @@ -419,6 +443,29 @@ VM_MAD = [ # type = "xml" ] #------------------------------------------------------------------------------- +#------------------------------------------------------------------------------- +# SoftLayer Virtualization Driver Manager Configuration +# -r number of retries when monitoring a host +# -t number of threads, i.e. number of actions performed at the same time +#------------------------------------------------------------------------------- +#VM_MAD = [ +# name = "sl", +# executable = "one_vmm_sh", +# arguments = "-t 15 -r 0 sl", +# type = "xml" ] +#------------------------------------------------------------------------------- + +# Azure Virtualization Driver Manager Configuration +# -r number of retries when monitoring a host +# -t number of threads, i.e. number of actions performed at the same time +#------------------------------------------------------------------------------- +#VM_MAD = [ +# name = "az", +# executable = "one_vmm_sh", +# arguments = "-t 15 -r 0 az", +# type = "xml" ] +#------------------------------------------------------------------------------- + #------------------------------------------------------------------------------- # Dummy Virtualization Driver Configuration #------------------------------------------------------------------------------- @@ -666,6 +713,9 @@ VM_RESTRICTED_ATTR = "CONTEXT/FILES" VM_RESTRICTED_ATTR = "NIC/MAC" VM_RESTRICTED_ATTR = "NIC/VLAN_ID" VM_RESTRICTED_ATTR = "NIC/BRIDGE" +VM_RESTRICTED_ATTR = "NIC_DEFAULT/MAC" +VM_RESTRICTED_ATTR = "NIC_DEFAULT/VLAN_ID" +VM_RESTRICTED_ATTR = "NIC_DEFAULT/BRIDGE" #VM_RESTRICTED_ATTR = "RANK" #VM_RESTRICTED_ATTR = "SCHED_RANK" diff --git a/share/install_gems/install_gems b/share/install_gems/install_gems index 8b779ec546..aa3fa69fd6 100755 --- a/share/install_gems/install_gems +++ b/share/install_gems/install_gems @@ -20,7 +20,7 @@ end GROUPS={ :quota => [SQLITE, 'sequel'], :sunstone => ['json', 'rack', 'sinatra', 'thin', SQLITE], - :cloud => %w{amazon-ec2 rack sinatra thin uuidtools curb json}, + :cloud => %w{softlayer_api configparser amazon-ec2 rack sinatra thin uuidtools curb json azure}, :auth_ldap => 'net-ldap', :vmware => %w{builder trollop}, :oneflow => %w{sinatra json treetop parse-cron}, diff --git a/src/common/Attribute.cc b/src/common/Attribute.cc index ba26c33992..993355d3aa 100644 --- a/src/common/Attribute.cc +++ b/src/common/Attribute.cc @@ -172,6 +172,35 @@ void VectorAttribute::replace(const map& attr) { attribute_value = attr; } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void VectorAttribute::merge(VectorAttribute* vattr, bool replace) +{ + map::const_iterator it; + map::iterator jt; + + const map& source_values = vattr->value(); + + for(it=source_values.begin(); it!=source_values.end(); it++) + { + jt = attribute_value.find(it->first); + + if (jt != attribute_value.end()) + { + if (replace) + { + attribute_value.erase(jt); + } + else + { + continue; + } + } + + attribute_value.insert(make_pair(it->first,it->second)); + } +} /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ diff --git a/src/dm/DispatchManagerActions.cc b/src/dm/DispatchManagerActions.cc index 7818a5cd1b..7f3c83f10c 100644 --- a/src/dm/DispatchManagerActions.cc +++ b/src/dm/DispatchManagerActions.cc @@ -1355,7 +1355,7 @@ int DispatchManager::attach_nic( int max_nic_id; int uid; int oid; - int network_id; + int rc; VectorAttribute * nic; @@ -1387,7 +1387,15 @@ int DispatchManager::attach_nic( return -1; } - vm->get_nic_info(max_nic_id); + nic = vm->get_attach_nic_info(tmpl, max_nic_id, error_str); + + if ( nic == 0 ) + { + vm->unlock(); + + NebulaLog::log("DiM", Log::ERROR, error_str); + return -1; + } vm->set_state(VirtualMachine::HOTPLUG_NIC); @@ -1400,17 +1408,21 @@ int DispatchManager::attach_nic( vm->unlock(); - nic = VirtualMachine::set_up_attach_nic(oid, - tmpl, - max_nic_id, - uid, - network_id, - error_str); + rc = VirtualMachine::set_up_attach_nic(oid, + nic, + max_nic_id, + uid, + error_str); vm = vmpool->get(vid, true); if ( vm == 0 ) { - VirtualMachine::release_network_leases(nic, vid); + delete nic; + + if ( rc == 0 ) + { + VirtualMachine::release_network_leases(nic, vid); + } oss << "Could not attach a new NIC to VM " << vid << ", VM does not exist after setting its state to HOTPLUG." ; @@ -1421,8 +1433,10 @@ int DispatchManager::attach_nic( return -1; } - if ( nic == 0 ) + if ( rc != 0 ) { + delete nic; + vm->set_state(VirtualMachine::RUNNING); vmpool->update(vm); @@ -1430,6 +1444,7 @@ int DispatchManager::attach_nic( vm->unlock(); NebulaLog::log("DiM", Log::ERROR, error_str); + return -1; } else diff --git a/src/im_mad/remotes/az.d/poll b/src/im_mad/remotes/az.d/poll new file mode 100755 index 0000000000..55f3a234be --- /dev/null +++ b/src/im_mad/remotes/az.d/poll @@ -0,0 +1,27 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2010-2014, C12G Labs S.L # +# # +# 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. # +# -------------------------------------------------------------------------- # + +$: << File.join(File.dirname(__FILE__), '../../vmm/az') + +require 'az_driver' + +host = ARGV[-1] +az_drv = AzureDriver.new(host) + +az_drv.monitor_all_vms + diff --git a/src/im_mad/remotes/sl.d/poll b/src/im_mad/remotes/sl.d/poll new file mode 100755 index 0000000000..78eca8523b --- /dev/null +++ b/src/im_mad/remotes/sl.d/poll @@ -0,0 +1,26 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2010-2014, C12G Labs S.L # +# # +# 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. # +# -------------------------------------------------------------------------- # + +$: << File.join(File.dirname(__FILE__), '../../vmm/sl') + +require 'sl_driver' + +host = ARGV[-1] +sl_drv = SLDriver.new(host) + +sl_drv.monitor_all_vms diff --git a/src/sunstone/public/js/plugins/templates-tab.js b/src/sunstone/public/js/plugins/templates-tab.js index 6a5fb2d3da..81776c304e 100644 --- a/src/sunstone/public/js/plugins/templates-tab.js +++ b/src/sunstone/public/js/plugins/templates-tab.js @@ -1830,6 +1830,14 @@ function wizard_tab_content(){ '
'+ ''+ '
'+ + '
'+ + '
'+ + ''+ + ''+ + '
'+ + '
'+ '
'+ ''; } @@ -3618,6 +3626,13 @@ function initialize_create_template_dialog(dialog) { } }); + var default_model = $('#DEFAULT_MODEL', dialog).val(); + if (default_model){ + vm_json["NIC_DEFAULT"] = { + "MODEL": default_model + }; + } + // // GRAPHICS // @@ -4017,6 +4032,14 @@ function fillTemplatePopUp(template, dialog){ delete template.NIC } + var nic_default = template.NIC_DEFAULT + if (nic_default != undefined) { + if (nic_default.MODEL) { + $('#DEFAULT_MODEL', dialog).val(nic_default.MODEL); + } + + delete template.NIC_DEFAULT; + } // // OS diff --git a/src/vm/VirtualMachine.cc b/src/vm/VirtualMachine.cc index 7988454415..c3b5b9bfec 100644 --- a/src/vm/VirtualMachine.cc +++ b/src/vm/VirtualMachine.cc @@ -115,6 +115,11 @@ const char * VirtualMachine::monit_db_bootstrap = "CREATE TABLE IF NOT EXISTS " "vm_monitoring (vmid INTEGER, last_poll INTEGER, body MEDIUMTEXT, " "PRIMARY KEY(vmid, last_poll))"; +const char * VirtualMachine::NO_NIC_DEFAULTS[] = {"NETWORK_ID", "NETWORK", + "NETWORK_UID", "NETWORK_UNAME"}; + +const int VirtualMachine::NUM_NO_NIC_DEFAULTS = 4; + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -332,6 +337,17 @@ int VirtualMachine::insert(SqlDB * db, string& error_str) goto error_os; } + // ------------------------------------------------------------------------ + // Parse the defaults to merge + // ------------------------------------------------------------------------ + + rc = parse_defaults(error_str); + + if ( rc != 0 ) + { + goto error_defaults; + } + // ------------------------------------------------------------------------ // Get network leases // ------------------------------------------------------------------------ @@ -433,6 +449,7 @@ error_memory: goto error_common; error_os: +error_defaults: error_name: error_common: NebulaLog::log("ONE",Log::ERROR, error_str); @@ -612,6 +629,57 @@ int VirtualMachine::parse_os(string& error_str) return 0; } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int VirtualMachine::parse_defaults(string& error_str) +{ + int num; + + vector attr; + VectorAttribute* vatt = 0; + + num = user_obj_template->remove("NIC_DEFAULT", attr); + + if ( num == 0 ) + { + return 0; + } + + if ( num > 1 ) + { + error_str = "Only one NIC_DEFAULT attribute can be defined."; + return -1; + } + + vatt = dynamic_cast(attr[0]); + + if ( vatt == 0 ) + { + error_str = "Wrong format for NIC_DEFAULT attribute."; + return -1; + } + + for (int i=0; i < NUM_NO_NIC_DEFAULTS; i++) + { + if(vatt->vector_value(NO_NIC_DEFAULTS[i]) != "") + { + ostringstream oss; + oss << "Attribute " << NO_NIC_DEFAULTS[i] + << " is not allowed inside NIC_DEFAULT."; + + error_str = oss.str(); + + return -1; + } + } + + obj_template->set(vatt); + + return 0; +} + + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -2121,7 +2189,10 @@ long long VirtualMachine::get_volatile_disk_size(Template * tmpl) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -void VirtualMachine::get_nic_info(int& max_nic_id) +VectorAttribute * VirtualMachine::get_attach_nic_info( + VirtualMachineTemplate * tmpl, + int& max_nic_id, + string& error_str) { vector nics; VectorAttribute * nic; @@ -2129,6 +2200,10 @@ void VirtualMachine::get_nic_info(int& max_nic_id) int nic_id; int num_nics; + // ------------------------------------------------------------------------- + // Get the highest NIC_ID + // ------------------------------------------------------------------------- + max_nic_id = -1; num_nics = obj_template->get("NIC", nics); @@ -2149,46 +2224,46 @@ void VirtualMachine::get_nic_info(int& max_nic_id) max_nic_id = nic_id; } } -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -VectorAttribute * VirtualMachine::set_up_attach_nic( - int vm_id, - VirtualMachineTemplate * tmpl, - int max_nic_id, - int uid, - int& network_id, - string& error_str) -{ - vector nics; - VectorAttribute * new_nic; - - Nebula& nd = Nebula::instance(); - VirtualNetworkPool* vnpool = nd.get_vnpool(); - - network_id = -1; // ------------------------------------------------------------------------- - // Get the NIC attribute from the template + // Get the new NIC attribute from the template // ------------------------------------------------------------------------- + nics.clear(); + if ( tmpl->get("NIC", nics) != 1 ) { error_str = "The template must contain one NIC attribute"; return 0; } - new_nic = dynamic_cast(nics[0]); + nic = dynamic_cast(nics[0]); - if ( new_nic == 0 ) + if ( nic == 0 ) { error_str = "Internal error parsing NIC attribute"; return 0; } - new_nic = new_nic->clone(); + nic = nic->clone(); + + merge_nic_defaults(nic); + + return nic; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int VirtualMachine::set_up_attach_nic( + int vm_id, + VectorAttribute * new_nic, + int max_nic_id, + int uid, + string& error_str) +{ + Nebula& nd = Nebula::instance(); + VirtualNetworkPool* vnpool = nd.get_vnpool(); // ------------------------------------------------------------------------- // Acquire the new network lease @@ -2198,11 +2273,10 @@ VectorAttribute * VirtualMachine::set_up_attach_nic( if ( rc == -1 ) //-2 is not using a pre-defined network { - delete new_nic; - return 0; + return -1; } - return new_nic; + return 0; } /* -------------------------------------------------------------------------- */ @@ -2554,6 +2628,8 @@ int VirtualMachine::get_network_leases(string& estr) continue; } + merge_nic_defaults(nic); + rc = vnpool->nic_attribute(nic, i, uid, oid, estr); if (rc == -1) @@ -2568,6 +2644,35 @@ int VirtualMachine::get_network_leases(string& estr) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +void VirtualMachine::merge_nic_defaults(VectorAttribute* nic) +{ + vector nics_def; + VectorAttribute * nic_def = 0; + + int num; + + num = obj_template->get("NIC_DEFAULT", nics_def); + + if (num == 0) + { + return; + } + else + { + nic_def = dynamic_cast(nics_def[0]); + + if ( nic_def == 0 ) + { + return; + } + } + + nic->merge(nic_def, false); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void VirtualMachine::release_network_leases() { string vnid; diff --git a/src/vmm/LibVirtDriverKVM.cc b/src/vmm/LibVirtDriverKVM.cc index 5fcbaa2838..2a95a0e971 100644 --- a/src/vmm/LibVirtDriverKVM.cc +++ b/src/vmm/LibVirtDriverKVM.cc @@ -103,20 +103,34 @@ int LibVirtDriver::deployment_description_kvm( const VectorAttribute * disk; const VectorAttribute * context; - string type = ""; - string target = ""; - string bus = ""; - string ro = ""; - string driver = ""; - string cache = ""; - string disk_io = ""; - string source = ""; - string clone = ""; - string ceph_host = ""; - string ceph_secret= ""; - string ceph_user = ""; - string gluster_host = ""; - string gluster_volume = ""; + string type = ""; + string target = ""; + string bus = ""; + string ro = ""; + string driver = ""; + string cache = ""; + string disk_io = ""; + string source = ""; + string clone = ""; + string ceph_host = ""; + string ceph_secret = ""; + string ceph_user = ""; + string gluster_host = ""; + string gluster_volume = ""; + + string total_bytes_sec = ""; + string read_bytes_sec = ""; + string write_bytes_sec = ""; + string total_iops_sec = ""; + string read_iops_sec = ""; + string write_iops_sec = ""; + + string default_total_bytes_sec = ""; + string default_read_bytes_sec = ""; + string default_write_bytes_sec = ""; + string default_total_iops_sec = ""; + string default_read_iops_sec = ""; + string default_write_iops_sec = ""; int disk_id; string default_driver = ""; @@ -367,21 +381,29 @@ int LibVirtDriver::deployment_description_kvm( // ------------------------------------------------------------------------ // Disks // ------------------------------------------------------------------------ - get_default("DISK","DRIVER",default_driver); + get_default("DISK", "DRIVER", default_driver); if (default_driver.empty()) { default_driver = "raw"; } - get_default("DISK","CACHE",default_driver_cache); + get_default("DISK", "CACHE", default_driver_cache); if (default_driver_cache.empty()) { default_driver_cache = "default"; } - get_default("DISK","IO",default_driver_disk_io); + get_default("DISK", "IO", default_driver_disk_io); + + get_default("DISK", "TOTAL_BYTES_SEC", default_total_bytes_sec); + get_default("DISK", "READ_BYTES_SEC", default_read_bytes_sec); + get_default("DISK", "WRITE_BYTES_SEC", default_write_bytes_sec); + get_default("DISK", "TOTAL_IOPS_SEC", default_total_iops_sec); + get_default("DISK", "READ_IOPS_SEC", default_read_iops_sec); + get_default("DISK", "WRITE_IOPS_SEC", default_write_iops_sec); + // ------------------------------------------------------------------------ num = vm->get_template_attribute("DISK",attrs); @@ -395,20 +417,59 @@ int LibVirtDriver::deployment_description_kvm( continue; } - type = disk->vector_value("TYPE"); - target = disk->vector_value("TARGET"); - ro = disk->vector_value("READONLY"); - driver = disk->vector_value("DRIVER"); - cache = disk->vector_value("CACHE"); - disk_io = disk->vector_value("IO"); - source = disk->vector_value("SOURCE"); - clone = disk->vector_value("CLONE"); - ceph_host = disk->vector_value("CEPH_HOST"); - ceph_secret = disk->vector_value("CEPH_SECRET"); - ceph_user = disk->vector_value("CEPH_USER"); + type = disk->vector_value("TYPE"); + target = disk->vector_value("TARGET"); + ro = disk->vector_value("READONLY"); + driver = disk->vector_value("DRIVER"); + cache = disk->vector_value("CACHE"); + disk_io = disk->vector_value("IO"); + source = disk->vector_value("SOURCE"); + clone = disk->vector_value("CLONE"); + + ceph_host = disk->vector_value("CEPH_HOST"); + ceph_secret = disk->vector_value("CEPH_SECRET"); + ceph_user = disk->vector_value("CEPH_USER"); + gluster_host = disk->vector_value("GLUSTER_HOST"); gluster_volume = disk->vector_value("GLUSTER_VOLUME"); + total_bytes_sec = disk->vector_value("TOTAL_BYTES_SEC"); + read_bytes_sec = disk->vector_value("READ_BYTES_SEC"); + write_bytes_sec = disk->vector_value("WRITE_BYTES_SEC"); + total_iops_sec = disk->vector_value("TOTAL_IOPS_SEC"); + read_iops_sec = disk->vector_value("READ_IOPS_SEC"); + write_iops_sec = disk->vector_value("WRITE_IOPS_SEC"); + + if ( total_bytes_sec.empty() && !default_total_bytes_sec.empty()) + { + total_bytes_sec = default_total_bytes_sec; + } + + if ( read_bytes_sec.empty() && !default_read_bytes_sec.empty()) + { + read_bytes_sec = default_read_bytes_sec; + } + + if ( write_bytes_sec.empty() && !default_write_bytes_sec.empty()) + { + write_bytes_sec = default_write_bytes_sec; + } + + if ( total_iops_sec.empty() && !default_total_iops_sec.empty()) + { + total_iops_sec = default_total_iops_sec; + } + + if ( read_iops_sec.empty() && !default_read_iops_sec.empty()) + { + read_iops_sec = default_read_iops_sec; + } + + if ( write_iops_sec.empty() && !default_write_iops_sec.empty()) + { + write_iops_sec = default_write_iops_sec; + } + disk->vector_value_str("DISK_ID", disk_id); if (target.empty()) @@ -552,7 +613,56 @@ int LibVirtDriver::deployment_description_kvm( file << " io='" << default_driver_disk_io << "'"; } - file << "/>" << endl << "\t\t" << endl; + file << "/>" << endl; + + // ---- I/O Options ---- + + if (!(total_bytes_sec.empty() && read_bytes_sec.empty() && + write_bytes_sec.empty() && total_iops_sec.empty() && + read_iops_sec.empty() && write_iops_sec.empty())) + { + file << "\t\t\t" << endl; + + if ( !total_bytes_sec.empty() ) + { + file << "\t\t\t\t" << total_bytes_sec + << "" << endl; + } + + if ( !read_bytes_sec.empty() ) + { + file << "\t\t\t\t" << read_bytes_sec + << "" << endl; + } + + if ( !write_bytes_sec.empty() ) + { + file << "\t\t\t\t" << write_bytes_sec + << "" << endl; + } + + if ( !total_iops_sec.empty() ) + { + file << "\t\t\t\t" << total_iops_sec + << "" << endl; + } + + if ( !read_iops_sec.empty() ) + { + file << "\t\t\t\t" << read_iops_sec + << "" << endl; + } + + if ( !write_iops_sec.empty() ) + { + file << "\t\t\t\t" << write_iops_sec + << "" << endl; + } + + file << "\t\t\t" << endl; + } + + file << "\t\t" << endl; } attrs.clear(); diff --git a/src/vmm_mad/exec/vmm_exec_kvm.conf b/src/vmm_mad/exec/vmm_exec_kvm.conf index 19f0a7e0ec..de32d1060f 100644 --- a/src/vmm_mad/exec/vmm_exec_kvm.conf +++ b/src/vmm_mad/exec/vmm_exec_kvm.conf @@ -21,7 +21,7 @@ # - os [kernel,initrd,boot,root,kernel_cmd,arch,machine] # - vcpu # - features [acpi, pae, apic, hyperv, localtime] -# - disk [driver, cache, io] +# - disk [driver, cache, io, total_bytes_sec, total_iops_sec, read_bytes_sec, write_bytes_sec, read_iops_sec, write_iops_sec] # - nic [filter, model] # - raw # - hyperv_options: options used for FEATURES = [ HYPERV = yes ] diff --git a/src/vmm_mad/remotes/az/attach_disk b/src/vmm_mad/remotes/az/attach_disk new file mode 100644 index 0000000000..8aeaf8eba5 --- /dev/null +++ b/src/vmm_mad/remotes/az/attach_disk @@ -0,0 +1,20 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +SCRIPT_NAME=$(basename $0) +echo "Action $SCRIPT_NAME not supported" 1>&2 diff --git a/src/vmm_mad/remotes/az/attach_nic b/src/vmm_mad/remotes/az/attach_nic new file mode 100755 index 0000000000..8aeaf8eba5 --- /dev/null +++ b/src/vmm_mad/remotes/az/attach_nic @@ -0,0 +1,20 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +SCRIPT_NAME=$(basename $0) +echo "Action $SCRIPT_NAME not supported" 1>&2 diff --git a/src/vmm_mad/remotes/az/az_driver.conf b/src/vmm_mad/remotes/az/az_driver.conf new file mode 100644 index 0000000000..eed2de0c9b --- /dev/null +++ b/src/vmm_mad/remotes/az/az_driver.conf @@ -0,0 +1,126 @@ +proxy_uri: +regions: + default: + region_name: "West Europe" + # pem_management_cert --> Absolute path to pem management certificate + # Info on creating certificates: + # http://azure.microsoft.com/en-us/documentation/articles/linux-use-ssh-key/ + pem_management_cert: + subscription_id: + managament_endpoint: + capacity: + Small: 5 + Medium: 1 + Large: 0 + west-europe: + region_name: "West Europe" + pem_management_cert: + subscription_id: + managament_endpoint: + capacity: + Small: 5 + Medium: 1 + Large: 0 + north-europe: + region_name: "North Europe" + pem_management_cert: + subscription_id: + managament_endpoint: + capacity: + Small: + Medium: + Large: + east-us: + region_name: "East US" + pem_management_cert: + subscription_id: + managament_endpoint: + capacity: + Small: + Medium: + Large: + southcentral-us: + region_name: "South Central US" + pem_management_cert: + subscription_id: + managament_endpoint: + capacity: + Small: + Medium: + Large: + west-us: + region_name: "West US" + pem_management_cert: + subscription_id: + managament_endpoint: + capacity: + Small: + Medium: + Large: + east-asia: + region_name: "East Asia" + pem_management_cert: + subscription_id: + managament_endpoint: + capacity: + Small: + Medium: + Large: + southeast-asia: + region_name: "Southeast Asia" + pem_management_cert: + subscription_id: + managament_endpoint: + capacity: + Small: + Medium: + Large: + japan-west: + region_name: "Japan West" + pem_management_cert: + subscription_id: + managament_endpoint: + capacity: + Small: + Medium: + Large: + brazil-south: + region_name: "Brazil South" + pem_management_cert: + subscription_id: + managament_endpoint: + capacity: + Small: + Medium: + Large: +instance_types: + ExtraSmall: + cpu: 0.1 + memory: 0.75 + Small: + cpu: 1 + memory: 1.75 + Medium: + cpu: 2 + memory: 3.5 + Large: + cpu: 4 + memory: 7 + ExtraLarge: + cpu: 8 + memory: 14 + A5: + cpu: 2 + memory: 14 + A6: + cpu: 4 + memory: 28 + A7: + cpu: 8 + memory: 56 + A8: + cpu: 8 + memory: 56 + A9: + cpu: 16 + memory: 112 diff --git a/src/vmm_mad/remotes/az/az_driver.default b/src/vmm_mad/remotes/az/az_driver.default new file mode 100644 index 0000000000..431ab181e6 --- /dev/null +++ b/src/vmm_mad/remotes/az/az_driver.default @@ -0,0 +1,55 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + + + + + + + + diff --git a/src/vmm_mad/remotes/az/az_driver.rb b/src/vmm_mad/remotes/az/az_driver.rb new file mode 100755 index 0000000000..39eda7b2ef --- /dev/null +++ b/src/vmm_mad/remotes/az/az_driver.rb @@ -0,0 +1,552 @@ +#!/usr/bin/env ruby +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +# -------------------------------------------------------------------------- # + +ONE_LOCATION = ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) + +if !ONE_LOCATION + RUBY_LIB_LOCATION = "/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION) + ETC_LOCATION = "/etc/one/" if !defined?(ETC_LOCATION) +else + RUBY_LIB_LOCATION = ONE_LOCATION + "/lib/ruby" if !defined?(RUBY_LIB_LOCATION) + ETC_LOCATION = ONE_LOCATION + "/etc/" if !defined?(ETC_LOCATION) +end + +AZ_DRIVER_CONF = "#{ETC_LOCATION}/az_driver.conf" +AZ_DRIVER_DEFAULT = "#{ETC_LOCATION}/az_driver.default" + +# Load Azure credentials and environment +require 'yaml' +require 'rubygems' +require 'azure' +require 'uri' + +$: << RUBY_LIB_LOCATION + +require 'CommandManager' +require 'scripts_common' +require 'rexml/document' +require 'VirtualMachineDriver' + +# The main class for the Azure driver +class AzureDriver + ACTION = VirtualMachineDriver::ACTION + POLL_ATTRIBUTE = VirtualMachineDriver::POLL_ATTRIBUTE + VM_STATE = VirtualMachineDriver::VM_STATE + + # Azure commands constants + AZ = { + :run => { + :cmd => :create_virtual_machine, + :args => { + "INSTANCE_TYPE" => { + :opt => 'vm_size' + }, + "IMAGE" => { + :opt => 'image' + }, + "VM_USER" => { + :opt => 'vm_user' + }, + "VM_PASSWORD" => { + :opt => 'password' + }, + "LOCATION" => { + :opt => 'location' + }, + "STORAGE_ACCOUNT" => { + :opt => 'storage_account_name' + }, + "WIN_RM" => { + :opt => 'winrm_transport', + :proc => lambda { |str| str.split(",") } + }, + "CLOUD_SERVICE" => { + :opt => 'cloud_service_name' + }, + "TCP_ENDPOINTS" => { + :opt => 'tcp_endpoints' + }, + "SSHPORT" => { + :opt => 'ssh_port' + }, + "AFFINITY_GROUP" => { + :opt => 'affinity_group_name' + }, + "VIRTUAL_NETWORK_NAME" => { + :opt => 'virtual_network_name' + }, + "SUBNET" => { + :opt => 'subnet_name' + }, + "AVAILABILITY_SET" => { + :opt => 'availability_set_name' + } + } + }, + :shutdown => { + :cmd => :shutdown_virtual_machine + }, + :reboot => { + :cmd => :restart_virtual_machine + }, + :stop => { + :cmd => :shutdown_virtual_machine + }, + :start => { + :cmd => :start_virtual_machine + }, + :delete => { + :cmd => :delete_virtual_machine + } + } + + # Azure attributes that will be retrieved in a polling action + AZ_POLL_ATTRS = [ + :availability_set_name, + :cloud_service_name, + :data_disks, + :deployment_name, + :disk_name, + :hostname, + :image, + :ipaddress, + :media_link, + :os_type, + :role_size, + :tcp_endpoints, + :udp_endpoints, + :virtual_network_name + ] + + # Azure constructor, loads credentials and endpoint + def initialize(host) + @host = host + + @public_cloud_az_conf = YAML::load(File.read(AZ_DRIVER_CONF)) + + @instance_types = @public_cloud_az_conf['instance_types'] + + regions = @public_cloud_az_conf['regions'] + @region = regions[host] || regions["default"] + + # Sanitize region data + if @region['pem_management_cert'].nil? + raise "pem_management_cert not defined for #{host}" + end + + if @region['subscription_id'].nil? + raise "subscription_id not defined for #{host}" + end + + # Set default endpoint if not declared + if @region['managment_endpoint'].nil? + @region['managment_endpoint']="https://management.core.windows.net" + end + + Azure.configure do |config| + # Configure these 3 properties to use Storage + config.management_certificate = @region['pem_management_cert'] + config.subscription_id = @region['subscription_id'] + config.management_endpoint = @region['management_endpoint'] + end + + @azure_vms = Azure::VirtualMachineManagementService.new + end + + # DEPLOY action + def deploy(id, host, xml_text) + load_default_template_values + + az_info = get_deployment_info(host, xml_text) + + if !az_value(az_info, 'IMAGE') + STDERR.puts("Cannot find IMAGE in deployment file") + exit(-1) + end + + csn = az_value(az_info, 'CLOUD_SERVICE') + + csn = "OpenNebulaDefaultCloudServiceName-#{id}" if !csn + + create_params = create_params(id,csn,az_info) + create_options = create_options(id,csn,az_info) + instance = nil + + begin + in_silence do + instance = @azure_vms.create_virtual_machine(create_params, + create_options) + end + rescue => e + STDERR.puts(e.message) + exit(-1) + end + + if instance.class == Azure::VirtualMachineManagement::VirtualMachine + puts(instance.vm_name) + else + STDERR.puts(instance) + exit (-1) + end + end + + # Shutdown an Azure instance + def shutdown(deploy_id) + az_action(deploy_id, :shutdown) + az_action(deploy_id, :delete) + end + + # Reboot an Azure instance + def reboot(deploy_id) + az_action(deploy_id, :reboot) + end + + # Cancel an Azure instance + def cancel(deploy_id) + az_action(deploy_id, :delete) + end + + # Stop an Azure instance + def save(deploy_id) + az_action(deploy_id, :shutdown) + end + + # Cancel an Azure instance + def restore(deploy_id) + az_action(deploy_id, :start) + end + + # Get info (IP, and state) for an Azure instance + def poll(id, deploy_id) + i = get_instance(deploy_id) + puts parse_poll(i) + end + + # Get the info of all Aure instances. An Azure instance must have + # a name compliant with the "one-#####_csn" format, where ##### are intengers + def monitor_all_vms + totalmemory = 0 + totalcpu = 0 + @region['capacity'].each { |name, size| + cpu, mem = instance_type_capacity(name) + + totalmemory += mem * size.to_i + totalcpu += cpu * size.to_i + } + + host_info = "HYPERVISOR=AZURE\n" + host_info << "PUBLIC_CLOUD=YES\n" + host_info << "PRIORITY=-1\n" + host_info << "TOTALMEMORY=#{totalmemory.round}\n" + host_info << "TOTALCPU=#{totalcpu}\n" + host_info << "HOSTNAME=\"#{@host}\"\n" + + vms_info = "VM_POLL=YES\n" + + usedcpu = 0 + usedmemory = 0 + + begin + @azure_vms.list_virtual_machines.each do |vm| + poll_data=parse_poll(vm) + + if vm.vm_name.start_with?('one-') and + vm.vm_name.match(/([^_]+)_(.+)/) and + vm.vm_name.match(/([^_]+)_(.+)/).size > 1 + + one_id = vm.vm_name.match(/([^_]+)_(.+)/)[1].split("-")[1] + end + + vms_info << "VM=[\n" + vms_info << " ID=#{one_id || -1},\n" + vms_info << " DEPLOY_ID=#{vm.vm_name},\n" + vms_info << " POLL=\"#{poll_data}\" ]\n" + + if one_id + cpu, mem = instance_type_capacity(vm.role_size) + usedcpu += cpu + usedmemory += mem + end + + end + rescue => e + STDERR.puts(e.message) + exit(-1) + end + + host_info << "USEDMEMORY=#{usedmemory.round}\n" + host_info << "USEDCPU=#{usedcpu.round}\n" + host_info << "FREEMEMORY=#{(totalmemory - usedmemory).round}\n" + host_info << "FREECPU=#{(totalcpu - usedcpu).round}\n" + + puts host_info + puts vms_info + end + +private + + # Get the associated capacity of the instance_type as cpu (in 100 percent + # e.g. 800 for 8 cores) and memory (in KB) + def instance_type_capacity(name) + return 0, 0 if @instance_types[name].nil? + return @instance_types[name]['cpu'].to_i * 100 , + @instance_types[name]['memory'].to_i * 1024 * 1024 + end + + # Get the Azure section of the template. If more than one Azure section + # the LOCATION element is used and matched with the host + def get_deployment_info(host, xml_text) + xml = REXML::Document.new xml_text + + az = nil + + all_az_elements = xml.root.get_elements("//USER_TEMPLATE/PUBLIC_CLOUD") + + # First, let's see if we have an Azure location that matches + # our host name + all_az_elements.each { |element| + cloud_host = element.elements["LOCATION"] + type = element.elements["TYPE"].text + + next if !type.downcase.eql? "azure" + + if cloud_host and cloud_host.text.upcase.eql? host.upcase + az = element + end + } + + if !az + # If we don't find an Azure location, and ONE just + # knows about one Azure location, let's use that + if all_az_elements.size == 1 and + all_az_elements[0].elements["TYPE"].text.downcase.eql? "azure" + az = all_az_elements[0] + else + STDERR.puts( + "Cannot find Azure element in VM template "<< + "or couldn't find any Azure location matching "<< + "one of the templates.") + exit(-1) + end + end + + # If LOCATION not explicitly defined, try to get default, if not + # try to use hostname as datacenter + if !az.elements["LOCATION"] + location=REXML::Element.new("LOCATION") + if @defaults["LOCATION"] + location.text=@defaults["LOCATION"] + else + location.text=host + end + az.elements << location + end + + # Translate region name form keyword to actual value + region_keyword = az.elements["LOCATION"].text + translated_region = @public_cloud_az_conf["regions"][region_keyword] + az.elements["LOCATION"].text=translated_region["region_name"] + + az + end + + # Retrive the vm information from the Azure instance + def parse_poll(instance) + info = "#{POLL_ATTRIBUTE[:usedmemory]}=0 " \ + "#{POLL_ATTRIBUTE[:usedcpu]}=0 " \ + "#{POLL_ATTRIBUTE[:nettx]}=0 " \ + "#{POLL_ATTRIBUTE[:netrx]}=0 " + + state = "" + if !instance + state = VM_STATE[:deleted] + else + state = case instance.deployment_status + when "Running", "Starting" + VM_STATE[:active] + when "Suspended", "Stopping", + VM_STATE[:paused] + else + VM_STATE[:deleted] + end + end + info << "#{POLL_ATTRIBUTE[:state]}=#{state} " + + AZ_POLL_ATTRS.map { |key| + value = instance.send(key) + if !value.nil? && !value.empty? + if key.to_s.upcase == "TCP_ENDPOINTS" or + key.to_s.upcase == "UDP_ENDPOINTS" + value_str = format_endpoints(value) + elsif value.kind_of?(Hash) + value_str = value.inspect + else + value_str = value + end + + info << "AZ_#{key.to_s.upcase}=#{value_str.gsub("\"","")} " + + end + } + + info + end + + def format_endpoints(endpoints) + endpnt_str = "" + + endpoints.each{|ep| + endpnt_str += "name=#{ep[:name]}," + + "vip=#{ep[:vip]},publicport=#{ep[:public_port]}," + + "local_port=#{ep[:local_port]}," + + "local_port=#{ep[:protocol]};" + } + + endpnt_str.chop + end + + def create_params(id,csn,az_info) + params = { + # Name will always be 'one-_' + :vm_name => "one-#{id}_#{csn}", + :vm_user => az_value(az_info, 'VM_USER'), + :image => az_value(az_info, 'IMAGE'), + :password => az_value(az_info, 'VM_PASSWORD'), + :location => az_value(az_info, 'LOCATION') + }.delete_if { |k, v| v.nil? } + end + + def create_options(id,csn,az_info) + options = { + :storage_account_name => az_value(az_info, 'STORAGE_ACCOUNT'), + :winrm_transport => az_value(az_info, 'WIN_RM'), + :cloud_service_name => csn, + :tcp_endpoints => az_value(az_info, 'TCP_ENDPOINTS'), + # TODO possibly taking the values from user template + # and create temp files + # :private_key_file => 'c:/private_key.key', + # :certificate_file => 'c:/certificate.pem', + :ssh_port => az_value(az_info, 'SSHPORT'), + :vm_size => az_value(az_info, 'INSTANCE_TYPE'), + :affinity_group_name => az_value(az_info, 'AFFINITY_GROUP'), + :virtual_network_name => az_value(az_info, 'VIRTUAL_NETWORK_NAME'), + :subnet_name => az_value(az_info, 'SUBNET'), + :availability_set_name => az_value(az_info, 'AVAILABILITY_SET') + }.delete_if { |k, v| v.nil? } + end + + # Execute an Azure command + # +deploy_id+: String, VM id in Azure + # +az_action+: Symbol, one of the keys of the Azure hash constant (i.e :run) + def az_action(deploy_id, az_action) + name, csn = deploy_id.match(/([^_]+)_(.+)/)[1..-1] + + begin + in_silence do + @azure_vms.send(AZ[az_action][:cmd], deploy_id, csn) + end + rescue => e + STDERR.puts e.message + exit(-1) + end + end + + # Returns the value of the xml specified by the name or the default + # one if it does not exist + # +xml+: REXML Document, containing Azure information + # +name+: String, xpath expression to retrieve the value + # +block+: Block, block to be applied to the value before returning it + def az_value(xml, name, &block) + value = value_from_xml(xml, name) || @defaults[name] + if block_given? && value + block.call(value) + else + value + end + end + + def value_from_xml(xml, name) + if xml + element = xml.elements[name] + element.text.strip if element && element.text + end + end + + # Load the default values that will be used to create a new instance, if + # not provided in the template. These values are defined in the AZ_CONF + # file + def load_default_template_values + @defaults = Hash.new + + if File.exists?(AZ_DRIVER_DEFAULT) + fd = File.new(AZ_DRIVER_DEFAULT) + xml = REXML::Document.new fd + fd.close() + + return if !xml || !xml.root + + az = xml.root.elements["AZURE"] + + return if !az + + AZ.each {|action, hash| + if hash[:args] + hash[:args].each { |key, value| + @defaults[key] = value_from_xml(az, key) + } + end + } + end + end + + def in_silence + begin + orig_stderr = $stderr.clone + orig_stdout = $stdout.clone + $stderr.reopen File.new('/dev/null', 'w') + $stdout.reopen File.new('/dev/null', 'w') + retval = yield + rescue Exception => e + $stdout.reopen orig_stdout + $stderr.reopen orig_stderr + raise e + ensure + $stdout.reopen orig_stdout + $stderr.reopen orig_stderr + end + retval + end + + # Retrive the instance from Azure. If OpenNebula asks for it, then the + # vm_name must comply with the notation name_csn + def get_instance(vm_name) + begin + csn = vm_name.match(/([^_]+)_(.+)/)[2] + + instance = @azure_vms.get_virtual_machine(vm_name,csn) + if instance + return instance + else + raise "Instance #{vm_name} does not exist" + end + rescue => e + STDERR.puts e.message + exit(-1) + end + end +end + diff --git a/src/vmm_mad/remotes/az/cancel b/src/vmm_mad/remotes/az/cancel new file mode 100755 index 0000000000..55e10ea8ce --- /dev/null +++ b/src/vmm_mad/remotes/az/cancel @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2010-2014, C12G Labs S.L # +# # +# 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. # +# -------------------------------------------------------------------------- # + +$: << File.dirname(__FILE__) + +require 'az_driver' + +deploy_id = ARGV[0] +host = ARGV[1] + +az_drv = AzureDriver.new(host) + +az_drv.cancel(deploy_id) + diff --git a/src/vmm_mad/remotes/az/deploy b/src/vmm_mad/remotes/az/deploy new file mode 100755 index 0000000000..944f4f46dc --- /dev/null +++ b/src/vmm_mad/remotes/az/deploy @@ -0,0 +1,34 @@ +#!/usr/bin/env ruby + +# ---------------------------------------------------------------------------- # +# Copyright 2010-2014, C12G Labs S.L # +# # +# 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. # +# ---------------------------------------------------------------------------- # + +$: << File.dirname(__FILE__) + +require 'az_driver' + +dfile = ARGV[0] +host = ARGV[1] +id = ARGV[2] + +az_drv = AzureDriver.new(host) + +text=File.read(dfile) + +puts az_drv.deploy(id, host, text) + +exit 0 + diff --git a/src/vmm_mad/remotes/az/detach_disk b/src/vmm_mad/remotes/az/detach_disk new file mode 100755 index 0000000000..8aeaf8eba5 --- /dev/null +++ b/src/vmm_mad/remotes/az/detach_disk @@ -0,0 +1,20 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +SCRIPT_NAME=$(basename $0) +echo "Action $SCRIPT_NAME not supported" 1>&2 diff --git a/src/vmm_mad/remotes/az/detach_nic b/src/vmm_mad/remotes/az/detach_nic new file mode 100755 index 0000000000..8aeaf8eba5 --- /dev/null +++ b/src/vmm_mad/remotes/az/detach_nic @@ -0,0 +1,20 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +SCRIPT_NAME=$(basename $0) +echo "Action $SCRIPT_NAME not supported" 1>&2 diff --git a/src/vmm_mad/remotes/az/migrate b/src/vmm_mad/remotes/az/migrate new file mode 100755 index 0000000000..8aeaf8eba5 --- /dev/null +++ b/src/vmm_mad/remotes/az/migrate @@ -0,0 +1,20 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +SCRIPT_NAME=$(basename $0) +echo "Action $SCRIPT_NAME not supported" 1>&2 diff --git a/src/vmm_mad/remotes/az/migrate_local b/src/vmm_mad/remotes/az/migrate_local new file mode 100755 index 0000000000..8aeaf8eba5 --- /dev/null +++ b/src/vmm_mad/remotes/az/migrate_local @@ -0,0 +1,20 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +SCRIPT_NAME=$(basename $0) +echo "Action $SCRIPT_NAME not supported" 1>&2 diff --git a/src/vmm_mad/remotes/az/poll b/src/vmm_mad/remotes/az/poll new file mode 100755 index 0000000000..4a30d87e90 --- /dev/null +++ b/src/vmm_mad/remotes/az/poll @@ -0,0 +1,30 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2010-2014, C12G Labs S.L # +# # +# 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. # +# -------------------------------------------------------------------------- # + +$: << File.dirname(__FILE__) + +require 'az_driver' + +deploy_id = ARGV[0] +host = ARGV[1] +id = ARGV[2] + +az_drv = AzureDriver.new(host) + +az_drv.poll(id, deploy_id) + diff --git a/src/vmm_mad/remotes/az/reboot b/src/vmm_mad/remotes/az/reboot new file mode 100755 index 0000000000..65fdcf0bfd --- /dev/null +++ b/src/vmm_mad/remotes/az/reboot @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2010-2014, C12G Labs S.L # +# # +# 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. # +# -------------------------------------------------------------------------- # + +$: << File.dirname(__FILE__) + +require 'az_driver' + +deploy_id = ARGV[0] +host = ARGV[1] + +az_drv = AzureDriver.new(host) + +az_drv.reboot(deploy_id) + diff --git a/src/vmm_mad/remotes/az/reset b/src/vmm_mad/remotes/az/reset new file mode 100755 index 0000000000..8aeaf8eba5 --- /dev/null +++ b/src/vmm_mad/remotes/az/reset @@ -0,0 +1,20 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +SCRIPT_NAME=$(basename $0) +echo "Action $SCRIPT_NAME not supported" 1>&2 diff --git a/src/vmm_mad/remotes/az/restore b/src/vmm_mad/remotes/az/restore new file mode 100755 index 0000000000..5eb5c10c13 --- /dev/null +++ b/src/vmm_mad/remotes/az/restore @@ -0,0 +1,28 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2010-2014, C12G Labs S.L # +# # +# 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. # +# -------------------------------------------------------------------------- # + +$: << File.dirname(__FILE__) + +require 'az_driver' + +checkpoint_file = ARGV[0] +host = ARGV[1] +deploy_id = ARGV[2] + +az_drv = AzureDriver.new(host) +az_drv.restore(deploy_id) diff --git a/src/vmm_mad/remotes/az/save b/src/vmm_mad/remotes/az/save new file mode 100755 index 0000000000..01ae33e1b3 --- /dev/null +++ b/src/vmm_mad/remotes/az/save @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2010-2014, C12G Labs S.L # +# # +# 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. # +# -------------------------------------------------------------------------- # + +$: << File.dirname(__FILE__) + +require 'az_driver' + +deploy_id = ARGV[0] +file = ARGV[1] +host = ARGV[2] + +az_drv = AzureDriver.new(host) +az_drv.save(deploy_id) + diff --git a/src/vmm_mad/remotes/az/shutdown b/src/vmm_mad/remotes/az/shutdown new file mode 100755 index 0000000000..7e6a3c314a --- /dev/null +++ b/src/vmm_mad/remotes/az/shutdown @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2010-2014, C12G Labs S.L # +# # +# 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. # +# -------------------------------------------------------------------------- # + +$: << File.dirname(__FILE__) + +require 'az_driver' + +deploy_id = ARGV[0] +host = ARGV[1] + +az_drv = AzureDriver.new(host) + +az_drv.shutdown(deploy_id) + diff --git a/src/vmm_mad/remotes/az/snapshot_create b/src/vmm_mad/remotes/az/snapshot_create new file mode 100755 index 0000000000..8aeaf8eba5 --- /dev/null +++ b/src/vmm_mad/remotes/az/snapshot_create @@ -0,0 +1,20 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +SCRIPT_NAME=$(basename $0) +echo "Action $SCRIPT_NAME not supported" 1>&2 diff --git a/src/vmm_mad/remotes/az/snapshot_delete b/src/vmm_mad/remotes/az/snapshot_delete new file mode 100755 index 0000000000..8aeaf8eba5 --- /dev/null +++ b/src/vmm_mad/remotes/az/snapshot_delete @@ -0,0 +1,20 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +SCRIPT_NAME=$(basename $0) +echo "Action $SCRIPT_NAME not supported" 1>&2 diff --git a/src/vmm_mad/remotes/az/snapshot_revert b/src/vmm_mad/remotes/az/snapshot_revert new file mode 100755 index 0000000000..8aeaf8eba5 --- /dev/null +++ b/src/vmm_mad/remotes/az/snapshot_revert @@ -0,0 +1,20 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +SCRIPT_NAME=$(basename $0) +echo "Action $SCRIPT_NAME not supported" 1>&2 diff --git a/src/vmm_mad/remotes/ec2/attach_disk b/src/vmm_mad/remotes/ec2/attach_disk old mode 100644 new mode 100755 diff --git a/src/vmm_mad/remotes/sl/attach_disk b/src/vmm_mad/remotes/sl/attach_disk new file mode 100755 index 0000000000..8aeaf8eba5 --- /dev/null +++ b/src/vmm_mad/remotes/sl/attach_disk @@ -0,0 +1,20 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +SCRIPT_NAME=$(basename $0) +echo "Action $SCRIPT_NAME not supported" 1>&2 diff --git a/src/vmm_mad/remotes/sl/attach_nic b/src/vmm_mad/remotes/sl/attach_nic new file mode 100755 index 0000000000..8aeaf8eba5 --- /dev/null +++ b/src/vmm_mad/remotes/sl/attach_nic @@ -0,0 +1,20 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +SCRIPT_NAME=$(basename $0) +echo "Action $SCRIPT_NAME not supported" 1>&2 diff --git a/src/vmm_mad/remotes/sl/cancel b/src/vmm_mad/remotes/sl/cancel new file mode 100755 index 0000000000..05844b2977 --- /dev/null +++ b/src/vmm_mad/remotes/sl/cancel @@ -0,0 +1,28 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2010-2014, C12G Labs S.L # +# # +# 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. # +# -------------------------------------------------------------------------- # + +$: << File.dirname(__FILE__) + +require 'sl_driver' + +deploy_id = ARGV[0] +host = ARGV[1] + +sl_drv = SLDriver.new(host) + +sl_drv.cancel(deploy_id) diff --git a/src/vmm_mad/remotes/sl/deploy b/src/vmm_mad/remotes/sl/deploy new file mode 100755 index 0000000000..6410f5af2c --- /dev/null +++ b/src/vmm_mad/remotes/sl/deploy @@ -0,0 +1,33 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2010-2014, C12G Labs S.L # +# # +# 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. # +# -------------------------------------------------------------------------- # + +$: << File.dirname(__FILE__) + +require 'sl_driver' + +dfile = ARGV[0] +host = ARGV[1] +id = ARGV[2] + +sl_drv = SLDriver.new(host) + +text=File.read(dfile) + +puts sl_drv.deploy(id, host, text) + +exit 0 diff --git a/src/vmm_mad/remotes/sl/detach_disk b/src/vmm_mad/remotes/sl/detach_disk new file mode 100755 index 0000000000..8aeaf8eba5 --- /dev/null +++ b/src/vmm_mad/remotes/sl/detach_disk @@ -0,0 +1,20 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +SCRIPT_NAME=$(basename $0) +echo "Action $SCRIPT_NAME not supported" 1>&2 diff --git a/src/vmm_mad/remotes/sl/detach_nic b/src/vmm_mad/remotes/sl/detach_nic new file mode 100755 index 0000000000..8aeaf8eba5 --- /dev/null +++ b/src/vmm_mad/remotes/sl/detach_nic @@ -0,0 +1,20 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +SCRIPT_NAME=$(basename $0) +echo "Action $SCRIPT_NAME not supported" 1>&2 diff --git a/src/vmm_mad/remotes/sl/migrate b/src/vmm_mad/remotes/sl/migrate new file mode 100755 index 0000000000..8aeaf8eba5 --- /dev/null +++ b/src/vmm_mad/remotes/sl/migrate @@ -0,0 +1,20 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +SCRIPT_NAME=$(basename $0) +echo "Action $SCRIPT_NAME not supported" 1>&2 diff --git a/src/vmm_mad/remotes/sl/migrate_local b/src/vmm_mad/remotes/sl/migrate_local new file mode 100755 index 0000000000..8aeaf8eba5 --- /dev/null +++ b/src/vmm_mad/remotes/sl/migrate_local @@ -0,0 +1,20 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +SCRIPT_NAME=$(basename $0) +echo "Action $SCRIPT_NAME not supported" 1>&2 diff --git a/src/vmm_mad/remotes/sl/poll b/src/vmm_mad/remotes/sl/poll new file mode 100755 index 0000000000..711cdd6016 --- /dev/null +++ b/src/vmm_mad/remotes/sl/poll @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2010-2014, C12G Labs S.L # +# # +# 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. # +# -------------------------------------------------------------------------- # + +$: << File.dirname(__FILE__) + +require 'sl_driver' + +deploy_id = ARGV[0] +host = ARGV[1] +id = ARGV[2] + +sl_drv = SLDriver.new(host) + +sl_drv.poll(id, deploy_id) diff --git a/src/vmm_mad/remotes/sl/reboot b/src/vmm_mad/remotes/sl/reboot new file mode 100755 index 0000000000..7fd2fea3bb --- /dev/null +++ b/src/vmm_mad/remotes/sl/reboot @@ -0,0 +1,28 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2010-2014, C12G Labs S.L # +# # +# 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. # +# -------------------------------------------------------------------------- # + +$: << File.dirname(__FILE__) + +require 'sl_driver' + +deploy_id = ARGV[0] +host = ARGV[1] + +sl_drv = SLDriver.new(host) + +sl_drv.reboot(deploy_id) diff --git a/src/vmm_mad/remotes/sl/reset b/src/vmm_mad/remotes/sl/reset new file mode 100755 index 0000000000..8aeaf8eba5 --- /dev/null +++ b/src/vmm_mad/remotes/sl/reset @@ -0,0 +1,20 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +SCRIPT_NAME=$(basename $0) +echo "Action $SCRIPT_NAME not supported" 1>&2 diff --git a/src/vmm_mad/remotes/sl/restore b/src/vmm_mad/remotes/sl/restore new file mode 100755 index 0000000000..d41da8c05f --- /dev/null +++ b/src/vmm_mad/remotes/sl/restore @@ -0,0 +1,27 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2010-2014, C12G Labs S.L # +# # +# 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. # +# -------------------------------------------------------------------------- # + +$: << File.dirname(__FILE__) + +require 'sl_driver' + +host = ARGV[-1] +deploy_id = ARGV[2] + +sl_drv = SLDriver.new(host) +sl_drv.restore(deploy_id) diff --git a/src/vmm_mad/remotes/sl/save b/src/vmm_mad/remotes/sl/save new file mode 100755 index 0000000000..b2fde5e7c3 --- /dev/null +++ b/src/vmm_mad/remotes/sl/save @@ -0,0 +1,27 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2010-2014, C12G Labs S.L # +# # +# 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. # +# -------------------------------------------------------------------------- # + +$: << File.dirname(__FILE__) + +require 'sl_driver' + +deploy_id = ARGV[0] +host = ARGV[1] + +sl_drv = SLDriver.new(host) +sl_drv.save(deploy_id) diff --git a/src/vmm_mad/remotes/sl/shutdown b/src/vmm_mad/remotes/sl/shutdown new file mode 100755 index 0000000000..3dd8942734 --- /dev/null +++ b/src/vmm_mad/remotes/sl/shutdown @@ -0,0 +1,28 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2010-2014, C12G Labs S.L # +# # +# 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. # +# -------------------------------------------------------------------------- # + +$: << File.dirname(__FILE__) + +require 'sl_driver' + +deploy_id = ARGV[0] +host = ARGV[1] + +sl_drv = SLDriver.new(host) + +sl_drv.shutdown(deploy_id) diff --git a/src/vmm_mad/remotes/sl/sl_driver.conf b/src/vmm_mad/remotes/sl/sl_driver.conf new file mode 100644 index 0000000000..64f46a7bd7 --- /dev/null +++ b/src/vmm_mad/remotes/sl/sl_driver.conf @@ -0,0 +1,109 @@ +# Regions extracted from http://www.softlayer.com/data-centers +regions: + default: + region_name: sjc01 + username: + api_key: + capacity: + slcci.small: 5 + slcci.large: 0 + slcci.large: 0 + ams01: + region_name: ams01 + username: + api_key: + capacity: + slcci.small: 5 + slcci.large: 0 + slcci.large: 0 + sjc01: + region_name: sjc01 + username: + api_key: + capacity: + slcci.small: + slcci.large: + slcci.large: + dal01: + region_name: dal01 + username: + api_key: + capacity: + slcci.small: + slcci.large: + slcci.large: + dal02: + region_name: dal02 + username: + api_key: + capacity: + slcci.small: + slcci.large: + slcci.large: + dal05: + region_name: dal05 + username: + api_key: + capacity: + slcci.small: + slcci.large: + slcci.large: + dal06: + region_name: dal06 + username: + api_key: + capacity: + slcci.small: + slcci.large: + slcci.large: + dal07: + region_name: dal07 + username: + api_key: + capacity: + slcci.small: + slcci.large: + slcci.large: + hou02: + region_name: hou02 + username: + api_key: + capacity: + slcci.small: + slcci.large: + slcci.large: + sea01: + region_name: sea01 + username: + api_key: + capacity: + slcci.small: + slcci.large: + slcci.large: + sng01: + region_name: sng01 + username: + api_key: + capacity: + slcci.small: + slcci.large: + slcci.large: + wdc01: + region_name: wdc01 + username: + api_key: + capacity: + slcci.small: + slcci.large: + slcci.large: +instance_types: + slcci.small: + cpu: 1 + memory: 1 + slcci.medium: + cpu: 2 + memory: 4 + slcci.large: + cpu: 4 + memory: 8 + diff --git a/src/vmm_mad/remotes/sl/sl_driver.default b/src/vmm_mad/remotes/sl/sl_driver.default new file mode 100644 index 0000000000..1563b51398 --- /dev/null +++ b/src/vmm_mad/remotes/sl/sl_driver.default @@ -0,0 +1,37 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + + + diff --git a/src/vmm_mad/remotes/sl/sl_driver.rb b/src/vmm_mad/remotes/sl/sl_driver.rb new file mode 100644 index 0000000000..f322f9609b --- /dev/null +++ b/src/vmm_mad/remotes/sl/sl_driver.rb @@ -0,0 +1,553 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +ONE_LOCATION = ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) + +if !ONE_LOCATION + RUBY_LIB_LOCATION = "/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION) + ETC_LOCATION = "/etc/one/" if !defined?(ETC_LOCATION) +else + RUBY_LIB_LOCATION = ONE_LOCATION + "/lib/ruby" if !defined?(RUBY_LIB_LOCATION) + ETC_LOCATION = ONE_LOCATION + "/etc/" if !defined?(ETC_LOCATION) +end + +SL_DRIVER_CONF = "#{ETC_LOCATION}/sl_driver.conf" +SL_DRIVER_DEFAULT = "#{ETC_LOCATION}/sl_driver.default" + +# Load SL credentials and needed gems +require 'yaml' +require 'rubygems' +require 'softlayer_api' + +$: << RUBY_LIB_LOCATION + +require 'CommandManager' +require 'scripts_common' +require 'rexml/document' +require 'VirtualMachineDriver' + +# The main class for the SoftLayer driver +class SLDriver + ACTION = VirtualMachineDriver::ACTION + POLL_ATTRIBUTE = VirtualMachineDriver::POLL_ATTRIBUTE + VM_STATE = VirtualMachineDriver::VM_STATE + + # Key that will be used to store the monitoring information in the template + SL_MONITOR_KEY = "SLDRIVER_MONITOR" + + # SL commands constants + SL = { + :run => { + :cmd => :createObject, # Create a new SL object + :args => { + "HOSTNAME" => { + :opt => 'hostname' + }, + "DOMAIN" => { + :opt => 'domain' + }, + "STARTCPUS" => { + :opt => 'startCpus' + }, + "MAXMEMORY" => { + :opt => 'maxMemory' + }, + "HOURLYBILLING" => { + :opt => 'hourlyBillingFlag' + }, + "LOCALDISK" => { + :opt => 'localDiskFlag' + }, + "DEDICATEDHOST" => { + :opt => 'dedicatedAccountHostOnlyFlag' + }, + "DATACENTER" => { + :opt => 'datacenter.name' + }, + "OPERATINGSYSTEM" => { + :opt => 'operatingSystemReferenceCode' + }, + "BLOCKDEVICETEMPLATE" => { + :opt => 'blockDeviceTemplateGroup.globalIdentifier' + }, + "BLOCKDEVICE" => { + :opt => 'blockDevices', + :proc => lambda {|str| + [{:device=>0, :diskImage=>{:capacity=>str}}]} + + }, + "NETWORKCOMPONENTSMAXSPEED" => { + :opt => 'networkComponents.maxSpeed', + }, + "PRIVATENETWORKONLY" => { + :opt => 'privateNetworkOnlyFlag', + :proc => lambda {|str| str=="YES"?true:false} + }, + "PRIMARYNETWORKVLAN" => { + :opt => 'primaryNetworkComponent.networkVlan.id' + }, + "PRIMARYBACKENDNETWORKVLAN" => { + :opt => 'primaryBackendNetworkComponent.networkVlan.id' + }, + "USERDATA" => { + :opt => 'userData.value' + }, + "SSHKEYS" => { + :opt => 'sshKeys', # Array of SSH Keys ids + :proc => lambda {|str| + sshArray = Array.new + str.split(",").each { |id| + sshArray << {:id=> id} + } + sshArray + } + + }, + "POSTSCRIPT" => { + :opt => 'postInstallScriptUri' + } + + } + }, + :terminate => { + :cmd => :deleteObject # Terminate a Virtual Guest + }, + :reboot => { + :cmd => :rebootDefault # Reboot a Virtual Guest + }, + :stop => { + :cmd => :pause # Stop a Virtual Guest + }, + :start => { + :cmd => :resume # Resume a Virtual Guest + }, + :poweroff => { + :cmd => :powerOff # Powers off a Virtual Guest + } + } + + # SoftLayer attributes that will be retrieved in a polling action. + SL_POLL_ATTRS = [ + :id, + :hostname, + :domain, + :fullyQualifiedDomainName, + :maxCpu, + :maxMemory, + :startCpus, + :uuid, + :globalIdentifier, + :primaryBackendIpAddress, + :primaryIpAddress + ] + + # SoftLayer constructor, loads credentials and endpoint + def initialize(host) + @host = host + + public_cloud_sl_conf = YAML::load(File.read(SL_DRIVER_CONF)) + + @instance_types = public_cloud_sl_conf['instance_types'] + + regions = public_cloud_sl_conf['regions'] + @region = regions[host] || regions["default"] + + client_cred={:username => @region['username'], + :api_key => @region['api_key']} + + client_cred[:endpoint_url]=@region['endpoint'] if @region['endpoint'] + + + @sl_client = SoftLayer::Client.new(client_cred) + end + + # DEPLOY action + def deploy(id, host, xml_text) + load_default_template_values + + sl_info = get_deployment_info(host, xml_text) + + opts = generate_options(:run, sl_info, { + :min_count => 1, + :max_count => 1}) + + if !opts[:startCpus] or !opts[:maxMemory] + xml = REXML::Document.new xml_text + instance_type = xml.root.get_elements("//INSTANCE_TYPE")[0].text + cpu, mem = instance_type_capacity(instance_type) + opts[:startCpus] = cpu / 100 + opts[:maxMemory] = mem / 1024 + end + + vg_srvc = @sl_client['Virtual_Guest'] + + begin + vgid = vg_srvc.createObject(opts)['id'] + vg = vg_srvc.object_with_id(vgid) + rescue => e + STDERR.puts(e.message) + exit(-1) + end + + begin + vg.setTags("#{id}") + rescue => e + STDERR.puts(e.message) + exit(-1) + end + + puts(vgid) + end + + # Shutdown a SoftLayer instance + def shutdown(deploy_id) + sl_action(deploy_id, :terminate) + end + + # Reboot a SoftLayer instance + def reboot(deploy_id) + sl_action(deploy_id, :reboot) + end + + # Cancel a SoftLayer instance + def cancel(deploy_id) + sl_action(deploy_id, :terminate) + end + + # Stop a SoftLayer instance + def save(deploy_id) + sl_action(deploy_id, :stop) + end + + # Start a SoftLayer instance + def restore(deploy_id)0 + sl_action(deploy_id, :start) + end + + # Get info (IP, and state) for a SoftLayer instance + def poll(id, deploy_id) + i = get_instance(deploy_id) + puts parse_poll(i) + end + + # Get the info of all the SoftLayer instances. A SoftLayer instance must include + # the OpenNebula VID as the first tag, otherwise it will be ignored + def monitor_all_vms + totalmemory = 0 + totalcpu = 0 + + @region['capacity'].each { |name, size| + cpu, mem = instance_type_capacity(name) + + totalmemory += mem * size.to_i + totalcpu += cpu * size.to_i + } + + host_info = "HYPERVISOR=SOFTLAYER\n" + host_info << "PUBLIC_CLOUD=YES\n" + host_info << "PRIORITY=-1\n" + host_info << "TOTALMEMORY=#{totalmemory.round}\n" + host_info << "TOTALCPU=#{totalcpu}\n" + host_info << "CPUSPEED=1000\n" + host_info << "HOSTNAME=\"#{@host}\"\n" + + vms = @sl_client['Account'].getVirtualGuests + + vms_info = "VM_POLL=YES\n" + + usedcpu = 0 + usedmemory = 0 + + begin + vms.each do |i| + vm = @sl_client['Virtual_Guest'].object_with_id(i['id'].to_i) + poll_data = parse_poll(vm) + # Retrieve Virtual Guest to consult tags + + tags = vm.getTagReferences + # We know that the first one is the ONE identifier + one_id=nil + begin + one_id=tags[0]["tag"]["name"] + rescue + end + next if one_id=="one_id" + vms_info << "VM=[\n" + vms_info << " ID=#{one_id || -1},\n" + vms_info << " DEPLOY_ID=#{i['id']},\n" + vms_info << " POLL=\"#{poll_data[0...-1]}\" ]\n" + + if one_id + usedcpu += i['maxCpu'].to_i * 100 + usedmemory += i['maxMemory'].to_i * 1024 + end + end + rescue => e + STDERR.puts(e.message) + exit(-1) + end + + host_info << "USEDMEMORY=#{usedmemory.round}\n" + host_info << "USEDCPU=#{usedcpu.round}\n" + host_info << "FREEMEMORY=#{(totalmemory - usedmemory).round}\n" + host_info << "FREECPU=#{(totalcpu - usedcpu).round}\n" + + puts host_info + puts vms_info + end + +private + + #Get the associated capacity of the instance_type as cpu (in 100 percent + #e.g. 800) and memory (in KB) + def instance_type_capacity(name) + if @instance_types[name].nil? + STDERR.puts("Instance type #{name} not recognized") + exit(-1) + end + + return @instance_types[name]['cpu'].to_i * 100 , + @instance_types[name]['memory'].to_i * 1024 * 1024 + end + + # Get the SoftLayer section of the template. If more than one SoftLayer + # section, the DATACENTER element is used and matched with the host + def get_deployment_info(host, xml_text) + xml = REXML::Document.new xml_text + + sl = nil + all_sl_elements = xml.root.get_elements("//USER_TEMPLATE/PUBLIC_CLOUD") + + # First, let's see if we have an SoftLayer site that matches + # our desired host name + all_sl_elements.each { |element| + cloud_host = element.elements["DATACENTER"] + type = element.elements["TYPE"].text + + next if !type.downcase.eql? "softlayer" + + if cloud_host and cloud_host.text.upcase.eql? host.upcase + sl = element + end + } + + if !sl + # If we don't find the SoftLayer site, and ONE just + # knows about one SoftLayer site, let's use that + if all_sl_elements.size == 1 and + all_sl_elements[0].elements["TYPE"].text.upcase.eql? "SOFTLAYER" + sl = all_sl_elements[0] + else + STDERR.puts( + "Cannot find SoftLayer element in VM template "<< + "or couldn't find any SoftLayer site matching "<< + "one of the templates.") + exit(-1) + end + end + + # If DATACENTER not explicitly defined, try to get default, if not + # try to use hostname as datacenter + if !sl.elements["DATACENTER"] + dc=REXML::Element.new("DATACENTER") + if @defaults["DATACENTER"] + dc.text=@defaults["DATACENTER"] + else + dc.text=host + end + sl.elements << dc + end + + sl + end + + # Retrieve the VM information from the SoftLayer instance + def parse_poll(vm) + info = "#{POLL_ATTRIBUTE[:usedmemory]}=0 " \ + "#{POLL_ATTRIBUTE[:usedcpu]}=0 " \ + "#{POLL_ATTRIBUTE[:nettx]}=0 " \ + "#{POLL_ATTRIBUTE[:netrx]}=0 " + + state = "" + + instance = vm.getObject + sl_state = vm.getPowerState["name"] + + password_mask = "mask[operatingSystem[passwords]]" + pass_info = vm.object_mask(password_mask).getObject + + begin + user = pass_info["operatingSystem"]["passwords"][0]["username"] + pwd = pass_info["operatingSystem"]["passwords"][0]["password"] + rescue + end + + if !instance + state = VM_STATE[:deleted] + else + state = case sl_state.upcase + when "RUNNING" + VM_STATE[:active] + when "PAUSED" + VM_STATE[:paused] + else + VM_STATE[:deleted] + end + end + info << "#{POLL_ATTRIBUTE[:state]}=#{state} " + + SL_POLL_ATTRS.map { |key| + value = instance["#{key}"].to_s + + if !value.nil? && !value.empty? + if value.is_a?(Array) + value = value.map {|v|}.join(",") + end + info << "SL_#{key.to_s.upcase}=#{value} " + end + } + + info << "SL_CRED_USER=#{user} SL_CRED_PASSWORD=#{pwd}" if user and pwd + + info + end + + # Execute a SoftLayer command + # +deploy_id+: String, VM id in SoftLayer + # +sl_action+: Symbol, one of the keys of the SL hash constant (i.e :run) + def sl_action(deploy_id, sl_action) + i = get_instance(deploy_id) + + begin + i.send(SL[sl_action][:cmd]) + rescue => e + STDERR.puts e.message + exit(-1) + end + end + + # Generate the options for the given command from the xml provided in the + # template. The available options for each command are defined in the 'SL' + # constant + def generate_options(action, xml, extra_params={}) + opts = extra_params || {} + + if SL[action][:args] + SL[action][:args].each {|k,v| + str = sl_value(xml, k, &v[:proc]) + + if str + if v[:opt].include? "." + opts=create_opts_hash(opts,v[:opt],str) + else + opts[v[:opt].to_sym] = str + end + end + } + end + + opts + end + + def create_opts_hash(opts,key_schema,value) + split_keys = key_schema.split(".") + new_hash = {} + current_hash = new_hash + last_hash = {} + + split_keys.size.times{ |i| + index = i + 1 # We skip the first key + + if index == split_keys.size # Last iteration + last_hash[split_keys[index-1].to_sym] = value + break + else + current_hash[split_keys[index].to_sym]={} + last_hash =current_hash + current_hash=current_hash[split_keys[index].to_sym] + end + } + opts[split_keys[0].to_sym] = new_hash + + opts + end + + # Returns the value of the xml specified by the name or the default + # one if it does not exist + # +xml+: REXML Document, containing SoftLayer information + # +name+: String, xpath expression to retrieve the value + # +block+: Block, block to be applied to the value before returning it + def sl_value(xml, name, &block) + value = value_from_xml(xml, name) || @defaults[name] + if block_given? && value + block.call(value) + else + value + end + end + + def value_from_xml(xml, name) + if xml + element = xml.elements[name] + element.text.strip if element && element.text + end + end + + # Load the default values that will be used to create a new instance, if + # not provided in the template. These values are defined in the + # SL_DRIVER_DEFAULT file + def load_default_template_values + @defaults = Hash.new + + if File.exists?(SL_DRIVER_DEFAULT) + fd = File.new(SL_DRIVER_DEFAULT) + xml = REXML::Document.new fd + fd.close() + + return if !xml || !xml.root + + sl = xml.root.elements["SOFTLAYER"] + + return if !sl + + SL.each {|action, hash| + if hash[:args] + hash[:args].each { |key, value| + @defaults[key] = value_from_xml(sl, key) + } + end + } + end + end + + # Retrieve the instance from SoftLayer + def get_instance(id) + begin + vg_srvc = @sl_client['Virtual_Guest'] + instance = vg_srvc.object_with_id(id) + if instance + return instance + else + raise "Instance #{id} does not exist" + end + rescue => e + STDERR.puts e.message + exit(-1) + end + end +end \ No newline at end of file diff --git a/src/vmm_mad/remotes/sl/snapshot_create b/src/vmm_mad/remotes/sl/snapshot_create new file mode 100755 index 0000000000..8aeaf8eba5 --- /dev/null +++ b/src/vmm_mad/remotes/sl/snapshot_create @@ -0,0 +1,20 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +SCRIPT_NAME=$(basename $0) +echo "Action $SCRIPT_NAME not supported" 1>&2 diff --git a/src/vmm_mad/remotes/sl/snapshot_delete b/src/vmm_mad/remotes/sl/snapshot_delete new file mode 100755 index 0000000000..8aeaf8eba5 --- /dev/null +++ b/src/vmm_mad/remotes/sl/snapshot_delete @@ -0,0 +1,20 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +SCRIPT_NAME=$(basename $0) +echo "Action $SCRIPT_NAME not supported" 1>&2 diff --git a/src/vmm_mad/remotes/sl/snapshot_revert b/src/vmm_mad/remotes/sl/snapshot_revert new file mode 100755 index 0000000000..8aeaf8eba5 --- /dev/null +++ b/src/vmm_mad/remotes/sl/snapshot_revert @@ -0,0 +1,20 @@ +#!/bin/bash + +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +SCRIPT_NAME=$(basename $0) +echo "Action $SCRIPT_NAME not supported" 1>&2 diff --git a/src/vmm_mad/remotes/vmware/vmware_driver.rb b/src/vmm_mad/remotes/vmware/vmware_driver.rb index 21da82a883..79ebdb43fe 100644 --- a/src/vmm_mad/remotes/vmware/vmware_driver.rb +++ b/src/vmm_mad/remotes/vmware/vmware_driver.rb @@ -429,11 +429,22 @@ class VMwareDriver # Append the raw datavmx to vmx file metadata = REXML::XPath.first(dfile_hash, "/domain/metadata/datavmx") - return if metadata.nil? - return if metadata.text.nil? - return if metadata.text.strip.empty? + if metadata.nil? || metadata.text.nil? || metadata.text.strip.empty? + metadata = '' + else + metadata = metadata.text + end - metadata = metadata.text + if !@reserve_memory + mem_txt = REXML::XPath.first(dfile_hash, "/domain/memory").text + mem = mem_txt.to_i/1024 + + metadata << "\\nsched.mem.min = \"#{mem}\"" + metadata << "\\nsched.mem.shares = \"normal\"" + metadata << "\\nsched.mem.pin = \"TRUE\"" + end + + return if metadata.strip.empty? # Get the ds_id for system_ds from the first disk disk = REXML::XPath.first(dfile_hash, "/domain//disk/source") @@ -447,15 +458,6 @@ class VMwareDriver path_to_vmx = "\$(find /vmfs/volumes/#{ds_id}/#{vm_id}/" path_to_vmx << " -name #{name}.vmx)" - if !@reserve_memory - mem_txt = REXML::XPath.first(dfile_hash, "/domain/memory").text - mem = mem_txt.to_i/1024 - - metadata << "\\nsched.mem.min = \"#{mem}\"" - metadata << "\\nsched.mem.shares = \"normal\"" - metadata << "\\nsched.mem.pin = \"TRUE\"" - end - metadata.gsub!("\\n","\n") sed_str = metadata.scan(/^([^ ]+) *=/).join("|") diff --git a/src/vnm_mad/remotes/OpenNebulaNetwork.rb b/src/vnm_mad/remotes/OpenNebulaNetwork.rb index 1b5a8e7209..e4c48962de 100644 --- a/src/vnm_mad/remotes/OpenNebulaNetwork.rb +++ b/src/vnm_mad/remotes/OpenNebulaNetwork.rb @@ -29,6 +29,15 @@ CONF = { :start_vlan => 2 } +def get_xen_command + if system("ps axuww | grep -v grep | grep '\\bxen\\b'") + "sudo xm" + else + "sudo xl" + end +end + + COMMANDS = { :ebtables => "sudo ebtables", :iptables => "sudo iptables", @@ -36,12 +45,14 @@ COMMANDS = { :ip => "sudo ip", :vconfig => "sudo vconfig", :virsh => "virsh -c qemu:///system", - :xm => "sudo xm", + :xm => get_xen_command, :ovs_vsctl=> "sudo ovs-vsctl", :ovs_ofctl=> "sudo ovs-ofctl", :lsmod => "lsmod" } + + # Set PATH ENV['PATH'] = "#{ENV['PATH']}:/bin:/sbin:/usr/bin"