diff --git a/build-ubunturepo b/build-ubunturepo index bc92165ee..8a36a17e4 100755 --- a/build-ubunturepo +++ b/build-ubunturepo @@ -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/ diff --git a/build-utils/lib/XCAT/BuildUtils.pm b/build-utils/lib/XCAT/BuildUtils.pm index 86bedd846..e0deac209 100644 --- a/build-utils/lib/XCAT/BuildUtils.pm +++ b/build-utils/lib/XCAT/BuildUtils.pm @@ -141,6 +141,7 @@ sub usage { } use constant XCAT_PROBE_HELPERS => qw( + CommandUtils.pm GlobalDef.pm NetworkUtils.pm ServiceNodeUtils.pm diff --git a/buildrpms.pl b/buildrpms.pl index 70c1c3e8b..1c8b86990 100755 --- a/buildrpms.pl +++ b/buildrpms.pl @@ -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 diff --git a/perl-xCAT/xCAT/CommandUtils.pm b/perl-xCAT/xCAT/CommandUtils.pm new file mode 100644 index 000000000..91229a377 --- /dev/null +++ b/perl-xCAT/xCAT/CommandUtils.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 $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 []> 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; diff --git a/perl-xCAT/xCAT/DHCP/Backend.pm b/perl-xCAT/xCAT/DHCP/Backend.pm index 7ba20339b..95db9e15c 100644 --- a/perl-xCAT/xCAT/DHCP/Backend.pm +++ b/perl-xCAT/xCAT/DHCP/Backend.pm @@ -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; diff --git a/perl-xCAT/xCAT/DHCP/Backend/Kea.pm b/perl-xCAT/xCAT/DHCP/Backend/Kea.pm index d4a54ba9d..1cf064669 100644 --- a/perl-xCAT/xCAT/DHCP/Backend/Kea.pm +++ b/perl-xCAT/xCAT/DHCP/Backend/Kea.pm @@ -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 { diff --git a/xCAT-probe/lib/perl/probe_utils.pm b/xCAT-probe/lib/perl/probe_utils.pm index 7e32742d8..c91e76f5b 100644 --- a/xCAT-probe/lib/perl/probe_utils.pm +++ b/xCAT-probe/lib/perl/probe_utils.pm @@ -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 { diff --git a/xCAT-server/lib/xcat/plugins/dhcp.pm b/xCAT-server/lib/xcat/plugins/dhcp.pm index 0c7babdf1..e6ddc1eba 100644 --- a/xCAT-server/lib/xcat/plugins/dhcp.pm +++ b/xCAT-server/lib/xcat/plugins/dhcp.pm @@ -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