You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
321 lines
12 KiB
321 lines
12 KiB
#!/usr/bin/perl |
|
# |
|
# This is called when the local network needs to be reconfigured. |
|
# |
|
# Exit codes; |
|
# 0 = Normal exit. |
|
# 1 = Job was already picked up by another running instance. |
|
# 2 = The host name did not update properly. |
|
# |
|
|
|
use strict; |
|
use warnings; |
|
use Data::Dumper; |
|
use Anvil::Tools; |
|
|
|
my $THIS_FILE = ($0 =~ /^.*\/(.*)$/)[0]; |
|
my $running_directory = ($0 =~ /^(.*?)\/$THIS_FILE$/)[0]; |
|
if (($running_directory =~ /^\./) && ($ENV{PWD})) |
|
{ |
|
$running_directory =~ s/^\./$ENV{PWD}/; |
|
} |
|
|
|
# Turn off buffering so that the pinwheel will display while waiting for the SSH call(s) to complete. |
|
$| = 1; |
|
|
|
my $anvil = Anvil::Tools->new(); |
|
$anvil->Log->level({set => 2}); |
|
$anvil->Log->secure({set => 0}); |
|
|
|
# Read switches |
|
$anvil->Get->switches; |
|
|
|
# Paths |
|
$anvil->Storage->read_config({file => $anvil->data->{path}{config}{'anvil.conf'}}); |
|
|
|
# Connect |
|
my $connections = $anvil->Database->connect({ |
|
sql_file => $anvil->data->{sys}{database}{schema}, |
|
test_table => "network_interfaces", |
|
}); |
|
|
|
print $THIS_FILE." ".__LINE__."; connections: [".$connections."]\n"; |
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0132", variables => { connections => $connections }}); |
|
if (not $connections) |
|
{ |
|
# No databases, exit. |
|
print $anvil->Words->string({key => "striker_error_0003"}); |
|
$anvil->nice_exit({exit_code => 2}); |
|
} |
|
|
|
pickup_job_details($anvil); |
|
|
|
reconfigure_network($anvil); |
|
|
|
$anvil->nice_exit({code => 0}); |
|
|
|
############################################################################################################# |
|
# Functions # |
|
############################################################################################################# |
|
|
|
# This does the work of reconfiguring the network |
|
sub reconfigure_network |
|
{ |
|
my ($anvil) = @_; |
|
|
|
my $prefix = $anvil->data->{variables}{form}{config_step1}{prefix}{value}; |
|
my $sequence = $anvil->data->{variables}{form}{config_step1}{sequence}{value}; |
|
my $domain = $anvil->data->{variables}{form}{config_step1}{domain}{value}; |
|
my $organization = $anvil->data->{variables}{form}{config_step1}{organization}{value}; |
|
my $bcn_count = 1; # TODO: This should be coming from the form, even though it's only '1' for now. |
|
my $ifn_count = $anvil->data->{variables}{form}{config_step1}{ifn_count}{value}; |
|
my $new_hostname = $prefix."-striker".sprintf("%02d", $sequence).".".$domain; |
|
my $pretty_hostname = $organization." - Striker ".sprintf("%02d", $sequence); |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { |
|
prefix => $prefix, |
|
sequence => $sequence, |
|
domain => $domain, |
|
organization => $organization, |
|
bcn_count => $bcn_count, |
|
ifn_count => $ifn_count, |
|
new_hostname => $new_hostname, |
|
pretty_hostname => $pretty_hostname, |
|
}}); |
|
|
|
# Set the hostname |
|
my $hostname = $anvil->System->hostname({set => $new_hostname, pretty => $pretty_hostname, debug => 2}); |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { hostname => $hostname }}); |
|
if ($hostname eq $new_hostname) |
|
{ |
|
# Success |
|
$anvil->Database->insert_or_update_jobs({ |
|
job_uuid => $anvil->{job}{uuid}, |
|
update_progress_only => 1, |
|
job_progress => 10, |
|
job_status => "message_0016,!!hostname!$new_hostname!!\n", |
|
}); |
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "message_0016", variables => { hostname => $new_hostname }}); |
|
} |
|
else |
|
{ |
|
# Failed |
|
$anvil->Database->insert_or_update_jobs({ |
|
job_uuid => $anvil->{job}{uuid}, |
|
update_progress_only => 1, |
|
job_progress => 100, |
|
job_status => "message_0017,!!hostname!$new_hostname!!,!!bad_hostname!$hostname!!\nfailed\n", |
|
}); |
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, key => "message_0017", variables => { |
|
hostname => $new_hostname, |
|
bad_hostname => $hostname, |
|
}}); |
|
$anvil->nice_exit({code => 2}); |
|
} |
|
|
|
# Now configure the network. |
|
foreach my $network (1..$bcn_count) |
|
{ |
|
my $link1_key = "bcn".$network."_iface1_mac"; |
|
my $link2_key = "bcn".$network."_iface2_mac"; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { |
|
link1_key => $link1_key, |
|
link2_key => $link2_key, |
|
}}); |
|
my $create_bond = 0; |
|
if ((exists $anvil->data->{variables}{form}{config_step2}{$link2_key}{value}) && ($anvil->Validate->is_mac({mac => $anvil->data->{variables}{form}{config_step2}{$link2_key}{value}}))) |
|
{ |
|
# Bonded |
|
} |
|
elsif ((exists $anvil->data->{variables}{form}{config_step2}{$link1_key}{value}) && ($anvil->Validate->is_mac({mac => $anvil->data->{variables}{form}{config_step2}{$link1_key}{value}}))) |
|
{ |
|
# Single |
|
} |
|
else |
|
{ |
|
# Doesn't exist, skip. |
|
} |
|
} |
|
|
|
=cut |
|
print Dumper $anvil->data->{variables}; |
|
|
|
$VAR1 = { |
|
'form' => { |
|
'config_step2' => { |
|
'bcn1_subnet' => { |
|
'value' => '255.255.0.0' |
|
}, |
|
'ifn1_iface1_mac' => { |
|
'value' => '52:54:00:dd:ad:8a' |
|
}, |
|
'dns' => { |
|
'value' => '8.8.8.8, 8.8.4.4' |
|
}, |
|
'gateway' => { |
|
'value' => '192.168.122.1' |
|
}, |
|
'bcn1_iface1_mac' => { |
|
'value' => '52:54:00:6b:35:bc' |
|
}, |
|
'striker_password' => { |
|
'value' => 'super secret password' |
|
}, |
|
'hostname' => { |
|
'value' => 'an-striker01.alteeve.com' |
|
}, |
|
'gateway_interface' => { |
|
'value' => 'ifn1' |
|
}, |
|
'ifn1_subnet' => { |
|
'value' => '255.255.0.0' |
|
}, |
|
'ifn1_ip' => { |
|
'value' => '192.168.122.201' |
|
}, |
|
'bcn1_ip' => { |
|
'value' => '10.1.4.1' |
|
}, |
|
'bcn1_iface2_mac' => { |
|
'value' => '52:54:00:5d:5d:3d' |
|
}, |
|
'ifn1_iface2_mac' => { |
|
'value' => '52:54:00:1e:36:03' |
|
}, |
|
'striker_user' => { |
|
'value' => 'striker' |
|
} |
|
}, |
|
}; |
|
=cut |
|
|
|
return(0); |
|
} |
|
|
|
# This will pick up the job, or exit. |
|
sub pickup_job_details |
|
{ |
|
my ($anvil) = @_; |
|
|
|
# If any job said it was picked up, and the "job_picked_up_by" PID doesn't exist, take it and update |
|
# it. |
|
|
|
my $query = " |
|
SELECT |
|
job_uuid, |
|
job_command, |
|
job_data, |
|
job_picked_up_by, |
|
job_picked_up_at, |
|
job_updated, |
|
job_progress |
|
FROM |
|
jobs |
|
WHERE |
|
job_name = 'configure::network' |
|
AND |
|
job_progress != 100 |
|
AND |
|
job_host_uuid = ".$anvil->data->{sys}{use_db_fh}->quote($anvil->Get->host_uuid)." |
|
LIMIT 1;"; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { query => $query }}); |
|
|
|
my $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__}); |
|
my $count = @{$results}; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { |
|
results => $results, |
|
count => $count, |
|
}}); |
|
my $job_uuid = $results->[0]->[0]; |
|
my $job_command = $results->[0]->[1]; |
|
my $job_data = defined $results->[0]->[2] ? $results->[0]->[2] : ""; |
|
my $job_picked_up_by = $results->[0]->[3]; |
|
my $job_picked_up_at = $results->[0]->[4]; |
|
my $job_updated = $results->[0]->[5]; |
|
my $job_progress = $results->[0]->[6]; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { |
|
job_uuid => $job_uuid, |
|
job_command => $job_command, |
|
job_data => $job_data, |
|
job_picked_up_by => $job_picked_up_by, |
|
job_picked_up_at => $job_picked_up_at, |
|
job_updated => $job_updated, |
|
job_progress => $job_progress, |
|
}}); |
|
|
|
# This will be used when updating the job |
|
$anvil->{job}{uuid} = $job_uuid; |
|
|
|
# See if the job was picked up by another running instance. |
|
if ($job_picked_up_by) |
|
{ |
|
# Check if the PID is still active. |
|
$anvil->System->pids({ignore_me => 1}); |
|
|
|
# Is the PID that picked up the job still alive? |
|
if (exists $anvil->{pids}{$job_picked_up_by}) |
|
{ |
|
print Dumper $anvil->{pids}{$job_picked_up_by}; |
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "log_0146", variables => { pid => $job_picked_up_by }}); |
|
$anvil->nice_exit({code => 1}); |
|
} |
|
else |
|
{ |
|
# The previous job is gone, we'll take this over. |
|
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "log_0147", variables => { |
|
pid => $job_picked_up_by, |
|
percent => $job_progress, |
|
}}); |
|
} |
|
} |
|
|
|
# This will store the variables from the database |
|
$anvil->data->{variables} = {}; |
|
|
|
# If we're still alive, pick up the details. |
|
$results = ""; |
|
$count = ""; |
|
$query = " |
|
SELECT |
|
variable_name, |
|
variable_value |
|
FROM |
|
variables |
|
WHERE |
|
variable_name |
|
LIKE |
|
'form::config_step%' |
|
AND |
|
variable_source_table = 'hosts' |
|
AND |
|
variable_source_uuid = ".$anvil->data->{sys}{use_db_fh}->quote($anvil->Get->host_uuid)." |
|
;"; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { query => $query }}); |
|
|
|
$results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__}); |
|
$count = @{$results}; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { |
|
results => $results, |
|
count => $count, |
|
}}); |
|
foreach my $row (@{$results}) |
|
{ |
|
my $this_variable = $row->[0]; |
|
my $this_value = defined $row->[1] ? $row->[1] : ""; |
|
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { |
|
this_variable => $this_variable, |
|
this_value => $this_value, |
|
}}); |
|
|
|
$anvil->_make_hash_reference($anvil->data->{variables}, $this_variable, $this_value); |
|
} |
|
|
|
# Record that we've picked up this job. |
|
$anvil->Database->insert_or_update_jobs({ |
|
job_uuid => $anvil->{job}{uuid}, |
|
update_progress_only => 1, |
|
job_progress => 1, |
|
job_status => "message_0015\n", |
|
}); |
|
|
|
return(0); |
|
}
|
|
|