Class: OctocatalogDiff::CatalogUtil::ENC::Script

Inherits:
Object
  • Object
show all
Defined in:
lib/octocatalog-diff/catalog-util/enc/script.rb

Overview

Support an ENC that executes a script on this system which returns the ENC data on STDOUT.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Script

Constructor

Parameters:

  • options (Hash)

    Options - must contain script name and node name, plus tempdir if it’s a relative path

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/octocatalog-diff/catalog-util/enc/script.rb', line 17

def initialize(options)
  # Make sure the node is in the options
  raise ArgumentError, 'OctocatalogDiff::CatalogUtil::ENC::Script#new requires :node' unless options.key?(:node)
  @node = options[:node]

  # Determine path to ENC and make sure it exists
  raise ArgumentError, 'OctocatalogDiff::CatalogUtil::ENC::Script#new requires :enc' unless options.key?(:enc)
  @script = script_path(options[:enc], options[:tempdir])

  # Other options we may recognize
  @pass_env_vars = options.fetch(:pass_env_vars, [])

  # Initialize the content and error message
  @content = nil
  @error_message = 'The execute method was never run'
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



13
14
15
# File 'lib/octocatalog-diff/catalog-util/enc/script.rb', line 13

def content
  @content
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



13
14
15
# File 'lib/octocatalog-diff/catalog-util/enc/script.rb', line 13

def error_message
  @error_message
end

#scriptObject (readonly)

Returns the value of attribute script.



13
14
15
# File 'lib/octocatalog-diff/catalog-util/enc/script.rb', line 13

def script
  @script
end

Instance Method Details

#execute(logger) ⇒ Object

Executor

Parameters:

  • logger (Logger)

    Logger object

Raises:

  • (Errno::ENOENT)


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
# File 'lib/octocatalog-diff/catalog-util/enc/script.rb', line 36

def execute(logger)
  logger.debug "Beginning OctocatalogDiff::CatalogUtil::ENC::Script#execute for #{@node} with #{@script}"
  logger.debug "Passing these extra environment variables: #{@pass_env_vars}" if @pass_env_vars.any?

  # Copy the script and make it executable
  # Then run the command in the restricted environment
  raise Errno::ENOENT, "ENC #{@script} wasn't found" unless File.file?(@script)
  file = Tempfile.open('enc.sh')
  file.close
  begin
    FileUtils.cp @script, file.path
    FileUtils.chmod 0o755, file.path
    env = {
      'HOME' => ENV['HOME'],
      'PATH' => ENV['PATH'],
      'PWD' => File.dirname(@script)
    }
    @pass_env_vars.each { |var| env[var] ||= ENV[var] }
    command = [file.path, @node].map { |x| Shellwords.escape(x) }.join(' ')
    out, err, status = Open3.capture3(env, command, unsetenv_others: true, chdir: File.dirname(@script))
    logger.debug "ENC exited #{status.exitstatus}: #{out.length} bytes to STDOUT, #{err.length} bytes to STDERR"
  ensure
    file.unlink
  end

  # Analyze the output
  if status.exitstatus.zero?
    @content = out
    @error_message = nil
    logger.warn "ENC STDERR: #{err}" unless err.empty?
  else
    @content = nil
    @error_message = "ENC failed with status #{status.exitstatus}: #{out} #{err}"
    logger.error "ENC failed - Status #{status.exitstatus}"
    logger.error "Failed ENC printed this to STDOUT: #{out}" unless out.empty?
    logger.error "Failed ENC printed this to STDERR: #{err}" unless err.empty?
  end
end