From e269f72bc70bc1678d2dd3563eac7b6e96a5669d Mon Sep 17 00:00:00 2001 From: Tino Vazquez Date: Thu, 19 Jun 2014 17:03:18 +0200 Subject: [PATCH] Feature #2989: Initial commit MS Azure drivers --- install.sh | 32 ++ share/etc/oned.conf | 21 + share/install_gems/install_gems | 2 +- src/im_mad/remotes/az.d/poll | 27 ++ src/vmm_mad/remotes/az/attach_disk | 20 + src/vmm_mad/remotes/az/attach_nic | 20 + src/vmm_mad/remotes/az/az_driver.conf | 126 ++++++ src/vmm_mad/remotes/az/az_driver.default | 55 +++ src/vmm_mad/remotes/az/az_driver.rb | 497 +++++++++++++++++++++++ src/vmm_mad/remotes/az/cancel | 29 ++ src/vmm_mad/remotes/az/deploy | 34 ++ src/vmm_mad/remotes/az/detach_disk | 20 + src/vmm_mad/remotes/az/detach_nic | 20 + src/vmm_mad/remotes/az/migrate | 20 + src/vmm_mad/remotes/az/migrate_local | 20 + src/vmm_mad/remotes/az/poll | 30 ++ src/vmm_mad/remotes/az/reboot | 29 ++ src/vmm_mad/remotes/az/reset | 20 + src/vmm_mad/remotes/az/restore | 28 ++ src/vmm_mad/remotes/az/save | 29 ++ src/vmm_mad/remotes/az/shutdown | 29 ++ src/vmm_mad/remotes/az/snapshot_create | 20 + src/vmm_mad/remotes/az/snapshot_delete | 20 + src/vmm_mad/remotes/az/snapshot_revert | 20 + 24 files changed, 1167 insertions(+), 1 deletion(-) create mode 100755 src/im_mad/remotes/az.d/poll create mode 100644 src/vmm_mad/remotes/az/attach_disk create mode 100755 src/vmm_mad/remotes/az/attach_nic create mode 100644 src/vmm_mad/remotes/az/az_driver.conf create mode 100644 src/vmm_mad/remotes/az/az_driver.default create mode 100755 src/vmm_mad/remotes/az/az_driver.rb create mode 100755 src/vmm_mad/remotes/az/cancel create mode 100755 src/vmm_mad/remotes/az/deploy create mode 100755 src/vmm_mad/remotes/az/detach_disk create mode 100755 src/vmm_mad/remotes/az/detach_nic create mode 100755 src/vmm_mad/remotes/az/migrate create mode 100755 src/vmm_mad/remotes/az/migrate_local create mode 100755 src/vmm_mad/remotes/az/poll create mode 100755 src/vmm_mad/remotes/az/reboot create mode 100755 src/vmm_mad/remotes/az/reset create mode 100755 src/vmm_mad/remotes/az/restore create mode 100755 src/vmm_mad/remotes/az/save create mode 100755 src/vmm_mad/remotes/az/shutdown create mode 100755 src/vmm_mad/remotes/az/snapshot_create create mode 100755 src/vmm_mad/remotes/az/snapshot_delete create mode 100755 src/vmm_mad/remotes/az/snapshot_revert diff --git a/install.sh b/install.sh index d44c1894b9..94885cbfad 100755 --- a/install.sh +++ b/install.sh @@ -250,12 +250,14 @@ 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/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/az \ $VAR_LOCATION/remotes/vnm \ $VAR_LOCATION/remotes/vnm/802.1Q \ $VAR_LOCATION/remotes/vnm/dummy \ @@ -410,6 +412,7 @@ 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_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 @@ -423,6 +426,7 @@ 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_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 @@ -572,6 +576,7 @@ INSTALL_ETC_FILES=( ETC_FILES:$ETC_LOCATION VMWARE_ETC_FILES:$ETC_LOCATION EC2_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 @@ -784,6 +789,28 @@ 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 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" + #------------------------------------------------------------------------------- # Information Manager Probes, to be installed under $REMOTES_LOCATION/im #------------------------------------------------------------------------------- @@ -831,6 +858,8 @@ 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_AZ_FILES="src/im_mad/remotes/az.d/poll" + IM_PROBES_VERSION="src/im_mad/remotes/VERSION" #------------------------------------------------------------------------------- @@ -1103,6 +1132,9 @@ 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" +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 880ac93b27..c06be8c2f1 100644 --- a/share/etc/oned.conf +++ b/share/etc/oned.conf @@ -319,6 +319,15 @@ IM_MAD = [ # arguments = "-c -t 1 -r 0 ec2" ] #------------------------------------------------------------------------------- +#------------------------------------------------------------------------------- +# Azure Information Driver Manager Configuration +#------------------------------------------------------------------------------- +#IM_MAD = [ +# name = "az", +# executable = "one_im_sh", +# arguments = "-c -t 1 -r 0 az" ] +#------------------------------------------------------------------------------- + #------------------------------------------------------------------------------- # Dummy Information Driver Manager Configuration #------------------------------------------------------------------------------- @@ -419,6 +428,18 @@ VM_MAD = [ # 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 #------------------------------------------------------------------------------- diff --git a/share/install_gems/install_gems b/share/install_gems/install_gems index 8b779ec546..e49c3e3947 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{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/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/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..fb5a80001b --- /dev/null +++ b/src/vmm_mad/remotes/az/az_driver.rb @@ -0,0 +1,497 @@ +#!/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) + az_info = get_deployment_info(host, xml_text) + + load_default_template_values + + if !az_value(az_info, 'IMAGE') + STDERR.puts("Cannot find IMAGE in deployment file") + exit(-1) + end + + csn = az_value(az_info, 'CLOUD_SERVICE_NAME') + + csn = "OpenNebulaDefaultCloudServiceName" if !csn + + create_params = create_params(id,csn,az_info) + create_options = create_options(id,csn,az_info) + + begin + instance = @azure_vms.create_virtual_machine(create_params, + create_options) + rescue => e + STDERR.puts(e.message) + exit(-1) + end + + puts(create_params[:vm_name]) + 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-#####" 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 + + + 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? + info << "AZ_#{key.to_s.upcase}=\"#{value.sub("\"","'")}\ " + end + } + + info + 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') + } + 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') + } + 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) + i = get_instance(deploy_id) + + begin + i.send(AZ[az_action][:cmd]) + 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 + + # Retrive the instance from Azure + def get_instance(id) + begin + name_and_csn = id.match(/([^_]+)_(.+)/) + name = name_and_csn[1] + csn = name_and_csn[2] + + instance = @azure_vms.get_virtual_machine(name,csn) + if instance.exists? + return instance + else + raise "Instance #{id} 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