Creating a platform

Overview

If you need a platform that’s not already available in our platforms repository it’s pretty easy to create a new onebased on a existing one.

Platforms are Docker images that are used to deploy your application code on tsuru. tsuru provides abase image which platform developers can use to build upon: base-platform.This platform provides a base deployment script, which handles package downloading andextraction in proper path, along with operating system package management.

Every platform in the repository extends the base-platformadding the specifics of each platform. They are a great way to learn how to create a new one.

An example

Let’s supose we wanted to create a nodejs platform. First, let’s define it’s Dockerfile:

  1. FROM tsuru/base-platform
  2. ADD . /var/lib/tsuru/nodejs
  3. RUN cp /var/lib/tsuru/nodejs/deploy /var/lib/tsuru
  4. RUN /var/lib/tsuru/nodejs/install

In this file, we are extending the tsuru/base-platform, adding our deploy and installscripts to the right place: /var/lib/tsuru.

The install script runs when we add or update the platform on tsuru. It’s the perfect placeto install dependencies that every application on it’s platform needs:

  1. #!/bin/bash -le
  2.  
  3. SOURCE_DIR=/var/lib/tsuru
  4. source ${SOURCE_DIR}/base/rc/config
  5.  
  6. apt-get update
  7. apt-get install git -y
  8. git clone https://github.com/creationix/nvm.git /etc/nvm
  9. cd /etc/nvm && git checkout `git describe --abbrev=0 --tags`
  10.  
  11. cat >> ${HOME}/.profile <<EOF
  12. if [ -e ${HOME}/.nvm_bin ]; then
  13. export PATH="${HOME}/.nvm_bin:$PATH"
  14. fi
  15. EOF

As it can be seen, we are just installing some dependencies and preparing the environment for our applications.The ${SOURCE_DIR}/base/rc/config provides some bootstrap configuration that are usually needed.

Now, let’s define our deploy script, which runs every time a deploy occurs:

  1. #!/bin/bash -le
  2.  
  3. SOURCE_DIR=/var/lib/tsuru
  4. source ${SOURCE_DIR}/base/rc/config
  5. source ${SOURCE_DIR}/base/deploy
  6.  
  7. export NVM_DIR=${HOME}/.nvm
  8. [ ! -e ${NVM_DIR} ] && mkdir -p ${NVM_DIR}
  9.  
  10. . /etc/nvm/nvm.sh
  11.  
  12. nvm install stable
  13.  
  14. rm -f ~/.nvm_bin
  15. ln -s $NVM_BIN ~/.nvm_bin
  16.  
  17. if [ -f ${CURRENT_DIR}/package.json ]; then
  18. pushd $CURRENT_DIR && npm install --production
  19. popd
  20. fi

Once again we run some base scripts to do some heavy lifting: ${SOURCE_DIR}/base/rc/config and${SOURCE_DIR}/base/deploy. After that, it’s just a matter of application specifics dependencies usingnpm.

Now, we can move on and add our newly created platform.

Adding your platform to tsuru

After creating you platform as a Dockerfile, you can add it to tsuru using the client:

  1. $ tsuru platform-add your-platform-name --dockerfile http://url-to-dockerfile

If you push your image to an Docker Registry, you can use:

  1. $ tsuru platform-add your-platform-name -i your-user/image-name

原文: https://docs.tsuru.io/1.6/managing/create-platform.html