Skip to content

Commit 6880dbb

Browse files
author
Vyacheslav Matjukhin
committed
run ubic-watchdog and ubic-update as services
* ubic-ping service renamed to ubic.ping * ubic-watchdog becomes ubic.watchdog service * ubic-update becomes ubic.update service * ubic-periodic helper script for running these scripts as services * but we still check watchdog itself via cron * debian maintainer scripts refactored
1 parent bf50d34 commit 6880dbb

File tree

17 files changed

+170
-64
lines changed

17 files changed

+170
-64
lines changed

bin/ubic-periodic

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env perl
2+
package ubic_periodic;
3+
4+
# ABSTRACT: run given command every N seconds
5+
6+
use strict;
7+
use warnings;
8+
9+
=head1 SYNOPSIS
10+
11+
ubic-periodic --period=60 --stdout=/var/log/ubic/watchdog.log --stderr=/var/log/ubic/watchdog.err.log ubic-watchdog
12+
13+
=cut
14+
15+
use Getopt::Long 2.33;
16+
use Pod::Usage;
17+
18+
return 1 if caller;
19+
20+
my $period = 60;
21+
my $stdout;
22+
my $stderr;
23+
24+
GetOptions(
25+
'period=i' => \$period,
26+
'stdout=s' => \$stdout,
27+
'stderr=s' => \$stderr,
28+
) or pod2usage(2);
29+
pod2usage(2) unless @ARGV == 1;
30+
31+
my $command = shift @ARGV;
32+
33+
while (1) {
34+
my $start_time = time;
35+
36+
# we reopen logs on every loop, so we don't have to restart periodic service on logrotate
37+
if ($stdout) {
38+
open STDOUT, '>>', $stdout or die "Can't open stdout: $!";
39+
}
40+
if ($stderr) {
41+
open STDERR, '>>', $stderr or die "Can't open stderr: $!";
42+
}
43+
system($command); # no, we don't check for failures
44+
45+
my $time = time;
46+
if ($time - $start_time < $period) {
47+
sleep $start_time + $period - $time;
48+
}
49+
}

debian/cron.d

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# vim: ft=crontab
22
LOGDIR=/var/log/ubic
33

4-
* * * * * root ubic-watchdog >>$LOGDIR/watchdog.log 2>>$LOGDIR/watchdog.err.log
5-
* * * * * root ubic-update >>$LOGDIR/update.log 2>>$LOGDIR/update.err.log
4+
* * * * * root ubic-watchdog ubic.watchdog >>$LOGDIR/watch_watchdog.log 2>>$LOGDIR/watch_watchdog.err.log

debian/postinst

+10-9
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ set -e
44
if [ "$1" = "configure" ]; then
55
if [ -n "$2" ]; then
66
if dpkg --compare-versions "$2" lt 1.21; then
7-
for dir in watchdog/lock watchdog/status; do
8-
chmod 777 /var/lib/ubic/$dir
9-
chmod +t /var/lib/ubic/$dir
10-
done
11-
elif dpkg --compare-versions "$2" lt 1.26; then
12-
ubic-admin setup --batch-mode --no-crontab
7+
ubic-admin setup --reconfigure --batch-mode --no-crontab --no-install-services
8+
elif dpkg --compare-versions "$2" lt 1.27; then
9+
ubic-admin setup --batch-mode --no-crontab --no-install-services
10+
update-rc.d ubic-ping remove
11+
update-rc.d ubic-watchdog defaults
1312
fi
13+
ubic try-restart -f ubic
1414
else
1515
# first installation
16-
ubic-admin setup --batch-mode --no-crontab # no-crontab is because we already have packaged crontab in deb package
17-
update-rc.d ubic-ping defaults >/dev/null
16+
# no-crontab is because we already have packaged crontab in deb package
17+
ubic-admin setup --batch-mode --no-crontab --no-install-services
18+
update-rc.d ubic-watchdog defaults
19+
ubic start -f ubic
1820
fi
19-
ubic start ubic-ping
2021
fi
2122

