| Line 42... |
Line 42... |
| 42 |
# changed so even if quiet set, if dryrun is set, will print what is returned from the called script
|
42 |
# changed so even if quiet set, if dryrun is set, will print what is returned from the called script
|
| 43 |
# Also, incorporates v1.3.0 on cluster.pm and v1.2.1 on havirt.pm
|
43 |
# Also, incorporates v1.3.0 on cluster.pm and v1.2.1 on havirt.pm
|
| 44 |
#
|
44 |
#
|
| 45 |
# v1.2.2 20250511 RWR
|
45 |
# v1.2.2 20250511 RWR
|
| 46 |
# Added variance flag (--variance), and fixed the version flag (--version or -V)
|
46 |
# Added variance flag (--variance), and fixed the version flag (--version or -V)
|
| - |
|
47 |
#
|
| - |
|
48 |
# v1.2.3 20260102 RWR
|
| - |
|
49 |
# Fixed module loading to use absolute paths from $FindBin::RealBin so havirt can be run from any directory
|
| - |
|
50 |
# Override config paths after reading YAML to ensure correct script directory regardless of working directory
|
| 47 |
|
51 |
|
| 48 |
#use experimental "switch";
|
52 |
#use experimental "switch";
|
| 49 |
|
53 |
|
| 50 |
# requires File::Slurp.
|
54 |
# requires File::Slurp, File::LibXML, YAML::Tiny, Data::Dumper
|
| 51 |
# In Debian derivatives
|
55 |
# In Debian derivatives
|
| 52 |
# apt install libfile-slurp-perl
|
56 |
# apt install libfile-slurp-perl libxml-libxml-perl libyaml-tiny-perl libdata-dumper-perl
|
| 53 |
|
57 |
|
| 54 |
# apt install libxml-libxml-perl libyaml-tiny-perl
|
- |
|
| 55 |
|
58 |
|
| 56 |
|
59 |
|
| 57 |
BEGIN {
|
60 |
BEGIN {
|
| 58 |
use FindBin;
|
61 |
use FindBin;
|
| 59 |
use File::Spec;
|
62 |
use File::Spec;
|
| Line 69... |
Line 72... |
| 69 |
use YAML::Tiny;
|
72 |
use YAML::Tiny;
|
| 70 |
|
73 |
|
| 71 |
# define the version number
|
74 |
# define the version number
|
| 72 |
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
|
75 |
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
|
| 73 |
use version;
|
76 |
use version;
|
| 74 |
our $VERSION = version->declare("1.2.2");
|
77 |
our $VERSION = version->declare("1.2.3");
|
| 75 |
|
78 |
|
| 76 |
|
79 |
|
| 77 |
# see https://perldoc.perl.org/Getopt/Long.html
|
80 |
# see https://perldoc.perl.org/Getopt/Long.html
|
| 78 |
use Getopt::Long;
|
81 |
use Getopt::Long;
|
| 79 |
# allow -vvn (ie, --verbose --verbose --dryrun)
|
82 |
# allow -vvn (ie, --verbose --verbose --dryrun)
|
| Line 106... |
Line 109... |
| 106 |
}
|
109 |
}
|
| 107 |
|
110 |
|
| 108 |
&makeConfig( $config,$configFileName ) unless -f $configFileName;
|
111 |
&makeConfig( $config,$configFileName ) unless -f $configFileName;
|
| 109 |
$config = readConfig( $configFileName );
|
112 |
$config = readConfig( $configFileName );
|
| 110 |
|
113 |
|
| - |
|
114 |
# Override script dir to always use the actual script location
|
| - |
|
115 |
$config->{'script dir'} = $FindBin::RealBin;
|
| - |
|
116 |
$config->{'db dir'} = $config->{'script dir'} . '/var';
|
| - |
|
117 |
$config->{'conf dir'} = $config->{'script dir'} . '/conf';
|
| - |
|
118 |
$config->{'status db filename'} = $config->{'script dir'} . '/var/status.yaml';
|
| - |
|
119 |
$config->{'last scan filename'} = $config->{'script dir'} . '/var/lastscan';
|
| - |
|
120 |
|
| 111 |
# handle any command line parameters that may have been passed in
|
121 |
# handle any command line parameters that may have been passed in
|
| 112 |
|
122 |
|
| 113 |
GetOptions (
|
123 |
GetOptions (
|
| 114 |
$config->{'flags'},
|
124 |
$config->{'flags'},
|
| 115 |
'debug|d+', # integer, can be incremented like -ddd
|
125 |
'debug|d+', # integer, can be incremented like -ddd
|
| Line 173... |
Line 183... |
| 173 |
my $execute = $command . '::' . $action;
|
183 |
my $execute = $command . '::' . $action;
|
| 174 |
|
184 |
|
| 175 |
# Load the module dynamically, die if it doesn't exist
|
185 |
# Load the module dynamically, die if it doesn't exist
|
| 176 |
my $module_path = $config->{'script dir'} . "/$command.pm";
|
186 |
my $module_path = $config->{'script dir'} . "/$command.pm";
|
| 177 |
if ( -f $module_path ) {
|
187 |
if ( -f $module_path ) {
|
| 178 |
require "$command.pm";
|
188 |
require $module_path;
|
| 179 |
Module->import($command); # For require, must manually import
|
189 |
Module->import($command); # For require, must manually import
|
| 180 |
} else {
|
190 |
} else {
|
| 181 |
die "Error, I don't know the command [$command]\n";
|
191 |
die "Error, I don't know the command [$command]\n";
|
| 182 |
}
|
192 |
}
|
| 183 |
|
193 |
|
| 184 |
if ( defined &{\&{$execute}} ) { # check if module::sub exists (ie, $command::action)
|
194 |
if ( defined &{\&{$execute}} ) { # check if module::sub exists (ie, $command::action)
|
| - |
|
195 |
print "running [$action] in $command.pm:$action\n with parameters " . join(", ", @ARGV) . "\n" if $main::config->{'flags'}->{'debug'};
|
| 185 |
my $message = &{\&{$execute}}(@ARGV); # yes, it exists, so call it with any remaining arguments
|
196 |
my $message = &{\&{$execute}}(@ARGV); # yes, it exists, so call it with any remaining arguments
|
| 186 |
print $message unless !$message || $main::config->{'flags'}->{'quiet'} && !$main::config->{'flags'}->{'dryrun'};
|
197 |
print $message unless !$message || $main::config->{'flags'}->{'quiet'} && !$main::config->{'flags'}->{'dryrun'};
|
| 187 |
} else { # method $action does not exist in module $command, so just a brief error message
|
198 |
} else { # method $action does not exist in module $command, so just a brief error message
|
| 188 |
die "Error, could not find action [$action] for module [$command]\n";
|
199 |
die "Error, could not find action [$action] for module [$command]\n";
|
| 189 |
}
|
200 |
}
|