Last active
January 18, 2017 21:46
-
-
Save preaction/af92f91b424f9d00a66e18ef43d76fa2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Pod::Usage qw( pod2usage ); | |
use Hannibal; | |
my ( $command ) = @ARGV; | |
die "Specify a command: init, update" unless $command; | |
my $app = Hannibal->new; | |
$app->startup; | |
my @bmcs = ( | |
{ | |
mac => '01:02:03:04:05:06', | |
}, | |
{ | |
mac => 'a1:a2:a3:a4:a5:a6', | |
}, | |
); | |
my @servers = ( | |
{ | |
'system-uuid' => '00000000-0000-4000-0000-000000000000', | |
'system-serial-number' => '012345', | |
'system-manufacturer' => 'Dell', | |
'system-product-name' => 'Whatever', | |
'baseboard-product-name' => 'Other', | |
'bmc-mac' => $bmcs[0]{mac}, | |
}, | |
{ | |
'system-uuid' => '00000000-0000-4000-0000-999999999999', | |
'system-serial-number' => 'adf113ad', | |
'system-manufacturer' => 'SuperMicro', | |
'system-product-name' => 'Whatever', | |
'baseboard-product-name' => 'Other', | |
'bmc-mac' => $bmcs[1]{mac}, | |
}, | |
); | |
my @locations = ( | |
{ | |
type => 'rack', | |
name => 'rack-1', | |
}, | |
{ | |
type => 'rack', | |
name => 'rack-2', | |
}, | |
); | |
my @switches = ( | |
{ | |
hostname => 'tor2.ec137.ord6', | |
}, | |
{ | |
hostname => 'tor9.ec001.ord2', | |
}, | |
); | |
my %edge_service = ( | |
id => 1, | |
pop_id => 1, | |
api_key => 'xxx', | |
ip => '127.0.0.1', | |
type => 'dhcp', | |
); | |
my @dhcp_cache = ( | |
{ # BMC 0 | |
ip => '10.0.0.1', | |
mac => $bmcs[0]{mac}, | |
hostname => '', | |
switch => $switches[0]{hostname}, | |
port => 'ge-0/0/30.0', | |
dhcp_relay_ip => '192.168.0.1', | |
ts => time(), | |
}, | |
{ # Server 0 | |
ip => '10.0.1.1', | |
mac => '02:03:04:05:06:07', | |
hostname => '', | |
switch => $switches[0]{hostname}, | |
port => 'ge-0/0/30.1', | |
dhcp_relay_ip => '192.168.0.1', | |
ts => time(), | |
}, | |
{ # BMC 1 | |
ip => '10.0.0.2', | |
mac => $bmcs[1]{mac}, | |
hostname => '', | |
switch => $switches[1]{hostname}, | |
port => 'ge-0/1/30.0', | |
dhcp_relay_ip => '192.168.0.1', | |
ts => time(), | |
}, | |
{ # Server 0 | |
ip => '10.0.1.2', | |
mac => 'a2:a3:a4:a5:a6:a7', | |
hostname => '', | |
switch => $switches[1]{hostname}, | |
port => 'ge-0/1/30.1', | |
dhcp_relay_ip => '192.168.0.1', | |
ts => time(), | |
}, | |
); | |
if ( $command eq 'init' ) { | |
# Initialize a database with some data | |
my $sql = $app->pg; | |
if ( !$sql->db->query( 'SELECT * FROM edge_service WHERE id=?', $edge_service{id} )->rows ) { | |
$sql->db->query( | |
'INSERT INTO edge_service ( id, api_key, ip, type ) VALUES ( ?, ?, ?, ? ) ', | |
@edge_service{qw( id api_key ip type )}, | |
); | |
} | |
my $bmc_m = $app->model->bmc; | |
for my $bmc ( @bmcs ) { | |
$bmc->{id} = $bmc_m->ensure_mac_exists( $bmc->{mac} ); | |
$bmc_m->update( { mac => $bmc->{mac} }, $bmc ); | |
} | |
my $loc_m = $app->model->location; | |
for my $i ( 0..$#locations ) { | |
my $loc = $locations[ $i ]; | |
$loc->{id} = $loc_m->create( $loc ); | |
$switches[ $i ]{ location_id } = $loc->{ id }; | |
} | |
my $switch_m = $app->model->switch; | |
for my $switch ( @switches ) { | |
if ( my $row = $sql->db->query( 'SELECT * FROM switch WHERE hostname=?', $switch->{hostname} )->hash ) { | |
$switch->{ id } = $row->{id}; | |
} | |
else { | |
$switch->{ id } = $switch_m->create( $switch ); | |
} | |
} | |
my $dhcp_m = $app->model->dhcp; | |
$dhcp_m->upsert_cache( $_->{mac}, 1, $_ ) for @dhcp_cache; | |
my $server_m = $app->model->server; | |
$server_m->insert_data( undef, $_ ) for @servers; | |
} | |
elsif ( $command eq 'update' ) { | |
# Update the data to see if websocket updates are working | |
print "Press Ctrl-C to stop updates...\n"; | |
my $bmc_m = $app->model->bmc; | |
my $power_state = 0; | |
while ( 1 ) { | |
$power_state = $power_state ? 0 : 1; | |
my $str = $power_state ? "on" : "off"; | |
print "Setting power state $str\n"; | |
$bmc_m->update( { mac => $_->{mac} }, { power_state => $str } ) for @bmcs; | |
sleep 2; | |
} | |
} | |
exit 0; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment