Skip to content

Commit 1b6dd0e

Browse files
author
AJ Christensen
committed
Remove Daemons from indexer and client
Replace it with Chef::Daemon! w00t
1 parent d23c68f commit 1b6dd0e

2 files changed

Lines changed: 77 additions & 65 deletions

File tree

chef-server/bin/chef-indexer

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,50 +19,60 @@
1919
# limitations under the License.
2020

2121
require 'rubygems'
22-
require 'daemons'
2322
require 'optparse'
2423
require 'chef'
2524
require 'chef/queue'
2625
require 'chef/search_index'
26+
require 'chef/daemon'
2727

28-
trap("INT") { Chef::Log.info("SIGINT received, stopping"); exit }
28+
trap("INT") { Chef.fatal!("SIGINT received, stopping", 2) }
2929

3030
config = {
3131
:config_file => "/etc/chef/server.rb",
32-
:log_level => :info,
3332
}
3433
opts = OptionParser.new do |opts|
3534
opts.banner = "Usage: #{$0} [-d DIR|-r FILE] (options)"
3635
opts.on("-c CONFIG", "--config CONFIG", "The Chef Config file to use") do |c|
3736
config[:config_file] = c
3837
end
39-
opts.on_tail("-d", "--daemonize", "Daemonize the process") do
38+
opts.on("-u USER", "--user USER", "User to change uid to before daemonizing") do |u|
39+
config[:user] = u
40+
end
41+
opts.on("-g GROUP", "--group GROUP", "Group to change gid to before daemonizing") do |g|
42+
config[:group] = g
43+
end
44+
opts.on("-d", "--daemonize", "Daemonize the process") do
4045
config[:daemonize] = true
4146
end
42-
opts.on_tail("-l LEVEL", "--loglevel LEVEL", "Set the log level (debug, info, warn, error, fatal)") do |l|
47+
opts.on("-l LEVEL", "--loglevel LEVEL", "Set the log level (debug, info, warn, error, fatal)") do |l|
4348
config[:log_level] = l.to_sym
4449
end
50+
opts.on("-L LOGLOCATION", "--logfile LOGLOCATION", "Set the log file location, defaults to STDOUT - recommended for daemonizing") do |lf|
51+
config[:log_location] = lf
52+
end
4553
opts.on_tail("-h", "--help", "Show this message") do
4654
puts opts
4755
exit
4856
end
4957
end
5058
opts.parse!(ARGV)
5159

52-
unless File.exists?(config[:config_file]) && File.readable?(config[:config_file])
53-
puts "I cannot find or read the config file: #{config[:config_file]}"
54-
puts opts
55-
exit
60+
unless File.exists?(config[:config_file]) and File.readable?(config[:config_file])
61+
Chef.fatal!("I cannot find or read the config file: #{config[:config_file]}", 1)
5662
end
5763

58-
# Load our config file
5964
Chef::Config.from_file(config[:config_file])
60-
if config[:log_level]
61-
Chef::Log.level(config[:log_level].to_sym)
62-
end
65+
Chef::Config.configure { |c| c.merge!(config) }
6366

64-
# daemonotron!
65-
Daemons.daemonize(:ontop => !config[:daemonize])
67+
if Chef::Config[:daemonize]
68+
unless Chef::Config[:log_location].is_a? IO
69+
Chef::Log.init(Chef::Config[:log_location])
70+
end
71+
Chef::Log.level(Chef::Config[:log_level])
72+
Chef::Daemon.daemonize("chef-indexer")
73+
else
74+
Chef::Log.level(Chef::Config[:log_level])
75+
end
6676

6777
# Get a Chef::SearchIndex object
6878
indexer = Chef::SearchIndex.new

chef/bin/chef-client

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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'
2222
require 'rubygems'
2323
require 'chef'
2424
require 'chef/client'
25+
require 'chef/daemon'
2526
require 'json'
26-
require 'daemons'
27-
require 'tmpdir'
2827

2928
config = {
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
}
4031
opts = 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
7170
end
7271
opts.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)
7875
end
7976

8077
if 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)
9283
end
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

10499
if 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
108107
end
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

Comments
 (0)