* Created System->hostname() to get and optionally set the hostname (static and pretty).

* Got anvil-configure-network setting the new hostname.
* Updated anvil-configure-network to exit only if the job was picked up by a still-running PID.

Signed-off-by: Digimer <digimer@alteeve.ca>
This commit is contained in:
Digimer 2018-04-01 02:26:38 -04:00
parent 217dfaedc7
commit d86750ba01
4 changed files with 105 additions and 40 deletions

View File

@ -767,6 +767,7 @@ sub _set_paths
'firewall-cmd' => "/usr/bin/firewall-cmd",
gethostip => "/usr/bin/gethostip",
hostname => "/usr/bin/hostname",
hostnamectl => "/usr/bin/hostnamectl",
ip => "/usr/sbin/ip",
'iptables-save' => "/usr/sbin/iptables-save",
journalctl => "/usr/bin/journalctl",

View File

@ -20,6 +20,7 @@ my $THIS_FILE = "System.pm";
# determine_host_type
# enable_daemon
# get_ips
# hostname
# is_local
# manage_firewall
# ping
@ -425,6 +426,69 @@ sub get_ips
return(0);
}
=head2 hostname
Get our set the local hostname. The current host name (or the new hostname if C<< set >> was used) is returned as a string.
Parameters;
=head3 set (optional)
If set, this will become the new host name.
=head3 pretty (optional)
If set, this will be set as the "pretty" host name.
=cut
sub hostname
{
my $self = shift;
my $parameter = shift;
my $anvil = $self->parent;
my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3;
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0125", variables => { method => "System->_is_local()" }});
my $pretty = $parameter->{pretty} ? $parameter->{pretty} : "";
my $set = $parameter->{set} ? $parameter->{set} : "";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
pretty => $pretty,
set => $set,
}});
# Set
if ($set)
{
# TODO: Sanity check the host name
my $shell_call = $anvil->data->{path}{exe}{hostnamectl}." set-hostname $set";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { shell_call => $shell_call }});
my $output = $anvil->System->call({shell_call => $shell_call});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { output => $output }});
}
# Pretty
if ($pretty)
{
# TODO: Escape this for bash properly
# $pretty =~ s/"/\\"/g;
my $shell_call = $anvil->data->{path}{exe}{hostnamectl}." set-hostname --pretty \"$pretty\"";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { shell_call => $shell_call }});
my $output = $anvil->System->call({shell_call => $shell_call});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { output => $output }});
}
# Get
my $shell_call = $anvil->data->{path}{exe}{hostnamectl}." --static";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { shell_call => $shell_call }});
my $hostname = $anvil->System->call({shell_call => $shell_call});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { hostname => $hostname }});
return($hostname);
}
=head2 is_local
This method takes a host name or IP address and looks to see if it matches the local system. If it does, it returns C<< 1 >>. Otherwise it returns C<< 0 >>.

View File

@ -217,6 +217,7 @@ The database connection error was:
<key name="log_0144">PostgreSQL server is not installed, unable to proceed.</key>
<key name="log_0145"><![CDATA[[ Warning ] - Unable to use the database on the host: [#!variable!host!#]. The local Anvil! version is: [#!variable!local_version!#], and the target host's is: [#!variable!target_version!#]. If you are upgrading, we will resync and use it once the host and our version is again the same.]]></key>
<key name="log_0146">A job to configure the network was found, but it has already been picked up by: [#!variable!pid!#].</key>
<key name="log_0147">A job to configure the network was found, and it was picked up by: [#!variable!pid!#], but that process is not running and it appears to only be: [#!variable!percent!# %] complete. Taking the job.</key>
<!-- Test words. Do NOT change unless you update 't/Words.t' or tests will needlessly fail. -->
<key name="t_0000">Test</key>

View File

@ -68,6 +68,7 @@ sub reconfigure_network
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,
@ -76,9 +77,19 @@ sub reconfigure_network
bcn_count => $bcn_count,
ifn_count => $ifn_count,
new_hostname => $new_hostname,
pretty_hostname => $pretty_hostname,
}});
# Set the hostname
my $hostnam = $anvil->System->hostname({set => $new_hostname, pretty => $pretty_hostname, debug => 2});
if ($hostname eq $new_hostname)
{
# Success
}
else
{
# Failed
}
=cut
print Dumper $anvil->data->{variables};
@ -128,24 +139,6 @@ $VAR1 = {
'value' => 'striker'
}
},
'config_step1' => {
'domain' => {
'value' => 'alteeve.com'
},
'sequence' => {
'value' => '1'
},
'ifn_count' => {
'value' => '1'
},
'prefix' => {
'value' => 'an'
},
'organization' => {
'value' => 'Alteeve'
}
}
}
};
=cut
@ -207,17 +200,23 @@ LIMIT 1;";
if ($job_picked_up_by)
{
# Check if the PID is still active.
my ($pids) = $anvil->System->pids({program_name => $THIS_FILE, ignore_me => 1});
my $count = @{$pids};
print "count: [".$count."], my pid: [".$$."]\n";
$anvil->System->pids({ignore_me => 1});
# Any other instance running?.
if ($count)
# Is the PID that picked up the job still alive?
if (exists $anvil->{pids}{$job_picked_up_by})
{
# Yup, exit.
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