diff --git a/spec/defines/daemon_spec.rb b/spec/defines/daemon_spec.rb new file mode 100644 index 000000000..029e7935f --- /dev/null +++ b/spec/defines/daemon_spec.rb @@ -0,0 +1,157 @@ +require 'spec_helper' + +describe 'prometheus::daemon' do + let :title do + 'smurf_exporter' + end + + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) do + facts + end + + let :pre_condition do + 'include ::prometheus::params' + end + + [ + { + version: '1.2.3', + real_download_url: 'https://github.com/prometheus/smurf_exporter/releases/v1.2.3/smurf_exporter-1.2.3.any.tar.gz', + notify_service: 'Service[smurf_exporter]', + user: 'smurf_user', + group: 'smurf_group' + } + ].each do |parameters| + context "with parameters #{parameters}" do + let(:params) do + parameters + end + + prom_os = facts[:kernel].downcase + prom_arch = facts[:architecture] == 'i386' ? '386' : 'amd64' + + it { + is_expected.to contain_archive("/tmp/smurf_exporter-#{parameters[:version]}.tar.gz").with( + 'ensure' => 'present', + 'extract' => true, + 'extract_path' => '/opt', + 'source' => params[:real_download_url], + 'checksum_verify' => false, + 'creates' => "/opt/smurf_exporter-#{parameters[:version]}.#{prom_os}-#{prom_arch}/smurf_exporter", + 'cleanup' => true + ).that_comes_before("File[/opt/smurf_exporter-#{parameters[:version]}.#{prom_os}-#{prom_arch}/smurf_exporter]") + } + + it { + is_expected.to contain_file("/opt/smurf_exporter-#{parameters[:version]}.#{prom_os}-#{prom_arch}/smurf_exporter").with( + 'owner' => 'root', + 'group' => 0, + 'mode' => '0555' + ) + } + + it { + is_expected.to contain_file('/usr/local/bin/smurf_exporter').with( + 'ensure' => 'link', + 'target' => "/opt/smurf_exporter-#{parameters[:version]}.#{prom_os}-#{prom_arch}/smurf_exporter" + ).that_notifies('Service[smurf_exporter]') + } + + it { + is_expected.to contain_user('smurf_user').with( + 'ensure' => 'present', + 'system' => true, + 'groups' => [] + ) + } + + it { + is_expected.to contain_group('smurf_group').with( + 'ensure' => 'present', + 'system' => true + ) + } + + # prometheus::config + if ['debian-7-x86_64'].include?(os) + # init_style = 'debian' + + it { + is_expected.to contain_file('/etc/init.d/smurf_exporter').with( + 'mode' => '0555', + 'owner' => 'root', + 'group' => 'root', + 'content' => File.read(fixtures('files', 'daemon.debian')) + ) + } + elsif ['centos-6-x86_64', 'redhat-6-x86_64'].include?(os) + # init_style = 'sysv' + + it { + is_expected.to contain_file('/etc/init.d/smurf_exporter').with( + 'mode' => '0555', + 'owner' => 'root', + 'group' => 'root', + 'content' => File.read(fixtures('files', 'daemon.sysv')) + ) + } + elsif ['centos-7-x86_64', 'debian-8-x86_64', 'redhat-7-x86_64', 'ubuntu-16.04-x86_64'].include?(os) + # init_style = 'systemd' + + it { + is_expected.to contain_file('/etc/systemd/system/smurf_exporter.service').with( + 'mode' => '0644', + 'owner' => 'root', + 'group' => 'root', + 'content' => File.read(fixtures('files', 'daemon.systemd')) + ).that_notifies('Exec[smurf_exporter-systemd-reload]') + } + + it { + is_expected.to contain_exec('smurf_exporter-systemd-reload').with( + 'command' => 'systemctl daemon-reload', + 'path' => ['/usr/bin', '/bin', '/usr/sbin'], + 'refreshonly' => true + ).that_notifies('Service[smurf_exporter]') + } + elsif ['ubuntu-14.04-x86_64'].include?(os) + # init_style = 'upstart' + + it { + is_expected.to contain_file('/etc/init/smurf_exporter.conf').with( + 'mode' => '0444', + 'owner' => 'root', + 'group' => 'root', + 'content' => File.read(fixtures('files', 'daemon.upstart')) + ) + } + + it { + is_expected.to contain_file('/etc/init.d/smurf_exporter').with( + 'ensure' => 'link', + 'target' => '/lib/init/upstart-job', + 'owner' => 'root', + 'group' => 'root', + 'mode' => '0755' + ) + } + else + it { + is_expected.to raise_error(Puppet::Error, %r{I don.t know how to create an init script for style}) + } + end + + it { + is_expected.to contain_service('smurf_exporter').with( + 'ensure' => 'running', + 'name' => 'smurf_exporter', + 'enable' => true + ) + } + end + end + end + end +end diff --git a/spec/fixtures/files/daemon.debian b/spec/fixtures/files/daemon.debian new file mode 100644 index 000000000..e6cbf5075 --- /dev/null +++ b/spec/fixtures/files/daemon.debian @@ -0,0 +1,161 @@ +#! /bin/sh +### BEGIN INIT INFO +# Provides: smurf_exporter +# Required-Start: $local_fs $remote_fs +# Required-Stop: $local_fs $remote_fs +# Default-Start: 2 3 4 5 +# Default-Stop: S 0 1 6 +# Short-Description: Prometheus smurf_exporter +# Description: Prometheus smurf_exporter +# +### END INIT INFO + +# Do NOT "set -e" + +# PATH should only include /usr/* if it runs after the mountnfs.sh script +PATH=/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin +DESC="Prometheus smurf_exporter" +NAME=smurf_exporter +DAEMON=/usr/local/bin/$NAME +PIDFILE=/var/run/$NAME/$NAME.pid +DAEMON_ARGS='' +USER=smurf_user +SCRIPTNAME=/etc/init.d/$NAME + +# Exit if the package is not installed +[ -x "$DAEMON" ] || exit 0 + +# Read configuration variable file if it is present +[ -r /etc/default/$NAME ] && . /etc/default/$NAME + +# Load the VERBOSE setting and other rcS variables +[ -f /etc/default/rcS ] && . /etc/default/rcS + +# Define LSB log_* functions. +# Depend on lsb-base (>= 3.0-6) to ensure that this file is present. +. /lib/lsb/init-functions + +# +# Function to create run directory +# +mkrundir() { + [ ! -d /var/run/smurf_exporter ] && mkdir -p /var/run/smurf_exporter + chown $USER /var/run/smurf_exporter +} + +# +# Function that starts the daemon/service +# +do_start() +{ + # Return + # 0 if daemon has been started + # 1 if daemon was already running + # 2 if daemon could not be started + mkrundir + start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --chuid $USER --background --make-pidfile --test > /dev/null \ + || return 1 + start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --chuid $USER --background --make-pidfile -- \ + $DAEMON_ARGS \ + || return 2 +} + + +# +# Function that stops the daemon/service +# +do_stop() +{ + # Return + # 0 if daemon has been stopped + # 1 if daemon was already stopped + # 2 if daemon could not be stopped + # other if a failure occurred + start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME + RETVAL="$?" + [ "$RETVAL" = 2 ] && return 2 + # Wait for children to finish too if this is a daemon that forks + # and if the daemon is only ever run from this initscript. + # If the above conditions are not satisfied then add some other code + # that waits for the process to drop all resources that could be + # needed by services started subsequently. A last resort is to + # sleep for some time. + start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON + [ "$?" = 2 ] && return 2 + # Many daemons don't delete their pidfiles when they exit. + rm -f $PIDFILE + return "$RETVAL" +} + +# +# Function that sends a SIGHUP to the daemon/service +# +do_reload() { + # + # If the daemon can reload its configuration without + # restarting (for example, when it is sent a SIGHUP), + # then implement that here. + # + start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME + return 0 +} + +case "$1" in + start) + [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" + do_start + case "$?" in + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; + esac + ;; + stop) + [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" + do_stop + case "$?" in + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; + esac + ;; + #reload|force-reload) + # + # If do_reload() is not implemented then leave this commented out + # and leave 'force-reload' as an alias for 'restart'. + # + #log_daemon_msg "Reloading $DESC" "$NAME" + #do_reload + #log_end_msg $? + #;; + restart|force-reload) + # + # If the "reload" option is implemented then remove the + # 'force-reload' alias + # + log_daemon_msg "Restarting $DESC" "$NAME" + do_stop + case "$?" in + 0|1) + do_start + case "$?" in + 0) log_end_msg 0 ;; + 1) log_end_msg 1 ;; # Old process is still running + *) log_end_msg 1 ;; # Failed to start + esac + ;; + *) + # Failed to stop + log_end_msg 1 + ;; + esac + ;; + status) + status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $? + ;; + *) + #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2 + echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2 + exit 3 + ;; +esac + +: diff --git a/spec/fixtures/files/daemon.systemd b/spec/fixtures/files/daemon.systemd new file mode 100644 index 000000000..b60c030ed --- /dev/null +++ b/spec/fixtures/files/daemon.systemd @@ -0,0 +1,17 @@ +[Unit] +Description=Prometheus smurf_exporter +Wants=basic.target +After=basic.target network.target + +[Service] +User=smurf_user +Group=smurf_group +ExecStart=/usr/local/bin/smurf_exporter + +ExecReload=/bin/kill -HUP $MAINPID +KillMode=process +Restart=always +RestartSec=42s + +[Install] +WantedBy=multi-user.target diff --git a/spec/fixtures/files/daemon.sysv b/spec/fixtures/files/daemon.sysv new file mode 100644 index 000000000..dda8611e7 --- /dev/null +++ b/spec/fixtures/files/daemon.sysv @@ -0,0 +1,125 @@ +#!/bin/bash +# +# /etc/rc.d/init.d/smurf_exporter +# +# Daemonize the prometheus smurf_exporter. +# +# chkconfig: 2345 95 20 +# description: Prometheus smurf_exporter +# processname: smurf_exporter +# pidfile: /var/run/smurf_exporter/pidfile + +# Source function library. +. /etc/init.d/functions + +DAEMON=/usr/local/bin/smurf_exporter +PID_FILE=/var/run/smurf_exporter/smurf_exporter.pid +LOG_FILE=/var/log/smurf_exporter + +[ -e /etc/sysconfig/smurf_exporter ] && . /etc/sysconfig/smurf_exporter + +export GOMAXPROCS=${GOMAXPROCS:-2} + +# +# Create the /var/run/smurf_exporter directory, which can live on a tmpfs +# filesystem and be destroyed between reboots. +# +mkrundir() { + [ ! -d /var/run/smurf_exporter ] && mkdir -p /var/run/smurf_exporter + chown smurf_user /var/run/smurf_exporter +} + +# +# Create a PID file if it doesn't already exist, for clean upgrades +# from previous init-script controlled daemons. +# +KILLPROC_OPT="-p ${PID_FILE}" +mkpidfile() { + # Create PID file if it didn't exist + mkrundir + [ ! -f $PID_FILE ] && pidofproc $DAEMON > $PID_FILE + chown smurf_user /var/run/smurf_exporter + if [ $? -ne 0 ] ; then + rm $PID_FILE + KILLPROC_OPT="" + fi +} + +start() { + echo -n "Starting prometheus smurf_exporter: " + mkrundir + [ -f $PID_FILE ] && rm $PID_FILE + daemon --user=smurf_user \ + --pidfile="$PID_FILE" \ + "$DAEMON" '' >> "$LOG_FILE" & + retcode=$? + mkpidfile + touch /var/lock/subsys/smurf_exporter + return $retcode +} + +stop() { + DELAY=5 # seconds maximum to wait for a leave + + echo -n "Shutting down prometheus smurf_exporter: " + mkpidfile + + smurf_exporter_pid=$(cat $PID_FILE) + killproc $KILLPROC_OPT $DAEMON -INT + retcode=$? + + # We'll wait if necessary to make sure the leave works, and return + # early if we can. If not, escalate to harsher signals. + try=0 + while [ $try -lt $DELAY ]; do + if ! checkpid $smurf_exporter_pid ; then + rm -f /var/lock/subsys/smurf_exporter + return $retcode + fi + sleep 1 + let try+=1 + done + + # If acting as a server, use a SIGTERM to avoid a leave. + # This behavior is also configurable. Avoid doing a "leave" because + # having servers missing is a bad thing that we want to notice. + # + # A SIGTERM will mark the node as "failed" until it rejoins. + # killproc with no arguments uses TERM, then escalates to KILL. + killproc $KILLPROC_OPT $DAEMON + retcode=$? + + rm -f /var/lock/subsys/smurf_exporter $PID_FILE + return $retcode +} + +case "$1" in + start) + start + ;; + stop) + stop + ;; + status) + status -p ${PID_FILE} $DAEMON + ;; + restart) + stop + start + ;; + reload) + mkpidfile + killproc $KILLPROC_OPT $DAEMON -HUP + ;; + condrestart) + [ -f /var/lock/subsys/smurf_exporter ] && restart || : + ;; + *) + echo "Usage: smurf_exporter {start|stop|status|reload|restart}" + exit 1 + ;; +esac +retcode=$? +# Don't let the [OK] get stomped on. +echo +exit $retcode diff --git a/spec/fixtures/files/daemon.upstart b/spec/fixtures/files/daemon.upstart new file mode 100644 index 000000000..c74a7754a --- /dev/null +++ b/spec/fixtures/files/daemon.upstart @@ -0,0 +1,31 @@ +# Prometheus smurf_exporter (Upstart unit) +description "Prometheus smurf_exporter" +start on runlevel [2345] +stop on runlevel [06] + +env DAEMON=/usr/local/bin/smurf_exporter +env USER=smurf_user +env GROUP=smurf_group +env DEFAULTS=/etc/default/smurf_exporter +env RUNDIR=/var/run/smurf_exporter +env PID_FILE=/var/run/smurf_exporter/smurf_exporter.pid +pre-start script + [ -e $DEFAULTS ] && . $DEFAULTS + + mkdir -p $RUNDIR || true + chmod 0750 $RUNDIR || true + chown $USER:$GROUP $RUNDIR || true +end script + +script + # read settings like GOMAXPROCS from "/etc/default/smurf_exporter", if available. + [ -e $DEFAULTS ] && . $DEFAULTS + + export GOMAXPROCS=${GOMAXPROCS:-2} + exec start-stop-daemon -c $USER -g $GROUP -p $PID_FILE -x $DAEMON -S -- + +end script + +respawn +respawn limit 10 10 +kill timeout 10