mirror of
https://github.com/xcat2/xcat-core.git
synced 2026-09-04 20:17:55 +00:00
refactor(utils): centralize executable lookup
This commit is contained in:
@@ -363,6 +363,7 @@ then
|
||||
#3 symbolic link can't work during package
|
||||
if [ $file_low = "xcat-probe" ]; then
|
||||
mkdir -p ${CURDIR}/lib/perl/xCAT/
|
||||
cp -f ${CURDIR}/../perl-xCAT/xCAT/CommandUtils.pm ${CURDIR}/lib/perl/xCAT/
|
||||
cp -f ${CURDIR}/../perl-xCAT/xCAT/NetworkUtils.pm ${CURDIR}/lib/perl/xCAT/
|
||||
cp -f ${CURDIR}/../perl-xCAT/xCAT/GlobalDef.pm ${CURDIR}/lib/perl/xCAT/
|
||||
cp -f ${CURDIR}/../perl-xCAT/xCAT/ServiceNodeUtils.pm ${CURDIR}/lib/perl/xCAT/
|
||||
|
||||
@@ -141,6 +141,7 @@ sub usage {
|
||||
}
|
||||
|
||||
use constant XCAT_PROBE_HELPERS => qw(
|
||||
CommandUtils.pm
|
||||
GlobalDef.pm
|
||||
NetworkUtils.pm
|
||||
ServiceNodeUtils.pm
|
||||
|
||||
@@ -65,6 +65,7 @@ system('mkdir', '-p', map { "$ENV{HOME}/rpmbuild/$_" } qw(SOURCES SPECS BUILD BU
|
||||
my $VERSION = read_line("Version") // die "Cannot read Version\n";
|
||||
my $PWD = Cwd::cwd();
|
||||
my @XCAT_PROBE_HELPERS = qw(
|
||||
CommandUtils.pm
|
||||
GlobalDef.pm
|
||||
NetworkUtils.pm
|
||||
ServiceNodeUtils.pm
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package xCAT::CommandUtils;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
our @SYSTEM_FALLBACK_DIRS = qw(
|
||||
/usr/sbin
|
||||
/usr/bin
|
||||
/sbin
|
||||
/bin
|
||||
);
|
||||
|
||||
=head1 NAME
|
||||
|
||||
xCAT::CommandUtils - dependency-light command lookup helpers
|
||||
|
||||
=head1 FUNCTIONS
|
||||
|
||||
=head2 find_executable
|
||||
|
||||
Find an executable by searching PATH in order, followed by the standard system
|
||||
directories. Pass C<path =E<gt> $value> to search that value instead of the
|
||||
process PATH. Like the callers this function replaces, empty entries and an
|
||||
entry named C<0> are ignored. Pass C<fallback_dirs =E<gt> []> to disable the
|
||||
standard fallbacks, or supply an array reference to replace them. The matching
|
||||
candidate path is returned, or undef when none is found.
|
||||
|
||||
=cut
|
||||
|
||||
sub find_executable {
|
||||
my ( $command, %args ) = @_;
|
||||
|
||||
return unless defined($command) && length($command);
|
||||
|
||||
my $path = exists( $args{path} ) ? $args{path} : $ENV{PATH};
|
||||
foreach my $dir ( split /:/, $path || '' ) {
|
||||
next unless $dir;
|
||||
my $candidate = "$dir/$command";
|
||||
return $candidate if -x $candidate;
|
||||
}
|
||||
|
||||
my $fallback_dirs = exists( $args{fallback_dirs} )
|
||||
? $args{fallback_dirs}
|
||||
: \@SYSTEM_FALLBACK_DIRS;
|
||||
foreach my $dir ( @{ $fallback_dirs || [] } ) {
|
||||
next unless defined($dir) && length($dir);
|
||||
my $candidate = "$dir/$command";
|
||||
return $candidate if -x $candidate;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
1;
|
||||
@@ -3,6 +3,7 @@ package xCAT::DHCP::Backend;
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use xCAT::CommandUtils;
|
||||
use xCAT::StringUtils qw(trim);
|
||||
|
||||
my %valid_backend = map { $_ => 1 } qw(auto isc kea);
|
||||
@@ -142,18 +143,7 @@ sub _osver {
|
||||
|
||||
sub _command_exists {
|
||||
my ($command) = @_;
|
||||
|
||||
foreach my $dir ( split /:/, $ENV{PATH} || '' ) {
|
||||
next unless $dir;
|
||||
my $path = "$dir/$command";
|
||||
return 1 if -x $path;
|
||||
}
|
||||
|
||||
foreach my $path ( "/usr/sbin/$command", "/usr/bin/$command", "/sbin/$command", "/bin/$command" ) {
|
||||
return 1 if -x $path;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return xCAT::CommandUtils::find_executable($command) ? 1 : 0;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
@@ -8,6 +8,7 @@ use File::Basename;
|
||||
use File::Path qw/make_path/;
|
||||
use Math::BigInt;
|
||||
use Text::ParseWords qw/shellwords/;
|
||||
use xCAT::CommandUtils;
|
||||
use xCAT::DHCP::Range;
|
||||
use xCAT::NetworkUtils;
|
||||
|
||||
@@ -1206,18 +1207,7 @@ sub _expand_kea_build_path {
|
||||
|
||||
sub _command_path {
|
||||
my ($command) = @_;
|
||||
|
||||
foreach my $dir ( split /:/, $ENV{PATH} || '' ) {
|
||||
next unless $dir;
|
||||
my $path = "$dir/$command";
|
||||
return $path if -x $path;
|
||||
}
|
||||
|
||||
foreach my $path ( "/usr/sbin/$command", "/usr/bin/$command", "/sbin/$command", "/bin/$command" ) {
|
||||
return $path if -x $path;
|
||||
}
|
||||
|
||||
return;
|
||||
return xCAT::CommandUtils::find_executable($command);
|
||||
}
|
||||
|
||||
sub _shell_quote {
|
||||
|
||||
@@ -8,6 +8,7 @@ use File::Copy;
|
||||
use Time::Local;
|
||||
use Socket;
|
||||
use List::Util qw/sum/;
|
||||
use xCAT::CommandUtils;
|
||||
|
||||
#-----------------------------------------
|
||||
|
||||
@@ -175,11 +176,7 @@ sub _netplan_get {
|
||||
|
||||
sub _command_available {
|
||||
my $cmd = shift;
|
||||
for my $dir (split /:/, $ENV{PATH} || '') {
|
||||
next unless $dir;
|
||||
return 1 if -x "$dir/$cmd";
|
||||
}
|
||||
return 0;
|
||||
return xCAT::CommandUtils::find_executable( $cmd, fallback_dirs => [] ) ? 1 : 0;
|
||||
}
|
||||
|
||||
sub _capture_command {
|
||||
|
||||
@@ -24,6 +24,7 @@ use Getopt::Long;
|
||||
Getopt::Long::Configure("bundling");
|
||||
Getopt::Long::Configure("pass_through");
|
||||
use Socket;
|
||||
use xCAT::CommandUtils;
|
||||
my $candoipv6 = eval {
|
||||
require Socket6;
|
||||
1;
|
||||
@@ -3185,18 +3186,7 @@ sub local_ipv4_routes
|
||||
sub kea_command_path
|
||||
{
|
||||
my ($command) = @_;
|
||||
|
||||
foreach my $dir (split /:/, $ENV{PATH} || '') {
|
||||
next unless $dir;
|
||||
my $path = "$dir/$command";
|
||||
return $path if -x $path;
|
||||
}
|
||||
|
||||
foreach my $path ( "/usr/sbin/$command", "/usr/bin/$command", "/sbin/$command", "/bin/$command" ) {
|
||||
return $path if -x $path;
|
||||
}
|
||||
|
||||
return;
|
||||
return xCAT::CommandUtils::find_executable($command);
|
||||
}
|
||||
|
||||
sub kea_subnet4_intent
|
||||
|
||||
Reference in New Issue
Block a user