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
47
# File 'lib/cheffish/chef_run.rb', line 34

def client
  @client ||= begin
    chef_config = self.chef_config.dup
    chef_config[:log_level] ||= :debug if !chef_config.has_key?(:log_level)
    chef_config[:verbose_logging] = false if !chef_config.has_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



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

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

#convergeObject



83
84
85
86
87
88
89
90
91
# File 'lib/cheffish/chef_run.rb', line 83

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

#converge_failed?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/cheffish/chef_run.rb', line 106

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

#converged?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/cheffish/chef_run.rb', line 102

def converged?
  !!@converged
end

#event_sinkObject



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

def event_sink
  @event_sink ||= EventSink.new
end

#logged_errorsObject



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

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

#logged_infoObject



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

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

#logged_warningsObject



65
66
67
# File 'lib/cheffish/chef_run.rb', line 65

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

#logsObject



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

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

#output_for_failure_messageObject



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

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

#resetObject



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

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

#resourcesObject



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

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



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

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

#up_to_date?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/cheffish/chef_run.rb', line 114

def up_to_date?
  !client.updated?
end

#updated?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/cheffish/chef_run.rb', line 110

def updated?
  client.updated?
end