#!/usr/bin/perl -w

=head1 NAME

    mk-gearman-package-files - создание файлов для пакета с gearman

=head1 DESCRIPTION

=cut

use strict;
use warnings;
use utf8;

use SvnRelease::MkFiles;

my $mkfiles = new SvnRelease::MkFiles(
    files => [
        {tmpl => 'gearman_conf', file => "/etc/default/[% instance %]", perm => 0644, conf => 1 },
        {tmpl => 'gearman_init_d', file => "/etc/init.d/[% instance %]", perm => 0755 },
        {tmpl => 'monrun_conf', file => "/etc/monrun/conf.d/[% instance %].conf", },
        {tmpl => 'monrun_restart_conf', file => "/etc/monrun/conf.d/[% instance %]-restart.conf", },
        {tmpl => 'debian_postinst', file => "/DEBIAN/postinst", perm => 0755, },
        {tmpl => 'debian_postrm', file => "/DEBIAN/postrm", perm => 0755, },
    ],
    templates_fh => \*DATA,
    substvars => {
        'gearmand:Depends' => 'gearman-job-server (=1:0.33-hl1), yandex-du-check-gearmand',
    },

    );

my ($instance, @dummy) = $mkfiles->parse_options("instance_name");
if (@dummy || $instance !~ /^[a-z0-9_\-]+$/) {
    $mkfiles->usage("Incorrect instance name");
}

$mkfiles->{vars}->{instance} = $instance;
$mkfiles->mk();

__DATA__
<< gearman_conf
# Additional options that are passed to the Daemon.
DAEMON_OPTS="-d --pidfile=/var/run/[% instance %].pid"

# Change this to true to enable the germand server
ENABLED="true"

ulimit -n 4096

<< gearman_init_d
#! /bin/sh

### BEGIN INIT INFO
# Provides: gearmand
# Required-Start: $syslog $network $local_fs
# Required-Stop: $syslog $network $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Gearman distributed work system
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/gearmand
NAME=[% instance %]
DESC="gearman distributed job server"

test -x $DAEMON || exit 0

# Include gearman-server defaults if available
if [ -f /etc/default/[% instance %] ] ; then
    . /etc/default/[% instance %]
fi

set -e

if [ $ENABLED = "false" ] ; then
    exit 0;
fi

case "$1" in
  start)
    echo -n "Starting $DESC: "
    start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
        --exec $DAEMON -- --pid-file /var/run/$NAME.pid -u root -d -R -w 1 -t 10 -j 0 --port 7003 --log-file=/var/log/gearmand/error.log
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
    start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
        -n $NAME --oknodo
    echo "$NAME."
    #rm /var/run/$NAME.pid
    ;;
  #reload)
    #
    #   If the daemon can reload its config files on the fly
    #   for example by sending it SIGHUP, do it here.
    #
    #   If the daemon responds to changes in its config file
    #   directly anyway, make this a do-nothing entry.
    #
    # echo "Reloading $DESC configuration files."
    # start-stop-daemon --stop --signal 1 --quiet --pidfile \
    #   /var/run/$NAME.pid --exec $DAEMON
  #;;
  force-reload)
    #
    #   If the "reload" option is implemented, move the "force-reload"
    #   option to the "reload" entry above. If not, "force-reload" is
    #   just the same as "restart" except that it does nothing if the
    #   daemon isn't already running.
    # check wether $DAEMON is running. If so, restart
    start-stop-daemon --stop --test --quiet --pidfile \
        /var/run/$NAME.pid --exec $DAEMON \
    && $0 restart \
    || exit 0
    ;;
  restart)
    echo -n "Restarting $DESC: "
    start-stop-daemon --stop --quiet --pidfile \
        /var/run/$NAME.pid --exec $DAEMON
    sleep 1
    start-stop-daemon --start --quiet --pidfile \
        /var/run/$NAME.pid --exec $DAEMON -- --pid-file /var/run/$NAME.pid -u root -d --port 7003 --log-file=/var/log/gearmand/error.log
    echo "$NAME."
    ;;
  *)
    N=/etc/init.d/$NAME
    # echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
    echo "Usage: $N {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0


<< debian_postinst
#!/bin/bash -e

. /usr/share/debconf/confmodule

update-rc.d [% instance %] defaults >/dev/null



<< debian_postrm
#!/bin/bash -e

. /usr/share/debconf/confmodule

update-rc.d -f [% instance %] remove >/dev/null


<< monrun_conf
[[% instance %]]
execution_interval=60
execution_timeout=30
command=(echo status | nc.traditional -w 1 127.0.0.1 7003 | grep -q .) && echo '0;Ok' || echo '2;Problem with [% instance %].'

<< monrun_restart_conf
[[% instance %]_restart]
execution_interval=60
execution_timeout=30
command=/usr/local/sbin/check-gearmand.sh
type=direct

