Class: Hive::ExecutionScript
- Inherits:
-
Object
- Object
- Hive::ExecutionScript
- Defined in:
- lib/hive/execution_script.rb
Instance Method Summary collapse
- #append_bash_cmd(shell_command) ⇒ Object
- #helper_path ⇒ Object
-
#initialize(config) ⇒ ExecutionScript
constructor
A new instance of ExecutionScript.
- #prepend_bash_cmd(shell_command) ⇒ Object
- #run ⇒ Object
- #set_env(var, value) ⇒ Object
- #terminate ⇒ Object
- #unset_env(var) ⇒ Object
Constructor Details
#initialize(config) ⇒ ExecutionScript
Returns a new instance of ExecutionScript.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/hive/execution_script.rb', line 5 def initialize(config) @path = config[:file_system].executed_script_path @log_path = config[:file_system].logs_path @log = config[:log] @keep_running = config[:keep_running] @log.debug "Creating execution script with path=#{@path}" @env = { 'HIVE_SCHEDULER' => Hive.config.network.scheduler, 'HIVE_WORKING_DIRECTORY' => config[:file_system].testbed_path } @env_unset = [ 'BUNDLE_GEMFILE', 'BUNDLE_BIN_PATH', 'GEM_PATH', 'RUBYOPT', 'rvm_' ] # Environment variables that should not be made visible in the execution # script uploaded with the results @env_secure = { 'HIVE_CERT' => Hive.config.network.cert } @script_lines = [] end |
Instance Method Details
#append_bash_cmd(shell_command) ⇒ Object
35 36 37 38 |
# File 'lib/hive/execution_script.rb', line 35 def append_bash_cmd(shell_command) @log.debug "bash.rb - Appending bash command to #{@path} script: " + shell_command @script_lines << shell_command end |
#helper_path ⇒ Object
57 58 59 60 |
# File 'lib/hive/execution_script.rb', line 57 def helper_path scripts_dir = File.(File.dirname(__FILE__) + "../../../scripts/") File.join(scripts_dir, 'hive-script-helper.sh') end |
#prepend_bash_cmd(shell_command) ⇒ Object
30 31 32 33 |
# File 'lib/hive/execution_script.rb', line 30 def prepend_bash_cmd(shell_command) @log.debug "bash.rb - Prepending bash command to #{@path} script: " + shell_command @script_lines = ([] << shell_command << @script_lines).flatten end |
#run ⇒ Object
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 |
# File 'lib/hive/execution_script.rb', line 62 def run @log.info 'bash.rb - Writing script out to file' File.open(@path, 'w') do |f| f.write("#!/bin/bash --login\n") f.write(". #{helper_path}\n") f.write("# Set environment\n") @env.each do |key, value| # An escaped ' in a single quoted string in bash looks like '"'"' if value.kind_of?(Array) f.write("export #{key}=(" + value.collect { |v| "'#{v.to_s.gsub("'", '\'"\'"\'')}'" }.join(' ') + ")\n" ) else f.write("export #{key}='#{value.to_s.gsub("'", '\'"\'"\'')}'\n") end end @env_unset.each do |var| f.write("unset #{var}\n") end f.write("cd $HIVE_WORKING_DIRECTORY") f.write("\n# Test execution\n") f.write(@script_lines.join("\n")) end File.chmod(0700, @path) pid = Process.spawn @env_secure, "#{@path}", pgroup: true, in: '/dev/null', out: "#{@log_path}/stdout.log", err: "#{@log_path}/stderr.log" @pgid = Process.getpgid(pid) exit_value = nil running = true while running begin Timeout.timeout(30) do Process.wait pid exit_value = $?.exitstatus running = false end rescue Timeout::Error Process.kill(-9, @pgid) if ! ( @keep_running.nil? || @keep_running.call ) # Do something. Eg, upload log files. end end # Kill off anything that is still running terminate # Return exit value of the script exit_value end |
#set_env(var, value) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/hive/execution_script.rb', line 40 def set_env(var, value) @env[var] = value # TODO What if the element appears multiple times? if (i = @env_unset.index(var)) @env_unset.delete(i) end ## In Ruby 2, replace the above 'if' block with ... #@env_unset.remove(var) end |
#terminate ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/hive/execution_script.rb', line 110 def terminate if @pgid begin Process.kill(-9, @pgid) rescue => e @log.warn e end @pgid = nil end end |
#unset_env(var) ⇒ Object
52 53 54 55 |
# File 'lib/hive/execution_script.rb', line 52 def unset_env(var) @env.delete(var) @env_unset << var end |