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 Also known as: execute_automation_script_from_gem



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

def execute_automation_script(modul, name, params)
  begin
    BrpmAuto.setup(params)

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

    module_version = params["module_version"] || BrpmAuto.get_latest_installed_version(modul)
    module_path = BrpmAuto.get_module_gem_path(modul, module_version)

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

    BrpmAuto.require_module(modul, module_version)

    automation_script_path = "#{module_path}/automations/#{name}.rb"

    BrpmAuto.log "Executing the automation script #{automation_script_path}..."
    load automation_script_path

  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 #{name} - total duration: #{Time.at(duration).utc.strftime("%H:%M:%S")}"
    BrpmAuto.log ""

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

.execute_automation_script_from_other_process(modul, name, params_file) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/brpm_script_executor.rb', line 55

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

  puts "Loading params file #{params_file}..."
  params = YAML.load_file(params_file)

  puts "Loading the BRPM Content framework..."
  BrpmAuto.setup(params)
  BrpmAuto.log "The BRPM Content framework is loaded now. (version: #{BrpmAuto.version})"

  BrpmAuto.log "Deleting params file #{params_file}..."
  FileUtils.rm(params_file)

  if params["SS_run_key"] and params["SS_script_support_path"]
    BrpmAuto.log "Loading script_support libraries..."
    require "#{params["SS_script_support_path"]}/ssh_script_header.rb"
    require "#{params["SS_script_support_path"]}/script_helper.rb"
    require "#{params["SS_script_support_path"]}/file_in_utf.rb"
  end

  execute_automation_script(modul, name, params)
end

.execute_automation_script_in_separate_process(modul, name, params) ⇒ Object



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

def execute_automation_script_in_separate_process(modul, name, params)
  BrpmAuto.setup(params)

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

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

  env_vars = {}
  env_vars["GEM_HOME"] = ENV["BRPM_CONTENT_HOME"] || "#{ENV["BRPM_HOME"]}/modules"

  BrpmAuto.log "Finding the module path..."
  module_version = params["module_version"] || BrpmAuto.get_latest_installed_version(modul)
  module_path = BrpmAuto.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

  require_statements = ""
  gemfile_path = "#{module_path}/Gemfile"
  unless File.exists?(gemfile_path)
    BrpmAuto.log_error("This module doesn't have a Gemfile. Expected it at #{gemfile_path}.")
    return
  end

  BrpmAuto.log "Found a Gemfile (#{gemfile_path}) so the automation script will have to run inside bundler."
  env_vars["BUNDLE_GEMFILE"] = gemfile_path
  require_statements += "require 'bundler/setup'; "
  # TODO Bundler.require

  BrpmAuto.log "Executing automation script '#{name}' from module '#{modul}' in a separate process..."
  result = Bundler.clean_system(env_vars, RbConfig.ruby, "-e", "#{require_statements}; require 'brpm_script_executor'; BrpmScriptExecutor.execute_automation_script_from_other_process(\"#{modul}\", \"#{name}\", \"#{params_file}\")")
  if result.nil?
    BrpmAuto.log_error("The process that executed the automation script returned with 'Command execution failed'.")
  elsif result == false
    BrpmAuto.log_error("The process that executed the automation script  returned with non-zero exit code: #{$?.exitstatus}")
  end

  result
end

.execute_resource_automation_script(modul, name, params, parent_id, offset, max_records) ⇒ Object Also known as: execute_resource_automation_script_from_gem



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

def execute_resource_automation_script(modul, name, params, parent_id, offset, max_records)
  begin
    BrpmAuto.setup(params)

    BrpmAuto.log ""
    BrpmAuto.log ">>>>>>>>>>>>>> START resource automation #{name}"
    start_time = Time.now

    BrpmAuto.log "Loading module #{modul} and its dependencies..."
    module_path = BrpmAuto.require_module(modul) #TODO: get the module version of the calling script
    BrpmAuto.log "Finished loading the module."

    automation_script_path = "#{module_path}/resource_automations/#{name}.rb"

    BrpmAuto.log "Executing the resource automation script #{automation_script_path}..."
    load automation_script_path

    BrpmAuto.log "Calling execute_resource_automation_script(params, parent_id, offset, max_records)..."
    execute_script(params, parent_id, offset, max_records)

  rescue Exception => e
    BrpmAuto.log_error "#{e}"
    BrpmAuto.log "\n\t" + e.backtrace.join("\n\t")

    raise e
  ensure
    stop_time = Time.now
    duration = stop_time - start_time

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

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