Automate Perl Module Deployment
By Martin C. Brown2005-04-27
Use CPAN to Create a Rigid Installation
The problem with both of the previous solutions is that they rely on using CPAN and some manipulation of the configuration file to get CPAN to do the work. CPAN is not an ideal solution within a local area network (LAN), even with the above tricks. Therefore, you need to bypass those system limitations. To do so, I'll show you how to configure the master distribution, create an installation set for each platform, and then run the installation on each computer.
Configure the master distributionFor this first step, you're going to use some of the elements we've already covered. Begin by configuring one computer to use CPAN as normal and setting up your default set of modules. Next, use NFS to set up a shared directory that you'll use to distribute the necessary files to the other computers in the network. Now, edit the $destdir variable you see in Listing 4, then run the Listing 4 script. This script does three things:
• Identifies the modules
• Downloads the source files
• Copies the source files to the distribution directory
The first stage determines the list of installed modules by examining the content of the perllocal.pod file. This file is updated each time a module is installed using the MakeMaker system. By using this file rather than the built-in modules in CPAN, you get a list of third-party modules installed with your base Perl installation. The second stage uses CPAN to download the source tarball by whatever methods are configured within CPAN itself before finally copying the tarball or .zip file. As part of that process, you also create a list of the modules that were copied, which you'll use when you install the files on the other computers.
Listing 4. Code to create a base distribution
use CPAN;You now have a directory containing all the source installation packages that you can use to build a suite of ready-compiled distributions for each platform to which you need to distribute the corresponding Perl module. How you proceed from there will depend on your environment. If you have a homogeneous environment, you need to perform the next stage just once for your entire network. If, however, you have a heterogeneous network with a variety of different platforms, you'll need to repeat the next step for each different environment.
use Config;
use File::Copy;
use File::Spec::Functions;
# Set up the directories to store the module packages
my $basedestdir = catfile('export','cpaninst');
mkdir($basedestdir);
my $destdir = catfile($basedestdir,'srcs');
mkdir($destdir);
# Extract a list of the third party modules installed on this machine
my $files = {};
my $podfile = catfile($Config{'archlibexp'},'perllocal.pod');
open(DATA,$podfile) or die "Can't open module list ($podfile): $!";
while(<DATA>)
{
if (m/.*C<Module> L<(.*)>/)
{
my ($module) = split /\|/,$1,0;
my $mod = expand('Module',$module);
next unless $mod;
$mod->get(); #Make sure to download the version again, in case we no
#longer have it locally
$files->{$mod->{RO}->{CPAN_FILE}} = 1; #Save the location of the file
}
}
close(DATA);
# Now copy each source installer to the source destination directory
# We save a copy of the module file, for reference, as part of the process
my $modulelist = catfile($basedestdir,'modules.lst');
open(DATA,">$modulelist") or die "Can't open the module list file ($modulelist): $!";
foreach my $file (keys %{$files})
{
my $src = catfile($CPAN::Config->{'keep_source_where'},$file);
my ($vol,$path,$filename) = splitpath($file);
my $dest = catfile($destdir,$filename);
copy($src,$dest) or warn "Copy of $src failed: $!\n";
print DATA "$filename\n";
}
close(DATA);
Create an installation set for each platform
For each platform, you need to extract the source file, run MakeMaker, then run the installer. To perform this task automatically, use the script in Listing 5. Unfortunately, not all MakeMaker installations are completely automatic, so you'll need to follow the interactive systems -- for example, to set directory locations or default options -- each time you build the distribution.
Listing 5. Code to build the modules on each platform
srcdir="/export/cpan"Run the installation on each computerWhen the script in Listing 5 has run, you'll have a single directory that contains all the "ready-to-install" modules for a given platform. To install these modules on any other computer using the same platform, simply change into each directory and run make install. Again, you can use a script such as the one in Listing 6 for this process.
srcfile="$srcdir/module.lst"
platformdir="/export/cpan/solaris-x86"
for file in `cat $srcfile`
do
cd $platformdir
tar zxf $srcdir/$file
dir=`echo $file|sed -e "s/\.tar\.gz//g"`
cd $dir
perl Makefile.PL
make
done
Listing 6. Code to install the files on each host
srcdir="/export/cpan"
srcfile="$srcdir/module.lst"
platformdir="/export/cpan/solaris-x86"
for file in `cat $srcfile`
do
cd $platformdir
tar zxf $srcdir/$file
dir=`echo $file|sed -e "s/\.tar\.gz//g"`
cd $dir
make install
done
Tutorial Pages:
» Scripts go Beyond CPAN to Ease Network Installations
» Perl Module Installation
» Automating CPAN
» Automating CPAN Across a Network
» Use CPAN to Create a Rigid Installation
» Conclusions
» Resources
First published by IBM DeveloperWorks
| Related Tutorials: » Random subroutines in Perl » Log Script Use » Creating Perl Modules for Web Sites » Bit Vector, Using Perl Vec » Build a Perl/CGI Voting System » Perl Range Operator |
