* Got anvil-download-file downloading files. Much testing is needed, and it's just the barebones so far.

* Added to Convert->time the 'translate' parameter that controls if the returned string is already translated or not.
* Updated Storage->change_mode and ->change_owner to rename the parameter 'target' to 'path' to help prevent future confusion woth most other instances of the 'target' parameter meaning a target machine.

Signed-off-by: Digimer <digimer@alteeve.ca>
main
Digimer 6 years ago
parent c41adb17ff
commit 605c3c4ffb
  1. 2
      Anvil/Tools.pm
  2. 32
      Anvil/Tools/Convert.pm
  3. 2
      Anvil/Tools/Log.pm
  4. 54
      Anvil/Tools/Storage.pm
  5. 9
      share/words.xml
  6. 194
      tools/anvil-download-file
  7. 4
      tools/anvil-manage-files
  8. 4
      tools/striker-manage-install-target

@ -1037,7 +1037,7 @@ sub _set_paths
usermod => "/usr/sbin/usermod", usermod => "/usr/sbin/usermod",
uuidgen => "/usr/bin/uuidgen", uuidgen => "/usr/bin/uuidgen",
virsh => "/usr/bin/virsh", virsh => "/usr/bin/virsh",
wget => "", wget => "/usr/bin/wget",
}, },
json => { json => {
files => "files.json", files => "files.json",

@ -18,7 +18,7 @@ my $THIS_FILE = "Convert.pm";
# hostname_to_ip # hostname_to_ip
# human_readable_to_bytes # human_readable_to_bytes
# round # round
# time
=pod =pod
@ -989,6 +989,10 @@ If set to C<< 1 >>, the long suffixes will be used instead of the default C<< w/
B<< Note >>: The suffixes are translatable in both short (default) and long formats. See the C<< suffix_0002 >> through C<< suffix_0011 >> string keys. B<< Note >>: The suffixes are translatable in both short (default) and long formats. See the C<< suffix_0002 >> through C<< suffix_0011 >> string keys.
=head3 translate (optional, default '0')
When set to C<< 1 >>, the string returned will be translated into plain language. When set to C<< 0 >> (the default), the string returned will not have translated words, but instead return the C<< #!string!x!# >> codes for insertion into templates, email bodies or other uses where translation will be done later.
=cut =cut
sub time sub time
{ {
@ -997,19 +1001,15 @@ sub time
my $anvil = $self->parent; my $anvil = $self->parent;
my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3; my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3;
my $time = $parameter->{'time'} ? $parameter->{'time'} : 0; my $time = $parameter->{'time'} ? $parameter->{'time'} : 0;
my $long = $parameter->{long} ? $parameter->{long} : 0; my $long = $parameter->{long} ? $parameter->{long} : 0;
my $translate = $parameter->{translate} ? $parameter->{translate} : 0;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
'time' => $time, 'time' => $time,
long => $long, long => $long,
translate => $translate,
}}); }});
if (not $time)
{
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Storage->update_file()", parameter => "time" }});
return("#!error!#");
}
# Remote commas and verify we're left with a number. # Remote commas and verify we're left with a number.
$time =~ s/,//g; $time =~ s/,//g;
if ($time =~ /^\d+\.\d+$/) if ($time =~ /^\d+\.\d+$/)
@ -1110,8 +1110,14 @@ sub time
}}); }});
} }
# Return an already-translated string # Return an already-translated string, if asked.
$say_time = $anvil->Words->string({debug => $debug, string => $say_time}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { translate => $translate }});
if ($translate)
{
$say_time = $anvil->Words->string({debug => $debug, string => $say_time});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { say_time => $say_time }});
}
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { say_time => $say_time }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { say_time => $say_time }});
return($say_time); return($say_time);
} }

@ -407,7 +407,7 @@ sub entry
print $THIS_FILE." ".__LINE__."; HANDLE::log::main: [".$anvil->data->{HANDLE}{'log'}{main}."]\n" if $test; print $THIS_FILE." ".__LINE__."; HANDLE::log::main: [".$anvil->data->{HANDLE}{'log'}{main}."]\n" if $test;
# Make sure it can be written to by apache. # Make sure it can be written to by apache.
$anvil->Storage->change_mode({test => $test, debug => $debug, target => $log_file, mode => "0666"}); $anvil->Storage->change_mode({test => $test, debug => $debug, path => $log_file, mode => "0666"});
} }
if (not $anvil->data->{HANDLE}{'log'}{main}) if (not $anvil->data->{HANDLE}{'log'}{main})

@ -320,13 +320,13 @@ fi";
This changes the mode of a file or directory. This changes the mode of a file or directory.
$anvil->Storage->change_mode({target => "/tmp/foo", mode => "0644"}); $anvil->Storage->change_mode({path => "/tmp/foo", mode => "0644"});
If it fails to write the file, an alert will be logged. If it fails to write the file, an alert will be logged.
Parameters; Parameters;
=head3 target (required) =head3 path (required)
This is the file or directory to change the mode on. This is the file or directory to change the mode on.
@ -342,18 +342,18 @@ sub change_mode
my $anvil = $self->parent; my $anvil = $self->parent;
my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3; my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3;
my $target = defined $parameter->{target} ? $parameter->{target} : ""; my $path = defined $parameter->{path} ? $parameter->{path} : "";
my $mode = defined $parameter->{mode} ? $parameter->{mode} : ""; my $mode = defined $parameter->{mode} ? $parameter->{mode} : "";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
target => $target, mode => $mode,
mode => $mode, path => $path,
}}); }});
my $error = 0; my $error = 0;
if (not $target) if (not $path)
{ {
# No target... # No path...
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Storage->change_mode()", parameter => "target" }}); $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Storage->change_mode()", parameter => "path" }});
$error = 1; $error = 1;
} }
if (not $mode) if (not $mode)
@ -371,7 +371,7 @@ sub change_mode
if (not $error) if (not $error)
{ {
my $shell_call = $anvil->data->{path}{exe}{'chmod'}." $mode $target"; my $shell_call = $anvil->data->{path}{exe}{'chmod'}." $mode $path";
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0011", variables => { shell_call => $shell_call }}); $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0011", variables => { shell_call => $shell_call }});
open (my $file_handle, $shell_call." 2>&1 |") or $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0014", variables => { shell_call => $shell_call, error => $! }}); open (my $file_handle, $shell_call." 2>&1 |") or $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0014", variables => { shell_call => $shell_call, error => $! }});
while(<$file_handle>) while(<$file_handle>)
@ -390,23 +390,23 @@ sub change_mode
This changes the owner and/or group of a file or directory. This changes the owner and/or group of a file or directory.
$anvil->Storage->change_owner({target => "/tmp/foo", user => "apache", group => "apache" }); $anvil->Storage->change_owner({path => "/tmp/foo", user => "apache", group => "apache" });
If it fails to write the file, an alert will be logged and 'C<< 1 >>' will be returned. Otherwise, 'C<< 0 >>' will be returned. If it fails to write the file, an alert will be logged and 'C<< 1 >>' will be returned. Otherwise, 'C<< 0 >>' will be returned.
Parameters; Parameters;
=head3 target (required) =head3 path (required)
This is the file or directory to change the mode on. This is the file or directory to change the mode on.
=head3 group (optional, default is the main group of the user running the program) =head3 group (optional, default is the main group of the user running the program)
This is the group name or UID to set the target to. This is the group name or UID to set the path to.
=head3 user (optional, default is the user running the program) =head3 user (optional, default is the user running the program)
This is the user name or UID to set the target to. This is the user name or UID to set the path to.
=cut =cut
sub change_owner sub change_owner
@ -416,11 +416,11 @@ sub change_owner
my $anvil = $self->parent; my $anvil = $self->parent;
my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3; my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3;
my $target = defined $parameter->{target} ? $parameter->{target} : ""; my $path = defined $parameter->{path} ? $parameter->{path} : "";
my $group = defined $parameter->{group} ? $parameter->{group} : getgrgid($(); my $group = defined $parameter->{group} ? $parameter->{group} : getgrgid($();
my $user = defined $parameter->{user} ? $parameter->{user} : getpwuid($<); my $user = defined $parameter->{user} ? $parameter->{user} : getpwuid($<);
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
target => $target, path => $path,
group => $group, group => $group,
user => $user, user => $user,
}}); }});
@ -435,15 +435,15 @@ sub change_owner
my $string = ""; my $string = "";
my $error = 0; my $error = 0;
if (not $target) if (not $path)
{ {
# No target... # No path...
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Storage->change_owner()", parameter => "target" }}); $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Storage->change_owner()", parameter => "path" }});
$error = 1; $error = 1;
} }
if (not -e $target) if (not -e $path)
{ {
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "alert", key => "log_0051", variables => {target => $target }}); $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "alert", key => "log_0051", variables => {path => $path }});
$error = 1; $error = 1;
} }
@ -464,7 +464,7 @@ sub change_owner
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { error => $error, string => $string }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { error => $error, string => $string }});
if ((not $error) && ($string ne "")) if ((not $error) && ($string ne ""))
{ {
my $shell_call = $anvil->data->{path}{exe}{'chown'}." $string $target"; my $shell_call = $anvil->data->{path}{exe}{'chown'}." ".$string." ".$path;
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0011", variables => { shell_call => $shell_call }}); $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0011", variables => { shell_call => $shell_call }});
open (my $file_handle, $shell_call." 2>&1 |") or $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0014", variables => { shell_call => $shell_call, error => $! }}); open (my $file_handle, $shell_call." 2>&1 |") or $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0014", variables => { shell_call => $shell_call, error => $! }});
while(<$file_handle>) while(<$file_handle>)
@ -1066,12 +1066,12 @@ fi;";
print $THIS_FILE." ".__LINE__."; mode: [".$mode."]\n" if $test; print $THIS_FILE." ".__LINE__."; mode: [".$mode."]\n" if $test;
if ($mode) if ($mode)
{ {
$anvil->Storage->change_mode({debug => $debug, target => $working_directory, mode => $mode}); $anvil->Storage->change_mode({debug => $debug, path => $working_directory, mode => $mode});
} }
print $THIS_FILE." ".__LINE__."; user: [".$user."], group: [".$group."]\n" if $test; print $THIS_FILE." ".__LINE__."; user: [".$user."], group: [".$group."]\n" if $test;
if (($user) or ($group)) if (($user) or ($group))
{ {
$anvil->Storage->change_owner({debug => $debug, target => $working_directory, user => $user, group => $group}); $anvil->Storage->change_owner({debug => $debug, path => $working_directory, user => $user, group => $group});
} }
if (not -e $working_directory) if (not -e $working_directory)
@ -2845,7 +2845,7 @@ fi";
if ($secure) if ($secure)
{ {
$anvil->System->call({debug => $debug, shell_call => $anvil->data->{path}{exe}{touch}." ".$file}); $anvil->System->call({debug => $debug, shell_call => $anvil->data->{path}{exe}{touch}." ".$file});
$anvil->Storage->change_mode({debug => $debug, target => $file, mode => $mode}); $anvil->Storage->change_mode({debug => $debug, path => $file, mode => $mode});
} }
# Now write the file. # Now write the file.
@ -2857,11 +2857,11 @@ fi";
if ($mode) if ($mode)
{ {
$anvil->Storage->change_mode({debug => $debug, target => $file, mode => $mode}); $anvil->Storage->change_mode({debug => $debug, path => $file, mode => $mode});
} }
if (($user) or ($group)) if (($user) or ($group))
{ {
$anvil->Storage->change_owner({debug => $debug, target => $file, user => $user, group => $group}); $anvil->Storage->change_owner({debug => $debug, path => $file, user => $user, group => $group});
} }
} }
} }

@ -291,7 +291,7 @@ It also has replacement variables: [#!variable!first!#] and [#!variable!second!#
<key name="log_0035"><![CDATA[[ Warning ] - The config file: [#!variable!file!#] appears to have a malformed line: [#!variable!count!#:#!variable!line!#].]]></key> <key name="log_0035"><![CDATA[[ Warning ] - The config file: [#!variable!file!#] appears to have a malformed line: [#!variable!count!#:#!variable!line!#].]]></key>
<key name="log_0036"><![CDATA[[ Error ] - The method Database->read_variable() was called but both the 'variable_name' and 'variable_uuid' parameters were not passed or both were empty.]]></key> <key name="log_0036"><![CDATA[[ Error ] - The method Database->read_variable() was called but both the 'variable_name' and 'variable_uuid' parameters were not passed or both were empty.]]></key>
<key name="log_0037"><![CDATA[[ Error ] - The method Database->insert_or_update_variables() method was called but both the 'variable_name' and 'variable_uuid' parameters were not passed or both were empty.]]></key> <key name="log_0037"><![CDATA[[ Error ] - The method Database->insert_or_update_variables() method was called but both the 'variable_name' and 'variable_uuid' parameters were not passed or both were empty.]]></key>
<key name="log_0038"><![CDATA[[ Error ] - The method Storage->change_mode() was called without an invalid 'mode' parameter. It should have been three or four digits, but: [#!variable!mode!#] was passed.]]></key> <key name="log_0038"><![CDATA[[ Error ] - The method Storage->change_mode() was called without an invalid 'mode' parameter. It should have been three or four digits, or 'x+/-y' format, but: [#!variable!mode!#] was passed.]]></key>
<key name="log_0039">The host: [#!variable!host!#] has released its database lock.</key> <key name="log_0039">The host: [#!variable!host!#] has released its database lock.</key>
<key name="log_0040"><![CDATA[[ Error ] - The method Storage->write_file() was asked to write the file: [#!variable!file!#] but it already exists and 'overwrite' was not set. Aborting.]]></key> <key name="log_0040"><![CDATA[[ Error ] - The method Storage->write_file() was asked to write the file: [#!variable!file!#] but it already exists and 'overwrite' was not set. Aborting.]]></key>
<key name="log_0041"><![CDATA[[ Error ] - The method Storage->write_file() was asked to write the file: [#!variable!file!#] but it is not a full path. Aborting.]]></key> <key name="log_0041"><![CDATA[[ Error ] - The method Storage->write_file() was asked to write the file: [#!variable!file!#] but it is not a full path. Aborting.]]></key>
@ -304,7 +304,7 @@ It also has replacement variables: [#!variable!first!#] and [#!variable!second!#
<key name="log_0048"><![CDATA[[ Warning ] - Testing of Anvil::Tools is beginning. This will generate warnings and alerts and are not a concern.]]></key> <key name="log_0048"><![CDATA[[ Warning ] - Testing of Anvil::Tools is beginning. This will generate warnings and alerts and are not a concern.]]></key>
<key name="log_0049"><![CDATA[[ Warning ] - Testing of Anvil::Tools is complete.]]></key> <key name="log_0049"><![CDATA[[ Warning ] - Testing of Anvil::Tools is complete.]]></key>
<key name="log_0050">[ Error ] - There is a local database defined, but it does not appear to exist and we could not initialize the database server. Is 'postgresql-server' installed?</key> <key name="log_0050">[ Error ] - There is a local database defined, but it does not appear to exist and we could not initialize the database server. Is 'postgresql-server' installed?</key>
<key name="log_0051"><![CDATA[[ Error ] - The method Storage->change_owner() was asked to change the ownership of: [#!variable!target!#] which doesn't exist.]]></key> <key name="log_0051"><![CDATA[[ Error ] - The method Storage->change_owner() was asked to change the ownership of: [#!variable!path!#] which doesn't exist.]]></key>
<key name="log_0052"><![CDATA[[ Error ] - The method Storage->#!variable!method!#() was called but the source file: [#!variable!source_file!#] doesn't exist.]]></key> <key name="log_0052"><![CDATA[[ Error ] - The method Storage->#!variable!method!#() was called but the source file: [#!variable!source_file!#] doesn't exist.]]></key>
<key name="log_0053"><![CDATA[[ Error ] - The 'Database->connect()' method tried to connect to the same database twice: [#!variable!target!#].]]></key> <key name="log_0053"><![CDATA[[ Error ] - The 'Database->connect()' method tried to connect to the same database twice: [#!variable!target!#].]]></key>
<key name="log_0054"> <key name="log_0054">
@ -602,6 +602,7 @@ We will keep looking.</key>
<key name="log_0294"><![CDATA[[ Error ] - The method Convert->time() was passed the 'time' of: [#!variable!time!#] which does not appear to be a whole number.]]></key> <key name="log_0294"><![CDATA[[ Error ] - The method Convert->time() was passed the 'time' of: [#!variable!time!#] which does not appear to be a whole number.]]></key>
<key name="log_0295"><![CDATA[[ Error ] - The method Remote->call() was passed the 'timeout' of: [#!variable!timeout!#] which does not appear to be a whole number.]]></key> <key name="log_0295"><![CDATA[[ Error ] - The method Remote->call() was passed the 'timeout' of: [#!variable!timeout!#] which does not appear to be a whole number.]]></key>
<key name="log_0296">We have a connection open already to: [#!variable!connection!#], skipping connect stage.</key> <key name="log_0296">We have a connection open already to: [#!variable!connection!#], skipping connect stage.</key>
<key name="log_0297">The file: [#!variable!file!#] has beed successfully downloaded.</key>
<!-- Test words. Do NOT change unless you update 't/Words.t' or tests will needlessly fail. --> <!-- Test words. Do NOT change unless you update 't/Words.t' or tests will needlessly fail. -->
<key name="t_0000">Test</key> <key name="t_0000">Test</key>
@ -878,7 +879,9 @@ Failed to generate an RSA public key for the user: [#!variable!user!#]. The outp
#!variable!output!# #!variable!output!#
==== ====
</key> </key>
<key name="error_0058">Failed to backup: [#!variable!file!#], skipping..</key> <key name="error_0058">Failed to backup: [#!variable!file!#], skipping.</key>
<key name="error_0059">The file to be downloaded: [#!variable!file!#], already exists. Either remove it, or call again with '--overwrite'.</key>
<key name="error_0060">Something went wrong moving the downloaded file from the temporary location: [#!variable!source_file!#] to the output: [#!variable!target_file!#]. Useful errors may be above this message.</key>
<!-- These are units, words and so on used when displaying information. --> <!-- These are units, words and so on used when displaying information. -->
<key name="unit_0001">Yes</key> <key name="unit_0001">Yes</key>

@ -12,6 +12,8 @@
# 5 = The requested URL failed because there is no route to that host. # 5 = The requested URL failed because there is no route to that host.
# 6 = Abort requested, but UUID or PID not passed # 6 = Abort requested, but UUID or PID not passed
# 7 = The requested URL failed because the network is unreachable. # 7 = The requested URL failed because the network is unreachable.
# 8 = The file to download already exists.
# 9 = Something went wrong moving the file from temp to the output directory.
# #
# #
# TODO: # TODO:
@ -43,14 +45,18 @@ $anvil->Log->secure({set => 1});
$anvil->data->{switches}{abort} = ""; $anvil->data->{switches}{abort} = "";
$anvil->data->{switches}{'job-uuid'} = ""; $anvil->data->{switches}{'job-uuid'} = "";
$anvil->data->{switches}{overwrite} = "";
$anvil->data->{switches}{'save-to'} = ""; # /mnt/shared/files by default $anvil->data->{switches}{'save-to'} = ""; # /mnt/shared/files by default
$anvil->data->{switches}{script} = "";
$anvil->data->{switches}{url} = ""; $anvil->data->{switches}{url} = "";
$anvil->Get->switches; $anvil->Get->switches;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
'switches::abort' => $anvil->data->{switches}{abort}, 'switches::abort' => $anvil->data->{switches}{abort},
'switches::job-uuid' => $anvil->data->{switches}{'job-uuid'}, 'switches::job-uuid' => $anvil->data->{switches}{'job-uuid'},
'switches::save-to' => $anvil->data->{switches}{'save-to'}, 'switches::overwrite' => $anvil->data->{switches}{overwrite},
'switches::url' => $anvil->data->{switches}{url}, 'switches::save-to' => $anvil->data->{switches}{'save-to'},
'switches::script' => $anvil->data->{switches}{script},
'switches::url' => $anvil->data->{switches}{url},
}}); }});
@ -79,22 +85,24 @@ $anvil->nice_exit({exit_code => 0});
# Private functions. # # Private functions. #
############################################################################################################# #############################################################################################################
sub abort_download sub download_file
{ {
my ($anvil) = @_; my ($anvil) = @_;
my $failed = 0; my $failed = 0;
my $url = $anvil->data->{switches}{url}; my $url = $anvil->data->{switches}{url};
my $file_name = ($url =~ /^.*\/(.*)$/)[0]; my $file_name = ($url =~ /^.*\/(.*)$/)[0];
my $temp_file = $anvil->data->{path}{directories}{shared}{temp}."/".$file; my $temp_file = $anvil->data->{path}{directories}{shared}{temp}."/".$file_name;
my $save_to = $anvil->data->{switches}{'save-to'} ? $anvil->data->{switches}{'save-to'} : $anvil->data->{path}{directories}{shared}{files}; my $save_to = $anvil->data->{switches}{'save-to'} ? $anvil->data->{switches}{'save-to'} : $anvil->data->{path}{directories}{shared}{files};
my $out_file = $save_to."/".$file; my $out_file = $save_to."/".$file_name;
$save_to =~ s/\/\///g; $save_to =~ s/\/\///g;
my $unix_start = time;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
url => $url, url => $url,
file_name => $file_name, file_name => $file_name,
temp_file => $temp_file, temp_file => $temp_file,
save_to => $save_to, unix_start => $unix_start,
save_to => $save_to,
}}); }});
# Is this a supported protocol? # Is this a supported protocol?
@ -104,6 +112,37 @@ sub abort_download
$anvil->nice_exit({exit_code => 1}); $anvil->nice_exit({exit_code => 1});
} }
# If the target file exists, exit.
if ((-e $out_file) && (not $anvil->data->{switches}{overwrite}))
{
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, priority => "err", key => "error_0059", variables => { file => $out_file }});
$anvil->nice_exit({exit_code => 8});
}
# Make sure the output directory exists.
if (not -e $save_to)
{
my $failed = $anvil->Storage->make_directory({
debug => 2,
directory => $save_to,
mode => "0777",
user => "apache",
group => "apache",
});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { failed => $failed }});
}
if (not -e $anvil->data->{path}{directories}{shared}{temp})
{
my $failed = $anvil->Storage->make_directory({
debug => 2,
directory => $anvil->data->{path}{directories}{shared}{temp},
mode => "0777",
user => "apache",
group => "apache",
});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { failed => $failed }});
}
### NOTE: We don't use System->call because we need to track the output in real time. ### NOTE: We don't use System->call because we need to track the output in real time.
# Try to download it. # Try to download it.
my $bytes_downloaded = 0; my $bytes_downloaded = 0;
@ -112,12 +151,19 @@ sub abort_download
my $rate = 0; # Bytes/sec my $rate = 0; # Bytes/sec
my $time_left = 0; # Seconds my $time_left = 0; # Seconds
my $running_time = 0; my $running_time = 0;
my $average_rate = 0;
my $report_interval = 5; # Seconds between status file update
my $next_report = time + $report_interval;
my $shell_call = $anvil->data->{path}{exe}{wget}." --continue --progress=dot:binary ".$url." --output-document ".$temp_file; my $shell_call = $anvil->data->{path}{exe}{wget}." --continue --progress=dot:binary ".$url." --output-document ".$temp_file;
open (my $file_handle, $shell_call." 2>&1 |") or $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, secure => $secure, priority => "err", key => "log_0014", variables => { shell_call => $shell_call, error => $! }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
next_report => $next_report,
shell_call => $shell_call,
}});
open (my $file_handle, $shell_call." 2>&1 |") or $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 0, secure => 0, priority => "err", key => "log_0014", variables => { shell_call => $shell_call, error => $! }});
while(<$file_handle>) while(<$file_handle>)
{ {
chomp; chomp;
my $line = $anvil->Words->clean_spaces({ string => $_ });; my $line = $anvil->Words->clean_spaces({string => $_});;
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0017", variables => { line => $line }}); $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0017", variables => { line => $line }});
# Check for problems # Check for problems
@ -162,12 +208,22 @@ sub abort_download
### NOTE: According to: http://savannah.gnu.org/bugs/index.php?22765, wget uses base-2. ### NOTE: According to: http://savannah.gnu.org/bugs/index.php?22765, wget uses base-2.
# Convert # Convert
$bytes_downloaded = $downloaded * 1024; $bytes_downloaded = $downloaded * 1024;
my $say_downloaded = $anvil->Convert->bytes_to_human_readable({'bytes' => $bytes_downloaded}); my $say_downloaded = $anvil->Convert->bytes_to_human_readable({debug => 3, 'bytes' => $bytes_downloaded});
my $say_percent = $percent."%"; my $say_percent = $percent."%";
my $byte_rate = $anvil->Convert->human_readable_to_bytes({size => $rate, base2 => 1}); my $byte_rate = $anvil->Convert->human_readable_to_bytes({debug => 3, size => $rate, base2 => 1});
my $say_rate = $anvil->Convert->bytes_to_human_readable({'bytes' => $byte_rate})."/s"; my $say_rate = $anvil->Convert->bytes_to_human_readable({debug => 3, 'bytes' => $byte_rate})."/s";
$running_time = time - $unix_start; $running_time = time - $unix_start;
my $say_running_time = $an->Readable->time({'time' => $running_time, process => 1}); my $say_running_time = $anvil->Convert->time({debug => 3, 'time' => $running_time, translate => 1});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
bytes_downloaded => $bytes_downloaded,
say_downloaded => $say_downloaded,
say_percent => $say_percent,
byte_rate => $byte_rate,
say_rate => $say_rate,
running_time => $running_time,
say_running_time => $say_running_time,
}});
# Time left is a bit more complicated # Time left is a bit more complicated
my $days = 0; my $days = 0;
my $hours = 0; my $hours = 0;
@ -176,55 +232,111 @@ sub abort_download
if ($time_left =~ /(\d+)d/) if ($time_left =~ /(\d+)d/)
{ {
$days = $1; $days = $1;
#print "$THIS_FILE ".__LINE__."; == days: [$days]\n"; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { days => $days }});
} }
if ($time_left =~ /(\d+)h/) if ($time_left =~ /(\d+)h/)
{ {
$hours = $1; $hours = $1;
#print "$THIS_FILE ".__LINE__."; == hours: [$hours]\n"; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { hours => $hours }});
} }
if ($time_left =~ /(\d+)m/) if ($time_left =~ /(\d+)m/)
{ {
$minutes = $1; $minutes = $1;
#print "$THIS_FILE ".__LINE__."; == minutes: [$minutes]\n"; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { minutes => $minutes }});
} }
if ($time_left =~ /(\d+)s/) if ($time_left =~ /(\d+)s/)
{ {
$seconds = $1; $seconds = $1;
#print "$THIS_FILE ".__LINE__."; == seconds: [$seconds]\n"; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { seconds => $seconds }});
} }
my $seconds_left = (($days * 86400) + ($hours * 3600) + ($minutes * 60) + $seconds); my $seconds_left = (($days * 86400) + ($hours * 3600) + ($minutes * 60) + $seconds);
my $say_time_left = $an->Readable->time({'time' => $seconds_left, suffix => "long", process => 1}); my $say_time_left = $anvil->Convert->time({debug => 3, 'time' => $seconds_left, suffix => "long", translate => 1});
$running_time = 1 if not $running_time; $running_time = 1 if not $running_time;
$average_rate = int($bytes_downloaded / $running_time); $average_rate = int($bytes_downloaded / $running_time);
my $say_average_rate = $anvil->Convert->bytes_to_human_readable({'bytes' => $average_rate})."/s"; my $say_average_rate = $anvil->Convert->bytes_to_human_readable({debug => 3, 'bytes' => $average_rate})."/s";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
seconds_left => $seconds_left,
say_time_left => $say_time_left,
running_time => $running_time,
average_rate => $average_rate,
say_average_rate => $say_average_rate,
}});
#print "$THIS_FILE ".__LINE__."; downloaded: [$downloaded], bytes_downloaded: [$bytes_downloaded], say_downloaded: [$say_downloaded], percent: [$percent], rate: [$rate], byte_rate: [$byte_rate], say_rate: [$say_rate], time_left: [$time_left]\n"; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
'time' => time,
next_report => $next_report,
}});
if (time > $next_report) if (time > $next_report)
{ {
#print "$THIS_FILE ".__LINE__."; say_downloaded: [$say_downloaded], percent: [$percent], say_rate: [$say_rate], running_time: [$running_time], say_running_time: [$say_running_time], seconds_left: [$seconds_left], say_time_left: [$say_time_left]\n"; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
#print "$file; Downloaded: [$say_downloaded]/[$say_percent], Rate/Avg: [$say_rate]/[$say_average_rate], Running: [$say_running_time], Left: [$say_time_left]\n"; say_downloaded => $say_downloaded,
#print "$THIS_FILE ".__LINE__."; bytes_downloaded=$bytes_downloaded, percent=$percent, current_rate=$byte_rate, average_rate=$average_rate, seconds_running=$running_time, seconds_left=$seconds_left, out_file=$out_file\n"; percent => $percent,
$next_report += $report_interval; say_rate => $say_rate,
running_time => $running_time,
say_running_time => $say_running_time,
seconds_left => $seconds_left,
say_time_left => $say_time_left,
say_percent => $say_percent,
say_average_rate => $say_average_rate,
average_rate => $average_rate,
}});
my $shell_call = $progress_file; my $line = "bytes_downloaded=$bytes_downloaded percent=$percent current_rate=$byte_rate average_rate=$average_rate seconds_running=$running_time seconds_left=$seconds_left url=$url out_file=$out_file";
$an->Log->entry({log_level => 3, message_key => "an_variables_0001", message_variables => { $next_report += $report_interval;
name1 => "shell_call", value1 => $shell_call, $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
}, file => $THIS_FILE, line => __LINE__}); line => $line,
open (my $file_handle, ">$shell_call") or $an->Alert->error({title_key => "an_0003", message_key => "error_title_0015", message_variables => { shell_call => $shell_call, error => $! }, code => 2, file => $THIS_FILE, line => __LINE__}); next_report => $next_report,
print $file_handle "uuid=$uuid bytes_downloaded=$bytes_downloaded percent=$percent current_rate=$byte_rate average_rate=$average_rate seconds_running=$running_time seconds_left=$seconds_left url=$url out_file=$out_file\n"; }});
close $file_handle; print $line."\n";
} }
} }
} }
close $file_handle; close $file_handle;
# We should have the file now. Move it to the final position.
if (($failed) or (not -e $temp_file))
{
# Something went wrong. Unlink the temp file.
if (-e $temp_file)
{
unlink $temp_file;
}
$anvil->nice_exit({exit_code => $failed});
}
else
{
# Move it (overwrite set because we'd be dead by now if '--overwrite' wasn't used.
my $failed = $anvil->Storage->move_file({
debug => 2,
source_file => $temp_file,
target_file => $save_to,
overwrite => 1,
});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { failed => $failed }});
if (($failed) or (not -e $out_file))
{
# Something went wrong.
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, priority => "err", key => "error_0060", variables => {
source_file => $temp_file,
target_file => $save_to,
}});
$anvil->nice_exit({exit_code => 8});
}
}
# Set it executable if '--script' was used.
if ($anvil->data->{switches}{script})
{
$anvil->Storage->change_mode({debug => 2, path => $out_file, mode => "a+x"});
}
# Tell the user that we're done.
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, , key => "log_0297", variables => { file => $out_file }});
return(0); return(0);
} }
sub download_file sub abort_download
{ {
my ($anvil) = @_; my ($anvil) = @_;

@ -834,7 +834,7 @@ sub handle_script
{ {
# Switch it on # Switch it on
$executable = 1; $executable = 1;
$anvil->Storage->change_mode({target => $anvil->data->{switches}{file}, mode => "a+x"}); $anvil->Storage->change_mode({path => $anvil->data->{switches}{file}, mode => "a+x"});
} }
else else
{ {
@ -848,7 +848,7 @@ sub handle_script
{ {
# Switch it off. # Switch it off.
$executable = 1; $executable = 1;
$anvil->Storage->change_mode({target => $anvil->data->{switches}{file}, mode => "a-x"}); $anvil->Storage->change_mode({path => $anvil->data->{switches}{file}, mode => "a-x"});
} }
else else
{ {

@ -601,8 +601,8 @@ sub setup_boot_environment
elsif ($kickstart_success eq "1") elsif ($kickstart_success eq "1")
{ {
# Updated # Updated
$anvil->Storage->change_mode({target => $kickstart_file, mode => "0664"}); $anvil->Storage->change_mode({path => $kickstart_file, mode => "0664"});
$anvil->Storage->change_owner({target => $kickstart_file, user => "apache", group => "apache" }); $anvil->Storage->change_owner({path => $kickstart_file, user => "apache", group => "apache" });
print $anvil->Words->string({key => "message_0097", variables => { file => $kickstart_file }})."\n"; print $anvil->Words->string({key => "message_0097", variables => { file => $kickstart_file }})."\n";
update_progress($anvil, $progress, "message_0097,!!file!".$kickstart_file."!!"); update_progress($anvil, $progress, "message_0097,!!file!".$kickstart_file."!!");

Loading…
Cancel
Save