Skip to content

Commit 0fae40c

Browse files
author
AJ Christensen
committed
CHEF-151: Chef Indexer application class
More specs Fix problem with chef_repo require rake
1 parent 5ea212f commit 0fae40c

9 files changed

Lines changed: 203 additions & 256 deletions

File tree

chef-server/bin/chef-indexer

Lines changed: 6 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env ruby
22
#
3-
# ./chef-indexer - Build indexes from Chef Queues!
3+
# ./chef-indexer - Run the chef indexer
44
#
5-
# Author:: Adam Jacob (<adam@opscode.com>)
5+
# Author:: AJ Christensen (<aj@opscode.com>)
66
# Copyright:: Copyright (c) 2008 Opscode, Inc.
77
# License:: Apache License, Version 2.0
88
#
@@ -18,83 +18,9 @@
1818
# See the License for the specific language governing permissions and
1919
# limitations under the License.
2020

21-
require 'rubygems'
22-
require 'optparse'
23-
require 'chef'
24-
require 'chef/queue'
25-
require 'chef/search_index'
26-
require 'chef/daemon'
27-
28-
trap("INT") { Chef::Application.fatal!("SIGINT received, stopping", 2) }
29-
30-
config = {
31-
:config_file => "/etc/chef/server.rb",
32-
}
33-
opts = OptionParser.new do |opts|
34-
opts.banner = "Usage: #{$0} (options)"
35-
opts.on("-c CONFIG", "--config CONFIG", "The Chef Config file to use") do |c|
36-
config[:config_file] = c
37-
end
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
45-
config[:daemonize] = true
46-
end
47-
opts.on("-l LEVEL", "--loglevel LEVEL", "Set the log level (debug, info, warn, error, fatal)") do |l|
48-
config[:log_level] = l.to_sym
49-
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
53-
opts.on_tail("-h", "--help", "Show this message") do
54-
puts opts
55-
exit
56-
end
57-
end
58-
opts.parse!(ARGV)
59-
60-
unless File.exists?(config[:config_file]) and File.readable?(config[:config_file])
61-
Chef::Application.fatal!("I cannot find or read the config file: #{config[:config_file]}", 1)
62-
end
21+
$: << File.join(File.dirname(__FILE__), "..", "lib")
6322

64-
Chef::Config.from_file(config[:config_file])
65-
Chef::Config.configure { |c| c.merge!(config) }
66-
67-
Chef::Daemon.change_privilege
68-
69-
if Chef::Config[:daemonize]
70-
unless Chef::Config[:log_location].is_a? IO
71-
Chef::Log.init(Chef::Config[:log_location])
72-
end
73-
Chef::Log.level(Chef::Config[:log_level])
74-
Chef::Daemon.daemonize("chef-indexer")
75-
else
76-
Chef::Log.level(Chef::Config[:log_level])
77-
end
23+
require 'rubygems'
24+
require 'chef/application/Indexer'
7825

79-
# Get a Chef::SearchIndex object
80-
indexer = Chef::SearchIndex.new
81-
Chef::Queue.connect
82-
Chef::Queue.subscribe(:queue, "index")
83-
Chef::Queue.subscribe(:queue, "remove")
84-
loop do
85-
object, headers = Chef::Queue.receive_msg
86-
Chef::Log.info("Headers #{headers.inspect}")
87-
if headers["destination"] == "/queue/chef/index"
88-
start_timer = Time.new
89-
indexer.add(object)
90-
indexer.commit
91-
final_timer = Time.new
92-
Chef::Log.info("Indexed object from #{headers['destination']} in #{final_timer - start_timer} seconds")
93-
elsif headers["destination"] == "/queue/chef/remove"
94-
start_timer = Time.new
95-
indexer.delete(object)
96-
indexer.commit
97-
final_timer = Time.new
98-
Chef::Log.info("Removed object from #{headers['destination']} in #{final_timer - start_timer} seconds")
99-
end
100-
end
26+
Chef::Application::Indexer.new.run

chef/bin/chef-client

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# ./chef-client - Run the chef client
44
#
5-
# Author:: AJ Christensen (<adam@opscode.com>)
5+
# Author:: AJ Christensen (<aj@opscode.com>)
66
# Copyright:: Copyright (c) 2008 Opscode, Inc.
77
# License:: Apache License, Version 2.0
88
#

chef/bin/chef-solo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env ruby
22
#
3-
# ./chef-client - Run the chef client
3+
# ./chef-solo - Run the chef client, in stand-alone mode
44
#
5-
# Author:: AJ Christensen (<adam@opscode.com>)
5+
# Author:: AJ Christensen (<aj@opscode.com>)
66
# Copyright:: Copyright (c) 2008 Opscode, Inc.
77
# License:: Apache License, Version 2.0
88
#

chef/lib/chef/application/client.rb

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -110,33 +110,21 @@ def initialize
110110
# Re-open the JSON attributes and load them into the node
111111
def reconfigure
112112
super
113+
114+
Chef::Config[:delay] = Chef::Config[:interval] + (Chef::Config[:splay] ? rand(Chef::Config[:splay]) : 0)
113115

114116
if Chef::Config[:json_attribs]
115117
require 'net/http'
116118
require 'open-uri'
117119

118-
json_io = nil
119-
begin
120-
json_io = Kernel.open(Chef::Config[:json_attribs])
121-
rescue SocketError => error
122-
Chef::Application.fatal!("I cannot connect to #{Chef::Config[:json_attribs]}", 2)
123-
rescue Errno::ENOENT => error
124-
Chef::Application.fatal!("I cannot find #{Chef::Config[:json_attribs]}", 2)
125-
rescue Errno::EACCES => error
126-
Chef::Application.fatal!("Permissions are incorrect on #{Chef::Config[:json_attribs]}. Please chmod a+r #{Chef::Config[:json_attribs]}", 2)
127-
rescue Exception => error
128-
Chef::Application.fatal!("Got an unexpected error reading #{Chef::Config[:json_attribs]}: #{error.message}", 2)
129-
end
130-
131120
begin
132-
@chef_client_json = JSON.parse(json_io.read)
133-
rescue JSON::ParserError => error
134-
Chef::Application.fatal!("Could not parse the provided JSON file (#{Chef::Config[:json_attribs]})!: " + error.message, 2)
135-
exit 2
121+
open(Chef::Config[:json_attribs]) do |json_io|
122+
@chef_client_json = JSON.parse(json_io.read)
123+
end
124+
rescue Exception
125+
Chef::Application.fatal!("Error parsing json attributes", 2)
136126
end
137-
end
138-
139-
Chef::Config[:delay] = Chef::Config[:interval] + (Chef::Config[:splay] ? rand(Chef::Config[:splay]) : 0)
127+
end
140128
end
141129

142130
# Setup an instance of the chef client
@@ -158,17 +146,17 @@ def run_application
158146
loop do
159147
@chef_client.run
160148

161-
if Chef::Config[:interval]
149+
if Chef::Config[:delay]
162150
Chef::Log.debug("Sleeping for #{Chef::Config[:delay]} seconds")
163151
sleep Chef::Config[:delay]
164152
else
165-
exit 0
153+
Chef::Application.fatal! "Exiting", 0
166154
end
167155
end
168156
rescue SystemExit => e
169157
raise
170158
rescue Exception => e
171-
if Chef::Config[:interval]
159+
if Chef::Config[:delay]
172160
Chef::Log.error("#{e.class}")
173161
Chef::Log.fatal("#{e}\n#{e.backtrace.join("\n")}")
174162
Chef::Log.fatal("Sleeping for #{Chef::Config[:delay]} seconds before trying again")

chef/lib/chef/application/indexer.rb

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,111 @@
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
1717