2223
#DEBHELPER#

debian/postrm

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#!/bin/sh
22
set -e
33

4-
if [ "$1" = "purge" ] ; then
5-
update-rc.d ubic-ping remove >/dev/null || exit $?
4+
if [ \( "$1" = "purge" \) -o \( "$1" = "remove" \) ] ; then
5+
update-rc.d ubic.watchdog remove >/dev/null || exit $?
66
fi
77

88
#DEBHELPER#
9-

debian/prerm

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ set -e
33

44
#DEBHELPER#
55

6-
ubic stop ubic-ping
6+
if [ "$1" = "remove" ]; then
7+
ubic stop -f
8+
fi

etc/init.d/ubic-watchdog

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/perl
2+
3+
### BEGIN INIT INFO
4+
# Provides: ubic-watchdog
5+
# Required-Start: $all
6+
# Required-Stop:
7+
# Default-Start: 2 3 4 5
8+
# Default-Stop: 0 1 6
9+
# Short-Description: ubic watchdog service
10+
# Description: ubic.watchdog brings other ubic services to their expected state
11+
### END INIT INFO
12+
13+
use Ubic::Run;
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
# vim: ft=perl
2+
13
use Ubic::Ping::Service;
4+
25
Ubic::Ping::Service->new;

etc/ubic/service/ubic/update

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# vim: ft=perl
2+
3+
use Ubic::Service::SimpleDaemon;
4+
5+
Ubic::Service::SimpleDaemon->new(
6+
bin => 'ubic-periodic --period=60 --stdout=/var/log/ubic/update.log --stderr=/var/log/ubic/update.err.log ubic-update',
7+
);

etc/ubic/service/ubic/watchdog

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# vim: ft=perl
2+
3+
use Ubic::Service::SimpleDaemon;
4+
5+
Ubic::Service::SimpleDaemon->new(
6+
bin => 'ubic-periodic --period=60 --stdout=/var/log/ubic/watchdog.log --stderr=/var/log/ubic/watchdog.err.log ubic-watchdog',
7+
);

lib/Ubic/Admin/Setup.pm

