2021-07-02 10:22:33 +02:00
#!/usr/bin/env ruby
# -------------------------------------------------------------------------- #
2022-04-07 19:49:58 +02:00
# Copyright 2002-2022, OpenNebula Project, OpenNebula Systems #
2021-07-02 10:22:33 +02:00
# #
# 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. #
#--------------------------------------------------------------------------- #
##############################################################################
# Hook configuration
##############################################################################
# - Create /var/lib/one/remotes/hooks/autostart folder if it doesn't exist
# - Create hook template as follows:
# $ cat > autostart-vm.tmpl <<EOF
# NAME = autostart-vm
# TYPE = state
# COMMAND = autostart/vm
# ARGUMENTS = \$TEMPLATE
# ARGUMENTS_STDIN = yes
# RESOURCE = VM
# STATE = POWEROFF
# LCM_STATE = LCM_INIT
# ON = CUSTOM
# EOF
# - Create hook as follows:
# $ onehook create autostart-vm.tmpl
##############################################################################
##############################################################################
# Environment Configuration
##############################################################################
2021-07-22 16:29:26 +02:00
ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION)
2021-07-02 10:22:33 +02:00
if !ONE_LOCATION
2021-07-22 16:29:26 +02:00
LIB_LOCATION ||= '/usr/lib/one'
RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby'
GEMS_LOCATION ||= '/usr/share/one/gems'
LOG_LOCATION ||= '/var/log/one'
LOG_FILE ||= '/var/log/one/autostart-vm.log'
2021-07-02 10:22:33 +02:00
else
2021-07-22 16:29:26 +02:00
LIB_LOCATION ||= ONE_LOCATION + '/lib'
RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby'
GEMS_LOCATION ||= ONE_LOCATION + '/share/gems'
LOG_LOCATION ||= ONE_LOCATION + '/var'
LOG_FILE ||= ONE_LOCATION + 'autostart-vm.log'
2021-07-02 10:22:33 +02:00
end
2021-07-22 16:29:26 +02:00
# %%RUBYGEMS_SETUP_BEGIN%%
2021-07-02 10:22:33 +02:00
if File.directory?(GEMS_LOCATION)
real_gems_path = File.realpath(GEMS_LOCATION)
if !defined?(Gem) || Gem.path != [real_gems_path]
$LOAD_PATH.reject! {|l| l =~ /vendor_ruby/ }
2021-07-22 16:29:26 +02:00
# Suppress warnings from Rubygems
# https://github.com/OpenNebula/one/issues/5379
begin
verb = $VERBOSE
$VERBOSE = nil
require 'rubygems'
Gem.use_paths(real_gems_path)
ensure
$VERBOSE = verb
end
2021-07-02 10:22:33 +02:00
end
end
2021-07-22 16:29:26 +02:00
# %%RUBYGEMS_SETUP_END%%
2021-07-02 10:22:33 +02:00
$LOAD_PATH << RUBY_LIB_LOCATION
require 'opennebula'
2021-07-22 16:29:26 +02:00
# rubocop:disable Style/MixinUsage
2021-07-02 10:22:33 +02:00
include OpenNebula
2021-07-22 16:29:26 +02:00
# rubocop:enable Style/MixinUsage
2021-07-02 10:22:33 +02:00
require 'getoptlong'
require 'base64'
require 'open3'
################################################################################
# Arguments
################################################################################
# Get arguments from standard input
2021-07-22 16:29:26 +02:00
standard_input = $stdin.read
2021-07-02 10:22:33 +02:00
ARGV.replace(standard_input.split(' '))
raw_vm_template = Base64.decode64(ARGV[0])
xml_vm_template = Nokogiri::XML(raw_vm_template)
VM_ID = xml_vm_template.xpath('VM/ID').text
################################################################################
# Methods
################################################################################
2021-07-08 16:59:05 +02:00
def log(msg, level = 'I')
2021-07-02 10:22:33 +02:00
File.open(LOG_FILE, 'a') do |f|
msg.lines do |l|
f.puts "[#{Time.now}][VM #{VM_ID}][#{level}] #{l}"
end
end
end
def log_error(msg)
2021-07-08 16:59:05 +02:00
log(msg, 'E')
2021-07-02 10:22:33 +02:00
end
def exit_error
2021-07-08 16:59:05 +02:00
log_error('Exiting due to previous error.')
2021-07-02 10:22:33 +02:00
exit(-1)
end
################################################################################
# Main
################################################################################
2021-07-08 16:59:05 +02:00
log 'OpenNebula Autostart VM Hook launched'
2021-07-02 10:22:33 +02:00
begin
2021-07-08 16:59:05 +02:00
client = Client.new
rescue StandardError => e
2021-07-02 10:22:33 +02:00
log_error e.to_s
exit_error
end
vm = OpenNebula::VirtualMachine.new_with_id(VM_ID, client)
rc = vm.info
if OpenNebula.is_error?(rc)
2021-07-08 16:59:05 +02:00
log_error rc.message.to_s
2021-07-02 10:22:33 +02:00
exit_error
end
2021-07-08 16:59:05 +02:00
log vm.name.to_s
2021-07-02 10:22:33 +02:00
# Skip if AUTOSTART not enabled
autostart = vm['USER_TEMPLATE/AUTOSTART']
2021-07-22 16:29:26 +02:00
if !autostart ||
(autostart.downcase != 'true' && autostart.downcase != 'yes')
2021-07-08 16:59:05 +02:00
log 'skip: autostart not enabled'
2021-07-02 10:22:33 +02:00
exit 0
end
# ACTION in last history record of guest is equal to 'monitor' if an active VM
# was powered off by monitor.
# Skip if VM is not poweroff by monitor
2021-07-08 16:59:05 +02:00
last_action = vm['HISTORY_RECORDS/HISTORY[last()]/ACTION']
2021-07-02 10:22:33 +02:00
last_action_str = OpenNebula::VirtualMachine.get_history_action(last_action)
if last_action_str != 'monitor'
log "skip: last_action (#{last_action_str}) is not 'monitor'"
exit 0
end
# Autostart VM
2021-07-08 16:59:05 +02:00
log 'resume'
2021-07-02 10:22:33 +02:00
rc = vm.resume
2021-07-08 16:59:05 +02:00
log_error rc.message.to_s if OpenNebula.is_error?(rc)
2021-07-02 10:22:33 +02:00
exit 0