18-
require 'chef/application'
18+
require 'chef/application'
19+
require 'chef/queue'
20+
require 'chef/search'
21+
require 'chef/search_index'
22+
require 'chef/config'
23+
require 'chef/daemon'
24+
require 'chef/log'
25+
26+
27+
class Chef::Application::Indexer < Chef::Application
28+
29+
option :config_file,
30+
:short => "-c CONFIG",
31+
:long => "--config CONFIG",
32+
:default => 'config.rb',
33+
:description => "The configuration file to use"
34+
35+
option :log_level,
36+
:short => "-l LEVEL",
37+
:long => "--log_level LEVEL",
38+
:description => "Set the log level (debug, info, warn, error, fatal)",
39+
:proc => lambda { |l| l.to_sym }
40+
41+
option :log_location,
42+
:short => "-L LOGLOCATION",
43+
:long => "--logfile LOGLOCATION",
44+
:description => "Set the log file location, defaults to STDOUT - recommended for daemonizing",
45+
:proc => nil
46+
47+
option :help,
48+
:short => "-h",
49+
:long => "--help",
50+
:description => "Show this message",
51+
:on => :tail,
52+
:boolean => true,
53+
:show_options => true,
54+
:exit => 0
55+
56+
option :user,
57+
:short => "-u USER",
58+
:long => "--user USER",
59+
:description => "User to change uid to before daemonizing",
60+
:proc => nil
61+
62+
option :group,
63+
:short => "-g GROUP",
64+
:long => "--group GROUP",
65+
:description => "Group to change gid to before daemonizing",
66+
:proc => nil
67+
68+
option :daemonize,
69+
:short => "-d",
70+
:long => "--daemonize",
71+
:description => "Daemonize the process",
72+
:proc => lambda { |p| true }
73+
74+
def initialize
75+
super
76+
77+
@chef_search_indexer = nil
78+
end
79+
80+
# Create a new search indexer and connect to the stomp queues
81+
def setup_application
82+
@chef_search_indexer = Chef::SearchIndex.new
83+
Chef::Queue.connect
84+
Chef::Queue.subscribe(:queue, "index")
85+
Chef::Queue.subscribe(:queue, "remove")
86+
end
87+
88+
# Run the indexer, optionally daemonizing.
89+
def run_application
90+
if Chef::Config[:daemonize]
91+
Chef::Daemon.change_privilege
92+
Chef::Daemon.daemonize("chef-client")
93+
end
94+
95+
loop do
96+
object, headers = Chef::Queue.receive_msg
97+
Chef::Log.info("Headers #{headers.inspect}")
98+
if headers["destination"] == "/queue/chef/index"
99+
start_timer = Time.new
100+
indexer.add(object)
101+
indexer.commit
102+
final_timer = Time.new
103+
Chef::Log.info("Indexed object from #{headers['destination']} in #{final_timer - start_timer} seconds")
104+
elsif headers["destination"] == "/queue/chef/remove"
105+
start_timer = Time.new
106+
indexer.delete(object)
107+
indexer.commit
108+
final_timer = Time.new
109+
Chef::Log.info("Removed object from #{headers['destination']} in #{final_timer - start_timer} seconds")
110+
end
111+
end
112+
rescue SystemExit => e
113+
raise
114+
rescue Exception => e
115+
if Chef::Config[:interval]
116+
Chef::Log.error("#{e.class}")
117+
Chef::Log.fatal("#{e}\n#{e.backtrace.join("\n")}")
118+
Chef::Log.fatal("Sleeping for #{Chef::Config[:delay]} seconds before trying again")
119+
sleep Chef::Config[:delay]
120+
retry
121+
else
122+
raise
123+
end
124+
end
125+
end

chef/lib/chef/tasks/chef_repo.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
require 'chef'
2222
require 'chef/cookbook/metadata'
2323
require 'tempfile'
24+
require 'rake'
2425

2526
desc "Update your repository from source control"
2627
task :update do

0 commit comments

Comments
 (0)