Merge branch 'fix/20338-start-vm-at-reboot'

This commit is contained in:
Daniel Dehennin 2017-05-10 16:00:33 +02:00
commit fe18f44a75
3 changed files with 66 additions and 21 deletions

View File

@ -76,7 +76,7 @@ EchoGras "*** Machines Virtuelles"
VMs=$( one 'onevm list -l ID,NAME,STAT' | tail -n +2 | grep -ve '\-TEST ' | awk '{print $1}' )
if [[ -z ${VMs} ]]
then
printf ". %${len_pf}s " "Pas de machines virtuelles instanciées"
printf ". %${len_pf}s " "Aucune machine virtuelle instanciée"
echo ""
else
for VM in $VMs ; do

View File

@ -1,6 +1,8 @@
[Unit]
Description=OpenNebula Node starter
After=opennebula.service opennebula-sunstone.service libvirt-bin.service
ConditionPathExists=/etc/eole/release
After=multi-user.target
[Service]
Type=oneshot
@ -9,8 +11,8 @@ Environment=ENDPOINT=http://127.0.0.1:2633/RPC2
TimeoutSec=1min
RemainAfterExit=yes
Restart=no
ExecStart=/usr/share/eole/sbin/onevm-all -w -c ${CREDS} -e ${ENDPOINT} -a "resume"
ExecStop=/usr/share/eole/sbin/onevm-all -w -c ${CREDS} -e ${ENDPOINT} -a "suspend"
ExecStart=/usr/share/eole/sbin/onevm-all -t 20 -w -c ${CREDS} -e ${ENDPOINT} -a "resume"
ExecStop=/usr/share/eole/sbin/onevm-all -t 20 -w -c ${CREDS} -e ${ENDPOINT} -a "suspend"
[Install]
WantedBy=multi-user.target

View File

@ -5,6 +5,11 @@
##############################################################################
ONE_LOCATION=ENV["ONE_LOCATION"]
USER=ENV["user"]
RUNVMFILE="/var/lib/one/running.bck"
TIMEOUT=20
# oneadmin user flag value
USERFLAG=-2
if !ONE_LOCATION
RUBY_LIB_LOCATION="/usr/lib/one/ruby"
@ -55,13 +60,16 @@ end
# AIM: Suspend a virtual machine
#
def _do_suspend(vm, wait)
fd = File.open(RUNVMFILE,'a')
if vm.status == "runn"
puts("Suspending #{vm.name} ...")
fd.write("#{vm.id}\n")
vm.suspend
if wait
_wait(vm, "susp")
end
end
fd.close
end
#
@ -69,7 +77,10 @@ end
# PARAM: OpenNebula::VirtualMachine object
# AIM: Resum a suspended virtual machines
#
def _do_resume(vm, wait)
def _do_resume(vm, wait, force=FALSE)
if force
vm.resume
else
if vm.status == "susp"
puts("Resume on #{vm.name}")
vm.resume
@ -84,13 +95,16 @@ def _do_resume(vm, wait)
else
return -1
end
end
if wait
_wait(vm, "runn")
end
end
options = {:creds => nil, :action => nil, :endpoint => nil}
options = {:creds => nil, :action => nil, :endpoint => nil,
:timeout => nil}
parser = OptionParser.new do|opts|
opts.banner = "Usage: #{File.basename(__FILE__)} [options]"
@ -106,6 +120,10 @@ parser = OptionParser.new do|opts|
options[:endpoint] = value;
end
opts.on('-t', '--timeout timeout', 'Timeout for opennebula connection') do |value|
options[:timeout] = value.to_i;
end
opts.on('-w', '--wait', 'Wait for action ends') do |w|
options[:wait] = w
end
@ -114,6 +132,8 @@ parser = OptionParser.new do|opts|
puts opts
exit
end
end
parser.parse!
@ -133,6 +153,10 @@ if not options[:endpoint]
options[:endpoint] = "http://#{ip}:2633/RPC2"
end
if not options[:timeout]
options[:timeout] = TIMEOUT
end
# Actions
SUPPORTED = ['status', 'boot', 'resume', 'shutdown', 'suspend']
@ -154,13 +178,25 @@ end
begin
client = Client.new(CREDENTIALS, options[:endpoint])
vm_pool = VirtualMachinePool.new(client, -1)
vm_pool = VirtualMachinePool.new(client, USERFLAG)
if File.exist?(RUNVMFILE)
running_vms = File.open(RUNVMFILE,'r')
else
running_vms = []
end
rc = vm_pool.info
if OpenNebula.is_error?(rc)
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
vm_pool.each do |vm|
case options[:action]
@ -175,11 +211,18 @@ begin
when "suspend"
_do_suspend(vm, options[:wait])
when "resume"
_do_resume(vm, options[:wait])
force = FALSE
if running_vms.include?('vm.id')
force = TRUE
end
_do_resume(vm, options[:wait], force)
else
puts("#{vm.name}\t#{vm.status}")
end
end
if options[:action] == "resume"
File.truncate(RUNVMFILE, 0)
end
rescue Exception => e
puts e.message
exit(-1)