Class: BrpmScriptExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/brpm_script_executor.rb

Class Method Summary collapse

Class Method Details

.execute_automation_script(modul, name, params) ⇒ Object



113
114
115
# File 'lib/brpm_script_executor.rb', line 113

def execute_automation_script(modul, name, params)
  execute_automation_script_internal(modul, name, params, "automation")
end

.execute_automation_script_from_other_process(modul, name, params_file, automation_type, parent_id = nil, offset = nil, max_records = nil) ⇒ Object



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
# File 'lib/brpm_script_executor.rb', line 86

def execute_automation_script_from_other_process(modul, name, params_file, automation_type, parent_id = nil, offset = nil, max_records = nil)
  raise "Params file #{params_file} doesn't exist." unless File.exists?(params_file)

  puts "Loading the params from #{params_file} and cleaning it up..."
  params = YAML.load_file(params_file)
  FileUtils.rm(params_file)

  puts "  Setting up the BRPM Content framework..."
  BrpmAuto.setup(params)
  BrpmAuto.log "  BRPM Content framework is version #{BrpmAuto.version}."

  if BrpmAuto.params["SS_run_key"] and BrpmAuto.params["SS_script_support_path"]
    BrpmAuto.log "  Loading the BRPM core framework's libraries..."
    load File.expand_path("#{File.dirname(__FILE__)}/../infrastructure/create_output_file.rb")
  end

  result = execute_automation_script_internal(modul, name, params, automation_type, parent_id, offset, max_records)
  if automation_type == "resource_automation"
    result_file = "#{File.dirname(params_file)}/result_#{Process.pid}.yml"
    BrpmAuto.log "  Temporarily storing the result to #{result_file}..."
    FileUtils.rm(result_file) if File.exists?(result_file)
    File.open(result_file, "w") do |file|
      file.puts(result.to_yaml)
    end
  end
end

.execute_automation_script_in_separate_process(modul, name, params) ⇒ Object



9
10
11
# File 'lib/brpm_script_executor.rb', line 9

def execute_automation_script_in_separate_process(modul, name, params)
  execute_automation_script_in_separate_process_internal(modul, name, params, "automation")
end

.execute_automation_script_in_separate_process_internal(modul, name, params, automation_type, parent_id = nil, offset = nil, max_records = nil) ⇒ Object



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
# File 'lib/brpm_script_executor.rb', line 17

def execute_automation_script_in_separate_process_internal(modul, name, params, automation_type, parent_id = nil, offset = nil, max_records = nil)
  BrpmAuto.setup(params)
  BrpmAuto.log ""
  BrpmAuto.log "Executing #{automation_type} '#{name}' from module '#{modul}' in a separate process..."

  env_vars = {}
  env_vars["JRUBY_OPTS"] = "-Xcompile.invokedynamic=false -J-XX:+TieredCompilation -J-XX:TieredStopAtLevel=1 -J-noverify -Xcompile.mode=OFF" # improve jvm startup time (see http://rexchung.com/2013/08/02/speed-up-jruby-rails-startup-time/)
  env_vars["GEM_HOME"] = ENV["BRPM_CONTENT_HOME"] || "#{ENV["BRPM_HOME"]}/modules"

  BrpmAuto.log "Finding the module path..."
  case automation_type
  when "automation"
    module_version = params["module_version"] || get_latest_installed_version(modul)
  when "resource_automation"
    module_version = get_latest_installed_version(modul) #TODO: get the module version of the calling script
  else
    raise "Automation type #{automation_type} is not supported."
  end
  module_path = get_module_gem_path(modul, module_version)

  if File.exists?(module_path)
    BrpmAuto.log "Found module #{modul} #{module_version || ""} in path #{module_path}."
  else
    raise Gem::GemNotFoundException, "Module #{modul} version #{module_version} is not installed. Expected it on path #{module_path}."
  end

  gemfile_path = "#{module_path}/Gemfile"
  if File.exists?(gemfile_path)
    BrpmAuto.log "Using Gemfile #{gemfile_path}."
    env_vars["BUNDLE_GEMFILE"] = gemfile_path
    require_bundler = "require 'bundler/setup';"
  else
    BrpmAuto.log("This module doesn't have a Gemfile.")
    require_bundler = ""
  end

  working_path = File.expand_path(params["SS_output_dir"] || params["output_dir"] || Dir.pwd)
  params_file = "#{working_path}/params_#{params["SS_run_key"] || params["run_key"] || "000"}.yml"

  BrpmAuto.log "Temporarily storing the params to #{params_file}..."
  File.open(params_file, "w") do |file|
    file.puts(params.to_yaml)
  end

  BrpmAuto.log "Executing the script in a separate process..."
  return_value = Bundler.clean_system(env_vars, RbConfig.ruby, "-e", "#{require_bundler}require 'brpm_script_executor'; BrpmScriptExecutor.execute_automation_script_from_other_process(\"#{modul}\", \"#{name}\", \"#{params_file}\", \"#{automation_type}\", \"#{parent_id}\", \"#{offset}\", \"#{max_records}\")")
  FileUtils.rm(params_file) if File.exists?(params_file)
  if return_value.nil?
    message = "The process that executed the automation script returned with 'Command execution failed'."
    BrpmAuto.log_error message
    raise message
  elsif return_value == false
    message = "The process that executed the automation script returned with non-zero exit code: #{$?.exitstatus}"
    BrpmAuto.log_error message
    raise message
  end

  if automation_type == "resource_automation"
    result_file = "#{working_path}/result_#{$?.pid}.yml"
    BrpmAuto.log "Loading the result from #{result_file} and cleaning it up..."
    result = YAML.load_file(result_file)
    FileUtils.rm result_file

    result
  else
    return_value
  end
