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#
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
0 commit comments