Skip to content
Leo Kotschenreuther edited this page Aug 6, 2016 · 3 revisions

How to build your own custom buildpack for cloud foundry

This is a guide on how to create custom buildpacks for cloud foundry. Using the python-pdf2htmlEX-buildpack as an example, we will go through each step we need to take.

First of all, start off with a preexisting buildpack wherever possible. Since want to have python in our buildpack, we copied the python-buildpack. It includes already everything we need to use python. If you push your app to cloud foundry using this buildpack, it will automatically install all the requirements specified in your requirements.txt file.

So now let's see how we can add another program to our buildpack, in this case pdf2htmlEX, which converts pdfs to html files.

Step 1: Install the program on a Ubuntu 14.04 system

At first we need to get the binaries for pdf2htmlEX. Since the cloud foundry container is based on a Ubuntu 14.04 system, we are going to install pdf2htmlEX on a Ubuntu 14.04 system. Sometimes you can just install your program with apt-get. Unfortunately pdf2htmlEX is not part of the Ubuntu repositories, so we are going to compile it on our own.

At first we are installing all the dependencies we can install with apt-get:

sudo apt-get update && sudo apt-get install -qq git cmake autotools-dev libjpeg-dev libtiff4-dev libpng12-dev libgif-dev libxt-dev autoconf automake libtool bzip2 libxml2-dev libuninameslist-dev libspiro-dev python-dev libpango1.0-dev libcairo2-dev chrpath uuid-dev uthash-dev libopenjpeg-dev

Next we install poppler (for some reason I was not able to compile the newest version, so we are using version 0.33.0):

wget http://poppler.freedesktop.org/poppler-0.33.0.tar.xz
tar -xvf poppler-0.33.0.tar.xz
cd poppler-0.33.0/
./configure --enable-xpdf-headers
make
sudo make install
cd ..

Next we install fontforge:

git clone https://github.com/coolwanglu/fontforge.git fontforge.git
cd fontforge.git
git checkout pdf2htmlEX
./autogen.sh
./configure
make
sudo make install

Finally we can install pdf2htmlEX:

git clone git://github.com/coolwanglu/pdf2htmlEX.git
cd pdf2htmlEX
cmake .
make
sudo make install

Step 2: Collect all needed binaries, dependencies and other files

Now that pdf2htmlEX is installed on our Ubuntu, we have to collect all required files. These are the pdf2htmlEX binary, some dependencies and the data directory (js and html files that are required by pdf2htmlEX). We find all of them inside the /usr directory. If you are not sure where to find them, use the following command: find /usr -name filename. It searches for the specified file (named filename in this case) in the specified directory (which is /usr in our case). All the files we need for pdf2htmlEX are:

  • pdf2htmlEX
  • libfontforge.so.2
  • libgif.so.4
  • libgunicode.so.4
  • libgutils.so.2
  • libopenjpeg.so.2
  • libpoppler.so.52
  • libpython2.7.so.1.0
  • libspiro.so.0
  • libuninameslist.so.0
  • all the files in /usr/local/share/pdf2htmlEX

In the end we package them all together into a tar.gz file and put it into the top level directory of the buildpack. The tar.gz file contains a bin directory with the pdf2htmlEX binary, a lib directory with all the .so files and a data-dir directory with the files from /usr/local/share/pdf2htmlEX.

Step 3: Extend the compile script of the buildpack

In this case we have a compile script in the bin directory of the buildpack. Since the creators of the python buildpack created different steps and one script for each step, we added the compile file for pdf2htmlEX. Don't forget to call the new step from the main compile script!

At first we unpack the tar.gz file into our installation directory:

mkdir -p $INSTALL_DIR
tar -zxvf $ROOT_DIR/$PDF2HTMLEX_TGZ -C $INSTALL_DIR

Now we just have to update the PATH and LD_LIBRARYP_PATH variables so that the system is able to find our binary and it's dependencies. We write the export commands to the ENVSCRIPT which get's executed whenever we push an application using this buildpack in cloud foundry.

mkdir -p $BUILD_DIR/.profile.d
echo "export PATH=\"$PDF2HTMLEX_DIR/bin:\$PATH\"" > $ENVSCRIPT
echo "export LD_LIBRARY_PATH=\"$PDF2HTMLEX_DIR/lib:\$LD_LIBRARY_PATH\"" >> $ENVSCRIPT

Now we can run pdf2htmlEX from our python app as a system command.

Clone this wiki locally