Skip to content
Snippets Groups Projects
Commit ef1cf611 authored by Roman Tsisyk's avatar Roman Tsisyk
Browse files

Debian: remove old files

parent fa24aac8
No related branches found
No related tags found
No related merge requests found
Debian package provides some additional options in config-file:
* opt file_descriptors = INTVALUE
Init script will do 'ulimit -f INTVALUE' command before starting tarantool.
* opt save_snapshots = COUNT
Count of snapshots to save (default = 10). COUNT=0 disables removing
old snapshots.
* opt snapshot_period = HOURS
Period between two snapshot (default 24).
There are script tarantool_snapshot_rotate (1) that is started every hour
using cron.hourly.
......@@ -46,6 +46,9 @@ tarantool (1.6.7-523-g816224e) unstable; urgency=medium
- Actualize debian/copyright
- Remove support for GNU hurd architecture (doesn't work).
- Don't use /var/lib/tarantool/started and /var/lib/tarantool/snapshot dirs.
- Drop irrelevant README.Debian.
- Remove old cron scripts.
-- Roman Tsisyk <roman@tarantool.org> Mon, 14 Dec 2015 20:00:00 +0300
......
#!/bin/sh
set -e
CFG=$1
ACTION=$2
CONFIG_DIR=/var/lib/tarantool/started
SNAPSHOT_DIR=/var/lib/tarantool/snapshot
PID_DIR=/var/run/tarantool
LOG_DIR=/var/log/tarantool
BOX=/usr/bin/tarantool
SSD=start-stop-daemon
CFG_DIR=/etc/tarantool/instances.enabled
cd $CFG_DIR
usage="Usage: sh $0 /path/to/config.file start|stop"
if ! test -x $BOX; then
exit 0
fi
if test -z "$CFG"; then
echo $usage
exit 5
fi
if ! echo $ACTION|grep -q '^\(start\|stop\)$'; then
echo $usage
exit 5
fi
if ! test -r "$CFG"; then
if echo $CFG|grep -q '\.cfg$'; then
echo File $CFG not found
exit 10
else
if ! test -r "$CFG.cfg"; then
echo "Instance config '$CFG' not found"
exit 15
fi
CFG="$CFG.cfg"
fi
fi
CFG=`readlink -f "$CFG"`
if ! test -x $BOX; then
echo "$BOX not found or can't be started"
exit 20
fi
NAME=`basename $CFG .cfg`
PID=$PID_DIR/$NAME.pid
SCFG=$CONFIG_DIR/$NAME
RUNDIR=$SNAPSHOT_DIR/$NAME
LOG=$LOG_DIR/$NAME.log
LOGGER="exec /usr/lib/tarantool/logger $LOG"
SOCKETS=`grep \
'^[[:space:]]*\(opt[[:space:]]\+\)\?file_descriptors[[:space:]]*=[[:space:]]*[[:digit:]]\+' $CFG \
| tail -n 1 \
| sed 's/[^[:digit:]]//g'`
if test -z $SOCKETS; then
SOCKETS=1023
fi
if ! test -x $PID_DIR; then
install -otarantool -gtarantool -d -m0750 $PID_DIR
fi
SSDARGS_NO_PID="--quiet --chdir $RUNDIR --chuid tarantool --exec"
SSDARGS="--pidfile $PID $SSDARGS_NO_PID"
if [ $SOCKETS -gt 1024 -a $SOCKETS -lt 65000 ]; then
ulimit -n $SOCKETS
fi
ulimit -c unlimited
comment_str="#### - commented by init script"
sed "s/^[[:space:]]*file_descriptors.*/# & $comment_str/" $CFG > $SCFG
echo "pid_file = $PID" >> $SCFG
echo "logger = \"$LOGGER\"" >> $SCFG
grep '^[[:space:]]*admin_port[[:space:]]*=' $CFG |tail -n 1 >> $SCFG
grep '^[[:space:]]*primary_port[[:space:]]*=' $CFG |tail -n 1 >> $SCFG
$BOX -c $SCFG -v --check-config
if [ ! -d $RUNDIR ]; then
install -d -otarantool -gtarantool -m0750 $RUNDIR
cd $RUNDIR
if ! $SSD --start $SSDARGS $BOX -- --init-storage -v -c $SCFG;
then
rm -fr $RUNDIR
exit 25
fi
fi
if [ $ACTION = 'start' ]; then
echo -n " Starting '$NAME' ... "
else
echo -n " Stopping '$NAME' ... "
SSDARGS="--retry 15 $SSDARGS"
fi
if $SSD --$ACTION $SSDARGS $BOX -- -B -v -c $SCFG; then
echo "ok"
else
ret=$?
if [ $ret -eq 1 ]; then
if [ $ACTION = 'start' ]; then
echo "already started"
else
echo "already stoppped"
fi
else
echo "failed"
fi
fi
#!/usr/bin/perl
use warnings;
use strict;
use constant CONFIG_DIR => '/var/lib/tarantool/started';
use constant PID_DIR => '/var/run/tarantool';
use File::Spec::Functions 'catfile';
use File::Basename 'basename', 'dirname';
use IO::Socket::INET;
exit 0 unless -x PID_DIR;
exit 0 unless -x CONFIG_DIR;
for (glob catfile PID_DIR, '*.pid') {
my $cfg = catfile CONFIG_DIR, basename $_, '.pid';
unless(-r $cfg) {
warn "Config file '$cfg' is not found\n";
next;
}
my $admin_port;
if (open my $fh, '<', $cfg) {
my @cfg = <$fh>;
($admin_port) = grep /^\s*admin_port\s*=\s*\d+\s*$/, reverse @cfg;
} else {
warn "$!\n";
next;
}
unless($admin_port) {
warn "admin_port is not found in $cfg\n";
next;
}
$admin_port =~ s/\D+//g;
my $socket = IO::Socket::INET->new(PeerAddr => "localhost:$admin_port");
unless($socket) {
warn "Can't connect to localhost:$admin_port: $!";
next;
}
my $logger_pid;
local $SIG{ALRM} = sub { die "alarm\n" };
alarm 3;
eval {
print $socket "show info\n";
while(<$socket>) {
next unless /^\s*logger_pid:\s*(\d+)\s*$/;
$logger_pid = $1;
last;
}
};
alarm 0;
unless($logger_pid) {
warn "Can't define logger_pid\n";
next;
}
unless(kill 'HUP' => $logger_pid) {
warn "Can't send HUP to pid=$logger_pid: $!";
next;
}
}
=head1 NAME
/usr/lib/tarantool/tarantool_logrotate - utility to rotate
tarantool instances logs
=head1 SINOPSYS
tarantool_logrotate
=head1 DESCRIPTION
The utility tries to connect to each running tarantool instance to get
logger pid file, then it sends B<SIGHUP> to logger which initiates
log rotataion procedure.
#!/usr/bin/perl
use warnings;
use strict;
use constant CONFIG_DIR => '/var/lib/tarantool/started';
use constant PID_DIR => '/var/run/tarantool';
use constant SNAPSHOT_DIR => '/var/lib/tarantool/snapshot';
use File::Spec::Functions 'catfile';
use File::Basename 'basename', 'dirname';
use IO::Socket::INET;
use Getopt::Long;
use Pod::Usage;
use Data::Dumper;
pod2usage(-exitstatus => 0, -verbose => 2) unless
GetOptions
'help|h' => \my $help,
'verbose|v' => \my $verbose,
'snapshots|s=i' => \my $snapshots,
'snapshot_period|p=i' => \my $period,
;
sub DEBUGF($;@) {
return unless $verbose;
my ($fmt, @arg) = @_;
$fmt =~ s/\s*$/\n/;
printf STDERR $fmt, @arg;
}
sub list_files($) {
my $sndir = shift;
unless (-d $sndir) {
DEBUGF 'Snapshot directory "%s" was not found', $sndir;
return;
}
unless (-w $sndir) {
DEBUGF 'Can not write into directory %s', $sndir;
return;
}
my $dh;
opendir $dh, $sndir;
my @files = sort
grep /\.(snap|xlog)$/,
grep { -r $_ and -f $_ }
map { catfile $sndir, $_ }
readdir $dh;
return @files;
}
sub rotate_snapshot($$$) {
my ($pidfile, $snap_count, $snap_period) = @_;
$snap_count = $snapshots || 10 unless defined $snap_count;
$snap_period = $period || 24 unless defined $snap_period;
$snap_count = $snapshots if defined $snapshots;
$snap_period = $period if defined $period;
$snap_period ||= 0;
my $pid;
DEBUGF "\tBegin rotating process period=%s, count=%s",
$snap_period, $snap_count;
if (open my $ph, '<', $pidfile) {
$pid = <$ph>;
$pid =~ s/\D+//g;
} else {
warn "Can't open file $pidfile: $!\n";
return;
}
unless($pid) {
warn "PID was not received\n";
return;
}
my $sndir = catfile SNAPSHOT_DIR, basename $pidfile, '.pid';
my @files = list_files $sndir;
goto FINISH unless @files;
my ($last_snap) = grep /\.snap$/, reverse @files;
if ($last_snap) {{
my @stat = stat $last_snap;
last unless @stat;
my $ctime = $stat[10];
if (time - $ctime < $snap_period * 3600 - 3600 / 2) {
DEBUGF "\tLast snapshot was created %3.2f hours ago, ".
"do not create new",
(time - $ctime) / 3600;
return;
}
DEBUGF "\tLast snapshot was created %3.2f hours ago, creating new",
(time - $ctime) / 3600;
}} else {
DEBUGF "\tLast snapshot was not found, creating new";
}
while(@files and $files[0] =~ /\.xlog$/) {
DEBUGF "\tRemove orphaned %s", $files[0];
unless (unlink $files[0]) {
DEBUGF "\tCan't unlink file %s: %s", $files[0], $!;
return;
}
shift @files;
}
unless (kill 0 => $pid) {
DEBUGF "\tProcess %s is not started", $pidfile;
return;
}
if (kill USR1 => $pid) {
goto FINISH unless @files;
for (my $i = 0; $i < 5; $i++) {
sleep 1;
my @inpr = sort glob catfile SNAPSHOT_DIR, '*.snap.inprogress';
last unless @inpr;
if ($inpr[-1] and $inpr[-1] gt $files[-1]) {
DEBUGF "\tsnapshot %s is still in progress...", $inpr[-1];
next;
}
}
} else {
warn "Error while sending snapshot signal: $!";
return;
}
if ($snap_count) {
@files = list_files $sndir;
my $snaps = grep /\.snap$/, @files;
if ($snaps > $snap_count) {
my $to_remove = $snaps - $snap_count;
while (@files) {
my $file = shift @files;
$to_remove-- if $file =~ /\.snap$/;
DEBUGF "\tUnlink file: %s...", $file;
unless (unlink $file) {
DEBUGF "\tCan't unlink file %s: %s", $file, $!;
return;
}
last unless $to_remove > 0;
}
while(@files and $files[0] =~ /\.xlog$/) {
DEBUGF "\tRemove orphaned %s", $files[0];
unless (unlink $files[0]) {
DEBUGF "\tCan't unlink file %s: %s", $files[0], $!;
return;
}
shift @files;
}
}
} else {
DEBUGF "\tDon't remove any old snapshots";
}
FINISH:
}
DEBUGF "Looking through %s...", PID_DIR;
for (glob catfile PID_DIR, '*.pid') {
my $cfg = catfile CONFIG_DIR, basename $_, '.pid';
unless(-r $cfg) {
warn "Config file '$cfg' is not found\n";
next;
}
DEBUGF 'Found instance "%s" (%s)', basename($cfg), basename $_;
if (open my $fh, '<:encoding(UTF-8)', $cfg) {
my @lines = <$fh>;
my ($user_snapshots) =
grep /^\s*(?:opt\s+)?save_snapshots\s*=\s*\d+\s*(?:#.*)?$/,
reverse @lines;
my ($snapshot_period) =
grep /^\s*(?:opt\s+)?snapshot_period\s*=\s*\d+\s*(?:#.*)?$/,
reverse @lines;
if ($user_snapshots) {
for ($user_snapshots) {
s/#.*//;
s/\D//g;
}
unless($user_snapshots =~ /^[1-9]\d*$/) {
warn "wrong format of save_snapshots\n";
$user_snapshots = undef;
}
}
if ($snapshot_period) {
for ($snapshot_period) {
s/#.*//;
s/\D//g;
}
unless($snapshot_period =~ /^[1-9]\d*$/) {
warn "wrong format of snapshot_period\n";
$snapshot_period = undef;
}
}
rotate_snapshot $_, $user_snapshots, $snapshot_period;
} else {
warn "Can't open file $cfg: $!\n";
next;
}
}
exit 0 unless -x PID_DIR;
exit 0 unless -x CONFIG_DIR;
=head1 NAME
tarantool_snapshot_rotate - script to creates/rotates snapshots
=head1 SYNOPSIS
tarantool_snapshot_rotate
tarantool_snapshot_rotate --verbose
=head1 DESCRIPTION
The script passes through all started tarantool instances and creates
snapshots for each instance.
The script understands some additional options in tarantool.cfg:
=over
=item save_snapshots = COUNT
Count of snapshots to save (default = 10). COUNT=0 disables removing
old snapshots.
=back
=head1 OPTIONS
=over
=item -h | --help
show the helpscreen
=item -v | --verbose
log process to B<STDOUT>
=item -s | --snapshots COUNT
redefines B<save_snapshots> option of config file
=back
=cut
#!/bin/sh
test -x /usr/bin/tarantool || exit 0
test -x /usr/sbin/tarantool_snapshot_rotate || exit 0
exec /usr/sbin/tarantool_snapshot_rotate
......@@ -20,8 +20,6 @@ case "$1" in
install -d -o$SYSUSER -gadm -m2750 /var/log/tarantool
install -d -o$SYSUSER -g$SYSUSER -m750 /var/run/tarantool
install -d -o$SYSUSER -g$SYSUSER -m750 /var/lib/tarantool/started
install -d -o$SYSUSER -g$SYSUSER -m750 /var/lib/tarantool/snapshot
;;
esac
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment