opscode-cookbooks/chef-client — pull request #186 by ymattw (2014-08-27 22:15:01+00:00)
Title: Support freebsd with a new rc.d script

This is tested on Yahoo specific freebsd versions which are freebsd 9 and 10 based. The rc.d script use `pgrep` to find process id, it does not respect the pid file at all. The vars in the script are risky from name pollution, adding a private prefix would be better. Super hacky just to make it work. Hope someone from the community can enhance. Ref: https://www.freebsd.org/doc/en/articles/rc-scripting/.

Here's the output from a converge.

```
Compiling Cookbooks...
Converging 9 resources
Recipe: chef-client::bsd_service
* directory[/var/run] action create (up to date)
* directory[/var/chef/cache] action create (up to date)
* directory[/var/chef/backup] action create (up to date)
* directory[/var/log/chef] action create (up to date)
* directory[/etc/chef] action create (up to date)
* log[You specified service style 'bsd', chef-client service will be turned on by default, you need to setup /etc/rc.conf to turn if off manually.] action write

* template[/etc/rc.d/chef-client] action create
- create new file /etc/rc.d/chef-client
- update content in file /etc/rc.d/chef-client from none to d2605c
--- /etc/rc.d/chef-client 2014-08-27 22:01:47.000000000 +0000
+++ /tmp/chef-rendered-template20140827-5211-15oeef4 2014-08-27 22:01:47.000000000 +0000
@@ -1 +1,115 @@
+#!/bin/sh
+
+# Configuration file for the chef-client service, generated by template
+
+CONFIG=/etc/chef/client.rb
+PIDFILE=/var/run/client.pid
+LOGFILE=/var/log/chef/client.log
+# Sleep interval between runs.
+# This value is in seconds.
+INTERVAL=43200
+# Maximum amount of random delay before starting a run. Prevents every client
+# from contacting the server at the exact same time.
+# This value is in seconds.
+SPLAY=43200
+# Any additional chef-client options.
+OPTIONS=""
+
+. /etc/rc.subr
+
+name="chef-client"
+
+exec="/usr/bin/chef-client"
+prog=$name
+rcvar=chef_client_enable
+start_cmd="chef_client_start"
+stop_cmd="chef_client_stop"
+restart_cmd="chef_client_restart"
+status_cmd="chef_client_status"
+
+# Make sure settings are not empty
+config=${CONFIG-/etc/chef/client.rb}
+pidfile=${PIDFILE-/var/run/chef/client.pid}
+logfile=${LOGFILE-/var/log/chef/client.log}
+interval=${INTERVAL-1800}
+splay=${SPLAY-20}
+options=${OPTIONS-}
+
+# NOTE: No array support in posix bourne shell
+cmdline="$exec -d -c $config -L $logfile -P $pidfile -i $interval -s $splay $options"
+
+load_rc_config $name

+
+# Default to yes but configurable thru /etc/rc.conf manually
+: ${chef_client_enable:=YES}
+
+# Private function to get chef-client process id, supports multiple processes
+__chef_client_pids() {
+ # merge spaces because ending options could be a space
+ pattern=`echo ${cmdline}`
+ pids=`pgrep -f "$pattern"`
+
+ # merge newlines so output is better
+ echo $pids
+}
+
+chef_client_start() {
+ [ -x $exec ] || exit 5
+ [ -f $config ] || exit 6
+ echo -n "Starting $prog: "
+ # chef-client itself will detect if an instance already running by pid
+ nohup ${cmdline} < /dev/null > /dev/null 2>&1 &
+ if [ $? = 0 ]; then
+ echo "[OK]"
+ return 0
+ else
+ echo "[FAILED]"
+ return 1
+ fi
+}
+
+chef_client_stop() {
+ echo -n "Stopping $prog: "
+ pids=`__chef_client_pids`
+
+ if [ -n "$pids" ]; then
+ # First, try SIGTERM, then SIGKILL
+ kill $pids
+ sleep 1
+ kill -KILL $pids >/dev/null 2>&1
+ sleep 0.1
+
+ if kill -0 $pids 2>/dev/null; then
+ echo "[FAILED]"
+ return 1
+ fi
+ else
+ echo "not running"
+ rm -f $pidfile
+ return 0
+ fi
+
+ rm -f $pidfile
+ echo "[OK]"
+ return 0
+}
+
+chef_client_restart() {
+ chef_client_stop
+ chef_client_start
+}
+
+chef_client_status() {
+ pids=`__chef_client_pids`

+
+ if [ -n "$pids" ]; then
+ echo "${prog} (pid $pids) is running..."
+ return 0
+ else
+ echo "${prog} is stopped"
+ return 3
+ fi
+}
+
+run_rc_command "$1"
- change mode from '' to '0755'
* service[chef-client] action enable (up to date)
* service[chef-client] action start
- start service service[chef-client]
Recipe: ubiquity::default
* ruby_block[onboard to fleet] action run (skipped due to not_if)
Recipe: chef-client::bsd_service
* service[chef-client] action restart
- restart service service[chef-client]
[2014-08-27T22:01:48+00:00] WARN: Skipping final node save because override_runlist was given

Running handlers:
Running handlers complete
Chef Client finished, 4/10 resources updated in 8.99899528 seconds
bash-4.2# ps aux | grep chef
root 5336 0.0 2.3 125436 48440 - S 10:01PM 0:00.00 /opt/chef/embedded/bin/ruby /usr/bin/chef-client -d -c /etc/chef/client.rb -L /var/log/chef/client.log -P /var/run/client.pid -i 43200
root 5338 0.0 0.1 18792 2216 0 S+ 10:02PM 0:00.00 grep chef
bash-4.2#
```