/usr/lib/one/mads/one_vmm_ec2.rb is in opennebula 3.2.1-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | #!/usr/bin/env ruby
# ---------------------------------------------------------------------------- #
# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# ---------------------------------------------------------------------------- #
# Set up the environment for the driver
EC2_LOCATION = ENV["EC2_HOME"]
if !EC2_LOCATION
puts "EC2_HOME not set"
exit(-1)
end
EC2_JVM_CONCURRENCY = ENV["EC2_JVM_CONCURRENCY"]
ONE_LOCATION = ENV["ONE_LOCATION"]
if !ONE_LOCATION
RUBY_LIB_LOCATION = "/usr/lib/one/ruby"
ETC_LOCATION = "/etc/one/"
else
RUBY_LIB_LOCATION = ONE_LOCATION + "/lib/ruby"
ETC_LOCATION = ONE_LOCATION + "/etc/"
end
$: << RUBY_LIB_LOCATION
require 'pp'
require "VirtualMachineDriver"
require "CommandManager"
require "rexml/document"
# The main class for the EC2 driver
class EC2Driver < VirtualMachineDriver
# EC2 commands constants
EC2 = {
:run => "#{EC2_LOCATION}/bin/ec2-run-instances",
:terminate => "#{EC2_LOCATION}/bin/ec2-terminate-instances",
:describe => "#{EC2_LOCATION}/bin/ec2-describe-instances",
:associate => "#{EC2_LOCATION}/bin/ec2-associate-address",
:reboot => "#{EC2_LOCATION}/bin/ec2-reboot-instances",
:authorize => "#{EC2_LOCATION}/bin/ec2-authorize"
}
# EC2 constructor, loads defaults for the EC2Driver
def initialize(ec2_conf = nil)
if !EC2_JVM_CONCURRENCY
concurrency = 5
else
concurrency = EC2_JVM_CONCURRENCY.to_i
end
super('',
:concurrency => concurrency,
:threaded => true
)
@defaults = Hash.new
if ec2_conf && File.exists?(ec2_conf)
fd = File.new(ec2_conf)
xml = REXML::Document.new fd
fd.close()
return if !xml || !xml.root
ec2 = xml.root.elements["EC2"]
return if !ec2
@defaults["KEYPAIR"] = ec2_value(ec2,"KEYPAIR")
@defaults["AUTHORIZEDPORTS"] = ec2_value(ec2,"AUTHORIZEDPORTS")
@defaults["INSTANCETYPE"] = ec2_value(ec2,"INSTANCETYPE")
end
end
# DEPLOY action, also sets ports and ip if needed
def deploy(id, drv_message)
msg = decode(drv_message)
host = msg.elements["HOST"].text
local_dfile = msg.elements["LOCAL_DEPLOYMENT_FILE"].text
if !local_dfile
send_message(ACTION[:deploy],RESULT[:failure],id,
"Can not open deployment file #{local_dfile}")
return
end
tmp = File.new(local_dfile)
xml = REXML::Document.new tmp
tmp.close()
ec2 = nil
all_ec2_elements = xml.root.get_elements("EC2")
# First, let's see if we have an EC2 site that matches
# our desired host name
all_ec2_elements.each { |element|
cloud=element.elements["CLOUD"]
if cloud and cloud.text.upcase == host.upcase
ec2 = element
end
}
if !ec2
# If we don't find the EC2 site, and ONE just
# knows about one EC2 site, let's use that
if all_ec2_elements.size == 1
ec2 = all_ec2_elements[0]
else
send_message(ACTION[:deploy],RESULT[:failure],id,
"Can not find EC2 element in deployment file "<<
"#{local_dfile} or couldn't find any EC2 site matching "<<
"one of the template.")
return
end
end
ami = ec2_value(ec2,"AMI")
keypair = ec2_value(ec2,"KEYPAIR")
eip = ec2_value(ec2,"ELASTICIP")
ports = ec2_value(ec2,"AUTHORIZEDPORTS")
type = ec2_value(ec2,"INSTANCETYPE")
if !ami
send_message(ACTION[:deploy],RESULT[:failure],id,
"Can not find AMI in deployment file #{local_dfile}")
return
end
deploy_cmd = "#{EC2[:run]} #{ami}"
deploy_cmd << " -k #{keypair}" if keypair
deploy_cmd << " -t #{type}" if type
deploy_exe = LocalCommand.run(deploy_cmd, log_method(id))
if deploy_exe.code != 0
send_message(ACTION[:deploy],RESULT[:failure],id)
return
end
if !deploy_exe.stdout.match(/^INSTANCE\s*(.+?)\s/)
send_message(ACTION[:deploy],RESULT[:failure],id,
"Could not find instance id. Check ec2-describe-instances")
return
end
deploy_id = $1
if eip
ip_cmd = "#{EC2[:associate]} #{eip} -i #{deploy_id}"
ip_exe = LocalCommand.run(ip_cmd, log_method(id))
end
if ports
ports_cmd = "#{EC2[:authorize]} default -p #{ports}"
ports_exe = LocalCommand.run(ports_cmd, log_method(id))
end
send_message(ACTION[:deploy],RESULT[:success],id,deploy_id)
end
# Shutdown a EC2 instance
def shutdown(id, drv_message)
msg = decode(drv_message)
host = msg.elements["HOST"].text
deploy_id = msg.elements["DEPLOY_ID"].text
ec2_terminate(ACTION[:shutdown], id, deploy_id)
end
# Reboot a EC2 instance
def reboot(id, drv_message)
cmd = "#{EC2_LOCATION}/bin/ec2-reboot-instances #{deploy_id}"
exe = LocalCommand.run(cmd, log_method(id))
if exe.code != 0
result = RESULT[:failure]
else
result = RESULT[:success]
end
send_message(action,result,id)
end
# Cancel a EC2 instance
def cancel(id, drv_message)
msg = decode(drv_message)
host = msg.elements["HOST"].text
deploy_id = msg.elements["DEPLOY_ID"].text
ec2_terminate(ACTION[:cancel], id, deploy_id)
end
# Get info (IP, and state) for a EC2 instance
def poll(id, drv_message)
msg = decode(drv_message)
host = msg.elements["HOST"].text
deploy_id = msg.elements["DEPLOY_ID"].text
info = "#{POLL_ATTRIBUTE[:usedmemory]}=0 " \
"#{POLL_ATTRIBUTE[:usedcpu]}=0 " \
"#{POLL_ATTRIBUTE[:nettx]}=0 " \
"#{POLL_ATTRIBUTE[:netrx]}=0"
cmd = "#{EC2[:describe]} #{deploy_id}"
exe = LocalCommand.run(cmd, log_method(id))
if exe.code != 0
send_message(ACTION[:poll],RESULT[:failure],id)
return
end
exe.stdout.match(Regexp.new("INSTANCE\\s+#{deploy_id}\\s+(.+)"))
if !$1
info << " #{POLL_ATTRIBUTE[:state]}=#{VM_STATE[:deleted]}"
else
monitor_data = $1.split(/\s+/)
case monitor_data[3]
when "pending"
info << " #{POLL_ATTRIBUTE[:state]}=#{VM_STATE[:active]}"
when "running"
info<<" #{POLL_ATTRIBUTE[:state]}=#{VM_STATE[:active]}"<<
" IP=#{monitor_data[1]}"
when "shutting-down","terminated"
info << " #{POLL_ATTRIBUTE[:state]}=#{VM_STATE[:deleted]}"
end
end
send_message(ACTION[:poll], RESULT[:success], id, info)
end
private
def ec2_terminate(action, id, deploy_id)
cmd = "#{EC2_LOCATION}/bin/ec2-terminate-instances #{deploy_id}"
exe = LocalCommand.run(cmd, log_method(id))
if exe.code != 0
result = RESULT[:failure]
else
result = RESULT[:success]
end
send_message(action,result,id)
end
def ec2_value(xml,name)
value = nil
element = xml.elements[name]
value = element.text.strip if element && element.text
if !value
value = @defaults[name]
end
return value
end
end
# EC2Driver Main program
ec2_conf = ARGV.last
if ec2_conf
ec2_conf = ETC_LOCATION + ec2_conf if ec2_conf[0] != ?/
end
ec2_driver = EC2Driver.new(ec2_conf)
ec2_driver.start_driver
|