create initial user in netdisco-deploy. add COLOR to netdisco-deploy.

This commit is contained in:
Oliver Gorwits
2014-02-08 19:46:39 +00:00
parent 545f878cb7
commit 0286b046f2
2 changed files with 63 additions and 51 deletions

View File

@@ -37,16 +37,19 @@ BEGIN {
use App::Netdisco; use App::Netdisco;
use Dancer ':script'; use Dancer ':script';
use Dancer::Plugin::DBIC 'schema'; use Dancer::Plugin::DBIC 'schema';
use Dancer::Plugin::Passphrase;
info "App::Netdisco version $App::Netdisco::VERSION loaded."; info "App::Netdisco version $App::Netdisco::VERSION loaded.";
use 5.010_000; use 5.010_000;
use Term::UI; use Term::UI;
use Term::ReadLine; use Term::ReadLine;
use Term::ANSIColor;
use Archive::Extract; use Archive::Extract;
$Archive::Extract::PREFER_BIN = 1; $Archive::Extract::PREFER_BIN = 1;
use HTTP::Tiny; use HTTP::Tiny;
use Digest::MD5;
use Try::Tiny; use Try::Tiny;
use Encode; use Encode;
@@ -74,6 +77,7 @@ for its use.
=cut =cut
print color 'bold cyan';
say 'This is the Netdisco II deployment script.'; say 'This is the Netdisco II deployment script.';
say ''; say '';
say 'Before we continue, the following prerequisites must be in place:'; say 'Before we continue, the following prerequisites must be in place:';
@@ -85,6 +89,7 @@ say ' * Internet access (for OUIs and MIBs)';
say ''; say '';
say 'You will be asked to confirm all changes to your system.'; say 'You will be asked to confirm all changes to your system.';
say ''; say '';
print color 'reset';
my $term = Term::ReadLine->new('netdisco'); my $term = Term::ReadLine->new('netdisco');
my $bool = $term->ask_yn( my $bool = $term->ask_yn(
@@ -99,46 +104,46 @@ $bool = $term->ask_yn(
); );
deploy_db() if $bool; deploy_db() if $bool;
say ''; if (not setting('safe_password_store')) {
my $users = schema('netdisco')->resultset('User'); say '';
if ($users->count == 0) { print color 'bold red';
$bool = $term->ask_yn(
prompt => 'Would you like a default web user with Admin rights (discover, etc)?',
default => 'n',
);
if ($bool) {
$users->create({
username => 'guest',
admin => 'true',
port_control => 'true',
});
if (not setting('no_auth')) {
say '';
say '*** Done. Please add "no_auth: true" to your ~/environments/deployment.yml file. ***';
}
}
else {
say '';
$bool = $term->ask_yn(
prompt => 'Would you like a default web user with Port Control rights?',
default => 'n',
);
if ($bool) {
$users->create({
username => 'guest',
port_control => 'true',
});
if (not setting('no_auth')) {
say '';
say '*** Done. Please add "no_auth: true" to your ~/environments/deployment.yml file. ***';
}
}
}
}
elsif (!setting('safe_password_store')) {
say '*** WARNING: Weak password hashes are being stored in the database! ***'; say '*** WARNING: Weak password hashes are being stored in the database! ***';
say '*** WARNING: Please add "safe_password_store: true" to your ~/environments/deployment.yml file. ***'; say '*** WARNING: Please add "safe_password_store: true" to your ~/environments/deployment.yml file. ***';
print color 'reset';
}
sub _make_password {
my $pass = (shift || passphrase->generate_random);
if (setting('safe_password_store')) {
return passphrase($pass)->generate;
}
else {
return Digest::MD5::md5_hex($pass),
}
}
my $users = schema('netdisco')->resultset('User');
if ($users->count == 0) {
say '';
print color 'bold green';
say 'We need to create a user for inital login. This user will be a full Administrator.';
say 'Afterwards, you can go to Admin -> User Management to manage users.';
print color 'reset';
say '';
my $name = $term->get_reply(prompt => 'Username: ');
my $pass = $term->get_reply(prompt => 'Password: ');
$users->create({
username => $name,
password => _make_password($pass),
admin => 'true',
port_control => 'true',
});
print color 'bold blue';
say 'New user created.';
print color 'reset';
} }
say ''; say '';
@@ -150,24 +155,26 @@ deploy_oui() if $bool;
say ''; say '';
my $default_mibhome = dir($home, 'netdisco-mibs'); my $default_mibhome = dir($home, 'netdisco-mibs');
if (setting('mibhome') and setting('mibhome') ne $default_mibhome) { if (setting('mibhome') and setting('mibhome') ne $default_mibhome) {
my $mibhome = $term->get_reply( my $mibhome = $term->get_reply(
print_me => "MIB home options:", print_me => "MIB home options:",
prompt => "Download and update MIB files to...?", prompt => "Download and update MIB files to...?",
choices => [setting('mibhome'), $default_mibhome, 'Skip this.'], choices => [setting('mibhome'), $default_mibhome, 'Skip this.'],
default => 'Skip this.', default => 'Skip this.',
); );
deploy_mibs($mibhome) if $mibhome and $mibhome ne 'Skip this.'; deploy_mibs($mibhome) if $mibhome and $mibhome ne 'Skip this.';
} }
else { else {
$bool = $term->ask_yn( $bool = $term->ask_yn(
prompt => "Download and update MIB files?", default => 'n', prompt => "Download and update MIB files?", default => 'n',
); );
deploy_mibs($default_mibhome) if $bool; deploy_mibs($default_mibhome) if $bool;
} }
sub deploy_db { sub deploy_db {
system 'netdisco-db-deploy'; system 'netdisco-db-deploy';
print color 'bold blue';
say 'DB schema update complete.'; say 'DB schema update complete.';
print color 'reset';
} }
sub deploy_oui { sub deploy_oui {
@@ -204,7 +211,9 @@ sub deploy_oui {
} }
} }
print color 'bold blue';
say 'OUI update complete.'; say 'OUI update complete.';
print color 'reset';
} }
# This subroutine is from Wireshark's make-manuf # This subroutine is from Wireshark's make-manuf
@@ -254,7 +263,9 @@ sub deploy_mibs {
unlink $file; unlink $file;
} }
print color 'bold blue';
say 'MIBs update complete.'; say 'MIBs update complete.';
print color 'reset';
} }
exit 0; exit 0;

View File

@@ -28,8 +28,9 @@ safe_password_store: true
# ``````````````````````````````````````````````````````` # ```````````````````````````````````````````````````````
#domain_suffix: '.example.com' #domain_suffix: '.example.com'
# uncomment and set to true to disable authentication/login # uncomment and set to true to globally disable authentication/login,
# ````````````````````````````````````````````````````````` # and also create a user called "guest".
# ```````````````````````````````````````````````````````````````````
#no_auth: false #no_auth: false
# SNMP community string(s) # SNMP community string(s)