new netdisco-deploy script to do all deployment tasks at once

This commit is contained in:
Oliver Gorwits
2012-12-19 22:23:04 +00:00
parent 2c034c7c79
commit df54d5e4f9
3 changed files with 137 additions and 50 deletions

View File

@@ -7,11 +7,6 @@ use App::Netdisco;
use Dancer ':script';
use Dancer::Plugin::DBIC 'schema';
use 5.10.0;
use Term::UI;
use Term::ReadLine;
use HTTP::Tiny;
use Try::Tiny;
=head1 netdisco-db-deploy
@@ -23,10 +18,6 @@ user with rights to create tables in that database. Both the table and user
name must match those configured in your environment YAML file (default
C<environments/development.yml>).
Additionally this script will download the latest MAC address vendor prefix
data from the Internet, and update the OUI table in the database. Hence
Internet access is required to run the script.
Simply run this script, which connects to the database and runs without user
interaction. If there's no Nedisco schema, it is deployed. If there's an
unversioned schema then versioning is added, and updates applied. Otherwise
@@ -56,20 +47,6 @@ Version 4 (not yet created) B<will diverge from "classic" Netdisco 1.x>
=cut
say 'The following must be in place:';
say ' * Internet access';
say ' * Database added to PostgreSQL for Netdisco';
say ' * User added to PostgreSQL with rights to the Database';
say ' * "environments/development.yml" file configured with Database dsn/user/pass';
say '';
my $term = Term::ReadLine->new('netdisco');
my $bool = $term->ask_yn(
prompt => 'Ready to deploy?', default => 'n',
);
exit(0) unless $bool;
my $schema = schema('netdisco');
# installs the dbix_class_schema_versions table with version "1"
@@ -94,29 +71,4 @@ try {
# upgrade from whatever dbix_class_schema_versions says, to $VERSION
$schema->txn_do(sub { $schema->upgrade });
# now populate/update the OUI data
my $url = 'http://standards.ieee.org/develop/regauth/oui/oui.txt';
my $resp = HTTP::Tiny->new->get($url);
my %data = ();
if ($resp->{success}) {
foreach my $line (split /\n/, $resp->{content}) {
if ($line =~ m/^(.{2}-.{2}-.{2})\s+\(hex\)\s+(.*)\s*$/i) {
my ($oui, $company) = ($1, $2);
$oui =~ s/-/:/g;
$data{lc($oui)} = $company;
}
}
if ((scalar keys %data) > 15_000) {
$schema->txn_do(sub{
$schema->resultset('Oui')->delete;
$schema->resultset('Oui')->populate([
map {{oui => $_, company => $data{$_}}} keys %data
]);
});
}
}
exit 0;