Class: Bolt::ContainerResult

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/container_result.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil, object: nil) ⇒ ContainerResult

First argument can’t be named given the way that Puppet deserializes variables



29
30
31
32
# File 'lib/bolt/container_result.rb', line 29

def initialize(value = nil, object: nil)
  @value = value || {}
  @object = object
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



9
10
11
# File 'lib/bolt/container_result.rb', line 9

def object
  @object
end

#valueObject (readonly)

Returns the value of attribute value.



9
10
11
# File 'lib/bolt/container_result.rb', line 9

def value
  @value
end

Class Method Details

.from_exception(exception, exit_code, image, position: []) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/bolt/container_result.rb', line 11

def self.from_exception(exception, exit_code, image, position: [])
  details = Bolt::Result.create_details(position)
  error = {
    'kind' => 'puppetlabs.tasks/container-error',
    'issue_code' => 'CONTAINER_ERROR',
    'msg' => "Error running container '#{image}': #{exception}",
    'details' => details
  }
  error['details']['exit_code'] = exit_code
  ContainerResult.new({ '_error' => error }, object: image)
end

Instance Method Details

#[](key) ⇒ Object



40
41
42
# File 'lib/bolt/container_result.rb', line 40

def [](key)
  value[key]
end

#_pcore_init_hashObject



23
24
25
26
# File 'lib/bolt/container_result.rb', line 23

def _pcore_init_hash
  { 'value' => @value,
    'object' => @image }
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/bolt/container_result.rb', line 34

def eql?(other)
  self.class == other.class &&
    value == other.value
end

#errorObject

Warning: This will fail outside of a compilation. Use error_hash inside bolt. Is it crazy for this to behave differently outside a compiler?



99
100
101
102
103
# File 'lib/bolt/container_result.rb', line 99

def error
  if error_hash
    Puppet::DataTypes::Error.from_asserted_hash(error_hash)
  end
end

#error_hashObject

This allows access to errors outside puppet compilation it should be prefered over error in bolt code



92
93
94
# File 'lib/bolt/container_result.rb', line 92

def error_hash
  value['_error']
end

#ok?Boolean Also known as: ok, success?

Returns:

  • (Boolean)


84
85
86
# File 'lib/bolt/container_result.rb', line 84

def ok?
  error_hash.nil?
end

#safe_valueObject

This is the value with all non-UTF-8 characters removed, suitable for printing or converting to JSON. It should only be possible to have non-UTF-8 characters in stdout/stderr keys as they are not allowed from tasks but we scrub the whole thing just in case.



53
54
55
56
57
58
59
60
61
62
# File 'lib/bolt/container_result.rb', line 53

def safe_value
  Bolt::Util.walk_vals(value) do |val|
    if val.is_a?(String)
      # Replace invalid bytes with hex codes, ie. \xDE\xAD\xBE\xEF
      val.scrub { |c| c.bytes.map { |b| "\\x" + b.to_s(16).upcase }.join }
    else
      val
    end
  end
end

#statusObject



80
81
82
# File 'lib/bolt/container_result.rb', line 80

def status
  ok? ? 'success' : 'failure'
end

#stderrObject



68
69
70
# File 'lib/bolt/container_result.rb', line 68

def stderr
  value['stderr']
end

#stdoutObject



64
65
66
# File 'lib/bolt/container_result.rb', line 64

def stdout
  value['stdout']
end

#to_dataObject



72
73
74
75
76
77
78
# File 'lib/bolt/container_result.rb', line 72

def to_data
  {
    "object" => object,
    "status" => status,
    "value" => safe_value
  }
end

#to_json(opts = nil) ⇒ Object Also known as: to_s



44
45
46
# File 'lib/bolt/container_result.rb', line 44

def to_json(opts = nil)
  to_data.to_json(opts)
end