|
Line 0
Link Here
|
|
|
1 |
package Koha::Perl::Instance; |
| 2 |
|
| 3 |
# Koha::Perl::Instance - Shared Perl utilities for koha-* debian scripts |
| 4 |
# Copyright 2026 LMSCloud GmbH |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
use Modern::Perl; |
| 20 |
|
| 21 |
use Exporter qw( import ); |
| 22 |
our @EXPORT_OK = qw( in_koha_env get_instance_perl5lib reexec_in_koha_env ); |
| 23 |
|
| 24 |
=head1 NAME |
| 25 |
|
| 26 |
Koha::Perl::Instance - Shared utilities for Koha debian scripts |
| 27 |
|
| 28 |
=head1 SYNOPSIS |
| 29 |
|
| 30 |
use lib '/usr/share/koha/bin'; |
| 31 |
use Koha::Perl::Instance qw( in_koha_env reexec_in_koha_env ); |
| 32 |
|
| 33 |
# Re-exec with Koha environment if not already set up |
| 34 |
if ( !in_koha_env() ) { |
| 35 |
my $instance = pop @ARGV; |
| 36 |
reexec_in_koha_env({ instance => $instance, argv => \@ARGV }); |
| 37 |
} |
| 38 |
|
| 39 |
=head1 DESCRIPTION |
| 40 |
|
| 41 |
This module provides shared utilities for Perl-based koha-* debian scripts |
| 42 |
that need to run within a Koha instance's environment. |
| 43 |
|
| 44 |
=head1 FUNCTIONS |
| 45 |
|
| 46 |
=head2 in_koha_env |
| 47 |
|
| 48 |
if ( in_koha_env() ) { ... } |
| 49 |
|
| 50 |
Returns true if the script is running within a Koha environment |
| 51 |
(i.e., KOHA_CONF is set). |
| 52 |
|
| 53 |
=cut |
| 54 |
|
| 55 |
sub in_koha_env { |
| 56 |
return defined $ENV{KOHA_CONF} && $ENV{KOHA_CONF} ne q{}; |
| 57 |
} |
| 58 |
|
| 59 |
=head2 get_instance_perl5lib |
| 60 |
|
| 61 |
my $perl5lib = get_instance_perl5lib($instance); |
| 62 |
|
| 63 |
Returns the appropriate PERL5LIB path for the given Koha instance. |
| 64 |
Handles both package installs and git/dev installs. |
| 65 |
|
| 66 |
=cut |
| 67 |
|
| 68 |
sub get_instance_perl5lib { |
| 69 |
my ($instance) = @_; |
| 70 |
|
| 71 |
my $koha_conf = "/etc/koha/sites/$instance/koha-conf.xml"; |
| 72 |
my $perl5lib; |
| 73 |
|
| 74 |
# Check if this is a git install |
| 75 |
my $is_git = `bash -c '. /usr/share/koha/bin/koha-functions.sh; if is_git_install $instance; then echo 1; fi'`; |
| 76 |
chomp $is_git; |
| 77 |
|
| 78 |
if ($is_git) { |
| 79 |
|
| 80 |
# For git installs, get path from koha-conf.xml |
| 81 |
$perl5lib = `xmlstarlet sel -t -v 'yazgfs/config/intranetdir' $koha_conf 2>/dev/null`; |
| 82 |
chomp $perl5lib; |
| 83 |
$perl5lib = "$perl5lib:$perl5lib/lib" if $perl5lib; |
| 84 |
} |
| 85 |
|
| 86 |
if ( !$perl5lib ) { |
| 87 |
|
| 88 |
# Fall back to system default |
| 89 |
$perl5lib = `grep "^PERL5LIB=" /etc/default/koha-common 2>/dev/null`; |
| 90 |
chomp $perl5lib; |
| 91 |
$perl5lib =~ s/^PERL5LIB=\s*//; |
| 92 |
$perl5lib =~ s/^"//; |
| 93 |
$perl5lib =~ s/"$//; |
| 94 |
} |
| 95 |
|
| 96 |
return $perl5lib; |
| 97 |
} |
| 98 |
|
| 99 |
=head2 reexec_in_koha_env |
| 100 |
|
| 101 |
reexec_in_koha_env({ |
| 102 |
instance => $instance, |
| 103 |
argv => \@ARGV, |
| 104 |
script => $0, |
| 105 |
}); |
| 106 |
|
| 107 |
Re-executes the current script with the proper Koha environment set up. |
| 108 |
This function does not return on success. |
| 109 |
|
| 110 |
Arguments are passed directly to exec() without shell interpolation, |
| 111 |
avoiding quoting issues. |
| 112 |
|
| 113 |
Parameters: |
| 114 |
|
| 115 |
=over 4 |
| 116 |
|
| 117 |
=item instance |
| 118 |
|
| 119 |
The Koha instance name (required). |
| 120 |
|
| 121 |
=item argv |
| 122 |
|
| 123 |
Array ref of arguments to pass to the re-executed script (default: current @ARGV). |
| 124 |
|
| 125 |
=item script |
| 126 |
|
| 127 |
Path to the script to execute (default: $0). |
| 128 |
|
| 129 |
=back |
| 130 |
|
| 131 |
Dies with an error message if: |
| 132 |
|
| 133 |
=over 4 |
| 134 |
|
| 135 |
=item * instance is not provided |
| 136 |
|
| 137 |
=item * instance does not exist |
| 138 |
|
| 139 |
=item * exec fails |
| 140 |
|
| 141 |
=back |
| 142 |
|
| 143 |
=cut |
| 144 |
|
| 145 |
sub reexec_in_koha_env { |
| 146 |
my ($args) = @_; |
| 147 |
|
| 148 |
my $instance = $args->{instance}; |
| 149 |
my $argv = $args->{argv} // \@ARGV; |
| 150 |
my $script = $args->{script} // $0; |
| 151 |
|
| 152 |
die "Usage: sudo $0 [options] instancename\n" unless $instance; |
| 153 |
die "Usage: sudo $0 [options] instancename\n" if $instance =~ /^-/; |
| 154 |
die "Error: Koha instance '$instance' does not exist.\n" |
| 155 |
unless -e "/etc/koha/sites/$instance"; |
| 156 |
|
| 157 |
my $perl5lib = get_instance_perl5lib($instance); |
| 158 |
my $koha_conf = "/etc/koha/sites/$instance/koha-conf.xml"; |
| 159 |
|
| 160 |
exec( |
| 161 |
'/usr/bin/sudo', '-u', "$instance-koha", '-H', |
| 162 |
'env', "PERL5LIB=$perl5lib", "KOHA_CONF=$koha_conf", |
| 163 |
'/usr/bin/perl', $script, @{$argv} |
| 164 |
); |
| 165 |
die "Failed to exec: $!\n"; |
| 166 |
} |
| 167 |
|
| 168 |
1; |
| 169 |
|
| 170 |
__END__ |
| 171 |
|
| 172 |
=head1 AUTHOR |
| 173 |
|
| 174 |
Paul Derscheid <paul.derscheid@lmscloud.de> |
| 175 |
|
| 176 |
=head1 COPYRIGHT |
| 177 |
|
| 178 |
Copyright LMSCloud GmbH |
| 179 |
|
| 180 |
=head1 LICENSE |
| 181 |
|
| 182 |
This file is part of Koha. |
| 183 |
|
| 184 |
Koha is free software; you can redistribute it and/or modify it |
| 185 |
under the terms of the GNU General Public License as published by |
| 186 |
the Free Software Foundation; either version 3 of the License, or |
| 187 |
(at your option) any later version. |
| 188 |
|
| 189 |
Koha is distributed in the hope that it will be useful, but |
| 190 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 191 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 192 |
GNU General Public License for more details. |
| 193 |
|
| 194 |
You should have received a copy of the GNU General Public License |
| 195 |
along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 196 |
|
| 197 |
=cut |