diff --git a/manifests/config.pp b/manifests/config.pp index 07d7aada4..d86e7ba65 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -9,6 +9,21 @@ ) { if $prometheus::init_style { + if( versioncmp($::prometheus::version, '2.0.0') < 0 ){ + $daemon_flags = [ + "-config.file=${::prometheus::config_dir}/prometheus.yaml", + "-storage.local.path=${::prometheus::localstorage}", + "-web.console.templates=${::prometheus::shared_dir}/consoles", + "-web.console.libraries=${::prometheus::shared_dir}/console_libraries", + ] + } else { + $daemon_flags = [ + "--config.file=${::prometheus::config_dir}/prometheus.yaml", + "--storage.tsdb.path=${::prometheus::localstorage}", + "--web.console.templates=${::prometheus::shared_dir}/consoles", + "--web.console.libraries=${::prometheus::shared_dir}/console_libraries", + ] + } # the vast majority of files here are init-files # so any change there should trigger a full service restart diff --git a/spec/classes/prometheus_spec.rb b/spec/classes/prometheus_spec.rb index b82329807..e4994de43 100644 --- a/spec/classes/prometheus_spec.rb +++ b/spec/classes/prometheus_spec.rb @@ -7,183 +7,197 @@ facts end - context 'without parameters' do - # prometheus::install - it { - is_expected.to contain_file('/var/lib/prometheus').with( - 'ensure' => 'directory', - 'owner' => 'prometheus', - 'group' => 'prometheus', - 'mode' => '0755' - ) - } - - prom_version = '1.5.2' - prom_os = facts[:kernel].downcase - prom_arch = facts[:architecture] == 'i386' ? '386' : 'amd64' - - it { - is_expected.to contain_archive("/tmp/prometheus-#{prom_version}.tar.gz").with( - 'ensure' => 'present', - 'extract' => true, - 'extract_path' => '/opt', - 'source' => "https://github.com/prometheus/prometheus/releases/download/v#{prom_version}/prometheus-#{prom_version}.#{prom_os}-#{prom_arch}.tar.gz", - 'checksum_verify' => false, - 'creates' => "/opt/prometheus-#{prom_version}.#{prom_os}-#{prom_arch}/prometheus", - 'cleanup' => true - ).that_comes_before("File[/opt/prometheus-#{prom_version}.#{prom_os}-#{prom_arch}/prometheus]") - } - - it { - is_expected.to contain_file("/opt/prometheus-#{prom_version}.#{prom_os}-#{prom_arch}/prometheus").with( - 'owner' => 'root', - 'group' => 0, - 'mode' => '0555' - ) - } - - it { - is_expected.to contain_file('/usr/local/bin/prometheus').with( - 'ensure' => 'link', - 'target' => "/opt/prometheus-#{prom_version}.#{prom_os}-#{prom_arch}/prometheus" - ).that_notifies('Service[prometheus]') - } - - it { - is_expected.to contain_file('/usr/local/share/prometheus').with( - 'ensure' => 'directory', - 'owner' => 'prometheus', - 'group' => 'prometheus', - 'mode' => '0755' - ) - } - - it { - is_expected.to contain_file('/usr/local/share/prometheus/consoles').with( - 'ensure' => 'link', - 'target' => "/opt/prometheus-#{prom_version}.#{prom_os}-#{prom_arch}/consoles" - ).that_notifies('Service[prometheus]') - } - - it { - is_expected.to contain_file('/usr/local/share/prometheus/console_libraries').with( - 'ensure' => 'link', - 'target' => "/opt/prometheus-#{prom_version}.#{prom_os}-#{prom_arch}/console_libraries" - ).that_notifies('Service[prometheus]') - } - - it { - is_expected.to contain_user('prometheus').with( - 'ensure' => 'present', - 'system' => true, - 'groups' => [] - ) - } - - it { - is_expected.to contain_group('prometheus').with( - 'ensure' => 'present', - 'system' => true - ) - } - - # prometheus::config - if ['debian-7-x86_64'].include?(os) - # init_style = 'debian' + [{}, { version: '2.0.0-rc.1' }].each do |parameters| + context "with parameters #{parameters}" do + let(:params) do + parameters + end + # prometheus::install it { - is_expected.to contain_file('/etc/init.d/prometheus').with( - 'mode' => '0555', - 'owner' => 'root', - 'group' => 'root', - 'content' => File.read(fixtures('files', 'prometheus.debian')) + is_expected.to contain_file('/var/lib/prometheus').with( + 'ensure' => 'directory', + 'owner' => 'prometheus', + 'group' => 'prometheus', + 'mode' => '0755' ) } - elsif ['centos-6-x86_64', 'redhat-6-x86_64'].include?(os) - # init_style = 'sysv' + + prom_version = parameters[:version] || '1.5.2' + prom_major = prom_version[0] + prom_os = facts[:kernel].downcase + prom_arch = facts[:architecture] == 'i386' ? '386' : 'amd64' + + it { + is_expected.to contain_archive("/tmp/prometheus-#{prom_version}.tar.gz").with( + 'ensure' => 'present', + 'extract' => true, + 'extract_path' => '/opt', + 'source' => "https://github.com/prometheus/prometheus/releases/download/v#{prom_version}/prometheus-#{prom_version}.#{prom_os}-#{prom_arch}.tar.gz", + 'checksum_verify' => false, + 'creates' => "/opt/prometheus-#{prom_version}.#{prom_os}-#{prom_arch}/prometheus", + 'cleanup' => true + ).that_comes_before("File[/opt/prometheus-#{prom_version}.#{prom_os}-#{prom_arch}/prometheus]") + } it { - is_expected.to contain_file('/etc/init.d/prometheus').with( - 'mode' => '0555', + is_expected.to contain_file("/opt/prometheus-#{prom_version}.#{prom_os}-#{prom_arch}/prometheus").with( 'owner' => 'root', - 'group' => 'root', - 'content' => File.read(fixtures('files', 'prometheus.sysv')) + 'group' => 0, + 'mode' => '0555' ) } - 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_systemd__unit_file('prometheus.service') } - elsif ['ubuntu-14.04-x86_64'].include?(os) - # init_style = 'upstart' it { - is_expected.to contain_file('/etc/init/prometheus.conf').with( - 'mode' => '0444', - 'owner' => 'root', - 'group' => 'root', - 'content' => File.read(fixtures('files', 'prometheus.upstart')) + is_expected.to contain_file('/usr/local/bin/prometheus').with( + 'ensure' => 'link', + 'target' => "/opt/prometheus-#{prom_version}.#{prom_os}-#{prom_arch}/prometheus" + ).that_notifies('Service[prometheus]') + } + + it { + is_expected.to contain_file('/usr/local/share/prometheus').with( + 'ensure' => 'directory', + 'owner' => 'prometheus', + 'group' => 'prometheus', + 'mode' => '0755' ) } it { - is_expected.to contain_file('/etc/init.d/prometheus').with( + is_expected.to contain_file('/usr/local/share/prometheus/consoles').with( 'ensure' => 'link', - 'target' => '/lib/init/upstart-job', - 'owner' => 'root', - 'group' => 'root', - 'mode' => '0755' + 'target' => "/opt/prometheus-#{prom_version}.#{prom_os}-#{prom_arch}/consoles" + ).that_notifies('Service[prometheus]') + } + + it { + is_expected.to contain_file('/usr/local/share/prometheus/console_libraries').with( + 'ensure' => 'link', + 'target' => "/opt/prometheus-#{prom_version}.#{prom_os}-#{prom_arch}/console_libraries" + ).that_notifies('Service[prometheus]') + } + + it { + is_expected.to contain_user('prometheus').with( + 'ensure' => 'present', + 'system' => true, + 'groups' => [] ) } - else + it { - is_expected.to raise_error(Puppet::Error, %r{I don.t know how to create an init script for style}) + is_expected.to contain_group('prometheus').with( + 'ensure' => 'present', + 'system' => true + ) } - end - it { - is_expected.to contain_file('/etc/prometheus').with( - 'ensure' => 'directory', - 'owner' => 'prometheus', - 'group' => 'prometheus', - 'purge' => true, - 'recurse' => true - ) - } - - it { - is_expected.to contain_file('prometheus.yaml').with( - 'ensure' => 'present', - 'path' => '/etc/prometheus/prometheus.yaml', - 'owner' => 'prometheus', - 'group' => 'prometheus', - 'mode' => '0660', - 'content' => File.read(fixtures('files', 'prometheus.yaml')) - ).that_notifies('Class[prometheus::service_reload]') - } - - # prometheus::alerts - it { - is_expected.not_to contain_file('/etc/prometheus/alert.rules') - } - - # prometheus::run_service - it { - is_expected.to contain_service('prometheus').with( - 'ensure' => 'running', - 'name' => 'prometheus', - 'enable' => true, - 'hasrestart' => true - ) - } - - # prometheus::service_reload - it { - is_expected.to contain_exec('prometheus-reload').with( - # 'command' => 'systemctl reload prometheus', - 'path' => ['/usr/bin', '/bin', '/usr/sbin', '/sbin'], - 'refreshonly' => true - ) - } + # prometheus::config + if ['debian-7-x86_64'].include?(os) + # init_style = 'debian' + + it { + is_expected.to contain_file('/etc/init.d/prometheus').with( + 'mode' => '0555', + 'owner' => 'root', + 'group' => 'root', + 'content' => File.read(fixtures('files', "prometheus#{prom_major}.debian")) + ) + } + elsif ['centos-6-x86_64', 'redhat-6-x86_64'].include?(os) + # init_style = 'sysv' + + it { + is_expected.to contain_file('/etc/init.d/prometheus').with( + 'mode' => '0555', + 'owner' => 'root', + 'group' => 'root', + 'content' => File.read(fixtures('files', "prometheus#{prom_major}.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_class('systemd') } + + it { + is_expected.to contain_systemd__unit_file('prometheus.service').with( + 'content' => File.read(fixtures('files', "prometheus#{prom_major}.systemd")) + ) + } + elsif ['ubuntu-14.04-x86_64'].include?(os) + # init_style = 'upstart' + + it { + is_expected.to contain_file('/etc/init/prometheus.conf').with( + 'mode' => '0444', + 'owner' => 'root', + 'group' => 'root', + 'content' => File.read(fixtures('files', "prometheus#{prom_major}.upstart")) + ) + } + + it { + is_expected.to contain_file('/etc/init.d/prometheus').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_file('/etc/prometheus').with( + 'ensure' => 'directory', + 'owner' => 'prometheus', + 'group' => 'prometheus', + 'purge' => true, + 'recurse' => true + ) + } + + it { + is_expected.to contain_file('prometheus.yaml').with( + 'ensure' => 'present', + 'path' => '/etc/prometheus/prometheus.yaml', + 'owner' => 'prometheus', + 'group' => 'prometheus', + 'mode' => '0660', + 'content' => File.read(fixtures('files', 'prometheus.yaml')) + ).that_notifies('Class[prometheus::service_reload]') + } + + # prometheus::alerts + it { + is_expected.not_to contain_file('/etc/prometheus/alert.rules') + } + + # prometheus::run_service + it { + is_expected.to contain_service('prometheus').with( + 'ensure' => 'running', + 'name' => 'prometheus', + 'enable' => true, + 'hasrestart' => true + ) + } + + # prometheus::service_reload + it { + is_expected.to contain_exec('prometheus-reload').with( + # 'command' => 'systemctl reload prometheus', + 'path' => ['/usr/bin', '/bin', '/usr/sbin', '/sbin'], + 'refreshonly' => true + ) + } + end end end end diff --git a/spec/fixtures/files/prometheus1.debian b/spec/fixtures/files/prometheus1.debian new file mode 100644 index 000000000..4007d4c13 --- /dev/null +++ b/spec/fixtures/files/prometheus1.debian @@ -0,0 +1,177 @@ +#! /bin/sh +### BEGIN INIT INFO +# Provides: prometheus +# 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 monitoring system +# Description: The Prometheus monitoring system and time series database. https://prometheus.io/ +# +### 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 monitoring framework" +NAME=prometheus +DAEMON=/usr/local/bin/$NAME +PIDFILE=/var/run/$NAME/$NAME.pid +DAEMON_ARGS="-config.file=/etc/prometheus/prometheus.yaml + -storage.local.path=/var/lib/prometheus + -web.console.templates=/usr/local/share/prometheus/consoles + -web.console.libraries=/usr/local/share/prometheus/console_libraries + " +USER=prometheus +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/prometheus ] && mkdir -p /var/run/prometheus + chown $USER /var/run/prometheus +} + +# +# 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 + + for i in `seq 1 30`; do + if ! start-stop-daemon --quiet --stop --test --pidfile $PIDFILE --exec $DAEMON --user $USER; then + RETVAL=2 + sleep 1 + continue + fi + if "$DAEMON" info ${RPC_ADDR} >/dev/null; then + return 0 + fi + done + return "$RETVAL" +} + + +# +# 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/prometheus.systemd b/spec/fixtures/files/prometheus1.systemd similarity index 87% rename from spec/fixtures/files/prometheus.systemd rename to spec/fixtures/files/prometheus1.systemd index 0554daf44..65eb34b05 100644 --- a/spec/fixtures/files/prometheus.systemd +++ b/spec/fixtures/files/prometheus1.systemd @@ -7,14 +7,14 @@ After=basic.target network.target User=prometheus Group=prometheus ExecStart=/usr/local/bin/prometheus \ - -config.file=/etc/prometheus/prometheus.yaml\ + -config.file=/etc/prometheus/prometheus.yaml \ -storage.local.path=/var/lib/prometheus \ -web.console.templates=/usr/local/share/prometheus/consoles \ -web.console.libraries=/usr/local/share/prometheus/console_libraries \ ExecReload=/bin/kill -HUP $MAINPID KillMode=process -Restart=always +Restart=on-failure [Install] WantedBy=multi-user.target diff --git a/spec/fixtures/files/prometheus.sysv b/spec/fixtures/files/prometheus1.sysv similarity index 96% rename from spec/fixtures/files/prometheus.sysv rename to spec/fixtures/files/prometheus1.sysv index cf7e43728..513fcb4a2 100644 --- a/spec/fixtures/files/prometheus.sysv +++ b/spec/fixtures/files/prometheus1.sysv @@ -13,7 +13,6 @@ . /etc/init.d/functions DAEMON=/usr/local/bin/prometheus -CONFIG=/etc/prometheus/prometheus.yaml PID_FILE=/var/run/prometheus/prometheus.pid LOG_FILE=/var/log/prometheus @@ -52,7 +51,7 @@ start() { [ -f $PID_FILE ] && rm $PID_FILE daemon --user=prometheus \ --pidfile="$PID_FILE" \ - "$DAEMON" -log.format logger:stdout -config.file="$CONFIG" \ + "$DAEMON" -log.format logger:stdout -config.file=/etc/prometheus/prometheus.yaml \ -storage.local.path=/var/lib/prometheus \ -web.console.templates=/usr/local/share/prometheus/consoles \ -web.console.libraries=/usr/local/share/prometheus/console_libraries \ diff --git a/spec/fixtures/files/prometheus.upstart b/spec/fixtures/files/prometheus1.upstart similarity index 92% rename from spec/fixtures/files/prometheus.upstart rename to spec/fixtures/files/prometheus1.upstart index 6ff2df34f..6e483b53f 100644 --- a/spec/fixtures/files/prometheus.upstart +++ b/spec/fixtures/files/prometheus1.upstart @@ -4,7 +4,6 @@ start on runlevel [2345] stop on runlevel [06] env PROMETHEUS=/usr/local/bin/prometheus -env CONFIG=/etc/prometheus/prometheus.yaml env USER=prometheus env GROUP=prometheus env DEFAULTS=/etc/default/prometheus @@ -23,7 +22,7 @@ script [ -e $DEFAULTS ] && . $DEFAULTS export GOMAXPROCS=${GOMAXPROCS:-2} - exec start-stop-daemon -c $USER -g $GROUP -p $PID_FILE -x $PROMETHEUS -S -- -config.file=$CONFIG \ + exec start-stop-daemon -c $USER -g $GROUP -p $PID_FILE -x $PROMETHEUS -S -- -config.file=/etc/prometheus/prometheus.yaml \ -storage.local.path=/var/lib/prometheus \ -web.console.templates=/usr/local/share/prometheus/consoles \ -web.console.libraries=/usr/local/share/prometheus/console_libraries \ diff --git a/spec/fixtures/files/prometheus.debian b/spec/fixtures/files/prometheus2.debian similarity index 95% rename from spec/fixtures/files/prometheus.debian rename to spec/fixtures/files/prometheus2.debian index d0d6165a9..55242efb9 100644 --- a/spec/fixtures/files/prometheus.debian +++ b/spec/fixtures/files/prometheus2.debian @@ -18,11 +18,11 @@ DESC="Prometheus monitoring framework" NAME=prometheus DAEMON=/usr/local/bin/$NAME PIDFILE=/var/run/$NAME/$NAME.pid -DAEMON_ARGS=" -config.file=/etc/prometheus/prometheus.yaml - -storage.local.path=/var/lib/prometheus - -web.console.templates=/usr/local/share/prometheus/consoles - -web.console.libraries=/usr/local/share/prometheus/console_libraries - " +DAEMON_ARGS="--config.file=/etc/prometheus/prometheus.yaml + --storage.tsdb.path=/var/lib/prometheus + --web.console.templates=/usr/local/share/prometheus/consoles + --web.console.libraries=/usr/local/share/prometheus/console_libraries + " USER=prometheus SCRIPTNAME=/etc/init.d/$NAME diff --git a/spec/fixtures/files/prometheus2.systemd b/spec/fixtures/files/prometheus2.systemd new file mode 100644 index 000000000..8ee75040e --- /dev/null +++ b/spec/fixtures/files/prometheus2.systemd @@ -0,0 +1,20 @@ +[Unit] +Description=Prometheus Monitoring framework +Wants=basic.target +After=basic.target network.target + +[Service] +User=prometheus +Group=prometheus +ExecStart=/usr/local/bin/prometheus \ + --config.file=/etc/prometheus/prometheus.yaml \ + --storage.tsdb.path=/var/lib/prometheus \ + --web.console.templates=/usr/local/share/prometheus/consoles \ + --web.console.libraries=/usr/local/share/prometheus/console_libraries \ + +ExecReload=/bin/kill -HUP $MAINPID +KillMode=process +Restart=on-failure + +[Install] +WantedBy=multi-user.target diff --git a/spec/fixtures/files/prometheus2.sysv b/spec/fixtures/files/prometheus2.sysv new file mode 100644 index 000000000..bd850896e --- /dev/null +++ b/spec/fixtures/files/prometheus2.sysv @@ -0,0 +1,129 @@ +#!/bin/bash +# +# /etc/rc.d/init.d/prometheus +# +# Daemonize the prometheus monitoring system. +# +# chkconfig: 2345 95 20 +# description: Prometheus is an open-source systems monitoring and alerting toolkit originally built at SoundCloud +# processname: prometheus +# pidfile: /var/run/prometheus/pidfile + +# Source function library. +. /etc/init.d/functions + +DAEMON=/usr/local/bin/prometheus +PID_FILE=/var/run/prometheus/prometheus.pid +LOG_FILE=/var/log/prometheus + +[ -e /etc/sysconfig/prometheus ] && . /etc/sysconfig/prometheus + +export GOMAXPROCS=${GOMAXPROCS:-2} + +# +# Create the /var/run/prometheus directory, which can live on a tmpfs +# filesystem and be destroyed between reboots. +# +mkrundir() { + [ ! -d /var/run/prometheus ] && mkdir -p /var/run/prometheus + chown prometheus /var/run/prometheus +} + +# +# 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 prometheus /var/run/prometheus + if [ $? -ne 0 ] ; then + rm $PID_FILE + KILLPROC_OPT="" + fi +} + +start() { + echo -n "Starting prometheus: " + mkrundir + [ -f $PID_FILE ] && rm $PID_FILE + daemon --user=prometheus \ + --pidfile="$PID_FILE" \ + "$DAEMON" -log.format logger:stdout --config.file=/etc/prometheus/prometheus.yaml \ + --storage.tsdb.path=/var/lib/prometheus \ + --web.console.templates=/usr/local/share/prometheus/consoles \ + --web.console.libraries=/usr/local/share/prometheus/console_libraries \ + >> "$LOG_FILE" & + retcode=$? + mkpidfile + touch /var/lock/subsys/prometheus + return $retcode +} + +stop() { + DELAY=5 # seconds maximum to wait for a leave + + echo -n "Shutting down prometheus: " + mkpidfile + + prometheus_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 $prometheus_pid ; then + rm -f /var/lock/subsys/prometheus + 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/prometheus $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/prometheus ] && restart || : + ;; + *) + echo "Usage: prometheus {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/prometheus2.upstart b/spec/fixtures/files/prometheus2.upstart new file mode 100644 index 000000000..c73382121 --- /dev/null +++ b/spec/fixtures/files/prometheus2.upstart @@ -0,0 +1,34 @@ +# Prometheus Monitoring Framework (Upstart unit) +description "Prometheus Monitoring Framework" +start on runlevel [2345] +stop on runlevel [06] + +env PROMETHEUS=/usr/local/bin/prometheus +env USER=prometheus +env GROUP=prometheus +env DEFAULTS=/etc/default/prometheus +env RUNDIR=/var/run/prometheus +env PID_FILE=/var/run/prometheus/prometheus.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/prometheus", if available. + [ -e $DEFAULTS ] && . $DEFAULTS + + export GOMAXPROCS=${GOMAXPROCS:-2} + exec start-stop-daemon -c $USER -g $GROUP -p $PID_FILE -x $PROMETHEUS -S -- --config.file=/etc/prometheus/prometheus.yaml \ + --storage.tsdb.path=/var/lib/prometheus \ + --web.console.templates=/usr/local/share/prometheus/consoles \ + --web.console.libraries=/usr/local/share/prometheus/console_libraries \ + +end script + +respawn +respawn limit 10 10 +kill timeout 10 diff --git a/templates/prometheus.debian.erb b/templates/prometheus.debian.erb index d4a2e7022..f5c63bf8d 100644 --- a/templates/prometheus.debian.erb +++ b/templates/prometheus.debian.erb @@ -18,11 +18,8 @@ DESC="Prometheus monitoring framework" NAME=prometheus DAEMON=<%= scope.lookupvar('prometheus::bin_dir') %>/$NAME PIDFILE=/var/run/$NAME/$NAME.pid -DAEMON_ARGS=" -config.file=<%= scope.lookupvar('prometheus::config_dir')+'/prometheus.yaml' %> - -storage.local.path=<%= scope.lookupvar('prometheus::localstorage')%> - -web.console.templates=<%= scope.lookupvar('prometheus::shared_dir') %>/consoles - -web.console.libraries=<%= scope.lookupvar('prometheus::shared_dir') %>/console_libraries - <%= scope.lookupvar('prometheus::extra_options') %>" +DAEMON_ARGS="<%= @daemon_flags.join("\n ") %> + <%= scope.lookupvar('prometheus::extra_options') %>" USER=<%= scope.lookupvar('prometheus::user') %> SCRIPTNAME=/etc/init.d/$NAME diff --git a/templates/prometheus.launchd.erb b/templates/prometheus.launchd.erb index fffe37db8..e02df058f 100644 --- a/templates/prometheus.launchd.erb +++ b/templates/prometheus.launchd.erb @@ -16,14 +16,9 @@ <%= scope.lookupvar('prometheus::bin_dir') %>/prometheus agent - -config.file= - <%= scope.lookupvar('prometheus::config_dir')+'/prometheus.yaml' %> - -storage.local.path= - <%= scope.lookupvar('prometheus::localstorage')%> - -web.console.templates= - <%= scope.lookupvar('prometheus::shared_dir') %>/consoles - -web.console.libraries= - <%= scope.lookupvar('prometheus::shared_dir') %>/console_libraries + <%- daemon_flags.each do |flag| -%> + <%= flag %> + <%- end -%> <% require 'shellwords' %> <% for extra_option in Shellwords.split(scope.lookupvar('prometheus::extra_options')) %> <%= extra_option %> diff --git a/templates/prometheus.sles.erb b/templates/prometheus.sles.erb index 667e1145a..ffd4b71d2 100644 --- a/templates/prometheus.sles.erb +++ b/templates/prometheus.sles.erb @@ -36,10 +36,7 @@ case "$1" in echo -n "Starting prometheus " ## Start daemon with startproc(8). If this fails ## the return value is set appropriately by startproc. - startproc $PROMETHEUS_BIN -config.file="$CONFIG_FILE" \ - -storage.local.path=<%= scope.lookupvar('prometheus::localstorage')%> \ - -web.console.templates=<%= scope.lookupvar('prometheus::shared_dir') %>/consoles \ - -web.console.libraries=<%= scope.lookupvar('prometheus::shared_dir') %>/console_libraries \ + startproc $PROMETHEUS_BIN <%= @daemon_flags.join(" \\\n ") %> \ <%= scope.lookupvar('prometheus::extra_options') %> >> "$LOG_FILE" # Remember status and be verbose diff --git a/templates/prometheus.systemd.erb b/templates/prometheus.systemd.erb index 93714685c..480354815 100644 --- a/templates/prometheus.systemd.erb +++ b/templates/prometheus.systemd.erb @@ -7,10 +7,7 @@ After=basic.target network.target User=<%= scope.lookupvar('prometheus::user') %> Group=<%= scope.lookupvar('prometheus::group') %> ExecStart=<%= scope.lookupvar('prometheus::bin_dir') %>/prometheus \ - -config.file=<%= scope.lookupvar('prometheus::config_dir')+'/prometheus.yaml' %>\ - -storage.local.path=<%= scope.lookupvar('prometheus::localstorage')%> \ - -web.console.templates=<%= scope.lookupvar('prometheus::shared_dir') %>/consoles \ - -web.console.libraries=<%= scope.lookupvar('prometheus::shared_dir') %>/console_libraries \ + <%= @daemon_flags.join(" \\\n ") %> \ <%= scope.lookupvar('prometheus::extra_options') %> ExecReload=/bin/kill -HUP $MAINPID KillMode=process diff --git a/templates/prometheus.sysv.erb b/templates/prometheus.sysv.erb index 4d62b75d9..642572604 100644 --- a/templates/prometheus.sysv.erb +++ b/templates/prometheus.sysv.erb @@ -13,7 +13,6 @@ . /etc/init.d/functions DAEMON=<%= scope.lookupvar('prometheus::bin_dir') %>/prometheus -CONFIG=<%= scope.lookupvar('prometheus::config_dir')%>/prometheus.yaml PID_FILE=/var/run/prometheus/prometheus.pid LOG_FILE=/var/log/prometheus @@ -52,10 +51,7 @@ start() { [ -f $PID_FILE ] && rm $PID_FILE daemon --user=<%= scope.lookupvar('prometheus::user') %> \ --pidfile="$PID_FILE" \ - "$DAEMON" -log.format logger:stdout -config.file="$CONFIG" \ - -storage.local.path=<%= scope.lookupvar('prometheus::localstorage')%> \ - -web.console.templates=<%= scope.lookupvar('prometheus::shared_dir') %>/consoles \ - -web.console.libraries=<%= scope.lookupvar('prometheus::shared_dir') %>/console_libraries \ + "$DAEMON" -log.format logger:stdout <%= @daemon_flags.join(" \\\n ") %> \ <%= scope.lookupvar('prometheus::extra_options') %> >> "$LOG_FILE" & retcode=$? mkpidfile diff --git a/templates/prometheus.upstart.erb b/templates/prometheus.upstart.erb index 1d8b2ddc0..549bca642 100644 --- a/templates/prometheus.upstart.erb +++ b/templates/prometheus.upstart.erb @@ -4,7 +4,6 @@ start on runlevel [2345] stop on runlevel [06] env PROMETHEUS=<%= scope.lookupvar('prometheus::bin_dir') %>/prometheus -env CONFIG=<%= scope.lookupvar('prometheus::config_dir') %>/prometheus.yaml env USER=<%= scope.lookupvar('prometheus::user') %> env GROUP=<%= scope.lookupvar('prometheus::group') %> env DEFAULTS=/etc/default/prometheus @@ -23,10 +22,7 @@ script [ -e $DEFAULTS ] && . $DEFAULTS export GOMAXPROCS=${GOMAXPROCS:-2} - exec start-stop-daemon -c $USER -g $GROUP -p $PID_FILE -x $PROMETHEUS -S -- -config.file=$CONFIG \ - -storage.local.path=<%= scope.lookupvar('prometheus::localstorage')%> \ - -web.console.templates=<%= scope.lookupvar('prometheus::shared_dir') %>/consoles \ - -web.console.libraries=<%= scope.lookupvar('prometheus::shared_dir') %>/console_libraries \ + exec start-stop-daemon -c $USER -g $GROUP -p $PID_FILE -x $PROMETHEUS -S -- <%= @daemon_flags.join(" \\\n ") %> \ <%= scope.lookupvar('prometheus::extra_options') %> end script