#!/usr/bin/env perl | |
# | |
# vim: autoindent tabstop=4 shiftwidth=4 expandtab softtabstop=4 filetype=perl | |
# | |
use strict; | |
use warnings; | |
use DBI; | |
my $zones = { | |
z1 => { | |
_db_host => '172.24.0.1', | |
_db_database => 'cloud', | |
_db_username => 'ip-keeper', | |
_db_password => '***', | |
_ip_keeper_account_id => 666, # The ID of the account that will own the assigned addresses | |
_ip_keeper_domain_id => 13, # The ID of the domain to which the account belongs | |
_networks => [ 200, 201, 202 ] # The IDs of the networks that are being shared | |
}, | |
z2 => { | |
_db_host => '172.23.0.1', | |
_db_database => 'cloud', | |
_db_username => 'ip-keeper', | |
_db_password => '***', | |
_ip_keeper_account_id => 999, | |
_ip_keeper_domain_id => 31, | |
_networks => [ 210, 211, 212 ] | |
} | |
}; | |
while (my($zone_name, $zone_parameters) = each(%{ $zones })) { | |
$zone_parameters->{'dbh'} = DBI->connect( | |
sprintf('dbi:mysql:database=%s;host=%s', | |
$zone_parameters->{'_db_database'}, | |
$zone_parameters->{'_db_host'} | |
), | |
$zone_parameters->{'_db_username'}, | |
$zone_parameters->{'_db_password'}, | |
{ PrintError => 0 } | |
) | |
or die('Can\'t connect to the database: ' . $DBI::errstr); | |
$zone_parameters->{'sth_get_allocated'} = $zone_parameters->{'dbh'}->prepare( | |
sprintf( | |
'SELECT public_ip_address ' . | |
'FROM user_ip_address ' . | |
'WHERE ' . | |
'source_network_id IN (%s) AND ' . | |
'state = \'Allocated\' AND ' . | |
'NOT (account_id = ? AND domain_id = ?)', | |
join(',', map('?', @{ $zone_parameters->{'_networks'} })) | |
) | |
) | |
or die('Can\'t prepare the statement: ' . $zone_parameters->{'dbh'}->errstr); | |
$zone_parameters->{'sth_is_free'} = $zone_parameters->{'dbh'}->prepare( | |
sprintf( | |
'SELECT id, public_ip_address, state, account_id, domain_id ' . | |
'FROM user_ip_address ' . | |
'WHERE ' . | |
'public_ip_address = ? AND ' . | |
'source_network_id IN (%s) AND ' . | |
'state = \'Free\'', | |
join(',', map('?', @{ $zone_parameters->{'_networks'} })) | |
) | |
) | |
or die('Can\'t prepare the statement: ' . $zone_parameters->{'dbh'}->errstr); | |
$zone_parameters->{'sth_set_allocated'} = $zone_parameters->{'dbh'}->prepare( | |
'UPDATE user_ip_address ' . | |
'SET ' . | |
'allocated = NOW(), ' . | |
'account_id = ?, ' . | |
'domain_id = ?, ' . | |
'state = \'Allocated\' ' . | |
'WHERE id = ?' | |
) | |
or die('Can\'t prepare the statement: ' . $zone_parameters->{'dbh'}->errstr); | |
$zone_parameters->{'sth_get_free'} = $zone_parameters->{'dbh'}->prepare( | |
sprintf( | |
'SELECT public_ip_address ' . | |
'FROM user_ip_address ' . | |
'WHERE ' . | |
'source_network_id IN (%s) AND ' . | |
'state = \'Free\'', | |
join(',', map('?', @{ $zone_parameters->{'_networks'} })) | |
) | |
) | |
or die('Can\'t prepare the statement: ' . $zone_parameters->{'dbh'}->errstr); | |
$zone_parameters->{'sth_is_allocated'} = $zone_parameters->{'dbh'}->prepare( | |
sprintf( | |
'SELECT id, public_ip_address, state, account_id, domain_id ' . | |
'FROM user_ip_address ' . | |
'WHERE ' . | |
'public_ip_address = ? AND ' . | |
'source_network_id IN (%s) AND (' . | |
'state = \'Allocated\' AND ' . | |
'(account_id = ? AND domain_id = ?)' . | |
')', | |
join(',', map('?', @{ $zone_parameters->{'_networks'} })) | |
) | |
) | |
or die('Can\'t prepare the statement: ' . $zone_parameters->{'dbh'}->errstr); | |
$zone_parameters->{'sth_set_free'} = $zone_parameters->{'dbh'}->prepare( | |
'UPDATE user_ip_address ' . | |
'SET ' . | |
'allocated = NULL, ' . | |
'account_id = NULL, ' . | |
'domain_id = NULL, ' . | |
'state = \'Free\' ' . | |
'WHERE id = ?' | |
) | |
or die('Can\'t prepare the statement: ' . $zone_parameters->{'dbh'}->errstr); | |
} | |
my @zones_list = keys(%{ $zones }); | |
if(@zones_list != 2) { | |
die('There are more or less than 2 zones, can\'t work!'); | |
} | |
for(my $i = 0; $i < @zones_list; $i++) { | |
my $source_zone_name = $zones_list[$i]; | |
my $source_zone_parameters = $zones->{$source_zone_name}; | |
my $target_zone_name = $zones_list[not $i]; | |
my $target_zone_parameters = $zones->{$target_zone_name}; | |
printf("*** Processing the %s zone ***\n", $source_zone_name); | |
$source_zone_parameters->{'sth_get_allocated'}->execute( | |
@{ $source_zone_parameters->{'_networks'} }, | |
$source_zone_parameters->{'_ip_keeper_account_id'}, | |
$source_zone_parameters->{'_ip_keeper_domain_id'}, | |
) | |
or die(sprintf('Can\'t execute the statement: %s', $source_zone_parameters->{'dbh'}->errstr)); | |
while(my $result_source = $source_zone_parameters->{'sth_get_allocated'}->fetchrow_hashref) { | |
$target_zone_parameters->{'sth_is_free'}->execute( | |
$result_source->{'public_ip_address'}, | |
@{ $target_zone_parameters->{'_networks'} } | |
) | |
or die(sprintf('Can\'t execute the statement: %s', $target_zone_parameters->{'dbh'}->errstr)); | |
while(my $result_target = $target_zone_parameters->{'sth_is_free'}->fetchrow_hashref) { | |
printf( | |
'To set allocated: %s (%s) ...', | |
$result_target->{'public_ip_address'}, | |
$result_target->{'id'} | |
); | |
my $have_been_set = $target_zone_parameters->{'sth_set_allocated'}->execute( | |
$target_zone_parameters->{'_ip_keeper_account_id'}, | |
$target_zone_parameters->{'_ip_keeper_domain_id'}, | |
$result_target->{'id'} | |
) | |
or die(sprintf('Can\'t execute the statement: %s', $target_zone_parameters->{'dbh'}->errstr)); | |
printf(" %s results\n", $have_been_set); | |
} | |
} | |
$source_zone_parameters->{'sth_get_free'}->execute( | |
@{ $source_zone_parameters->{'_networks'} } | |
) | |
or die(sprintf('Can\'t execute the statement: %s', $source_zone_parameters->{'dbh'}->errstr)); | |
while(my $result_source = $source_zone_parameters->{'sth_get_free'}->fetchrow_hashref) { | |
$target_zone_parameters->{'sth_is_allocated'}->execute( | |
$result_source->{'public_ip_address'}, | |
@{ $target_zone_parameters->{'_networks'} }, | |
$target_zone_parameters->{'_ip_keeper_account_id'}, | |
$target_zone_parameters->{'_ip_keeper_domain_id'} | |
) | |
or die(sprintf('Can\'t execute the statement: %s', $target_zone_parameters->{'dbh'}->errstr)); | |
while(my $result_target = $target_zone_parameters->{'sth_is_allocated'}->fetchrow_hashref) { | |
printf( | |
'To set free: %s (%s) ...', | |
$result_target->{'public_ip_address'}, | |
$result_target->{'id'} | |
); | |
my $have_been_set = $target_zone_parameters->{'sth_set_free'}->execute( | |
$result_target->{'id'} | |
) | |
or die(sprintf('Can\'t execute the statement: %s', $target_zone_parameters->{'dbh'}->errstr)); | |
printf(" %s results\n", $have_been_set); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment