11#!/usr/bin/env ruby
22#
3- # ./chef-client - Build a meal with chef
3+ # ./chef-client-new - Build a meal with chef
44#
55# Author:: Adam Jacob (<adam@opscode.com>)
66# Copyright:: Copyright (c) 2008 OpsCode, Inc.
@@ -22,26 +22,23 @@ require 'optparse'
2222require 'rubygems'
2323require 'chef'
2424require 'chef/client'
25+ require 'chef/daemon'
2526require 'json'
26- require 'daemons'
27- require 'tmpdir'
2827
2928config = {
30- :config_file => "/etc/chef/client.rb" ,
31- :log_level => :info ,
32- :json_attribs => nil ,
33- :noop => false ,
34- :validation_token => nil ,
35- :node_name => nil ,
36- :daemonize => false ,
37- :splay => nil ,
38- :interval => nil
29+ :config_file => "/etc/chef/client.rb"
3930}
4031opts = OptionParser . new do |opts |
4132 opts . banner = "Usage: #{ $0} [-d DIR|-r FILE] (options)"
4233 opts . on ( "-c CONFIG" , "--config CONFIG" , "The Chef Config file to use" ) do |c |
4334 config [ :config_file ] = c
4435 end
36+ opts . on ( "-u USER" , "--user USER" , "User to change uid to before daemonizing" ) do |u |
37+ config [ :user ] = u
38+ end
39+ opts . on ( "-g GROUP" , "--group GROUP" , "Group to change gid to before daemonizing" ) do |g |
40+ config [ :group ] = g
41+ end
4542 opts . on ( "-d" , "--daemonize" , "Daemonize the process" ) do |d |
4643 config [ :daemonize ] = true
4744 end
@@ -57,74 +54,79 @@ opts = OptionParser.new do |opts|
5754 opts . on ( "-s SECONDS" , "--splay SECONDS" , "The splay time for running at intervals, in seconds" ) do |s |
5855 config [ :splay ] = s
5956 end
60- opts . on_tail ( "-l LEVEL" , "--loglevel LEVEL" , "Set the log level (debug, info, warn, error, fatal)" ) do |l |
57+ opts . on ( "-l LEVEL" , "--loglevel LEVEL" , "Set the log level (debug, info, warn, error, fatal)" ) do |l |
6158 config [ :log_level ] = l . to_sym
6259 end
63-
64- opts . on_tail ( "-t TOKEN" , "--token TOKEN" , "Set the openid validation token" ) do |t |
60+ opts . on ( "-L LOGLOCATION" , "--logfile LOGLOCATION" , "Set the log file location, defaults to STDOUT - recommended for daemonizing" ) do |lf |
61+ config [ :log_location ] = lf
62+ end
63+ opts . on ( "-t TOKEN" , "--token TOKEN" , "Set the openid validation token" ) do |t |
6564 config [ :validation_token ] = t
6665 end
6766 opts . on_tail ( "-h" , "--help" , "Show this message" ) do
6867 puts opts
69- exit
68+ exit 0
7069 end
7170end
7271opts . parse! ( ARGV )
7372
74- unless File . exists? ( config [ :config_file ] ) && File . readable? ( config [ :config_file ] )
75- puts "I cannot find or read the config file: #{ config [ :config_file ] } "
76- puts opts
77- exit 1
73+ unless File . exists? ( config [ :config_file ] ) and File . readable? ( config [ :config_file ] )
74+ Chef . fatal! ( "I cannot find or read the config file: #{ config [ :config_file ] } " , 1 )
7875end
7976
8077if config [ :json_attribs ]
81- unless File . exists? ( config [ :json_attribs ] )
82- puts "I cannot find #{ config [ :json_attribs ] } "
83- exit 2
78+ if File . exists? ( config [ :json_attribs ] ) and File . readable? ( config [ :json_attribs ] )
79+ config [ :json_attribs ] = JSON . parse ( IO . read ( config [ :json_attribs ] ) )
80+ else
81+ Chef . fatal! ( "I cannot find or read #{ config [ :json_attribs ] } " , 2 )
8482 end
85- config [ :json_attribs ] = JSON . parse ( IO . read ( config [ :json_attribs ] ) )
86- end
87-
88- # Load our config file
89- Chef ::Config . from_file ( config [ :config_file ] )
90- if config [ :log_level ]
91- Chef ::Log . level ( config [ :log_level ] . to_sym )
9283end
93- config [ :node_name ] ||= Chef ::Config [ :node_name ]
94- config [ :daemonize ] ||= Chef ::Config [ :daemonize ]
9584
96- Chef ::Config [ :interval ] = config [ :interval ] if config [ :interval ]
97- Chef ::Config [ :splay ] = config [ :splay ] if config [ :splay ]
9885
99- Kernel . trap ( "INT" ) { Chef ::Log . fatal ( "Recieved SIGINT - exiting!" ) ; exit 2 }
86+ Chef ::Config . from_file ( config [ :config_file ] )
87+ Chef ::Config . configure { |c | c . merge! ( config ) }
10088
101- sleepy_time = 0
102- run_forever = false
89+ if Chef ::Config [ :daemonize ]
90+ unless Chef ::Config [ :log_location ] . is_a? IO
91+ Chef ::Log . init ( Chef ::Config [ :log_location ] )
92+ end
93+ Chef ::Log . level ( Chef ::Config [ :log_level ] )
94+ Chef ::Daemon . daemonize ( "chef-client" )
95+ else
96+ Chef ::Log . level ( Chef ::Config [ :log_level ] )
97+ end
10398
10499if Chef ::Config [ :interval ]
105- sleepy_time = Chef ::Config [ :interval ] . to_i + rand ( Chef ::Config [ :splay ] )
106- Chef ::Log . debug ( "Setting sleep interval to #{ sleepy_time } " )
107- run_forever = true
100+ if Chef ::Config [ :splay ]
101+ delay = Chef ::Config [ :interval ] . to_i + rand ( Chef ::Config [ :splay ] )
102+ else
103+ delay = Chef ::Config [ :interval ] . to_i
104+ end
105+ else
106+ delay = 0
108107end
109108
110- while true
109+ loop do
111110 begin
112111 c = Chef ::Client . new
113- c . json_attribs = config [ :json_attribs ]
114- c . validation_token = config [ :validation_token ] if config [ :validation_token ]
115- c . node_name = config [ :node_name ]
112+ c . json_attribs = Chef :: Config [ :json_attribs ]
113+ c . validation_token = Chef :: Config [ :validation_token ]
114+ c . node_name = Chef :: Config [ :node_name ]
116115 c . run
117- exit 0 unless run_forever
118- Chef ::Log . debug ( "Sleeping for #{ sleepy_time } seconds" )
119- sleep sleepy_time
116+ if Chef ::Config [ :daemonize ]
117+ Chef ::Log . debug ( "Sleeping for #{ delay } seconds" )
118+ sleep delay
119+ else
120+ exit 0
121+ end
120122 rescue SystemExit => e
121123 raise
122124 rescue Exception => e
123- if run_forever
125+ if Chef :: Config [ :daemonize ]
124126 Chef ::Log . error ( "#{ e . class } " )
125127 Chef ::Log . fatal ( "#{ e } \n #{ e . backtrace . join ( "\n " ) } " )
126- Chef ::Log . fatal ( "Sleeping for #{ sleepy_time } seconds before trying again" )
127- sleep sleepy_time
128+ Chef ::Log . fatal ( "Sleeping for #{ delay } seconds before trying again" )
129+ sleep delay
128130 retry
129131 else
130132 raise
0 commit comments