From d82fe1e2b0c259299d81dca5fdeef343c20643af Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Mon, 4 Mar 2013 00:19:18 +0000 Subject: [PATCH 1/5] refactor web start scripts to be more portable --- Netdisco/Makefile.PL | 6 ++++-- Netdisco/bin/netdisco-web | 21 ++++++++++++--------- Netdisco/bin/netdisco-web-fg | 25 ++++++++++++++++++++++--- Netdisco/lib/App/Netdisco.pm | 2 +- Netdisco/lib/App/Netdisco/Web.pm | 5 ----- 5 files changed, 39 insertions(+), 20 deletions(-) diff --git a/Netdisco/Makefile.PL b/Netdisco/Makefile.PL index b87519ed..5c8d137c 100644 --- a/Netdisco/Makefile.PL +++ b/Netdisco/Makefile.PL @@ -4,6 +4,8 @@ name 'App-Netdisco'; license 'bsd'; all_from 'lib/App/Netdisco.pm'; +requires 'App::cpanminus' => 0; +requires 'App::local::lib::helper' => 0; requires 'DBD::Pg' => 0; requires 'DBD::SQLite' => 0; requires 'DBIx::Class' => 0; @@ -20,11 +22,11 @@ requires 'MCE' => 1.305; requires 'Net::MAC' => 0; requires 'NetAddr::IP' => 0; requires 'Path::Class' => 0; -requires 'Plack' => 0; requires 'Role::Tiny' => 0; +requires 'Socket6' => 0; +requires 'Starman' => 0; requires 'SNMP::Info' => '2.11'; requires 'SQL::Translator' => 0; -requires 'Socket6' => 0; requires 'Template' => 0; requires 'YAML' => 0; requires 'namespace::clean' => 0; diff --git a/Netdisco/bin/netdisco-web b/Netdisco/bin/netdisco-web index b8fed430..c181c097 100755 --- a/Netdisco/bin/netdisco-web +++ b/Netdisco/bin/netdisco-web @@ -2,23 +2,26 @@ use strict; use warnings FATAL => 'all'; + +use FindBin; +FindBin::again(); + use Path::Class; +use Daemon::Control; -BEGIN { - eval "use Daemon::Control"; - if ($@) { - exec(file($ENV{HOME}, 'bin', 'localenv'), $0, @ARGV); - } -} +my $home = ($ENV{NETDISCO_HOME} || $ENV{HOME}); +my $netdisco = file($FindBin::RealBin, 'netdisco-web-fg'); -my $log_dir = dir($ENV{HOME}, 'logs'); +my $log_dir = dir($home, 'logs'); mkdir $log_dir if ! -d $log_dir; +my @args = (scalar @ARGV > 1 ? @ARGV[1 .. $#ARGV] : ()); + Daemon::Control->new({ name => 'Netdisco Web', program => 'plackup', - program_args => [file($ENV{HOME}, 'bin', 'netdisco-web-fg')], - pid_file => file($ENV{HOME}, 'netdisco-web.pid'), + program_args => [@args, $netdisco->stringify], + pid_file => file($home, 'netdisco-web.pid'), stderr_file => file($log_dir, 'netdisco-web.log'), stdout_file => file($log_dir, 'netdisco-web.log'), })->run; diff --git a/Netdisco/bin/netdisco-web-fg b/Netdisco/bin/netdisco-web-fg index b001df42..076e662d 100755 --- a/Netdisco/bin/netdisco-web-fg +++ b/Netdisco/bin/netdisco-web-fg @@ -1,9 +1,28 @@ #!/usr/bin/env perl -use FindBin; -use lib "$FindBin::Bin/../lib"; -use App::Netdisco; +use strict; +use warnings FATAL => 'all'; +BEGIN { + # stuff useful locations into @INC and $PATH + use Config; + use FindBin; + FindBin::again(); + use Path::Class 'dir'; + + my $location = $FindBin::RealBin; + $ENV{PATH} = $location . $Config{path_sep} . $ENV{PATH}; + unshift @INC, + dir($location)->parent->subdir('lib')->stringify, + dir($location, 'lib')->stringify; +} + +use App::Netdisco; use Dancer; +debug sprintf "App::Netdisco %s", ($App::Netdisco::VERSION || 'HEAD'); + +my $home = ($ENV{NETDISCO_HOME} || $ENV{HOME}); +set('session_dir', dir($home, 'netdisco-web-sessions')); + use App::Netdisco::Web; dance; diff --git a/Netdisco/lib/App/Netdisco.pm b/Netdisco/lib/App/Netdisco.pm index c7fac57c..262fda60 100644 --- a/Netdisco/lib/App/Netdisco.pm +++ b/Netdisco/lib/App/Netdisco.pm @@ -7,7 +7,7 @@ use 5.010_000; use File::ShareDir 'dist_dir'; use Path::Class; -our $VERSION = '2.005000_002'; +our $VERSION = '2.005000_003'; BEGIN { if (not length ($ENV{DANCER_APPDIR} || '') diff --git a/Netdisco/lib/App/Netdisco/Web.pm b/Netdisco/lib/App/Netdisco/Web.pm index 06a89d63..347ed3dc 100644 --- a/Netdisco/lib/App/Netdisco/Web.pm +++ b/Netdisco/lib/App/Netdisco/Web.pm @@ -15,11 +15,6 @@ use App::Netdisco::Web::Device; use App::Netdisco::Web::TypeAhead; use App::Netdisco::Web::PortControl; -# need to set at runtime the sessions dir in user home -# because YAML config (session_dir) cannot interpolate $ENV{HOME} -use Path::Class 'dir'; -set('session_dir', dir($ENV{HOME}, 'netdisco-web-sessions')); - sub _load_web_plugins { my $plugin_list = shift; From a02a325dcc102067bd8ea7686aef139fd7fb3908 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Mon, 4 Mar 2013 19:29:22 +0000 Subject: [PATCH 2/5] remove unecessary PATH extension --- Netdisco/bin/netdisco-web-fg | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Netdisco/bin/netdisco-web-fg b/Netdisco/bin/netdisco-web-fg index 076e662d..8e71c7de 100755 --- a/Netdisco/bin/netdisco-web-fg +++ b/Netdisco/bin/netdisco-web-fg @@ -4,14 +4,9 @@ use strict; use warnings FATAL => 'all'; BEGIN { - # stuff useful locations into @INC and $PATH - use Config; - use FindBin; - FindBin::again(); + # stuff useful locations into @INC use Path::Class 'dir'; - my $location = $FindBin::RealBin; - $ENV{PATH} = $location . $Config{path_sep} . $ENV{PATH}; unshift @INC, dir($location)->parent->subdir('lib')->stringify, dir($location, 'lib')->stringify; From c4e6e4c8ebe2719d0a59aac212368bb99121cd51 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Mon, 4 Mar 2013 20:31:00 +0000 Subject: [PATCH 3/5] try really hard to find a localenv if one isn't already in place --- Netdisco/bin/netdisco-daemon | 29 ++++++++++++++++++++++------- Netdisco/bin/netdisco-web | 24 +++++++++++++++++++----- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/Netdisco/bin/netdisco-daemon b/Netdisco/bin/netdisco-daemon index e85bd003..e42bc152 100755 --- a/Netdisco/bin/netdisco-daemon +++ b/Netdisco/bin/netdisco-daemon @@ -2,25 +2,40 @@ use strict; use warnings FATAL => 'all'; -use Path::Class 'file'; +use FindBin; +FindBin::again(); + +our $home; + +# try really hard to find a localenv if one isn't already in place. BEGIN { + $home = ($ENV{NETDISCO_HOME} || $ENV{HOME}); eval "use Daemon::Control"; + if ($@) { - exec(file($ENV{HOME}, 'bin', 'localenv'), $0, @ARGV); + use File::Spec; + my $localenv = File::Spec->catfile($FindBin::RealBin, '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"; } } use Path::Class; -my $log_dir = dir($ENV{HOME}, 'logs'); -mkdir $log_dir if ! -d $log_dir; -my $program = file($ENV{HOME}, 'bin', 'netdisco-daemon-fg'); +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; Daemon::Control->new({ name => 'Netdisco Daemon', - program => ($ENV{NETDISCO_DAEMON} || $program), - pid_file => file($ENV{HOME}, 'netdisco-daemon.pid'), + program => $netdisco, + program_args => [@args], + pid_file => file($home, 'netdisco-daemon.pid'), stderr_file => file($log_dir, 'netdisco-daemon.log'), stdout_file => file($log_dir, 'netdisco-daemon.log'), })->run; diff --git a/Netdisco/bin/netdisco-web b/Netdisco/bin/netdisco-web index c181c097..7b698fed 100755 --- a/Netdisco/bin/netdisco-web +++ b/Netdisco/bin/netdisco-web @@ -6,17 +6,31 @@ use warnings FATAL => 'all'; use FindBin; FindBin::again(); -use Path::Class; -use Daemon::Control; +our $home; + +# try really hard to find a localenv if one isn't already in place. +BEGIN { + $home = ($ENV{NETDISCO_HOME} || $ENV{HOME}); + eval "use Daemon::Control"; + + if ($@) { + use File::Spec; + my $localenv = File::Spec->catfile($FindBin::RealBin, '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"; + } +} + +use Path::Class; -my $home = ($ENV{NETDISCO_HOME} || $ENV{HOME}); my $netdisco = file($FindBin::RealBin, 'netdisco-web-fg'); +my @args = (scalar @ARGV > 1 ? @ARGV[1 .. $#ARGV] : ()); my $log_dir = dir($home, 'logs'); mkdir $log_dir if ! -d $log_dir; -my @args = (scalar @ARGV > 1 ? @ARGV[1 .. $#ARGV] : ()); - Daemon::Control->new({ name => 'Netdisco Web', program => 'plackup', From 376b1cf9ddbcd98e764619989ea4dac551b0a67d Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Mon, 4 Mar 2013 20:31:32 +0000 Subject: [PATCH 4/5] support for running from git or build dir --- Netdisco/bin/netdisco-daemon-fg | 23 +++++++++++++++-------- Netdisco/bin/netdisco-web-fg | 10 ++++++---- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/Netdisco/bin/netdisco-daemon-fg b/Netdisco/bin/netdisco-daemon-fg index 1cda21b4..ecd0fdad 100755 --- a/Netdisco/bin/netdisco-daemon-fg +++ b/Netdisco/bin/netdisco-daemon-fg @@ -1,11 +1,20 @@ #!/usr/bin/env perl use FindBin; -use lib "$FindBin::Bin/../lib"; -use App::Netdisco; +FindBin::again(); +use Path::Class 'dir'; + +BEGIN { + # stuff useful locations into @INC + unshift @INC, + dir($FindBin::RealBin)->parent->subdir('lib')->stringify, + dir($FindBin::RealBin, 'lib')->stringify; +} # for netdisco app config +use App::Netdisco; use Dancer qw/:moose :script/; +info "App::Netdisco version $App::Netdisco::VERSION daemon loaded."; # callbacks and local job queue management use App::Netdisco::Daemon::Queue ':all'; @@ -13,15 +22,13 @@ use App::Netdisco::Daemon::Queue ':all'; # needed to quench AF_INET6 symbol errors use NetAddr::IP::Lite ':lower'; -# set temporary MCE files' location in home directory -use Path::Class 'dir'; -my $tmp_dir = ($ENV{NETDISCO_TEMP} || dir($ENV{HOME}, 'tmp')); -mkdir $tmp_dir if ! -d $tmp_dir; - use MCE; use Role::Tiny::With; -info "App::Netdisco version $App::Netdisco::VERSION daemon loaded."; +# 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; my $mce = MCE->new( spawn_delay => 0.15, diff --git a/Netdisco/bin/netdisco-web-fg b/Netdisco/bin/netdisco-web-fg index 8e71c7de..882d47ba 100755 --- a/Netdisco/bin/netdisco-web-fg +++ b/Netdisco/bin/netdisco-web-fg @@ -3,13 +3,15 @@ use strict; use warnings FATAL => 'all'; +use FindBin; +FindBin::again(); +use Path::Class 'dir'; + BEGIN { # stuff useful locations into @INC - use Path::Class 'dir'; - unshift @INC, - dir($location)->parent->subdir('lib')->stringify, - dir($location, 'lib')->stringify; + dir($FindBin::RealBin)->parent->subdir('lib')->stringify, + dir($FindBin::RealBin, 'lib')->stringify; } use App::Netdisco; From c4b60e47a2ba75043ffa2e20af680e56f37efc84 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Mon, 4 Mar 2013 20:47:11 +0000 Subject: [PATCH 5/5] allow netdisco-deploy to work from git and without localenv --- Netdisco/bin/netdisco-daemon | 3 +-- Netdisco/bin/netdisco-deploy | 37 ++++++++++++++++++++++++++++++------ Netdisco/bin/netdisco-web | 3 +-- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/Netdisco/bin/netdisco-daemon b/Netdisco/bin/netdisco-daemon index e42bc152..c8426cff 100755 --- a/Netdisco/bin/netdisco-daemon +++ b/Netdisco/bin/netdisco-daemon @@ -5,11 +5,10 @@ use warnings FATAL => 'all'; use FindBin; FindBin::again(); - our $home; -# try really hard to find a localenv if one isn't already in place. BEGIN { + # try really hard to find a localenv if one isn't already in place. $home = ($ENV{NETDISCO_HOME} || $ENV{HOME}); eval "use Daemon::Control"; diff --git a/Netdisco/bin/netdisco-deploy b/Netdisco/bin/netdisco-deploy index b76b1def..afc70fac 100755 --- a/Netdisco/bin/netdisco-deploy +++ b/Netdisco/bin/netdisco-deploy @@ -1,11 +1,36 @@ #!/usr/bin/env perl use FindBin; -use lib "$FindBin::Bin/../lib"; -use App::Netdisco; +FindBin::again(); +our $home; +BEGIN { + # try really hard to find a localenv if one isn't already in place. + $home = ($ENV{NETDISCO_HOME} || $ENV{HOME}); + eval "use Daemon::Control"; + + if ($@) { + use File::Spec; + my $localenv = File::Spec->catfile($FindBin::RealBin, '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"; + } +} + +BEGIN { + # stuff useful locations into @INC + use Path::Class; + unshift @INC, + dir($FindBin::RealBin)->parent->subdir('lib')->stringify, + dir($FindBin::RealBin, 'lib')->stringify; +} + +use App::Netdisco; use Dancer ':script'; use Dancer::Plugin::DBIC 'schema'; +info "App::Netdisco version $App::Netdisco::VERSION loaded."; use 5.010_000; use Term::UI; @@ -14,7 +39,6 @@ use Term::ReadLine; use Archive::Extract; use HTTP::Tiny; use Try::Tiny; -use Path::Class 'file'; =head1 netdisco-deploy @@ -31,7 +55,8 @@ Internet, and update the OUI table in the database. Hence Internet access is required to run the script. Similarly the latest Netdisco MIB bundle is also downloaded, placed into the -user's home directory, and Netdisco reconfigured for its use. +user's home directory (or C<$ENV{NETDISCO_HOME}>), and Netdisco reconfigured +for its use. =cut @@ -109,12 +134,12 @@ sub deploy_oui { sub deploy_mibs { my $url = 'http://downloads.sourceforge.net/project/netdisco/netdisco-mibs/latest-snapshot/netdisco-mibs-snapshot.tar.gz'; - my $file = file($ENV{HOME}, 'netdisco-mibs-snapshot.tar.gz'); + my $file = file($home, 'netdisco-mibs-snapshot.tar.gz'); my $resp = HTTP::Tiny->new->mirror($url, $file); if ($resp->{success}) { my $ae = Archive::Extract->new(archive => $file, type => 'tgz'); - $ae->extract(to => $ENV{HOME}); + $ae->extract(to => $home); unlink $file; } diff --git a/Netdisco/bin/netdisco-web b/Netdisco/bin/netdisco-web index 7b698fed..ee9ebc38 100755 --- a/Netdisco/bin/netdisco-web +++ b/Netdisco/bin/netdisco-web @@ -5,11 +5,10 @@ use warnings FATAL => 'all'; use FindBin; FindBin::again(); - our $home; -# try really hard to find a localenv if one isn't already in place. BEGIN { + # try really hard to find a localenv if one isn't already in place. $home = ($ENV{NETDISCO_HOME} || $ENV{HOME}); eval "use Daemon::Control";