end

.execute_automation_script_internal(modul, name, params, automation_type, parent_id = nil, offset = nil, max_records = nil) ⇒ Object



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
# File 'lib/brpm_script_executor.rb', line 121

def execute_automation_script_internal(modul, name, params, automation_type, parent_id = nil, offset = nil, max_records = nil)
  begin
    BrpmAuto.setup(params)

    BrpmAuto.log ""
    BrpmAuto.log ">>>>>>>>>>>>>> START #{automation_type} #{modul} #{name}"
    start_time = Time.now

    module_spec = Gem::Specification.find_by_name(modul) # will raise an error when the module is not installed
    module_path = module_spec.gem_dir

    BrpmAuto.require_module(modul)

    automation_script_path = "#{module_path}/#{automation_type}s/#{name}.rb"
    unless File.exists?(automation_script_path)
      raise "Could not find automation #{name} in module #{modul}. Expected it on path #{automation_script_path}."
    end

    BrpmAuto.log "Executing the #{automation_type} script #{name} from #{automation_script_path}..."
    load automation_script_path

    if automation_type == "resource_automation"
      execute_script(params, parent_id, offset, max_records)
    end
  rescue Exception => e
    BrpmAuto.log_error "#{e}"
    BrpmAuto.log "\n\t" + e.backtrace.join("\n\t")

    raise e
  ensure
    stop_time = Time.now
    duration = 0
    duration = stop_time - start_time unless start_time.nil?

    BrpmAuto.log ">>>>>>>>>>>>>> STOP #{automation_type} #{modul} #{name} - total duration: #{Time.at(duration).utc.strftime("%H:%M:%S")}"
    BrpmAuto.log ""

    #load File.expand_path("#{File.dirname(__FILE__)}/../infrastructure/write_to.rb") if BrpmAuto.params.run_from_brpm
  end
end

.execute_resource_automation_script(modul, name, params, parent_id, offset, max_records) ⇒ Object



117
118
119
# File 'lib/brpm_script_executor.rb', line 117

def execute_resource_automation_script(modul, name, params, parent_id, offset, max_records)
  execute_automation_script_internal(modul, name, params, "resource_automation", parent_id, offset, max_records)
end

.execute_resource_automation_script_in_separate_process(modul, name, params, parent_id, offset, max_records) ⇒ Object



13
14
15
# File 'lib/brpm_script_executor.rb', line 13

def execute_resource_automation_script_in_separate_process(modul, name, params, parent_id, offset, max_records)
  execute_automation_script_in_separate_process_internal(modul, name, params, "resource_automation", parent_id, offset, max_records)
end

.get_gems_root_pathObject



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/brpm_script_executor.rb', line 182

def get_gems_root_path
  if ENV["BRPM_CONTENT_HOME"]
    ENV["BRPM_CONTENT_HOME"] # gemset location is overridden
  elsif ENV["BRPM_HOME"]
    "#{ENV["BRPM_HOME"]}/modules" # default gemset location when BRPM is installed
  elsif ENV["GEM_HOME"]
    ENV["GEM_HOME"] # default gemset location when BRPM is not installed
  else
    raise "Unable to find out the gems root path."
  end
end

.get_latest_installed_version(module_name) ⇒ Object

Raises:

  • (Gem::GemNotFoundException)


168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/brpm_script_executor.rb', line 168

def get_latest_installed_version(module_name)
  latest_version_path = get_module_gem_path(module_name, "latest")
  return "latest" if File.exists?(latest_version_path)

  all_version_search = get_module_gem_path(module_name, "*")
  version_paths = Dir.glob(all_version_search)

  raise Gem::GemNotFoundException, "Could not find any installed version of module #{module_name}. Expected them in #{get_module_gem_path(module_name, "*")}" if version_paths.empty?

  versions = version_paths.map { |path| File.basename(path).sub("#{module_name}-", "") }

  versions.sort{ |a, b| Gem::Version.new(a) <=> Gem::Version.new(b) }.last
end

.get_module_gem_path(module_name, module_version) ⇒ Object

These methods are used to find gems outside of the active bundle so they should not rely on any logic from the gem or bundler libraries



164
165
166
# File 'lib/brpm_script_executor.rb', line 164

def get_module_gem_path(module_name, module_version)
  "#{get_gems_root_path}/gems/#{module_name}-#{module_version}"
end