From 8da953a979d97a9647791a9ec9e6e5148c2f29e1 Mon Sep 17 00:00:00 2001 From: oloc Date: Fri, 10 Jul 2015 11:11:49 +0200 Subject: [PATCH 1/6] Fixes #37 - Template of the init script and take care of the configdir. --- manifests/config.pp | 5 + .../etc/init.d/logstashforwarder.Debian.erb | 299 +++++++----------- 2 files changed, 128 insertions(+), 176 deletions(-) diff --git a/manifests/config.pp b/manifests/config.pp index cc1fa3d..dfbfd53 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -50,6 +50,11 @@ false => undef, } + file { "/etc/init.d/${logstashforwarder::service_name}": + ensure => file, + content => template("${module_name}/etc/init.d/logstashforwarder.Debian.erb"), + } + file { $logstashforwarder::configdir: ensure => directory, mode => '0644', diff --git a/templates/etc/init.d/logstashforwarder.Debian.erb b/templates/etc/init.d/logstashforwarder.Debian.erb index 210d761..133588d 100644 --- a/templates/etc/init.d/logstashforwarder.Debian.erb +++ b/templates/etc/init.d/logstashforwarder.Debian.erb @@ -1,203 +1,150 @@ -#!/bin/bash -# -# /etc/init.d/logstash -- startup script for LogStash. +#!/bin/sh +# Init script for logstash-forwarder +# Maintained by +# Generated by pleaserun. +# Implemented based on LSB Core 3.1: +# * Sections: 20.2, 20.3 # ### BEGIN INIT INFO -# Provides: logstash -# Required-Start: $all -# Required-Stop: $all +# Provides: logstash-forwarder +# Required-Start: $remote_fs $syslog +# Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 -# Short-Description: Starts logstash -# Description: Starts logstash using start-stop-daemon +# Short-Description: +# Description: no description given ### END INIT INFO -set -e - -PATH=/bin:/usr/bin:/sbin:/usr/sbin -NAME=logstash -DESC="Logstash Daemon" -DEFAULT=/etc/default/$NAME - -if [ `id -u` -ne 0 ]; then - echo "You need root privileges to run this script" - exit 1 -fi - -. /lib/lsb/init-functions - -if [ -r /etc/default/rcS ]; then - . /etc/default/rcS -fi - -# The following variables can be overwritten in $DEFAULT +PATH=/sbin:/usr/sbin:/bin:/usr/bin +export PATH -# Run logstash as this user ID and group ID -LS_USER=logstash -LS_GROUP=logstash +name=logstash-forwarder +#name=<%= scope.lookupvar('logstashforwarder::lsf_name') %> # need PR #54 +program=<%= @installpath %>/bin/$name +args=-config\ <%= @configdir %>/$name.conf +pidfile="/var/run/$name.pid" -JAVA=/usr/bin/java +[ -r /etc/default/$name ] && . /etc/default/$name +[ -r /etc/sysconfig/$name ] && . /etc/sysconfig/$name -# Directory where the logstash all in one jar lives -LS_HOME=/var/lib/logstash - -# Additional Java OPTS -LS_JAVA_OPTS=" -Djava.io.tmpdir=/var/logstash/" +trace() { + logger -t "/etc/init.d/$name" "$@" +} -# logstash log directory -LOG_DIR=/var/log/logstash +emit() { + trace "$@" + echo "$@" +} -# logstash configuration directory -CONF_DIR=/etc/logstash/conf.d +start() { -# logstash log file -LOG_FILE=$LOG_DIR/$NAME.log -# Open File limit -OPEN_FILES=2048 + # Setup any environmental stuff beforehand -# LogStash options -LS_OPTS="--log ${LOG_DIR}/${NAME}.log" -# Nice level -NICE=19 + # Run the program! -# End of variables that can be overwritten in $DEFAULT + chroot --userspec "$user":"$group" "$chroot" sh -c " + + cd \"$chdir\" + exec \"$program\" $args + " >> /var/log/$name/$name.log 2>> /var/log/$name/$name.err & -# overwrite settings from default file -if [ -f "$DEFAULT" ]; then - . "$DEFAULT" -fi + # Generate the pidfile from here. If we instead made the forked process + # generate it there will be a race condition between the pidfile writing + # and a process possibly asking for status. + echo $! > $pidfile + emit "$name started" + return 0 +} -# Define other required variables -PID_FILE=/var/run/$NAME.pid -DAEMON=$LS_JAR -DAEMON_OPTS="agent -f ${CONF_DIR} ${LS_OPTS}" +stop() { + # Try a few times to kill TERM the program + if status ; then + pid=$(cat "$pidfile") + trace "Killing $name (pid $pid) with SIGTERM" + kill -TERM $pid + # Wait for it to exit. + for i in 1 2 3 4 5 ; do + trace "Waiting $name (pid $pid) to die..." + status || break + sleep 1 + done + if status ; then + emit "$name stop failed; still running." + else + emit "$name stopped." + fi + fi +} -is_true() { - if [ "x$1" = "xtrue" -o "x$1" = "xyes" -o "x$1" = "x1" ] ; then - return 0 +status() { + if [ -f "$pidfile" ] ; then + pid=$(cat "$pidfile") + if ps -p $pid > /dev/null 2> /dev/null ; then + # process by this pid is running. + # It may not be our pid, but that's what you get with just pidfiles. + # TODO(sissel): Check if this process seems to be the same as the one we + # expect. It'd be nice to use flock here, but flock uses fork, not exec, + # so it makes it quite awkward to use in this case. + return 0 else - return 1 + return 2 # program is dead but pid file exists fi + else + return 3 # program is not running + fi } -# Check DAEMON exists -if ! test -e $DAEMON; then - log_failure_msg "Daemon $DAEMON doesn't exist" - exit 1 -fi +force_stop() { + if status ; then + stop + status && kill -KILL $(cat "$pidfile") + fi +} + +case "$1" in + force-start|start|stop|force-stop|restart) + trace "Attempting '$1' on $name" + ;; +esac case "$1" in - start) - if ! is_true "$START" ; then - echo "logstash not configured to start, please edit /etc/default/logstash to enable" - exit 0 - fi - - if [ -z "$JAVA" ]; then - log_failure_msg "no JDK found - $JAVA" - exit 1 - fi - - # Check if a config file exists - if [ ! "$(ls -A $CONF_DIR/*.conf 2> /dev/null)" ]; then - log_failure_msg "There aren't any configuration files in $CONF_DIR" - exit 1 - fi - - log_daemon_msg "Starting $DESC" - - if start-stop-daemon --test --start --pidfile "$PID_FILE" \ - --user "$LS_USER" --exec "$JAVA" \ - >/dev/null; then - # Prepare environment - ulimit -n $OPEN_FILES - - # Start Daemon - start-stop-daemon --start -b --user "$LS_USER" -c "$LS_USER":"$LS_GROUP" \ - -d "$LS_HOME" --pidfile "$PID_FILE" --make-pidfile \ - -N $NICE \ - --exec "$JAVA" -- $LS_JAVA_OPTS -jar $DAEMON $DAEMON_OPTS - - sleep 1 - - if start-stop-daemon --test --start --pidfile "$PID_FILE" \ - --user "$LS_USER" --exec "$JAVA" \ - >/dev/null; then - - if [ -f "$PID_FILE" ]; then - rm -f "$PID_FILE" - fi - - log_end_msg 1 - else - log_end_msg 0 - fi - else - log_progress_msg "(already running)" - log_end_msg 0 - fi - ;; - stop) - log_daemon_msg "Stopping $DESC" - - set +e - - if [ -f "$PID_FILE" ]; then - start-stop-daemon --stop --pidfile "$PID_FILE" \ - --user "$LS_USER" \ - --retry=TERM/20/KILL/5 >/dev/null - - if [ $? -eq 1 ]; then - log_progress_msg "$DESC is not running but pid file exists, cleaning up" - elif [ $? -eq 3 ]; then - PID="`cat $PID_FILE`" - log_failure_msg "Failed to stop $DESC (pid $PID)" - exit 1 - fi - - rm -f "$PID_FILE" - else - log_progress_msg "(not running)" - fi - - log_end_msg 0 - set -e - ;; - status) - set +e - - start-stop-daemon --test --start --pidfile "$PID_FILE" \ - --user "$LS_USER" --exec "$JAVA" \ - >/dev/null 2>&1 - - if [ "$?" = "0" ]; then - if [ -f "$PID_FILE" ]; then - log_success_msg "$DESC is not running, but pid file exists." - exit 1 - else - log_success_msg "$DESC is not running." - exit 3 - fi - else - log_success_msg "$DESC is running with pid `cat $PID_FILE`" - fi - - set -e - ;; - restart|force-reload) - if [ -f "$PID_FILE" ]; then - $0 stop - sleep 1 - fi - - $0 start - ;; - *) - log_success_msg "Usage: $0 {start|stop|restart|force-reload|status}" - exit 1 - ;; + force-start) + PRESTART=no + exec "$0" start + ;; + start) + status + code=$? + if [ $code -eq 0 ]; then + emit "$name is already running" + exit $code + else + start + exit $? + fi + ;; + stop) stop ;; + force-stop) force_stop ;; + status) + status + code=$? + if [ $code -eq 0 ] ; then + emit "$name is running" + else + emit "$name is not running" + fi + exit $code + ;; + restart) + + stop && start + ;; + *) + echo "Usage: $SCRIPTNAME {start|force-start|stop|force-start|force-stop|status|restart}" >&2 + exit 3 + ;; esac -exit 0 +exit $? From 1aa09dbafb63ab98ac7bc67970d0acac2ebe4f7b Mon Sep 17 00:00:00 2001 From: oloc Date: Thu, 16 Jul 2015 15:47:46 +0200 Subject: [PATCH 2/6] Update Gemfile to convert git into https to avoid RSA Key issue --- .travis.yml | 22 ++++++++++++++++++++-- Gemfile | 4 ++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index ba588c7..03be2ba 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ rvm: - 1.8.7 - 1.9.3 - 2.0.0 + - 2.1 before_install: rm Gemfile.lock || true script: - "bundle exec rake lint" @@ -15,6 +16,23 @@ env: - PUPPET_VERSION="~> 3.2.0" - PUPPET_VERSION="~> 3.3.0" - PUPPET_VERSION="~> 3.4.0" + - PUPPET_VERSION="~> 3.5.0" matrix: - allow_failures: - - rvm: 2.0.0 + exclude: + - rvm: 1.9.3 + env: PUPPET_VERSION="~> 2.7.0" + - rvm: 2.0.0 + env: PUPPET_VERSION="~> 2.7.0" + - rvm: 2.0.0 + env: PUPPET_VERSION="~> 3.1.0" + - rvm: 2.1 + env: PUPPET_VERSION="~> 2.7.0" + - rvm: 2.1 + env: PUPPET_VERSION="~> 3.1.0" + - rvm: 2.1 + env: PUPPET_VERSION="~> 3.2.0" + - rvm: 2.1 + env: PUPPET_VERSION="~> 3.3.0" + - rvm: 2.1 + env: PUPPET_VERSION="~> 3.4.0" + diff --git a/Gemfile b/Gemfile index b768425..e5da386 100644 --- a/Gemfile +++ b/Gemfile @@ -3,8 +3,8 @@ source 'https://rubygems.org' puppetversion = ENV['PUPPET_VERSION'] || '~> 3.7.0' gem 'puppet', puppetversion, :require => false -gem 'beaker', :git => 'git@github.com:puppetlabs/beaker.git', :branch => 'master' -gem 'beaker-rspec', :git => 'git@github.com:puppetlabs/beaker-rspec.git', :branch => 'master' +gem 'beaker', :git => 'https://github.com/puppetlabs/beaker.git', :branch => 'master' +gem 'beaker-rspec', :git => 'https://github.com/puppetlabs/beaker-rspec.git', :branch => 'master' gem 'metadata-json-lint', :git => 'https://github.com/nibalizer/metadata-json-lint.git', :branch => 'master' gem 'rspec-puppet', :git => 'https://github.com/rodjek/rspec-puppet.git', :branch => 'master' From 9d40004e1c54937005caa296c32646399c285d8e Mon Sep 17 00:00:00 2001 From: oloc Date: Thu, 16 Jul 2015 16:08:33 +0200 Subject: [PATCH 3/6] Update: remove the old Ruby 1.8.7 --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 03be2ba..6dcd006 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: ruby rvm: - - 1.8.7 - 1.9.3 - 2.0.0 - 2.1 From bb4d10e929a10a01f0c2acc110ebfd17bc048918 Mon Sep 17 00:00:00 2001 From: oloc Date: Mon, 20 Jul 2015 11:25:14 +0200 Subject: [PATCH 4/6] PR #46 - 2a482166dc2ae2357b74e4be44f1751dd3da156e --- manifests/repo.pp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifests/repo.pp b/manifests/repo.pp index d97462b..b75f4f2 100644 --- a/manifests/repo.pp +++ b/manifests/repo.pp @@ -39,7 +39,7 @@ location => 'http://packages.elasticsearch.org/logstashforwarder/debian', release => 'stable', repos => 'main', - key => 'D88E42B4', + key => '46095ACC8548582C1A2699A9D27D666CD88E42B4', key_source => 'http://packages.elasticsearch.org/GPG-KEY-elasticsearch', include_src => false, } From 5a7d904b8fc3fb42f7eb7dc62c3896c12499473b Mon Sep 17 00:00:00 2001 From: oloc Date: Tue, 21 Jul 2015 12:46:51 +0200 Subject: [PATCH 5/6] Add ensure present in the spec for init.d services. --- spec/classes/001_logstashforwarder_init_spec.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/classes/001_logstashforwarder_init_spec.rb b/spec/classes/001_logstashforwarder_init_spec.rb index f70c165..32497e9 100644 --- a/spec/classes/001_logstashforwarder_init_spec.rb +++ b/spec/classes/001_logstashforwarder_init_spec.rb @@ -214,6 +214,7 @@ let (:params) { default_params.merge({ + :ensure => 'present', :init_template => "logstashforwarder/etc/init.d/logstashforwarder.Debian.erb" }) } @@ -226,6 +227,7 @@ let (:params) { default_params.merge({ + :ensure => 'present', :init_template => "logstashforwarder/etc/init.d/logstashforwarder.Debian.erb", :restart_on_change => false }) From 1cd14cd4fc9d1bd148438c90a617ae6cf3901cf2 Mon Sep 17 00:00:00 2001 From: oloc Date: Tue, 21 Jul 2015 14:09:49 +0200 Subject: [PATCH 6/6] Move init.d file from config to service/init. --- manifests/config.pp | 5 ----- manifests/service/init.pp | 7 ++++++- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/manifests/config.pp b/manifests/config.pp index dfbfd53..cc1fa3d 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -50,11 +50,6 @@ false => undef, } - file { "/etc/init.d/${logstashforwarder::service_name}": - ensure => file, - content => template("${module_name}/etc/init.d/logstashforwarder.Debian.erb"), - } - file { $logstashforwarder::configdir: ensure => directory, mode => '0644', diff --git a/manifests/service/init.pp b/manifests/service/init.pp index 30f6458..aaca393 100644 --- a/manifests/service/init.pp +++ b/manifests/service/init.pp @@ -114,7 +114,12 @@ before => Service[$name], notify => $notify_service } - + } + else { + file { "/etc/init.d/${logstashforwarder::service_name}": + ensure => file, + content => template("${module_name}/etc/init.d/logstashforwarder.Debian.erb"), + } } }