Module: OpsWalrus::OpsFileScriptDSL

Included in:
OpsFileScript
Defined in:
lib/opswalrus/ops_file_script_dsl.rb

Instance Method Summary collapse

Instance Method Details

#current_dirObject

def ssh



211
212
213
# File 'lib/opswalrus/ops_file_script_dsl.rb', line 211

def current_dir
  File.dirname(File.realpath(@runtime_ops_file_path)).to_pathname
end

#debug(msg) ⇒ Object



278
279
280
# File 'lib/opswalrus/ops_file_script_dsl.rb', line 278

def debug(msg)
  puts msg.mustache(1) if App.instance.debug? || App.instance.trace?
end

#desc(msg) ⇒ Object



270
271
272
# File 'lib/opswalrus/ops_file_script_dsl.rb', line 270

def desc(msg)
  puts Style.green(msg.mustache(1))
end

#env(*keys) ⇒ Object



282
283
284
285
286
287
288
289
# File 'lib/opswalrus/ops_file_script_dsl.rb', line 282

def env(*keys)
  keys = keys.map(&:to_s)
  if keys.empty?
    @runtime_env.env
  else
    @runtime_env.env.dig(*keys)
  end
end

#exit(exit_status, message = nil) ⇒ Object

def reboot_and_exit(delay: 1)

# exit status 11 means Resource temporarily unavailable
exit(ExitCodeHostTemporarilyUnavailable, "Host temporarily unavailable. Rebooting.") if reboot(delay)

end



242
243
244
245
246
247
248
249
250
251
252
# File 'lib/opswalrus/ops_file_script_dsl.rb', line 242

def exit(exit_status, message = nil)
  if message
    puts message.mustache(1)
  end
  result = if exit_status == 0
    Invocation::Success.new(nil)
  else
    Invocation::EarlyExitError.new(message, exit_status)
  end
  throw :exit_now, result
end

#import(local_package_name) ⇒ Object

currently, import may only be used to import a package that is referenced in the script’s package file I may decide to extend this to work with dynamic package references

local_package_name is the local package name defined for the package dependency that is attempting to be referenced

Raises:



258
259
260
261
262
263
264
265
266
267
268
# File 'lib/opswalrus/ops_file_script_dsl.rb', line 258

def import(local_package_name)
  local_package_name = local_package_name.to_s
  package_reference = ops_file.package_file&.dependency(local_package_name)
  raise Error, "Unknown package reference: #{local_package_name}" unless package_reference
  import_reference = PackageDependencyReference.new(local_package_name, package_reference)
  namespace_or_ops_file = @runtime_env.resolve_import_reference(ops_file, import_reference)
  raise SymbolResolutionError, "Import reference '#{import_reference.summary}' not in load path for #{ops_file.ops_file_path}" unless namespace_or_ops_file
  invocation_context = LocalImportInvocationContext.new(@runtime_env, namespace_or_ops_file)
  # invocation_context = LocalImportInvocationContext.new(@runtime_env, namespace_or_ops_file)
  # invocation_context._invoke(*args, **kwargs)
end

#inventory(*args, **kwargs) ⇒ Object



215
216
217
218
219
220
221
222
# File 'lib/opswalrus/ops_file_script_dsl.rb', line 215

def inventory(*args, **kwargs)
  tags = args.map(&:to_s)

  kwargs = kwargs.transform_keys(&:to_s)
  tags.concat(kwargs["tags"]) if kwargs["tags"]

  @runtime_env.app.inventory(tags).hosts
end

#parse_stdout_and_script_return_value(command_output) ⇒ Object



416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/opswalrus/ops_file_script_dsl.rb', line 416

