Class: OpsWalrus::OperationRunner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, entry_point_ops_file) ⇒ OperationRunner

Returns a new instance of OperationRunner.



12
13
14
15
16
# File 'lib/opswalrus/operation_runner.rb', line 12

def initialize(app, entry_point_ops_file)
  @app = app
  @entry_point_ops_file = entry_point_ops_file
  # @entry_point_ops_file_in_bundle_dir = bundle!(@entry_point_ops_file)
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



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

def app
  @app
end

#entry_point_ops_fileObject

Returns the value of attribute entry_point_ops_file.



10
11
12
# File 'lib/opswalrus/operation_runner.rb', line 10

def entry_point_ops_file
  @entry_point_ops_file
end

Instance Method Details

#build_params_hash(runtime_kv_args, params_json_hash: nil) ⇒ Object

runtime_kv_args is an Array(String) of the form: [“arg1:val1”, “arg1:val2”, …] irb(main):057:0> build_params_hash([“names:foo”, “names:bar”, “names:baz”, “age:5”, “profile:name:corge”, “profile:language:en”, “height:5ft8in”])

> “bar”, “baz”], “age”=>“5”, “profile”=>{“name”=>“corge”, “language”=>“en”, “height”=>“5ft8in”}



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/opswalrus/operation_runner.rb', line 34

def build_params_hash(runtime_kv_args, params_json_hash: nil)
  runtime_kv_args.reduce(params_json_hash || {}) do |memo, kv_pair_string|
    param_name, str_value = kv_pair_string.split(":", 2)
    key, value = str_value.split(":", 2)
    if pre_existing_value = memo[param_name]
      memo[param_name] = if value    # we're dealing with a Hash parameter value
                           pre_existing_value.merge(key => try_convert(value))
                         else        # we're dealing with an Array parameter value or a scalar parameter value
                           array = pre_existing_value.is_a?(Array) ? pre_existing_value : [pre_existing_value]
                           array << try_convert(str_value)
                         end
    else
      memo[param_name] = if value    # we're dealing with a Hash parameter value
                           {key => try_convert(value)}
                         else        # we're dealing with an Array parameter value or a scalar parameter value
                           try_convert(str_value)
                         end
    end
    memo
  end
end

#run(runtime_kv_args, params_json_hash: nil) ⇒ Object

runtime_kv_args is an Array(String) of the form: [“arg1:val1”, “arg1:val2”, …] params_json_hash is a Hash representation of a JSON string returns:

Invocation::Success on success
or Invocation::EarlyExitError when the user's script intentionally exits early without running to completion
or Invocation::SshError when a connection error condition is raised
or Invocation::RuntimeError when some known error condition is raised
or Invocation::UnhandledError when some unknown error condition is raised


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
# File 'lib/opswalrus/operation_runner.rb', line 79

def run(runtime_kv_args, params_json_hash: nil)
  params_hash = build_params_hash(runtime_kv_args, params_json_hash: params_json_hash)

  App.instance.trace "Script:"
  App.instance.trace @entry_point_ops_file.script

  result = begin
    # update the bundle for the package
    # @entry_point_ops_file.package_file&.bundle!   # we don't do this here because when the script is run
                                                    # on a remote host, the package references may be invalid
                                                    # so we will be unable to bundle at runtime on the remote host
    catch(:exit_now) do
      ruby_script_return = RuntimeEnvironment.new(app).run(@entry_point_ops_file, params_hash)
      Invocation::Success.new(ruby_script_return)
    end
  rescue SSHKit::Command::Failed => e
    App.instance.error "Runtime error, command failed: #{e.message}"
    Invocation::SshError.new(e)
  rescue Error => e
    # puts "OperationRunner#run - #{Time.now.strftime('%s%L')}"
    App.instance.error "Runtime error: Ops script crashed."
    App.instance.error e.message
    App.instance.error e.backtrace.take(10).join("\n")
    Invocation::RuntimeError.new(e)
  rescue => e
    # puts "OperationRunner#run - #{Time.now.strftime('%s%L')}"
    App.instance.error "Unhandled runtime error: Ops script crashed."
    App.instance.error e.class
    App.instance.error e.message
    App.instance.error e.backtrace.take(10).join("\n")
    Invocation::UnhandledError.new(e)
  end

  result
end

#sudo_passwordObject



27
28
29
# File 'lib/opswalrus/operation_runner.rb', line 27

def sudo_password
  @app.sudo_password
end

#sudo_userObject

def bundle!(entry_point_ops_file)

path_to_entry_point_ops_file_in_bundle_dir = @app.bundler.build_bundle_for_ops_file(entry_point_ops_file)
OpsFile.new(app, path_to_entry_point_ops_file_in_bundle_dir)

end



23
24
25
# File 'lib/opswalrus/operation_runner.rb', line 23

def sudo_user
  @app.sudo_user
end

#try_convert(value) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/opswalrus/operation_runner.rb', line 56

def try_convert(value)
  case value.downcase
  when 'true'
    true
  when 'false'
    false
  when /^[0-9]+$/
    value.to_i
  when /^[0-9]+\.[0-9]+$/
    value.to_f
  else
    value
  end
end