Application Deployment

This document provides a high-level description on how application deploymentworks on tsuru.

Preparing Your Application

If you follow the 12 Factor app principles youshouldn’t have to change your application in order to deploy it on tsuru. Hereis what an application need to go on a tsuru cloud:

- Well defined requirements, both, on language level and operational systemlevel- Configuration of external resources using environment variables- A Procfile to tell how your process should be run

Let’s go a little deeper through each of those topics.

1. Requirements

Every well writen application nowdays has well defined dependencies. In Python,everything is on a requirements.txt or like file, in Ruby, they go on Gemfile,Node.js has the package.json, and so on. Some of those dependencies also haveoperational system level dependencies, like the Nokogiri Ruby gem orMySQL-Python package, tsuru bootstraps units as clean as possible, so you alsohave to declare those operational system requirements you need on a file calledrequirements.apt. This files should have the packages declared one per-lineand look like that:

  1. python-dev
  2. libmysqlclient-dev

If you need to add new repositories for installing system level dependencies,create a file called repositories.apt, with a repository per line.

2. Configuration With Environment Variables

Everything that vary between deploys (on different environments, likedevelopment or production) should be managed by environment variables. tsurutakes this principle very seriously, so all services available for usage intsuru that requires some sort of configuration does it via environmentvariables so you have no pain while deploying on different environments usingtsuru.

For instance, if you are going to use a database service on tsuru, like MySQL,when you bind your application into the service, tsuru will receive from theservice API everything you need to connect with MySQL, e.g: user name,password, url and database name. Having this information, tsuru will export onevery unit your application has the equivalent environment variables with theirvalues. The names of those variables are defined by the service providing them,in this case, the MySQL service.

Let’s take a look at the settings of tsuru hosted application built with Django:

  1. import os
  2.  
  3. DATABASES = {
  4. "default": {
  5. "ENGINE": "django.db.backends.mysql",
  6. "NAME": os.environ.get("MYSQLAPI_DB_NAME"),
  7. "USER": os.environ.get("MYSQLAPI_DB_USER"),
  8. "PASSWORD": os.environ.get("MYSQLAPI_DB_PASSWORD"),
  9. "HOST": os.environ.get("MYSQLAPI_HOST"),
  10. "PORT": "",
  11. }
  12. }

You might be asking yourself “How am I going to know those variables names?”,but don’t fear! When you bind your application with tsuru, it’ll return allvariables the service asked tsuru to export on your application’s units(without the values, since you are not gonna need them), if you lost theenvironments on your terminal history, again, don’t fear! You can always checkwhich service made what variables available to your application using thetsuru env-get command.

原文: https://docs.tsuru.io/1.6/using/deployment.html