rename *-daemon apps to be *-backend

Squashed commit of the following:

commit 39b438aa4b
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Sat May 6 16:40:11 2017 +0100

    add release notes

commit ca4ea90d35
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Sat May 6 16:32:06 2017 +0100

    update distmeta

commit 4e35b904b0
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Sat May 6 16:30:22 2017 +0100

    rename files from Daemon to Backend

commit 86a605ba68
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Sat May 6 16:26:43 2017 +0100

    rename daemon to backend in code

commit ffe8fc180f
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Sat May 6 16:15:57 2017 +0100

    add daemon files which exec to backend equivalents

commit 53e041594e
Author: Oliver Gorwits <oliver@cpan.org>
Date:   Sat May 6 15:32:49 2017 +0100

    rename netdisco-daemon to netdisco-backend
This commit is contained in:
Oliver Gorwits
2017-05-06 16:40:48 +01:00
parent db9d98b69b
commit d74ccac4f6
33 changed files with 537 additions and 461 deletions

186
bin/netdisco-backend Executable file
View File

@@ -0,0 +1,186 @@
#!/usr/bin/env perl
use strict;
use warnings;
our $home;
BEGIN {
use FindBin;
FindBin::again();
my $me = File::Spec->catfile($FindBin::RealBin, $FindBin::RealScript);
my $uid = (stat($me))[4] || 0;
$home = ($ENV{NETDISCO_HOME} || (getpwuid($uid))[7] || $ENV{HOME});
# try to find a localenv if one isn't already in place.
if (!exists $ENV{PERL_LOCAL_LIB_ROOT}) {
use File::Spec;
my $localenv = File::Spec->catfile($FindBin::Bin, 'localenv');
exec($localenv, $0, @ARGV) if -f $localenv;
$localenv = File::Spec->catfile($home, 'perl5', 'bin', 'localenv');
exec($localenv, $0, @ARGV) if -f $localenv;
die "Sorry, can't find libs required for App::Netdisco.\n"
if !exists $ENV{PERLBREW_PERL};
}
}
BEGIN {
use Path::Class;
# stuff useful locations into @INC and $PATH
unshift @INC,
dir($FindBin::RealBin)->parent->subdir('lib')->stringify,
dir($FindBin::RealBin, 'lib')->stringify;
use Config;
$ENV{PATH} = $FindBin::RealBin . $Config{path_sep} . $ENV{PATH};
}
use Daemon::Control;
use Filesys::Notify::Simple;
use File::Copy;
use App::Netdisco::Environment;
my $config = ($ENV{PLACK_ENV} || $ENV{DANCER_ENVIRONMENT}) .'.yml';
my $netdisco = file($FindBin::RealBin, 'netdisco-backend-fg');
my @args = (scalar @ARGV > 1 ? @ARGV[1 .. $#ARGV] : ());
my $log_dir = dir($home, 'logs');
mkdir $log_dir if ! -d $log_dir;
my $log_file = file($log_dir, 'netdisco-backend.log');
my $uid = (stat($netdisco->stringify))[4] || 0;
my $gid = (stat($netdisco->stringify))[5] || 0;
Daemon::Control->new({
name => 'Netdisco Backend',
program => \&restarter,
program_args => [@args],
pid_file => file($home, 'netdisco-backend.pid'),
stderr_file => $log_file,
stdout_file => $log_file,
redirect_before_fork => 0,
uid => $uid, gid => $gid,
})->run;
# the guts of this are borrowed from Plack::Loader::Restarter - many thanks!!
my $child = 0;
sub restarter {
my ($daemon, @program_args) = @_;
$0 = 'netdisco-backend';
$child = fork_and_start($daemon, @program_args);
exit(1) unless $child;
my $watcher = Filesys::Notify::Simple->new([$ENV{DANCER_ENVDIR}, $log_dir]);
warn "config watcher: watching $ENV{DANCER_ENVDIR} for updates.\n";
local $SIG{TERM} = sub { $child = signal_child('TERM', $child); exit(0); };
while (1) {
my @restart;
# this is blocking
$watcher->wait(sub {
my @events = @_;
@events = grep {$_->{path} eq $log_file or
file($_->{path})->basename eq $config} @events;
return unless @events;
@restart = @events;
});
my ($hupit, $rotate) = (0, 0);
next unless @restart;
foreach my $f (@restart) {
if ($f->{path} eq $log_file) {
++$rotate;
}
else {
warn "-- $f->{path} updated.\n";
++$hupit;
}
}
rotate_logs($child, $daemon, @program_args) if $rotate;
if ($hupit) {
signal_child('TERM', $child);
$child = fork_and_start($daemon, @program_args);
exit(1) unless $child;
}
}
}
sub fork_and_start {
my ($daemon, @daemon_args) = @_;
my $pid = fork;
die "Can't fork: $!" unless defined $pid;
if ($pid == 0) { # child
$daemon->redirect_filehandles;
exec( $netdisco->stringify, @daemon_args );
}
else {
return $pid;
}
}
sub signal_child {
my ($signal, $pid) = @_;
return unless $signal and $pid;
warn "config watcher: sending $signal to the server (pid:$pid)...\n";
kill $signal => $pid;
waitpid($pid, 0);
}
sub rotate_logs {
my $child = shift;
return unless (-f $log_file) and
((-s $log_file) > (10 * 1024768));
my @files = glob file($log_dir, '*');
foreach my $f (reverse sort @files) {
next unless $f =~ m/$log_file\.(\d)$/;
my $pos = $1;
unlink $f if $pos == 7;
my $next = $pos + 1;
(my $newf = $f) =~ s/\.$pos$/.$next/;
rename $f, $newf;
}
# if the log file's about 10M then the race condition in copy/truncate
# has a low risk of data loss. if the file's larger, then we rename and
# kill.
if ((-s $log_file) > (12 * 1024768)) {
rename $log_file, $log_file .'.1';
signal_child('TERM', $child);
$child = fork_and_start(@_);
exit(1) unless $child;
}
else {
copy $log_file, $log_file .'.1';
truncate $log_file, 0;
}
}
=head1 NAME
netdisco-backend - Job Control Daemon for Netdisco
=head1 SEE ALSO
=over 4
=item *
L<App::Netdisco>
=back
=cut

109
bin/netdisco-backend-fg Executable file
View File

@@ -0,0 +1,109 @@
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
FindBin::again();
use Path::Class 'dir';
# get a segfault if we load this later
use if $^O eq 'linux', 'Sys::Proctitle';
BEGIN {
# stuff useful locations into @INC
unshift @INC,
dir($FindBin::RealBin)->parent->subdir('lib')->stringify,
dir($FindBin::RealBin, 'lib')->stringify;
unshift @INC,
split m/:/, ($ENV{NETDISCO_INC} || '');
}
use App::Netdisco;
use Dancer qw/:moose :script/;
warning sprintf "App::Netdisco %s backend", ($App::Netdisco::VERSION || 'HEAD');
use App::Netdisco::Util::Backend;
use NetAddr::IP::Lite ':lower'; # to quench AF_INET6 symbol errors
use Role::Tiny::With;
# preload all worker modules into shared memory
use App::Netdisco::Backend::Job ();
use App::Netdisco::Backend::Util ();
use App::Netdisco::Backend::Worker::Common ();
use App::Netdisco::Backend::Worker::Interactive::DeviceActions ();
use App::Netdisco::Backend::Worker::Interactive::PortActions ();
use App::Netdisco::Backend::Worker::Manager ();
use App::Netdisco::Backend::Worker::Poller::Arpnip ();
use App::Netdisco::Backend::Worker::Poller::Common ();
use App::Netdisco::Backend::Worker::Poller::Device ();
use App::Netdisco::Backend::Worker::Poller::Expiry ();
use App::Netdisco::Backend::Worker::Poller::Macsuck ();
use App::Netdisco::Backend::Worker::Poller::Nbtstat ();
use App::Netdisco::Backend::Worker::Poller ();
use App::Netdisco::Backend::Worker::Scheduler ();
use MCE::Signal '-setpgrp';
use MCE::Flow Sereal => 1;
use MCE::Queue;
# set temporary MCE files' location in home directory
my $home = ($ENV{NETDISCO_HOME} || $ENV{HOME});
my $tmp_dir = ($ENV{NETDISCO_TEMP} || dir($home, 'tmp'));
mkdir $tmp_dir if ! -d $tmp_dir;
# process-table text
prctl 'netdisco-backend: master';
# shared local job queue
my $queue = MCE::Queue->new;
# support a scheduler-only node
setting('workers')->{'no_manager'} = 1
if setting('workers')->{tasks} eq '0';
# MCE::Util has a limit of ncpu if AUTO is used in max_workers,
# so we parse the field ourselves.
my $max_workers = parse_max_workers( setting('workers')->{tasks} ) || 0;
mce_flow {
task_name => [qw/ scheduler manager poller /],
max_workers => [ 1, 1, $max_workers ],
tmp_dir => $tmp_dir,
on_post_exit => sub { MCE->restart_worker },
}, _mk_wkr('Scheduler'), _mk_wkr('Manager'), _mk_wkr('Poller');
sub _mk_wkr {
my $role = shift;
return sub {
my $self = shift;
$self->{queue} = $queue;
prctl sprintf 'netdisco-backend: worker #%s %s: init', MCE->wid, lc($role);
info sprintf 'applying role %s to worker %s', $role, MCE->wid;
# post-fork, become manager, scheduler, poller, etc
Role::Tiny->apply_roles_to_object(
$self => "App::Netdisco::Backend::Worker::$role");
$self->worker_begin if $self->can('worker_begin');
$self->worker_body;
};
}
=head1 NAME
netdisco-backend-fg - Job Control for Netdisco
=head1 SEE ALSO
=over 4
=item *
L<App::Netdisco>
=back
=cut

View File

@@ -3,184 +3,17 @@
use strict;
use warnings;
our $home;
use File::Spec;
use Cwd 'realpath';
BEGIN {
use FindBin;
FindBin::again();
use FindBin;
FindBin::again();
my $me = File::Spec->catfile($FindBin::RealBin, $FindBin::RealScript);
my $uid = (stat($me))[4] || 0;
my $backend =
File::Spec->catfile(
(File::Spec->splitpath(
realpath(File::Spec->catfile( $FindBin::RealBin, $FindBin::RealScript ))
))[1],
'netdisco-backend');
$home = ($ENV{NETDISCO_HOME} || (getpwuid($uid))[7] || $ENV{HOME});
# try to find a localenv if one isn't already in place.
if (!exists $ENV{PERL_LOCAL_LIB_ROOT}) {
use File::Spec;
my $localenv = File::Spec->catfile($FindBin::Bin, 'localenv');
exec($localenv, $0, @ARGV) if -f $localenv;
$localenv = File::Spec->catfile($home, 'perl5', 'bin', 'localenv');
exec($localenv, $0, @ARGV) if -f $localenv;
die "Sorry, can't find libs required for App::Netdisco.\n"
if !exists $ENV{PERLBREW_PERL};
}
}
BEGIN {
use Path::Class;
# stuff useful locations into @INC and $PATH
unshift @INC,
dir($FindBin::RealBin)->parent->subdir('lib')->stringify,
dir($FindBin::RealBin, 'lib')->stringify;
use Config;
$ENV{PATH} = $FindBin::RealBin . $Config{path_sep} . $ENV{PATH};
}
use Daemon::Control;
use Filesys::Notify::Simple;
use File::Copy;
use App::Netdisco::Environment;
my $config = ($ENV{PLACK_ENV} || $ENV{DANCER_ENVIRONMENT}) .'.yml';
my $netdisco = file($FindBin::RealBin, 'netdisco-daemon-fg');
my @args = (scalar @ARGV > 1 ? @ARGV[1 .. $#ARGV] : ());
my $log_dir = dir($home, 'logs');
mkdir $log_dir if ! -d $log_dir;
my $log_file = file($log_dir, 'netdisco-daemon.log');
my $uid = (stat($netdisco->stringify))[4] || 0;
my $gid = (stat($netdisco->stringify))[5] || 0;
Daemon::Control->new({
name => 'Netdisco Daemon',
program => \&restarter,
program_args => [@args],
pid_file => file($home, 'netdisco-daemon.pid'),
stderr_file => $log_file,
stdout_file => $log_file,
redirect_before_fork => 0,
uid => $uid, gid => $gid,
})->run;
# the guts of this are borrowed from Plack::Loader::Restarter - many thanks!!
my $child = 0;
sub restarter {
my ($daemon, @program_args) = @_;
$0 = 'netdisco-daemon';
$child = fork_and_start($daemon, @program_args);
exit(1) unless $child;
my $watcher = Filesys::Notify::Simple->new([$ENV{DANCER_ENVDIR}, $log_dir]);
warn "config watcher: watching $ENV{DANCER_ENVDIR} for updates.\n";
local $SIG{TERM} = sub { $child = signal_child('TERM', $child); exit(0); };
while (1) {
my @restart;
# this is blocking
$watcher->wait(sub {
my @events = @_;
@events = grep {$_->{path} eq $log_file or
file($_->{path})->basename eq $config} @events;
return unless @events;
@restart = @events;
});
my ($hupit, $rotate) = (0, 0);
next unless @restart;
foreach my $f (@restart) {
if ($f->{path} eq $log_file) {
++$rotate;
}
else {
warn "-- $f->{path} updated.\n";
++$hupit;
}
}
rotate_logs($child, $daemon, @program_args) if $rotate;
if ($hupit) {
signal_child('TERM', $child);
$child = fork_and_start($daemon, @program_args);
exit(1) unless $child;
}
}
}
sub fork_and_start {
my ($daemon, @daemon_args) = @_;
my $pid = fork;
die "Can't fork: $!" unless defined $pid;
if ($pid == 0) { # child
$daemon->redirect_filehandles;
exec( $netdisco->stringify, @daemon_args );
}
else {
return $pid;
}
}
sub signal_child {
my ($signal, $pid) = @_;
return unless $signal and $pid;
warn "config watcher: sending $signal to the server (pid:$pid)...\n";
kill $signal => $pid;
waitpid($pid, 0);
}
sub rotate_logs {
my $child = shift;
return unless (-f $log_file) and
((-s $log_file) > (10 * 1024768));
my @files = glob file($log_dir, '*');
foreach my $f (reverse sort @files) {
next unless $f =~ m/$log_file\.(\d)$/;
my $pos = $1;
unlink $f if $pos == 7;
my $next = $pos + 1;
(my $newf = $f) =~ s/\.$pos$/.$next/;
rename $f, $newf;
}
# if the log file's about 10M then the race condition in copy/truncate
# has a low risk of data loss. if the file's larger, then we rename and
# kill.
if ((-s $log_file) > (12 * 1024768)) {
rename $log_file, $log_file .'.1';
signal_child('TERM', $child);
$child = fork_and_start(@_);
exit(1) unless $child;
}
else {
copy $log_file, $log_file .'.1';
truncate $log_file, 0;
}
}
=head1 NAME
netdisco-daemon - Job Control Daemon for Netdisco
=head1 SEE ALSO
=over 4
=item *
L<App::Netdisco>
=back
=cut
exec {$backend} 'netdisco-backend', @ARGV;

View File

@@ -3,107 +3,17 @@
use strict;
use warnings;
use File::Spec;
use Cwd 'realpath';
use FindBin;
FindBin::again();
use Path::Class 'dir';
# get a segfault if we load this later
use if $^O eq 'linux', 'Sys::Proctitle';
my $backend =
File::Spec->catfile(
(File::Spec->splitpath(
realpath(File::Spec->catfile( $FindBin::RealBin, $FindBin::RealScript ))
))[1],
'netdisco-backend-fg');
BEGIN {
# stuff useful locations into @INC
unshift @INC,
dir($FindBin::RealBin)->parent->subdir('lib')->stringify,
dir($FindBin::RealBin, 'lib')->stringify;
unshift @INC,
split m/:/, ($ENV{NETDISCO_INC} || '');
}
use App::Netdisco;
use Dancer qw/:moose :script/;
warning sprintf "App::Netdisco %s backend", ($App::Netdisco::VERSION || 'HEAD');
use App::Netdisco::Util::Daemon;
use NetAddr::IP::Lite ':lower'; # to quench AF_INET6 symbol errors
use Role::Tiny::With;
# preload all worker modules into shared memory
use App::Netdisco::Daemon::Job ();
use App::Netdisco::Daemon::Util ();
use App::Netdisco::Daemon::Worker::Common ();
use App::Netdisco::Daemon::Worker::Interactive::DeviceActions ();
use App::Netdisco::Daemon::Worker::Interactive::PortActions ();
use App::Netdisco::Daemon::Worker::Manager ();
use App::Netdisco::Daemon::Worker::Poller::Arpnip ();
use App::Netdisco::Daemon::Worker::Poller::Common ();
use App::Netdisco::Daemon::Worker::Poller::Device ();
use App::Netdisco::Daemon::Worker::Poller::Expiry ();
use App::Netdisco::Daemon::Worker::Poller::Macsuck ();
use App::Netdisco::Daemon::Worker::Poller::Nbtstat ();
use App::Netdisco::Daemon::Worker::Poller ();
use App::Netdisco::Daemon::Worker::Scheduler ();
use MCE::Signal '-setpgrp';
use MCE::Flow Sereal => 1;
use MCE::Queue;
# set temporary MCE files' location in home directory
my $home = ($ENV{NETDISCO_HOME} || $ENV{HOME});
my $tmp_dir = ($ENV{NETDISCO_TEMP} || dir($home, 'tmp'));
mkdir $tmp_dir if ! -d $tmp_dir;
# process-table text
prctl 'netdisco-daemon: master';
# shared local job queue
my $queue = MCE::Queue->new;
# support a scheduler-only node
setting('workers')->{'no_manager'} = 1
if setting('workers')->{tasks} eq '0';
# MCE::Util has a limit of ncpu if AUTO is used in max_workers,
# so we parse the field ourselves.
my $max_workers = parse_max_workers( setting('workers')->{tasks} ) || 0;
mce_flow {
task_name => [qw/ scheduler manager poller /],
max_workers => [ 1, 1, $max_workers ],
tmp_dir => $tmp_dir,
on_post_exit => sub { MCE->restart_worker },
}, _mk_wkr('Scheduler'), _mk_wkr('Manager'), _mk_wkr('Poller');
sub _mk_wkr {
my $role = shift;
return sub {
my $self = shift;
$self->{queue} = $queue;
prctl sprintf 'netdisco-daemon: worker #%s %s: init', MCE->wid, lc($role);
info sprintf 'applying role %s to worker %s', $role, MCE->wid;
# post-fork, become manager, scheduler, poller, etc
Role::Tiny->apply_roles_to_object(
$self => "App::Netdisco::Daemon::Worker::$role");
$self->worker_begin if $self->can('worker_begin');
$self->worker_body;
};
}
=head1 NAME
netdisco-daemon-fg - Job Control for Netdisco
=head1 SEE ALSO
=over 4
=item *
L<App::Netdisco>
=back
=cut
exec {$backend} 'netdisco-backend-fg', @ARGV;

View File

@@ -35,7 +35,7 @@ BEGIN {
# for netdisco app config
use App::Netdisco;
use App::Netdisco::Daemon::Job;
use App::Netdisco::Backend::Job;
use Dancer qw/:moose :script/;
info "App::Netdisco version $App::Netdisco::VERSION loaded.";
@@ -104,13 +104,13 @@ unless ($action) {
use App::Netdisco::Util::Device
qw/get_device delete_device renumber_device/;
with 'App::Netdisco::Daemon::Worker::Poller::Device';
with 'App::Netdisco::Daemon::Worker::Poller::Arpnip';
with 'App::Netdisco::Daemon::Worker::Poller::Macsuck';
with 'App::Netdisco::Daemon::Worker::Poller::Nbtstat';
with 'App::Netdisco::Daemon::Worker::Poller::Expiry';
with 'App::Netdisco::Daemon::Worker::Interactive::DeviceActions';
with 'App::Netdisco::Daemon::Worker::Interactive::PortActions';
with 'App::Netdisco::Backend::Worker::Poller::Device';
with 'App::Netdisco::Backend::Worker::Poller::Arpnip';
with 'App::Netdisco::Backend::Worker::Poller::Macsuck';
with 'App::Netdisco::Backend::Worker::Poller::Nbtstat';
with 'App::Netdisco::Backend::Worker::Poller::Expiry';
with 'App::Netdisco::Backend::Worker::Interactive::DeviceActions';
with 'App::Netdisco::Backend::Worker::Interactive::PortActions';
eval { Module::Load::load 'App::Netdisco::Util::Graph' };
sub graph {
@@ -233,7 +233,7 @@ foreach my $host (@hostlist) {
}
# what job are we asked to do?
my $job = App::Netdisco::Daemon::Job->new({
my $job = App::Netdisco::Backend::Job->new({
job => 0,
action => $action,
device => $dev,