Module: DriverExecHelper
- Included in:
- OpenNebulaDriver
- Defined in:
- lib/DriverExecHelper.rb
Overview
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.
Constant Summary collapse
- RESULT =
Action result strings for messages
{ :success => 'SUCCESS', :failure => 'FAILURE' }
Class Method Summary collapse
Instance Method Summary collapse
-
#action_command_line(action, parameters, default_name = nil, directory = '') ⇒ String
METHODS FOR COMMAND LINE & ACTION PATHS.
-
#action_is_local?(action, driver = '') ⇒ Boolean
True if the action is meant to be executed locally.
-
#action_script_name(action, default_name = nil) ⇒ Object
Name of the script file for the given action.
-
#get_info_from_execution(command_exe) ⇒ Object
This method returns the result in terms.
-
#initialize_helper(directory, options) ⇒ Object
Initialize module variables.
-
#log(number, message, all = true) ⇒ Object
Sends a log message to ONE.
-
#log_method(num) ⇒ Object
Generates a proc with that calls log with a hardcoded number.
-
#read_configuration ⇒ Object
Simple parser for the config file generated by OpenNebula.
-
#send_message(action = '-', result = , id = '-', info = '-') ⇒ Object
METHODS FOR LOGS & COMMAND OUTPUT.
Class Method Details
.failed?(rc_str) ⇒ Boolean
28 29 30 |
# File 'lib/DriverExecHelper.rb', line 28 def self.failed?(rc_str) rc_str == RESULT[:failure] end |
Instance Method Details
#action_command_line(action, parameters, default_name = nil, directory = '') ⇒ String
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. When action is a String starting with ‘/’ it’s considered alreay full path command and no modification is performed apart from adding params.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/DriverExecHelper.rb', line 70 def action_command_line(action, parameters, default_name = nil, directory = '') if action.is_a?(String) && action[0] == '/' return action + ' ' + parameters if parameters return action elsif action_is_local?(action, directory) script_path=File.join(@local_scripts_path, directory) else script_path=File.join(@remote_scripts_path, directory) end File.join(script_path, action_script_name(action, default_name))+ ' '+parameters end |
#action_is_local?(action, driver = '') ⇒ Boolean
True if the action is meant to be executed locally
91 92 93 94 95 96 |
# File 'lib/DriverExecHelper.rb', line 91 def action_is_local?(action, driver = '') @local_actions.include? action.to_s.upcase if driver.empty? @local_actions.include? action.to_s.upcase or @per_drvr_local_actions.include? "#{driver}-#{action}" end |
#action_script_name(action, default_name = nil) ⇒ Object
Name of the script file for the given action
102 103 104 105 106 |
# File 'lib/DriverExecHelper.rb', line 102 def action_script_name(action, default_name = nil) name=@local_actions[action.to_s.upcase] name || default_name || action.to_s.downcase end |
#get_info_from_execution(command_exe) ⇒ Object
This method returns the result in terms
157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/DriverExecHelper.rb', line 157 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. end info = '-' if info.nil? || info.empty? [result, info] end |
#initialize_helper(directory, options) ⇒ Object
Initialize module variables
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/DriverExecHelper.rb', line 33 def initialize_helper(directory, ) @config = read_configuration @remote_scripts_base_path = @config['SCRIPTS_REMOTE_DIR'] @local_actions = [:local_actions] @per_drvr_local_actions = [:per_drvr_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 |
#log(number, message, all = true) ⇒ Object
Sends a log message to ONE. The message
can be multiline, it will be automatically splitted by lines.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/DriverExecHelper.rb', line 125 def log(number, , all = true) msg = .strip msg.each_line do |line| all ? severity='I' : severity=nil if line.match(/^(ERROR|DEBUG|INFO):(.*)$/) line=Regexp.last_match(2) case Regexp.last_match(1) when 'ERROR' severity='E' when 'DEBUG' severity='D' when 'INFO' severity='I' end end ('LOG', severity, number, line.strip) if severity end end |
#log_method(num) ⇒ Object
Generates a proc with that calls log with a hardcoded number. It will be used to add loging to command actions
150 151 152 153 154 |
# File 'lib/DriverExecHelper.rb', line 150 def log_method(num) lambda {|, all = true| log(num, , all) } end |
#read_configuration ⇒ Object
Simple parser for the config file generated by OpenNebula
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 |
# File 'lib/DriverExecHelper.rb', line 174 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 = {} cfg = '' begin File.open(one_config, 'r') do |file| cfg=file.read end cfg.split("\n").each do |line| m=line.match(/^([^=]+)=(.*)$/) next unless 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 rescue StandardError => e STDERR.puts "Error reading config: #{e.inspect}" STDERR.flush end config end |
#send_message(action = '-', result = , id = '-', info = '-') ⇒ Object
METHODS FOR LOGS & COMMAND OUTPUT
Sends a message to the OpenNebula core through stdout rubocop:disable Metrics/ParameterLists
113 114 115 116 117 118 119 120 |
# File 'lib/DriverExecHelper.rb', line 113 def (action = '-', result = RESULT[:failure], id = '-', info = '-') @send_mutex.synchronize do STDOUT.puts "#{action} #{result} #{id} #{info}" STDOUT.flush end end |