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



154
155
156
# File 'lib/brpm_script_executor.rb', line 154

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



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

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 "#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}|  Loading the params from #{params_file} and cleaning it up..."
  params = YAML.load_file(params_file)
  FileUtils.rm(params_file)

  puts "#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}|  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 = params_file.sub!("params", "result")
    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
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
# 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..."

  BrpmAuto.log "Finding the module's version..."
  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

  case BrpmAuto.params["execute_automation_scripts_in_docker"]
  when "always"
    use_docker = true
  when "if_docker_image_exists"
    BrpmAuto.log "Checking if a docker image exists for bmcrlm/#{modul}:#{module_version}..."
    output = `docker history -q bmcrlm/#{modul}:#{module_version} 2>&1 >/dev/null`
    if output.empty?
      use_docker = true
    else
      BrpmAuto.log "The image doesn't exist locally, checking if we can pull it from the Docker Hub..."
      output = `docker pull bmcrlm/#{modul}:#{module_version}`
      use_docker = (output =~ /Image is up to date for/)
    end
  else
    use_docker = false
  end
  BrpmAuto.log "The automation script will be executed in a docker container." if	use_docker

  working_path = File.expand_path(BrpmAuto.params.output_dir)
  params_file = "params_#{params["SS_run_key"] || params["run_key"] || "000"}.yml"
  params_path = "#{working_path}/#{params_file}"
  automation_results_path = params["SS_automation_results_dir"] || working_path

  if use_docker
    params["SS_output_dir"] = "/workdir"
    params["SS_output_file"].sub!(working_path, "/workdir") if params["SS_output_file"]
    params["SS_automation_results_dir"] = "/automation_results"

    params["log_file"].sub!(working_path, "/workdir") if params["log_file"]
  end

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

  if use_docker
    BrpmAuto.log "Executing the script in a docker container..."
    command = "docker run -v #{working_path}:/workdir -v #{automation_results_path}:/automation_results --rm bmcrlm/#{modul}:#{module_version} /docker_execute_automation \"#{name}\" \"/workdir/#{params_file}\" \"#{automation_type}\""
    if automation_type == "resource_automation"
      command += " \"#{parent_id}\"" if parent_id
      command += " \"#{offset}\"" if offset
      command += " \"#{max_records}\"" if max_records
    end
    BrpmAuto.log command
    return_value = system(command)
  else
    env_vars = {}
    env_vars["GEM_HOME"] = ENV["BRPM_CONTENT_HOME"] || "#{ENV["BRPM_HOME"]}/modules"

    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

    BrpmAuto.log "Executing the script in a separate process..."
    return_value = Bundler.clean_system(env_vars, "ruby", "-e", "#{require_bundler}require 'brpm_script_executor'; BrpmScriptExecutor.execute_automation_script_from_other_process(\"#{modul}\", \"#{name}\", \"#{params_path}\", \"#{automation_type}\", \"#{parent_id}\", \"#{offset}\", \"#{max_records}\")")
  end

  FileUtils.rm(params_path) if File.exists?(params_path)
  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 = params_file.sub!("params", "result")
    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



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

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



158
159
160
# File 'lib/brpm_script_executor.rb', line 158

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



223
224
225
226
227
228
229
230
231
232
233
# File 'lib/brpm_script_executor.rb', line 223

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)


209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/brpm_script_executor.rb', line 209

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



205
206
207
# File 'lib/brpm_script_executor.rb', line 205

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