Class: Bolt::Result

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

Direct Known Subclasses

ApplyResult

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, error: nil, message: nil, value: nil) ⇒ Result

Returns a new instance of Result.



76
77
78
79
80
81
82
83
84
85
# File 'lib/bolt/result.rb', line 76

def initialize(target, error: nil, message: nil, value: nil)
  @target = target
  @value = value || {}
  @value_set = !value.nil?
  if error && !error.is_a?(Hash)
    raise "TODO: how did we get a string error"
  end
  @value['_error'] = error if error
  @value['_output'] = message if message
end

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



8
9
10
# File 'lib/bolt/result.rb', line 8

def target
  @target
end

#valueObject (readonly)

Returns the value of attribute value.



8
9
10
# File 'lib/bolt/result.rb', line 8

def value
  @value
end

Class Method Details

.for_command(target, stdout, stderr, exit_code) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bolt/result.rb', line 26

def self.for_command(target, stdout, stderr, exit_code)
  value = {
    'stdout' => stdout,
    'stderr' => stderr,
    'exit_code' => exit_code
  }
  unless exit_code == 0
    value['_error'] = {
      'kind' => 'puppetlabs.tasks/command-error',
      'issue_code' => 'COMMAND_ERROR',
      'msg' => "The command failed with exit code #{exit_code}",
      'details' => { 'exit_code' => exit_code }
    }
  end
  new(target, value: value)
end

.for_task(target, stdout, stderr, exit_code) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bolt/result.rb', line 43

def self.for_task(target, stdout, stderr, exit_code)
  begin
    value = JSON.parse(stdout)
    unless value.is_a? Hash
      value = nil
    end
  rescue JSON::ParserError
    value = nil
  end
  value ||= { '_output' => stdout }
  if exit_code != 0 && value['_error'].nil?
    msg = if stdout.empty?
            "The task failed with exit code #{exit_code}:\n#{stderr}"
          else
            "The task failed with exit code #{exit_code}"
          end
    value['_error'] = { 'kind' => 'puppetlabs.tasks/task-error',
                        'issue_code' => 'TASK_ERROR',
                        'msg' => msg,
                        'details' => { 'exit_code' => exit_code } }
  end
  new(target, value: value)
end

.for_upload(target, source, destination) ⇒ Object



67
68
69
# File 'lib/bolt/result.rb', line 67

def self.for_upload(target, source, destination)
  new(target, message: "Uploaded '#{source}' to '#{target.host}:#{destination}'")
end

.from_asserted_args(target, value) ⇒ Object

Satisfies the Puppet datatypes API



72
73
74
# File 'lib/bolt/result.rb', line 72

def self.from_asserted_args(target, value)
  new(target, value: value)
end

.from_exception(target, exception) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bolt/result.rb', line 10

def self.from_exception(target, exception)
  @exception = exception
  if @exception.is_a?(Bolt::Error)
    error = @exception.to_h
  else
    error = {
      'kind' => 'puppetlabs.tasks/exception-error',
      'issue_code' => 'EXCEPTION',
      'msg' => exception.message,
      'details' => { 'class' => exception.class.to_s }
    }
    error['details']['stack_trace'] = exception.backtrace.join('\n') if exception.backtrace
  end
  Result.new(target, error: error)
end

Instance Method Details

#==(other) ⇒ Object



115
116
117
# File 'lib/bolt/result.rb', line 115

def ==(other)
  eql?(other)
end

#[](key) ⇒ Object



111
112
113
# File 'lib/bolt/result.rb', line 111

def [](key)
  value[key]
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
109
# File 'lib/bolt/result.rb', line 105

def eql?(other)
  self.class == other.class &&
    target == other.target &&
    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?



146
147
148
149
150
151
# File 'lib/bolt/result.rb', line 146

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



139
140
141
# File 'lib/bolt/result.rb', line 139

def error_hash
  value['_error']
end

#generic_valueObject

TODO: what to call this it’s the value minus special keys This should be {} if a value was set otherwise it’s nil



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

def generic_value
  if @value_set
    value.reject { |k, _| %w[_error _output].include? k }
  end
end

#messageObject



87
88
89
# File 'lib/bolt/result.rb', line 87

def message
  @value['_output']
end

#ok?Boolean Also known as: ok

Returns:

  • (Boolean)


132
133
134
# File 'lib/bolt/result.rb', line 132

def ok?
  error_hash.nil?
end

#status_hashObject



91
92
93
94
95
# File 'lib/bolt/result.rb', line 91

def status_hash
  { node: @target.name,
    status: ok? ? 'success' : 'failure',
    result: @value }
end

#success?Boolean

TODO: remove in favor of ok?

Returns:

  • (Boolean)


128
129
130
# File 'lib/bolt/result.rb', line 128

def success?
  ok?
end

#to_json(opts = nil) ⇒ Object



119
120
121
# File 'lib/bolt/result.rb', line 119

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

#to_sObject



123
124
125
# File 'lib/bolt/result.rb', line 123

def to_s
  to_json
end