Class: Chef::Application::Client

Inherits:
Chef::Application show all
Defined in:
lib/chef/application/client.rb

Instance Method Summary collapse

Methods inherited from Chef::Application

#configure_chef, exit!, fatal!, #run

Constructor Details

#initializeClient

Returns a new instance of Client.



118
119
120
121
122
123
# File 'lib/chef/application/client.rb', line 118

def initialize
  super

  @chef_client = nil
  @chef_client_json = nil
end

Instance Method Details

#configure_loggingObject



157
158
159
160
# File 'lib/chef/application/client.rb', line 157

def configure_logging
  super
  Mixlib::Authentication::Log.logger = Chef::Log.logger
end

#reconfigureObject

Reconfigure the chef client Re-open the JSON attributes and load them into the node



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/chef/application/client.rb', line 127

def reconfigure 
  super 

  Chef::Config[:chef_server_url] = config[:chef_server_url] if config.has_key? :chef_server_url
 
  if Chef::Config[:daemonize]
    Chef::Config[:interval] ||= 1800
  end

  if Chef::Config[:json_attribs]
    begin
        json_io = open(Chef::Config[:json_attribs])
    rescue SocketError => error
      Chef::Application.fatal!("I cannot connect to #{Chef::Config[:json_attribs]}", 2)
    rescue Errno::ENOENT => error
      Chef::Application.fatal!("I cannot find #{Chef::Config[:json_attribs]}", 2)
    rescue Errno::EACCES => error
      Chef::Application.fatal!("Permissions are incorrect on #{Chef::Config[:json_attribs]}. Please chmod a+r #{Chef::Config[:json_attribs]}", 2)
    rescue Exception => error
      Chef::Application.fatal!("Got an unexpected error reading #{Chef::Config[:json_attribs]}: #{error.message}", 2)
    end

    begin
      @chef_client_json = JSON.parse(json_io.read)
    rescue JSON::ParserError => error
      Chef::Application.fatal!("Could not parse the provided JSON file (#{Chef::Config[:json_attribs]})!: " + error.message, 2)
    end
  end
end

#run_applicationObject

Run the chef client, optionally daemonizing or looping at intervals.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/chef/application/client.rb', line 174

def run_application
  if Chef::Config[:version]
    puts "Chef version: #{::Chef::VERSION}"
  end

  if Chef::Config[:daemonize]
    Chef::Daemon.daemonize("chef-client")
  end
  
  loop do
    begin
      if Chef::Config[:splay]
        splay = rand Chef::Config[:splay]
        Chef::Log.debug("Splay sleep #{splay} seconds")
        sleep splay
      end

      @chef_client.run
      
      if Chef::Config[:interval]
        Chef::Log.debug("Sleeping for #{Chef::Config[:interval]} seconds")
        sleep Chef::Config[:interval]
      else
        Chef::Application.exit! "Exiting", 0
      end
    rescue SystemExit => e
      raise
    rescue Exception => e
      if Chef::Config[:interval]
        Chef::Log.error("#{e.class}")
        Chef::Log.fatal("#{e}\n#{e.backtrace.join("\n")}")
        Chef::Log.fatal("Sleeping for #{Chef::Config[:interval]} seconds before trying again")
        sleep Chef::Config[:interval]
        retry
      else
        raise
      end
    end
  end
end

#setup_applicationObject

Setup an instance of the chef client Why is this so ugly? surely the client should just read out of chef::config instead of needing the values to be assigned like this..



164
165
166
167
168
169
170
171
# File 'lib/chef/application/client.rb', line 164

def setup_application
  Chef::Daemon.change_privilege

  @chef_client = Chef::Client.new
  @chef_client.json_attribs = @chef_client_json
  @chef_client.validation_token = Chef::Config[:validation_token]
  @chef_client.node_name = Chef::Config[:node_name]   
end