Class: Cheffish::ChefRun

Inherits:
Object
  • Object
show all
Defined in:
lib/cheffish/chef_run.rb

Direct Known Subclasses

RSpec::RecipeRunWrapper

Defined Under Namespace

Classes: EventSink, StringIOTee

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chef_config = {}) ⇒ ChefRun

Returns a new instance of ChefRun.

Parameters:

  • chef_config (defaults to: {})

    A hash with symbol keys that looks suspiciously similar to ‘Chef::Config`. Some possible options:

    • stdout: <IO object> - where to stream stdout to

    • stderr: <IO object> - where to stream stderr to

    • log_level: :debug|:info|:warn|:error|:fatal

    • log_location: <path|IO object> - where to stream logs to

    • verbose_logging: true|false - true if you want verbose logging in :debug



14
15
16
# File 'lib/cheffish/chef_run.rb', line 14

def initialize(chef_config = {})
  @chef_config = chef_config || {}
end

Instance Attribute Details

#chef_configObject (readonly)

Returns the value of attribute chef_config.



18
19
20
# File 'lib/cheffish/chef_run.rb', line 18

def chef_config
  @chef_config
end

Instance Method Details

#clientObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cheffish/chef_run.rb', line 34

def client
  @client ||= begin
    chef_config = self.chef_config.dup
    chef_config[:log_level] ||= :debug unless chef_config.key?(:log_level)
    chef_config[:verbose_logging] = false unless chef_config.key?(:verbose_logging)
    chef_config[:stdout] = StringIOTee.new(chef_config[:stdout])
    chef_config[:stderr] = StringIOTee.new(chef_config[:stderr])
    chef_config[:log_location] = StringIOTee.new(chef_config[:log_location])
    @client = ::Cheffish::BasicChefClient.new(nil,
      [ event_sink, Chef::Formatters.new(:doc, chef_config[:stdout], chef_config[:stderr]) ],
      **chef_config)
  end
end

#compile_recipe(&recipe) ⇒ Object



83
84
85
# File 'lib/cheffish/chef_run.rb', line 83

def compile_recipe(&recipe)
  client.load_block(&recipe)
end

#convergeObject



87
88
89
90
91
92
93
# File 'lib/cheffish/chef_run.rb', line 87

def converge
  client.converge
  @converged = true
rescue RuntimeError => e
  @raised_exception = e
  raise
end

#converge_failed?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/cheffish/chef_run.rb', line 108

def converge_failed?
  @raised_exception.nil? ? false : true
end

#converged?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/cheffish/chef_run.rb', line 104

def converged?
  !!@converged
end

#event_sinkObject



48
49
50
# File 'lib/cheffish/chef_run.rb', line 48

def event_sink
  @event_sink ||= EventSink.new
end

#logged_errorsObject



71
72
73
# File 'lib/cheffish/chef_run.rb', line 71

def logged_errors
  logs.lines.select { |l| l =~ /^\[[^\]]*\] ERROR:/ }.join("\n")
end

#logged_infoObject



75
76
77
# File 'lib/cheffish/chef_run.rb', line 75

def logged_info
  logs.lines.select { |l| l =~ /^\[[^\]]*\] INFO:/ }.join("\n")
end

#logged_warningsObject



67
68
69
# File 'lib/cheffish/chef_run.rb', line 67

def logged_warnings
  logs.lines.select { |l| l =~ /^\[[^\]]*\] WARN:/ }.join("\n")
end

#logsObject



63
64
65
# File 'lib/cheffish/chef_run.rb', line 63

def logs
  @client ? client.chef_config[:log_location].string : nil
end

#output_for_failure_messageObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/cheffish/chef_run.rb', line 120

def output_for_failure_message
  message = ""
  if stdout && !stdout.empty?
    message << "---                    ---\n"
    message << "--- Chef Client Output ---\n"
    message << "---                    ---\n"
    message << stdout
    message << "\n" unless stdout.end_with?("\n")
  end
  if stderr && !stderr.empty?
    message << "---                          ---\n"
    message << "--- Chef Client Error Output ---\n"
    message << "---                          ---\n"
    message << stderr
    message << "\n" unless stderr.end_with?("\n")
  end
  if logs && !logs.empty?
    message << "---                  ---\n"
    message << "--- Chef Client Logs ---\n"
    message << "---                  ---\n"
    message << logs
  end
  message
end

#resetObject



95
96
97
98
99
100
101
102
# File 'lib/cheffish/chef_run.rb', line 95

def reset
  @client = nil
  @converged = nil
  @stdout = nil
  @stderr = nil
  @logs = nil
  @raised_exception = nil
end

#resourcesObject



79
80
81
# File 'lib/cheffish/chef_run.rb', line 79

def resources
  client.run_context.resource_collection
end

#stderrObject



59
60
61
# File 'lib/cheffish/chef_run.rb', line 59

def stderr
  @client ? client.chef_config[:stderr].string : nil
end

#stdoutObject

output



55
56
57
# File 'lib/cheffish/chef_run.rb', line 55

def stdout
  @client ? client.chef_config[:stdout].string : nil
end

#up_to_date?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/cheffish/chef_run.rb', line 116

def up_to_date?
  !client.updated?
end

#updated?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/cheffish/chef_run.rb', line 112

def updated?
  client.updated?
end