2022 twenty-four merry days of Perl Feed

Lapland Packaging Department - Part 2

Carton - 2022-12-21

After getting to work on his second day, Tonttu quickly downloaded the Carton module's tarball. This was the chosen module to make the installation and packaging of other modules and projects easier.

Carton is capable of dependency handling, can download the source tarballs for modules and last but not least, can create a portable package for any project. This package can be copied to any machine and Carton will install all modules automatically, without the need for an internet connection. The nisse started to do some testing and cooked up the following proof of concept for Santa for the first part:

Tonttu made sure first, that Carton is installed on all servers in the warehouse. He used the manual compilation method he had jotted down the day before.

After that he wrote the following guide for his busy elf colleagues:

Task 1. - Preparing the distribution

Create a cpanfile for your project, listing all the used modules (of course only the ones not part of the Perl core, but installed extra). List the minimum required Perl version for your project (if any) in the first line.

# cpanfile
requires 'perl', '5.20.0';
requires 'Getopt::Long';
requires 'Data::Dumper';
requires 'Log::Log4perl';
requires 'MIME::Lite';
requires 'MIME::Types';
requires 'Mail::Address';
requires 'Net::SCP';
requires 'JSON';
requires 'DBI';
requires 'Excel::Writer::XLSX';

As you can see, only a handful of modules are in there, Carton will automatically take care of the dependencies.

Next step is to carton install the modules inside the local/ directory.

$ carton install
Installing modules using /home/cabox/workspace/projects/test_carton/cpanfile
Successfully installed Archive-Zip-1.68
Successfully installed Excel-Writer-XLSX-1.09
Successfully installed Net-SSH-0.09
Successfully installed String-ShellQuote-1.04
Successfully installed Net-SCP-0.08
5 distributions installed
Complete! Modules were installed into /home/cabox/workspace/projects/test_carton/local

Notice that after this step a file named cpanfile.snapshot has been generated, with all the modules and dependencies managed by Carton. As You can see from the below example there are way more modules in this file than in our list in the cpanfile:

$ cat cpanfile.snapshot
# carton snapshot format: version 1.0
DISTRIBUTIONS
  Archive-Zip-1.68
    pathname: P/PH/PHRED/Archive-Zip-1.68.tar.gz
    provides:
      Archive::Zip 1.68
      Archive::Zip::Archive 1.68
      Archive::Zip::BufferedFileHandle 1.68
      Archive::Zip::DirectoryMember 1.68
      Archive::Zip::FileMember 1.68
      Archive::Zip::Member 1.68
      Archive::Zip::MemberRead 1.68
      Archive::Zip::MockFileHandle 1.68
      Archive::Zip::NewFileMember 1.68
      Archive::Zip::StringMember 1.68
      Archive::Zip::Tree 1.68
      Archive::Zip::ZipFileMember 1.68
    requirements:
      Compress::Raw::Zlib 2.017
      Encode 0
      ExtUtils::MakeMaker 0
      File::Basename 0
      File::Copy 0
      File::Find 0
      File::Path 0
      File::Spec 0.80
      File::Temp 0
      IO::File 0
      IO::Handle 0
      IO::Seekable 0
      Time::Local 0
      perl 5.006
  Excel-Writer-XLSX-1.09
    pathname: J/JM/JMCNAMARA/Excel-Writer-XLSX-1.09.tar.gz
    provides:
      Excel::Writer::XLSX 1.09
      Excel::Writer::XLSX::Chart 1.09
      Excel::Writer::XLSX::Chart::Area 1.09
      Excel::Writer::XLSX::Chart::Bar 1.09
      Excel::Writer::XLSX::Chart::Column 1.09
      Excel::Writer::XLSX::Chart::Doughnut 1.09
      Excel::Writer::XLSX::Chart::Line 1.09
      Excel::Writer::XLSX::Chart::Pie 1.09
      Excel::Writer::XLSX::Chart::Radar 1.09
      Excel::Writer::XLSX::Chart::Scatter 1.09
      Excel::Writer::XLSX::Chart::Stock 1.09
      Excel::Writer::XLSX::Chartsheet 1.09
      Excel::Writer::XLSX::Drawing 1.09
      Excel::Writer::XLSX::Examples 1.09
      Excel::Writer::XLSX::Format 1.09
      Excel::Writer::XLSX::Package::App 1.09
      Excel::Writer::XLSX::Package::Comments 1.09
      Excel::Writer::XLSX::Package::ContentTypes 1.09
      Excel::Writer::XLSX::Package::Core 1.09
      Excel::Writer::XLSX::Package::Custom 1.09
      Excel::Writer::XLSX::Package::Metadata 1.09
      Excel::Writer::XLSX::Package::Packager 1.09
      Excel::Writer::XLSX::Package::Relationships 1.09
      Excel::Writer::XLSX::Package::SharedStrings 1.09
      Excel::Writer::XLSX::Package::Styles 1.09
      Excel::Writer::XLSX::Package::Table 1.09
      Excel::Writer::XLSX::Package::Theme 1.09
      Excel::Writer::XLSX::Package::VML 1.09
      Excel::Writer::XLSX::Package::XMLwriter 1.09
      Excel::Writer::XLSX::Shape 1.09
      Excel::Writer::XLSX::Utility 1.09
      Excel::Writer::XLSX::Workbook 1.09
      Excel::Writer::XLSX::Worksheet 1.09
    requirements:
      Archive::Zip 1.3
      ExtUtils::MakeMaker 0
      File::Temp 0.19
      IO::File 1.14
  Net-SCP-0.08.reprise
    pathname: I/IV/IVAN/Net-SCP-0.08.reprise.tar.gz
    provides:
      Net::SCP 0.08
    requirements:
      ExtUtils::MakeMaker 0
      IO::Handle 0
      Net::SSH 0
      String::ShellQuote 0
  Net-SSH-0.09
    pathname: I/IV/IVAN/Net-SSH-0.09.tar.gz
    provides:
      Net::SSH 0.09
    requirements:
      ExtUtils::MakeMaker 0
  String-ShellQuote-1.04
    pathname: R/RO/ROSCH/String-ShellQuote-1.04.tar.gz
    provides:
      String::ShellQuote 1.04
    requirements:
      ExtUtils::MakeMaker 0

Don't forget to exclude the local/ directory and add the files cpanfile and cpanfile.snapshot to your version control system!

At this point we have a package, which is not really portable. To make it portable, run

$ carton bundle
Bundling modules using /home/cabox/workspace/projects/test_carton/cpanfile
Copying P/PH/PHRED/Archive-Zip-1.68.tar.gz
Copying J/JM/JMCNAMARA/Excel-Writer-XLSX-1.09.tar.gz
Copying I/IV/IVAN/Net-SCP-0.08.reprise.tar.gz
Copying I/IV/IVAN/Net-SSH-0.09.tar.gz
Copying R/RO/ROSCH/String-ShellQuote-1.04.tar.gz
Writing /home/cabox/workspace/projects/test_carton/vendor/cache/modules/02packages.details.txt.gz
Complete! Modules were bundled into /home/cabox/workspace/projects/test_carton/vendor/cache
It will bundle your tar archives into the vendor/ directory.

Add vendor to your VCS or don't, it's up to you.

Now we are ready to deploy the package to a different machine.

Task 2: Deploying your Package to the Remote Machines

Copy the whole directory to the desired directory on the target systems.

This is the directory structure you should see (without your own stuff):

.
|-- cpanfile
|-- cpanfile.snapshot
|-- ... #Your stuff here
|-- ... #Your stuff here
`-- vendor
 `-- cache
     |-- authors
     `-- modules

Now it's time to install the modules using Carton (which has been previously installed by Tonttu):

$ carton install --deployment cached

This will use the module tarballs from the vendor/ directory and create local/ with the installed modules.

If therea are no errors, you may remove the vendor/ directory (optional).

Santa reviewed the proof of concept and was proud of Tonttu for doing such a great job.

After sharing the how-to, all the elves started to use the Carton packaging module and their job got a lot easier.

Santa was cheerful and satisfied, as the whole Packaging Department could fulfill more children's wishes much much faster than before.

Gravatar Image This article contributed by: Csaba Simándi <x1m4nd1@gmail.com>