2014-11-13 10:06:16 +01:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
2017-12-20 16:32:13 +01:00
|
|
|
# Do not buffer output
|
|
|
|
STDOUT.sync = TRUE
|
|
|
|
|
2014-11-13 10:06:16 +01:00
|
|
|
##############################################################################
|
|
|
|
# Environment Configuration
|
|
|
|
##############################################################################
|
|
|
|
ONE_LOCATION=ENV["ONE_LOCATION"]
|
|
|
|
USER=ENV["user"]
|
2017-05-03 11:40:08 +02:00
|
|
|
RUNVMFILE="/var/lib/one/running.bck"
|
|
|
|
TIMEOUT=20
|
|
|
|
|
|
|
|
# oneadmin user flag value
|
|
|
|
USERFLAG=-2
|
2014-11-13 10:06:16 +01:00
|
|
|
|
|
|
|
if !ONE_LOCATION
|
|
|
|
RUBY_LIB_LOCATION="/usr/lib/one/ruby"
|
|
|
|
else
|
|
|
|
RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
|
|
|
|
end
|
|
|
|
|
|
|
|
$: << RUBY_LIB_LOCATION
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
# Required libraries
|
|
|
|
##############################################################################
|
|
|
|
require 'opennebula'
|
|
|
|
require 'optparse'
|
|
|
|
|
|
|
|
include OpenNebula
|
|
|
|
|
|
|
|
MAXWAIT=60
|
|
|
|
INTERVAL=1
|
|
|
|
|
2017-12-20 16:32:13 +01:00
|
|
|
# List of supported actions
|
|
|
|
ACTIONS = [
|
|
|
|
'status', # Get the status of all VMs in OpenNebula VM pool
|
|
|
|
'suspend', # Suspend all VMs in RUNNING state
|
|
|
|
'resume', # Resume all VMs in SUSPENDED or UNKNOWN state
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
# Map each action with a target state
|
|
|
|
EXPECTED_STATUS_MAP = {
|
|
|
|
'status' => nil,
|
|
|
|
'boot' => 'runn',
|
|
|
|
'suspend' => 'susp',
|
|
|
|
'resume' => 'runn'
|
|
|
|
}
|
|
|
|
|
|
|
|
def dump_running_vms_file()
|
|
|
|
if File.exist?(RUNVMFILE)
|
|
|
|
running_vms = File.readlines(RUNVMFILE).uniq
|
|
|
|
else
|
|
|
|
running_vms = []
|
2014-11-13 10:06:16 +01:00
|
|
|
end
|
2017-12-20 16:32:13 +01:00
|
|
|
|
|
|
|
return running_vms
|
2014-11-13 10:06:16 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def CreoleGet(variable)
|
|
|
|
begin
|
|
|
|
value = `CreoleGet #{variable}`
|
|
|
|
return value
|
|
|
|
rescue
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-20 16:32:13 +01:00
|
|
|
def _do_wait(vms, action, maxwait)
|
|
|
|
if maxwait == 0 and action == 'resume'
|
|
|
|
# User explicitely don't want to wait
|
|
|
|
vms.clear
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
print "Wait #{maxwait}s for VMs to #{action}"
|
|
|
|
for try in 0..maxwait
|
|
|
|
vms.delete_if do |vm|
|
|
|
|
vm.info
|
|
|
|
vm.status == EXPECTED_STATUS_MAP[action]
|
|
|
|
end
|
|
|
|
break if vms.empty?
|
|
|
|
print "."
|
|
|
|
sleep(1)
|
|
|
|
end
|
|
|
|
if vms.empty?
|
|
|
|
puts " OK"
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
puts " FAIL"
|
|
|
|
return -1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-11-13 10:06:16 +01:00
|
|
|
#
|
|
|
|
# NAME: _do_suspend
|
|
|
|
# PARAM: OpenNebula::VirtualMachine object
|
|
|
|
# AIM: Suspend a virtual machine
|
|
|
|
#
|
2017-12-20 16:32:13 +01:00
|
|
|
def _do_suspend(vm)
|
2017-05-03 11:40:08 +02:00
|
|
|
fd = File.open(RUNVMFILE,'a')
|
2014-11-13 10:06:16 +01:00
|
|
|
if vm.status == "runn"
|
2017-12-20 16:32:13 +01:00
|
|
|
puts("Suspending #{vm.id} - #{vm.name}... ")
|
2017-05-03 11:40:08 +02:00
|
|
|
fd.write("#{vm.id}\n")
|
2017-12-20 16:32:13 +01:00
|
|
|
rc = vm.suspend
|
|
|
|
if OpenNebula.is_error?(rc)
|
|
|
|
puts rc.message
|
|
|
|
else
|
|
|
|
puts "scheduled"
|
2014-11-13 10:06:16 +01:00
|
|
|
end
|
|
|
|
end
|
2017-05-03 11:40:08 +02:00
|
|
|
fd.close
|
2014-11-13 10:06:16 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# NAME: _do_resume
|
|
|
|
# PARAM: OpenNebula::VirtualMachine object
|
|
|
|
# AIM: Resum a suspended virtual machines
|
|
|
|
#
|
2017-12-20 16:32:13 +01:00
|
|
|
def _do_resume(vm)
|
|
|
|
print("Resume #{vm.id} - #{vm.name}... ")
|
|
|
|
rc = vm.resume
|
|
|
|
if OpenNebula.is_error?(rc)
|
|
|
|
puts rc.message
|
2014-11-13 10:06:16 +01:00
|
|
|
else
|
2017-12-20 16:32:13 +01:00
|
|
|
puts "scheduled"
|
2014-11-13 10:06:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-05-03 11:40:08 +02:00
|
|
|
options = {:creds => nil, :action => nil, :endpoint => nil,
|
|
|
|
:timeout => nil}
|
2014-11-13 10:06:16 +01:00
|
|
|
|
|
|
|
parser = OptionParser.new do|opts|
|
2017-12-20 16:32:13 +01:00
|
|
|
opts.banner = "Usage: #{File.basename(__FILE__)} [options]"
|
|
|
|
opts.on('-c', '--creds file', 'Crediential file') do |value|
|
|
|
|
options[:creds] = value;
|
|
|
|
end
|
2014-11-13 10:06:16 +01:00
|
|
|
|
2017-12-20 16:32:13 +01:00
|
|
|
opts.on('-a', '--action action', 'Action to run') do |value|
|
|
|
|
options[:action] = value;
|
|
|
|
end
|
2014-11-13 10:06:16 +01:00
|
|
|
|
2017-12-20 16:32:13 +01:00
|
|
|
opts.on('-e', '--end-point url', 'End point URL') do |value|
|
|
|
|
options[:endpoint] = value;
|
|
|
|
end
|
2017-05-03 11:40:08 +02:00
|
|
|
|
2017-12-20 16:32:13 +01:00
|
|
|
opts.on('-t', '--timeout timeout', 'Timeout for opennebula connection') do |value|
|
|
|
|
options[:timeout] = value.to_i;
|
|
|
|
end
|
2014-11-13 10:06:16 +01:00
|
|
|
|
2017-12-20 16:32:13 +01:00
|
|
|
opts.on('-w', '--wait timeout', 'Wait for action ends') do |value|
|
|
|
|
options[:wait] = value.to_i
|
|
|
|
end
|
2017-05-03 11:40:08 +02:00
|
|
|
|
2017-12-20 16:32:13 +01:00
|
|
|
opts.on('-h', '--help', 'Displays Help') do
|
|
|
|
puts opts
|
|
|
|
exit
|
|
|
|
end
|
2017-05-03 11:40:08 +02:00
|
|
|
|
2014-11-13 10:06:16 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
parser.parse!
|
|
|
|
|
|
|
|
# OpenNebula credentials
|
|
|
|
if not options[:creds]
|
|
|
|
options[:creds] = "/var/lib/one/.one/one_auth"
|
|
|
|
end
|
|
|
|
|
|
|
|
if not options[:action]
|
2017-12-20 16:32:13 +01:00
|
|
|
options[:action] = "status"
|
2014-11-13 10:06:16 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if not options[:endpoint]
|
2017-12-20 16:32:13 +01:00
|
|
|
ip = CreoleGet('adresse_ip_eth0').chomp
|
|
|
|
options[:endpoint] = "http://#{ip}:2633/RPC2"
|
2014-11-13 10:06:16 +01:00
|
|
|
end
|
|
|
|
|
2017-05-03 11:40:08 +02:00
|
|
|
if not options[:timeout]
|
2017-12-20 16:32:13 +01:00
|
|
|
options[:timeout] = TIMEOUT
|
2017-05-03 11:40:08 +02:00
|
|
|
end
|
|
|
|
|
2017-12-20 16:32:13 +01:00
|
|
|
if not options[:wait]
|
|
|
|
options[:wait] = MAXWAIT
|
|
|
|
end
|
2014-11-13 10:06:16 +01:00
|
|
|
|
|
|
|
|
2017-12-20 16:32:13 +01:00
|
|
|
if not ACTIONS.include?(options[:action])
|
|
|
|
puts("Action : #{options[:action]}) is not supported")
|
|
|
|
exit(-1)
|
2014-11-13 10:06:16 +01:00
|
|
|
end
|
|
|
|
|
2017-12-20 16:32:13 +01:00
|
|
|
|
2014-11-13 10:06:16 +01:00
|
|
|
begin
|
|
|
|
File.readlines(options[:creds]).each do |line|
|
|
|
|
CREDENTIALS = line
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
puts("#{options[:creds]}: Problem loading credentials, check if file exists.")
|
2016-10-10 16:46:14 +02:00
|
|
|
exit(-1)
|
2014-11-13 10:06:16 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-12-20 16:32:13 +01:00
|
|
|
exit_code = 0
|
|
|
|
begin
|
|
|
|
client = Client.new(CREDENTIALS, options[:endpoint])
|
2017-05-03 11:40:08 +02:00
|
|
|
|
2017-12-20 16:32:13 +01:00
|
|
|
vm_pool = VirtualMachinePool.new(client, USERFLAG)
|
2014-11-13 10:06:16 +01:00
|
|
|
|
2017-12-20 16:32:13 +01:00
|
|
|
# Try to load vm pool infos from OpenNebula until timeout expires
|
2017-05-03 11:40:08 +02:00
|
|
|
rc = vm_pool.info
|
2017-12-20 16:32:13 +01:00
|
|
|
cnt = 0
|
|
|
|
while OpenNebula.is_error?(rc)
|
|
|
|
if cnt == options[:timeout]
|
|
|
|
puts rc.message
|
|
|
|
exit(-1)
|
|
|
|
end
|
|
|
|
rc = vm_pool.info
|
|
|
|
sleep(1)
|
|
|
|
cnt += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
if options[:action] == "resume"
|
|
|
|
running_vms = dump_running_vms_file()
|
|
|
|
running_vms.each do |vmid|
|
|
|
|
vm = VirtualMachine.new_with_id(vmid, client)
|
|
|
|
vm.info
|
|
|
|
_do_resume(vm)
|
|
|
|
end
|
|
|
|
|
2014-11-13 10:06:16 +01:00
|
|
|
else
|
2017-12-20 16:32:13 +01:00
|
|
|
vm_pool.each do |vm|
|
|
|
|
case options[:action]
|
|
|
|
when "status"
|
|
|
|
puts "#{vm.name}\t#{vm.status}"
|
|
|
|
|
|
|
|
when "suspend"
|
|
|
|
_do_suspend(vm)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Update list of suspended VMs
|
|
|
|
running_vms = dump_running_vms_file()
|
|
|
|
end
|
|
|
|
|
|
|
|
if options[:action] != 'status'
|
|
|
|
vms = []
|
|
|
|
running_vms.each do |vmid|
|
|
|
|
vm = VirtualMachine.new_with_id(vmid, client)
|
|
|
|
vms.push(vm)
|
|
|
|
end
|
|
|
|
exit_code = _do_wait(vms, options[:action], options[:wait])
|
|
|
|
end
|
|
|
|
|
|
|
|
if options[:action] == "resume"
|
|
|
|
if vms.empty?
|
|
|
|
File.truncate(RUNVMFILE, 0) if File.exists?(RUNVMFILE)
|
|
|
|
else
|
|
|
|
fd = File.open(RUNVMFILE,'w')
|
|
|
|
vms.each do |vm|
|
|
|
|
fd.write("#{vm.id}\n")
|
|
|
|
end
|
|
|
|
end
|
2014-11-13 10:06:16 +01:00
|
|
|
end
|
2017-12-20 16:32:13 +01:00
|
|
|
|
2014-11-13 10:06:16 +01:00
|
|
|
rescue Exception => e
|
2017-12-20 16:32:13 +01:00
|
|
|
puts e.message
|
|
|
|
puts e.backtrace
|
|
|
|
exit(-1)
|
2014-11-13 10:06:16 +01:00
|
|
|
end
|
2017-12-20 16:32:13 +01:00
|
|
|
|
|
|
|
exit(exit_code)
|
|
|
|
|
|
|
|
# Local Variables:
|
|
|
|
# ruby-indent-level: 4
|
|
|
|
# End:
|