App-Netdisco

 view release on metacpan or  search on metacpan

bin/netdisco-backend  view on Meta::CPAN

      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 Getopt::Long;
Getopt::Long::Configure ("pass_through");

my ($logfiles, $logsize) = (8,10);
my $result = GetOptions(
  'logfiles=i' => \$logfiles,
  'logsize=i'  => \$logsize,
);

use App::Netdisco::Environment;
my $config = ($ENV{PLACK_ENV} || $ENV{DANCER_ENVIRONMENT}) .'.yml';

# make sure there is a config file in place
my $template_config = file($ENV{DANCER_CONFDIR}, 'environments', $config);
my $app_config = file($ENV{DANCER_ENVDIR}, $config);
if (! -e $app_config and -e $template_config) {
  copy $template_config, $app_config;
}
if (! -e $app_config) {
  die "error: cannot find Netdisco config at $template_config or $app_config\n";
}

my $netdisco = file($FindBin::RealBin, 'netdisco-backend-fg');
my @args = (scalar @ARGV > 1 ? @ARGV[1 .. $#ARGV] : ());

my $foreground = (@ARGV and $ARGV[0] eq 'foreground');

my $log_dir = dir($home, 'logs');
my $log_file;
if (!$foreground) {
  mkdir $log_dir if ! -d $log_dir;
  $log_file = file($log_dir, 'netdisco-backend.log');
}

my $uid = (stat($netdisco->stringify))[4] || 0;
my $gid = (stat($netdisco->stringify))[5] || 0;

# only drop privileges if we are root; otherwise we cannot setuid anyway
# and Daemon::Control would emit a misleading "Operation not permitted"
# warning under e.g. OpenShift random UIDs.
my $can_drop_priv = ($> == 0);

my $old_pid = file($home, 'netdisco-daemon.pid');
my $new_pid = file($home, 'netdisco-backend.pid');
if (-f $old_pid) { File::Copy::move( $old_pid, $new_pid ) }

Daemon::Control->new({
  name => 'Netdisco Backend',
  program  => \&restarter,
  program_args => [@args],
  pid_file => $new_pid,
  ($foreground ? () : (
    stderr_file => $log_file,
    stdout_file => $log_file,
  )),
  redirect_before_fork => 0,
  ($can_drop_priv ? (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},
    ($foreground ? () : $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 {($log_file and $_->{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 ($log_file and $f->{path} eq $log_file) {
              ++$rotate;
          }
          else {
              warn "-- $f->{path} updated.\n";
              ++$hupit;
          }
      }
      $child = 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 );



( run in 0.613 second using v1.01-cache-2.11-cpan-5735350b133 )