+59-33
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ sub setup {
136136
my $opt_data_dir;
137137
my $opt_default_user = 'root';
138138
my $opt_sticky_777 = 1;
139-
my $opt_ubic_ping = 1;
139+
my $opt_install_services = 1;
140140
my $opt_crontab = 1;
141141

142142
GetOptions(
@@ -147,7 +147,7 @@ sub setup {
147147
'data-dir=s' => \$opt_data_dir,
148148
'default-user=s' => \$opt_default_user,
149149
'sticky-777!' => \$opt_sticky_777,
150-
'ubic-ping!' => \$opt_ubic_ping,
150+
'install-services!' => \$opt_install_services,
151151
'crontab!' => \$opt_crontab,
152152
) or die "Getopt failed";
153153

@@ -226,34 +226,27 @@ sub setup {
226226
$enable_1777 = prompt_bool("Enable 1777 grants for data dir?", $opt_sticky_777);
227227
}
228228

229-
my $enable_ubic_ping;
230-
if ($is_root) {
231-
print_tty "\n'ubic-ping' is a service provided by ubic out-of-the-box.\n";
232-
print_tty "It is a HTTP daemon which can report service statuses via simple REST API.\n";
233-
print_tty "Do you want to install 'ubic-ping' service into service tree?\n";
234-
print_tty "(You'll always be able to stop it or remove it.)\n";
235-
my $enable_ubic_ping = prompt_bool("Enable ubic-ping?", $opt_ubic_ping);
236-
237-
if ($enable_ubic_ping) {
238-
print_tty "NOTE: ubic-ping will be started on the port 12345; don't forget to protect it with the firewall.\n";
239-
}
229+
my $install_services;
230+
{
231+
print_tty "There are three standard services in ubic service tree:\n";
232+
print_tty " - ubic.watchdog (universal watchdog)\n";
233+
print_tty " - ubic.ping (http service status reporter)\n",
234+
print_tty " - ubic.update (helper process which updates service portmap, used by ubic.ping service)\n";
235+
print_tty "If you'll choose to install them, ubic.watchdog will be started automatically\n";
236+
print_tty "and two other services will be disabled.\n";
237+
$install_services = prompt_bool("Do you want to install standard services?", $opt_install_services);
238+
240239
}
241-
# TODO - do local users need ubic-ping? we need to allow them to configure its port, then
242240

243241
my $enable_crontab;
244242
{
245-
print_tty "\n'ubic-watchdog' is a script which checks all services and restarts them if\n";
243+
print_tty "\n'ubic.watchdog' is a service which checks all services and restarts them if\n";
246244
print_tty "there are any problems with their statuses.\n";
247-
print_tty "It is recommended to install it as a cron job.\n";
245+
print_tty "It is very small, but since it's so important that watchdog never goes down,\n",
246+
print_tty "it is recommended to install the cron job which checks watchdog itself.\n";
248247
print_tty "Even if you won't install it as a cron job, you'll always be able to run it\n";
249248
print_tty "manually.\n";
250-
$enable_crontab = prompt_bool("Install watchdog as a cron job?", $opt_crontab);
251-
252-
if ($enable_crontab) {
253-
print_tty "NOTE: ubic-watchdog logs will be redirected to /dev/null, since it's too hard to install\n";
254-
print_tty "logrotate configs on every platform.\n";
255-
print_tty "You can fix this yourself by manually editing crontab later.\n";
256-
}
249+
$enable_crontab = prompt_bool("Install watchdog's watchdog as a cron job?", $opt_crontab);
257250
}
258251

259252
my $config_file = ($is_root ? '/etc/ubic/ubic.cfg' : "$home/.ubic.cfg");
@@ -278,13 +271,40 @@ sub setup {
278271
xsystem('chmod', '1777', '--', "$data_dir/$subdir") if $enable_1777;
279272
}
280273

281-
if ($enable_ubic_ping) {
282-
print "Installing ubic-ping service...\n";
283-
284-
my $file = "$service_dir/ubic-ping";
285-
open my $fh, '>', $file or die "Can't write to '$file': $!";
286-
print {$fh} "use Ubic::Ping::Service;\nUbic::Ping::Service->new;\n" or die "Can't write to '$file': $!";
287-
close $fh or die "Can't close '$file': $!";
274+
xsystem('mkdir', '-p', '--', "$service_dir/ubic");
275+
276+
if ($install_services) {
277+
my $add_service = sub {
278+
my ($name, $content) = @_;
279+
print "Installing ubic.$name service...\n";
280+
281+
my $file = "$service_dir/ubic/$name";
282+
open my $fh, '>', $file or die "Can't write to '$file': $!";
283+
print {$fh} $content or die "Can't write to '$file': $!";
284+
close $fh or die "Can't close '$file': $!";
285+
};
286+
287+
$add_service->(
288+
'ping',
289+
"use Ubic::Ping::Service;\n"
290+
."Ubic::Ping::Service->new;\n"
291+
);
292+
293+
$add_service->(
294+
'watchdog',
295+
"use Ubic::Service::SimpleDaemon;\n"
296+
."Ubic::Service::SimpleDaemon->new(\n"
297+
."bin => 'ubic-periodic --period=60 --stdout=/var/log/ubic/watchdog.log --stderr=/var/log/ubic/watchdog.err.log ubic-watchdog',\n"
298+
.");\n"
299+
);
300+
301+
$add_service->(
302+
'update',
303+
"use Ubic::Service::SimpleDaemon;\n"
304+
."Ubic::Service::SimpleDaemon->new(\n"
305+
."bin => 'ubic-periodic --period=60 --stdout=/var/log/ubic/update.log --stderr=/var/log/ubic/update.err.log ubic-update',\n"
306+
.");\n"
307+
);
288308
}
289309

290310
if ($enable_crontab) {
@@ -302,8 +322,7 @@ sub setup {
302322
print {$fh} @_ or die "Can't write to pipe: $!";
303323
};
304324
$printc->($old_crontab."\n");
305-
$printc->("* * * * * ubic-watchdog >>/dev/null 2>>/dev/null\n");
306-
$printc->("* * * * * ubic-update >>/dev/null 2>>/dev/null\n");
325+
$printc->("* * * * * ubic-watchdog ubic.watchdog >>/dev/null 2>>/dev/null\n");
307326
close $fh or die "Can't close pipe: $!";
308327
}
309328
}
@@ -315,7 +334,14 @@ sub setup {
315334
default_user => $default_user,
316335
});
317336

318-
xsystem('ubic', 'start', 'ubic-ping') if $enable_ubic_ping;
337+
print "Starting ubic.watchdog...\n";
338+
xsystem('ubic start ubic.watchdog');
339+
340+
print "Installation complete.\n";
341+
print_tty "NOTE: There are three default services in ubic service tree:\n";
342+
print_tty " - ubic.watchdog (universal watchdog)\n";
343+
print_tty " - ubic.ping (http service status reporter)\n",
344+
print_tty " - ubic.update (helper process which updates service portmap, used by ubic.ping service)\n";
319345
}
320346

321347
=back

lib/Ubic/Manual/FAQ.pod

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ It makes possible to check service status via ubic-ping using its port instead o
2828
Resolving of service by port can't be done on-the-fly by ubic-ping, because all service definitions are cached internally by Ubic.pm (L<Ubic::Multiservice>, actually, but you don't have to know the difference), since constant reloading of service definition can cause memory leaks.
2929
So ubic-ping instead just loads portmap generated by ubic-update, resolves service name by port, and loads cached service status from another local file.
3030

