From d86750ba011913d569612ebcb3b8dbc27fdc8803 Mon Sep 17 00:00:00 2001 From: Digimer Date: Sun, 1 Apr 2018 02:26:38 -0400 Subject: [PATCH] * 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 --- Anvil/Tools.pm | 1 + Anvil/Tools/System.pm | 66 +++++++++++++++++++++++++++++- share/words.xml | 1 + tools/anvil-configure-network | 77 +++++++++++++++++------------------ 4 files changed, 105 insertions(+), 40 deletions(-) diff --git a/Anvil/Tools.pm b/Anvil/Tools.pm index 53c844f5..b7edbc84 100755 --- a/Anvil/Tools.pm +++ b/Anvil/Tools.pm @@ -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", diff --git a/Anvil/Tools/System.pm b/Anvil/Tools/System.pm index 230e7b12..661cb46c 100755 --- a/Anvil/Tools/System.pm +++ b/Anvil/Tools/System.pm @@ -20,6 +20,7 @@ my $THIS_FILE = "System.pm"; # determine_host_type # enable_daemon # get_ips +# hostname # is_local # manage_firewall # ping @@ -421,10 +422,73 @@ sub get_ips $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { "sys::networks::${in_iface}::mac" => $anvil->data->{sys}{networks}{$in_iface}{mac} }}); } } - + 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 >>. diff --git a/share/words.xml b/share/words.xml index c73ccec5..1a3612d2 100644 --- a/share/words.xml +++ b/share/words.xml @@ -217,6 +217,7 @@ The database connection error was: PostgreSQL server is not installed, unable to proceed. A job to configure the network was found, but it has already been picked up by: [#!variable!pid!#]. + 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. Test diff --git a/tools/anvil-configure-network b/tools/anvil-configure-network index 7e996a85..8d895520 100755 --- a/tools/anvil-configure-network +++ b/tools/anvil-configure-network @@ -61,24 +61,35 @@ 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 $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, + 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 $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