/usr/lib/one/ruby/DriverExecHelper.rb is in opennebula 4.12.3+dfsg-3.1build1.
This file is owned by root:root, with mode 0o644.
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 | # -------------------------------------------------------------------------- #
# Copyright 2002-2015, 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. #
#--------------------------------------------------------------------------- #
# This module provides an abstraction to generate an execution context for
# OpenNebula Drivers. The module has been designed to be included as part
# of a driver and not to be used standalone.
module DriverExecHelper
# Action result strings for messages
RESULT = {
:success => "SUCCESS",
:failure => "FAILURE"
}
def self.failed?(rc_str)
return rc_str == RESULT[:failure]
end
#Initialize module variables
def initialize_helper(directory, options)
@config = read_configuration
@remote_scripts_base_path = @config['SCRIPTS_REMOTE_DIR']
@local_actions = options[:local_actions]
if ENV['ONE_LOCATION'] == nil
@local_scripts_base_path = "/var/lib/one/remotes"
else
@local_scripts_base_path = "#{ENV['ONE_LOCATION']}/var/remotes"
end
# dummy paths
@remote_scripts_path = File.join(@remote_scripts_base_path, directory)
@local_scripts_path = File.join(@local_scripts_base_path, directory)
# mutex for logging
@send_mutex = Mutex.new
end
#
# METHODS FOR COMMAND LINE & ACTION PATHS
#
# Given the action name and the parameter returns full path of the script
# and appends its parameters. It uses @local_actions hash to know if the
# actions is remote or local. If the local actions has defined an special
# script name this is used, otherwise the action name in downcase is
# used as the script name.
#
# @param [String, Symbol] action name of the action
# @param [String] parameters arguments for the script
# @param [String, nil] default_name alternative name for the script
# @return [String] command line needed to execute the action
def action_command_line(action, parameters, default_name=nil)
if action_is_local? action
script_path=@local_scripts_path
else
script_path=@remote_scripts_path
end
File.join(script_path, action_script_name(action, default_name))+
" "+parameters
end
# True if the action is meant to be executed locally
#
# @param [String, Symbol] action name of the action
def action_is_local?(action)
@local_actions.include? action.to_s.upcase
end
# Name of the script file for the given action
#
# @param [String, Symbol] action name of the action
# @param [String, nil] default_name alternative name for the script
def action_script_name(action, default_name=nil)
name=@local_actions[action.to_s.upcase]
if name
name
else
default_name || action.to_s.downcase
end
end
#
# METHODS FOR LOGS & COMMAND OUTPUT
#
# Sends a message to the OpenNebula core through stdout
def send_message(action="-", result=RESULT[:failure], id="-", info="-")
@send_mutex.synchronize {
STDOUT.puts "#{action} #{result} #{id} #{info}"
STDOUT.flush
}
end
# Sends a log message to ONE. The +message+ can be multiline, it will
# be automatically splitted by lines.
def log(number, message)
in_error_message=false
msg=message.strip
msg.each_line {|line|
severity='I'
l=line.strip
if l=='ERROR MESSAGE --8<------'
in_error_message=true
next
elsif l=='ERROR MESSAGE ------>8--'
in_error_message=false
next
else
if in_error_message
severity='E'
elsif line.match(/^(ERROR|DEBUG|INFO):(.*)$/)
line=$2
case $1
when 'ERROR'
severity='E'
when 'DEBUG'
severity='D'
when 'INFO'
severity='I'
else
severity='I'
end
end
end
send_message("LOG", severity, number, line.strip)
}
end
# Generates a proc with that calls log with a hardcoded number. It will
# be used to add loging to command actions
def log_method(num)
lambda {|message|
log(num, message)
}
end
#This method returns the result in terms
def get_info_from_execution(command_exe)
if command_exe.code == 0
result = RESULT[:success]
info = command_exe.stdout
else
result = RESULT[:failure]
info = command_exe.get_error_message
end
info = "-" if info == nil || info.empty?
[result, info]
end
#
#
# Simple parser for the config file generated by OpenNebula
def read_configuration
one_config=nil
if ENV['ONE_LOCATION']
one_config = ENV['ONE_LOCATION']+'/var/config'
else
one_config = '/var/lib/one/config'
end
config=Hash.new
cfg=''
begin
open(one_config) do |file|
cfg=file.read
end
cfg.split(/\n/).each do |line|
m=line.match(/^([^=]+)=(.*)$/)
if m
name=m[1].strip.upcase
value=m[2].strip
if config[name]
if config[name].instance_of? Array
config[name] << value
else
config[name] = [config[name], value]
end
else
config[name]=value
end
end
end
rescue Exception => e
STDERR.puts "Error reading config: #{e.inspect}"
STDERR.flush
end
config
end
end
|