31-
In other words, if you don't use ubic-ping, you can both disable ubic-update and stop ubic-ping service.
31+
In other words, if you don't use ubic-ping, you can disable both ubic.update and stop ubic.ping service.
3232

3333
=head1 How is ubic compatible with SysV-style /etc/rcX.d/ symlinks?
3434

lib/Ubic/Multiservice.pm

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ This may be fixed in future: either C<Ubic::Multiservice> will no longer inherit
201201
202202
C<user>, C<group> and other metadata methods are not used for multiservices too.
203203
204-
Subservices are cached forever; this can cause troubles, but it is necessary to avoid memory leaks in C<ubic-ping>.
204+
Subservices are cached forever; this can cause troubles, but it is necessary to avoid memory leaks in C<Ubic::Ping>.
205205
206206
=head1 SEE ALSO
207207

lib/Ubic/Ping/Service.pm

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package Ubic::Ping::Service;
22

3-
# ABSTRACT: ubic-ping service
3+
# ABSTRACT: ubic.ping service
44

55
use strict;
66
use warnings;
@@ -42,7 +42,7 @@ sub new {
4242
my $pid;
4343
start_daemon({
4444
bin => qq{$perl -MUbic::Ping -e 'Ubic::Ping->new($port)->run;'},
45-
name => 'ubic-ping',
45+
name => 'ubic.ping',
4646
pidfile => $pidfile,
4747
stdout => $log,
4848
stderr => $log,

lib/Ubic/PortMap.pm

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use warnings;
1010
use Ubic::PortMap;
1111
1212
Ubic::PortMap::update();
13-
print Ubic::PortMap::port2name(12345); # ubic-ping
13+
print Ubic::PortMap::port2name(12345); # ubic.ping
1414
1515
=head1 METHODS
1616

lib/Ubic/Settings.pm

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ sub _load {
9292
};
9393

9494
unless (keys %$settings) {
95-
die "ubic is not configured, run 'ubic setup' first\n";
95+
die "ubic is not configured, run 'ubic-admin setup' first\n";
9696
}
9797
for (qw/ service_dir data_dir default_user /) {
9898
unless (defined $settings->{$_}) {

0 commit comments

Comments
 (0)