sclorg / s2i-perl-container

Perl container images based on Red Hat Software Collections and intended for OpenShift and general usage, that provide a platform for building and running Perl applications. Users can choose between Red Hat Enterprise Linux, Fedora, and CentOS based images.
http://softwarecollections.org
Apache License 2.0
16 stars 56 forks source link

Installed perl modules are not accessible in script #154

Open frenzymind opened 5 years ago

frenzymind commented 5 years ago

I have script begins with: `#!/usr/bin/perl

use strict; use warnings;

use Math::Round; use utf8; use XML::LibXML::Reader; `

cpanfile file: requires 'Math::Round', '== 0.06'; requires 'XML::LibXML::Reader', '== 2.0132';

Both located in tests2i folder, inside this folder I run command: s2i build . centos/perl-526-centos7 test-cos

Build log here:

---> Installing application source ... ---> Installing modules from cpanfile ... --> Working on Module::CoreList Fetching http://www.cpan.org/authors/id/B/BI/BINGOS/Module-CoreList-5.20181020.tar.gz ... OK Configuring Module-CoreList-5.20181020 ... OK Building Module-CoreList-5.20181020 ... OK Successfully installed Module-CoreList-5.20181020 (upgraded from 5.20171120) 1 distribution installed --> Working on . Configuring /opt/app-root/src ... OK ==> Found dependencies: XML::LibXML::Reader, Math::Round --> Working on XML::LibXML::Reader Fetching http://www.cpan.org/authors/id/S/SH/SHLOMIF/XML-LibXML-2.0132.tar.gz ... OK Configuring XML-LibXML-2.0132 ... OK ==> Found dependencies: XML::SAX, XML::SAX::Exception, XML::SAX::Base, XML::NamespaceSupport --> Working on XML::SAX Fetching http://www.cpan.org/authors/id/G/GR/GRANTM/XML-SAX-1.00.tar.gz ... OK Configuring XML-SAX-1.00 ... OK ==> Found dependencies: XML::NamespaceSupport, XML::SAX::Base --> Working on XML::NamespaceSupport Fetching http://www.cpan.org/authors/id/P/PE/PERIGRIN/XML-NamespaceSupport-1.12.tar.gz ... OK Configuring XML-NamespaceSupport-1.12 ... OK Building XML-NamespaceSupport-1.12 ... OK Successfully installed XML-NamespaceSupport-1.12 --> Working on XML::SAX::Base Fetching http://www.cpan.org/authors/id/G/GR/GRANTM/XML-SAX-Base-1.09.tar.gz ... OK Configuring XML-SAX-Base-1.09 ... OK Building XML-SAX-Base-1.09 ... OK Successfully installed XML-SAX-Base-1.09 Building XML-SAX-1.00 ... OK Successfully installed XML-SAX-1.00 Building XML-LibXML-2.0132 ... OK Successfully installed XML-LibXML-2.0132 --> Working on Math::Round Fetching http://backpan.perl.org/authors/id/G/GR/GROMMEL/Math-Round-0.06.tar.gz ... OK Configuring Math-Round-0.06 ... OK Building Math-Round-0.06 ... OK Successfully installed Math-Round-0.06 <== Installed dependencies for .. Finishing. 5 distributions installed Build completed successfully

It seems ok, dependencies found and installed. I run container: docker run -d -p 8080:8080 test-cos

and exec bash. Inside I try: perl zapret.pl

thats give error:

Can't locate Math/Round.pm in @INC (you may need to install the Math::Round module) (@INC contains: /opt/rh/rh-perl526/root/usr/local/lib64/perl5 /opt/rh/rh-perl526/root/usr/local/share/perl5 /opt/rh/rh-perl526/root/usr/lib64/perl5/vendor_perl /opt/rh/rh-perl526/root/usr/share/perl5/vendor_perl /opt/rh/rh-perl526/root/usr/lib64/perl5 /opt/rh/rh-perl526/root/usr/share/perl5) at zapret.pl line 6. BEGIN failed--compilation aborted at zapret.pl line 6.

What do I do wrong ?

pkubatrh commented 5 years ago

Hi @frenzymind I see

  export PATH=${PATH}:/opt/app-root/src/extlib/bin
  export PERL5LIB=/opt/app-root/src/extlib/lib/perl5

in the s2i run script which made your use case work for me. Though I would expect to find this in assemble instead. @ppisar might have more info for you.

frenzymind commented 5 years ago

@pkubatrh thank you man! I just found the same solution. But I look at httpd.conf and there is string: PerlSwitches -I./extlib/lib/perl5

and here I understand thing I have to do.

ppisar commented 5 years ago

Why assemble does not define PERL5LIB:

Assemble script is executed only when building an application image. Environment variables exported in the assemble script do not survive to run time. Therefore the PERL5LIB variable is set in the run script.

Assemble script does not use PERL5LIB variable not to infect cpanm installer with user-installed modules. cpanm tool obtains the path using "-l" option.

That means we do not need PERL5LIB in the assemble script.

Why "docker exec" does not have PERL5LIB:

"docker exec" executes a program into a pristine environemnt. Thus PERL5LIB does not exist there.

If we defined PERL5LIB in /opt/app-root/etc/scl_enable that's executed by bash when execing into a container, we would taint the environment and Perl configuration in centos/perl-526-centos7 base image with references specific and valid only for s2i application images. But we have to support also non-s2i use cases with centos/perl-526-centos7. If we did so we would divert perl's behevior from pristine SCL perl.

Clean solution would be not to use PERL5LIB at all and install modules into SCL perl's site directory, but there is contadicting requirment from OpenShift that all user's content must be installed under /opt/app-root. Thus we have to use PERL5LIB enviroment variable to explain Perl where to look the user-installed modules.

I'm sorry but I cannot see any solution that would not break some of the use cases. But now when I thinking about it more, I have an idea:

Maybe we could modify /opt/app-root/etc/scl_enable to define PERL5LIB at the end of the assemble script. Assemble script is executed only in s2i use case when bulding an application image. Thus it would not affect non-s2t use cases.