def parse_stdout_and_script_return_value(command_output)
  output_sections = command_output.split(/#{::OpsWalrus::App::SCRIPT_RESULT_HEADER}/)
  case output_sections.count
  when 1
    stdout, ops_script_retval = output_sections.first, nil
  when 2
    stdout, ops_script_retval = *output_sections
  else
    # this is unexpected
    ops_script_retval = output_sections.pop
    stdout = output_sections.join(::OpsWalrus::App::SCRIPT_RESULT_HEADER)
  end
  [stdout, ops_script_retval]
end

#read_secret(secret_name) ⇒ Object



291
292
293
# File 'lib/opswalrus/ops_file_script_dsl.rb', line 291

def read_secret(secret_name)
  @runtime_env.read_secret(secret_name)
end

#reboot(delay: 5) ⇒ Object

delay: integer? # default: 5 - 5 second delay before reboot



225
226
227
228
229
230
231
232
233
234
235
# File 'lib/opswalrus/ops_file_script_dsl.rb', line 225

def reboot(delay: 5)
  delay = 5 if delay < 1

  # desc "Rebooting"
  rebooting = sh? 'sudo /bin/sh -c "(sleep {{ delay }} && reboot) &"'.mustache
  # puts reboot_success

  exit(ExitCodeHostTemporarilyUnavailable, "Host temporarily unavailable. Rebooting.") if rebooting

  rebooting
end

#report_on(hostname, description = nil, cmd) ⇒ Object



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/opswalrus/ops_file_script_dsl.rb', line 352

def report_on(hostname, description = nil, cmd)
  cmd_id = Random.uuid.split('-').first

  output_block = StringIO.open do |io|
    if App.instance.info?   # this is true if log_level is trace, debug, info
      io.print Style.blue(hostname)
      io.print " | #{Style.green(description)}" if description
      io.puts
      io.print Style.yellow(cmd_id)
      io.print Style.green.bold(" > ")
      io.puts Style.yellow(cmd)
    elsif App.instance.warn? && description
      io.print Style.blue(hostname)
      io.print " | #{Style.green(description)}" if description
      io.puts
    end
    io.string
  end
  puts output_block unless output_block.empty?

  t1 = Time.now
  output, stderr, exit_status = yield
  t2 = Time.now
  seconds = t2 - t1

  stdout, remote_ops_script_retval = parse_stdout_and_script_return_value(output)

  output_block = StringIO.open do |io|
    if App.instance.info?   # this is true if log_level is trace, debug, info
      if App.instance.trace?
        io.puts Style.cyan(stdout)
        io.puts Style.red(stderr)
      elsif App.instance.debug?
        io.puts Style.cyan(stdout)
        io.puts Style.red(stderr)
      elsif App.instance.info?
        io.puts Style.cyan(stdout)
        io.puts Style.red(stderr)
      end
      io.print Style.yellow(cmd_id)
      io.print Style.blue(" | Finished in #{seconds} seconds with exit status ")
      if exit_status == 0
        io.puts Style.green("#{exit_status} (success)")
      else
        io.puts Style.red("#{exit_status} (failure)")
      end
      io.puts Style.green("*" * 80)
    elsif App.instance.warn? && description
      io.print Style.blue("Finished in #{seconds} seconds with exit status ")
      if exit_status == 0
        io.puts Style.green("#{exit_status} (success)")
      else
        io.puts Style.red("#{exit_status} (failure)")
      end
      io.puts Style.green("*" * 80)
    end
    io.string
  end
  puts output_block unless output_block.empty?

  out = remote_ops_script_retval || stdout
  [out, stderr, exit_status]
end

#sh(desc_or_cmd = nil, cmd = nil, input: nil, &block) ⇒ Object

runs the given command returns the stdout from the command



297
298
299
300
# File 'lib/opswalrus/ops_file_script_dsl.rb', line 297

def sh(desc_or_cmd = nil, cmd = nil, input: nil, &block)
  out, err, status = *shell!(desc_or_cmd, cmd, block, input: input)
  out
end

#sh?(desc_or_cmd = nil, cmd = nil, input: nil, &block) ⇒ Boolean

runs the given command returns true if the exit status was success; false otherwise

Returns:

  • (Boolean)


304
305
306
307
# File 'lib/opswalrus/ops_file_script_dsl.rb', line 304

def sh?(desc_or_cmd = nil, cmd = nil, input: nil, &block)
  out, err, status = *shell!(desc_or_cmd, cmd, block, input: input)
  status == 0
end

#shell(desc_or_cmd = nil, cmd = nil, input: nil, &block) ⇒ Object

returns the tuple: [stdout, stderr, exit_status]



310
311
312
# File 'lib/opswalrus/ops_file_script_dsl.rb', line 310

def shell(desc_or_cmd = nil, cmd = nil, input: nil, &block)
  shell!(desc_or_cmd, cmd, block, input: input)
end

#shell!(desc_or_cmd = nil, cmd = nil, block = nil, input: nil) ⇒ Object

returns the tuple: [stdout, stderr, exit_status]



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/opswalrus/ops_file_script_dsl.rb', line 315

def shell!(desc_or_cmd = nil, cmd = nil, block = nil, input: nil)
  return ["", "", 0] if !desc_or_cmd && !cmd && !block    # we were told to do nothing; like hitting enter at the bash prompt; we can do nothing successfully

  description = desc_or_cmd if cmd || block
  description = WalrusLang.render(description, block.binding) if description && block
  cmd = block.call if block
  cmd ||= desc_or_cmd

  cmd = if cmd =~ /{{.*}}/
    if block
      WalrusLang.render(cmd, block.binding)
    else
      offset = 3    # 3, because 1 references the stack frame corresponding to the caller of WalrusLang.eval,
                    # 2 references the stack frame corresponding to the caller of shell!,
                    # and 3 references the stack frame corresponding to the caller of either sh/sh?/shell
      WalrusLang.eval(cmd, offset)
    end
  else
    cmd
  end
  #cmd = Shellwords.escape(cmd)

  report_on(@runtime_env.local_hostname, description, cmd) do
    if App.instance.dry_run?
      ["", "", 0]
    else
      sshkit_cmd = @runtime_env.handle_input(input, inherit_existing_mappings: true, lookback_window_chars: DefaultLookbackWindowCharCount) do |interaction_handler|
        # puts "self=#{self.class.superclass}"
        # self is an instance of one of the dynamically defined subclasses of OpsFileScript
        App.instance.debug("OpsFileScriptDSL#shell! cmd=#{cmd} with input mappings #{interaction_handler.input_mappings.inspect} given input: #{input.inspect})")
        backend.execute_cmd(cmd, interaction_handler: interaction_handler)
      end
      [sshkit_cmd.full_stdout, sshkit_cmd.full_stderr, sshkit_cmd.exit_status]
    end
  end
end

#ssh(*args, **kwargs, &block) ⇒ Object

we run the block in the context of the host proxy object, s.t. ‘self` within the block evaluates to the host proxy object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
202
203
204
205
206
207
208
209
# File 'lib/opswalrus/ops_file_script_dsl.rb', line 140

def ssh(*args, **kwargs, &block)
  runtime_env = @runtime_env

  hosts = inventory(*args, **kwargs).map {|host| host_proxy_class.new(runtime_env, host) }
  sshkit_hosts = hosts.map(&:sshkit_host)
  sshkit_host_to_ops_host_map = sshkit_hosts.zip(hosts).to_h
  ops_file_script = local_host = self

  results_lock = Thread::Mutex.new
  results = {}
  # on sshkit_hosts do |sshkit_host|
  SSHKit::Coordinator.new(sshkit_hosts).each(in: kwargs[:in] || :parallel) do |sshkit_host|
    # in this context, self is an instance of one of the subclasses of SSHKit::Backend::Abstract, e.g. SSHKit::Backend::Netssh
    host = sshkit_host_to_ops_host_map[sshkit_host]
    sshkit_backend = self   # self is an instance of one of the subclasses of SSHKit::Backend::Abstract, e.g. SSHKit::Backend::Netssh

    ssh_password_interaction_mapping = host.ssh_password && ScopedMappingInteractionHandler.mapping_for_ssh_password_prompt(host.ssh_password)
    runtime_env.handle_input(ssh_password_interaction_mapping, inherit_existing_mappings: true) do |interaction_handler|
      App.instance.debug("OpsFileScriptDSL#ssh input mappings #{interaction_handler.input_mappings.inspect}")

      begin
        host.set_runtime_env(runtime_env)
        host.set_ops_file_script(ops_file_script)
        host.set_ssh_session_connection(sshkit_backend)

        stdout, stderr, exit_status = host._bootstrap_host(true)
        retval = if exit_status == 0
          host._zip_copy_and_run_ops_bundle(local_host, block)
        else
          puts "Failed to bootstrap #{host}. Unable to run operation."
        end

        results_lock.synchronize do
          results[host] = retval
        end

        retval
      rescue SSHKit::Command::Failed => e
        App.instance.error "[!] Command failed:"
        App.instance.error e.message
      rescue Net::SSH::ConnectionTimeout
        App.instance.error "[!] The host '#{host}' not alive!"
      rescue Net::SSH::Timeout
        App.instance.error "[!] The host '#{host}' disconnected/timeouted unexpectedly!"
      rescue Errno::ECONNREFUSED
        App.instance.error "[!] Incorrect port #{host.ssh_port} for #{host}"
      rescue Net::SSH::HostKeyMismatch => e
        App.instance.error "[!] The host fingerprint does not match the last observed fingerprint for #{host}"
        App.instance.error e.message
        App.instance.error "You might try `ssh-keygen -f ~/.ssh/known_hosts -R \"#{host}\"`"
      rescue Net::SSH::AuthenticationFailed
        App.instance.error "Wrong Password: #{host} | #{host.ssh_user}:#{host.ssh_password}"
      rescue Net::SSH::Authentication::DisallowedMethod
        App.instance.error "[!] The host '#{host}' doesn't accept password authentication method."
      rescue Errno::EHOSTUNREACH => e
        App.instance.error "[!] The host '#{host}' is unreachable"
      rescue RemoteInvocationError => e
        results[host] = e
      # rescue => e
      #   App.instance.error "[!] Command failed:"
      #   App.instance.error e.class
      #   App.instance.error e.message
      #   App.instance.error e.backtrace.join("\n")
      ensure
        host.clear_ssh_session
      end
    end # runtime_env.handle_input
  end # SSHKit::Coordinator
  results
end

#ssh_noprep(*args, **kwargs, &block) ⇒ Object

we run the block in the context of the host proxy object, s.t. ‘self` within the block evaluates to the host proxy object



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
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/opswalrus/ops_file_script_dsl.rb', line 73

def ssh_noprep(*args, **kwargs, &block)
  runtime_env = @runtime_env

  hosts = inventory(*args, **kwargs).map {|host| host_proxy_class.new(runtime_env, host) }
  sshkit_hosts = hosts.map(&:sshkit_host)
  sshkit_host_to_ops_host_map = sshkit_hosts.zip(hosts).to_h
  ops_file_script = local_host = self

  results_lock = Thread::Mutex.new
  results = {}
  # on sshkit_hosts do |sshkit_host|
  SSHKit::Coordinator.new(sshkit_hosts).each(in: kwargs[:in] || :parallel) do |sshkit_host|
    # in this context, self is an instance of one of the subclasses of SSHKit::Backend::Abstract, e.g. SSHKit::Backend::Netssh
    host = sshkit_host_to_ops_host_map[sshkit_host]
    sshkit_backend = self   # self is an instance of one of the subclasses of SSHKit::Backend::Abstract, e.g. SSHKit::Backend::Netssh

    ssh_password_interaction_mapping = host.ssh_password && ScopedMappingInteractionHandler.mapping_for_ssh_password_prompt(host.ssh_password)
    runtime_env.handle_input(ssh_password_interaction_mapping, inherit_existing_mappings: true) do |interaction_handler|
      App.instance.debug("OpsFileScriptDSL#ssh input mappings #{interaction_handler.input_mappings.inspect}")

      begin
        host.set_runtime_env(runtime_env)
        host.set_ops_file_script(ops_file_script)
        host.set_ssh_session_connection(sshkit_backend)

        # we run the block in the context of the host proxy object, s.t. `self` within the block evaluates to the host proxy object
        retval = host.instance_exec(local_host, &block)    # local_host is passed as the argument to the block

        results_lock.synchronize do
          results[host] = retval
        end

        retval
      rescue SSHKit::Command::Failed => e
        App.instance.error "[!] Command failed:"
        App.instance.error e.message
      rescue Net::SSH::ConnectionTimeout
        App.instance.error "[!] The host '#{host}' not alive!"
      rescue Net::SSH::Timeout
        App.instance.error "[!] The host '#{host}' disconnected/timeouted unexpectedly!"
      rescue Errno::ECONNREFUSED
        App.instance.error "[!] Incorrect port #{host.ssh_port} for #{host}"
      rescue Net::SSH::HostKeyMismatch => e
        App.instance.error "[!] The host fingerprint does not match the last observed fingerprint for #{host}"
        App.instance.error e.message
        App.instance.error "You might try `ssh-keygen -f ~/.ssh/known_hosts -R \"#{host}\"`"
      rescue Net::SSH::AuthenticationFailed
        App.instance.error "Wrong Password: #{host} | #{host.ssh_user}:#{host.ssh_password}"
      rescue Net::SSH::Authentication::DisallowedMethod
        App.instance.error "[!] The host '#{host}' doesn't accept password authentication method."
      rescue Errno::EHOSTUNREACH => e
        App.instance.error "[!] The host '#{host}' is unreachable"
      rescue RemoteInvocationError => e
        results[host] = e
      rescue => e
        App.instance.error e.class
        App.instance.error e.message
        App.instance.error e.backtrace.join("\n")
      ensure
        host.clear_ssh_session
      end
    end # runtime_env.handle_input
  end # SSHKit::Coordinator
  results
end

#warn(msg) ⇒ Object



274
275
276
# File 'lib/opswalrus/ops_file_script_dsl.rb', line 274

def warn(msg)
  puts Style.yellow(msg.mustache(1))
end