Deploying Ruby applications

Overview

This document is a hands-on guide to deploying a simple Ruby application intsuru. The example application will be a very simple Rails project associatedto a MySQL service.

Creating the app

To create an app, you use the command app-create:

  1. $ tsuru app-create <app-name> <app-platform>

For Ruby, the app platform is ruby! Let’s be over creative and develop anever-developed tutorial-app: a blog, and its name will also be very creative,let’s call it “blog”:

  1. $ tsuru app-create blog ruby

To list all available platforms, use the command platform-list.

You can see all your applications using the command app-list:

  1. $ tsuru app-list
  2. +-------------+-------------------------+-------------+
  3. | Application | Units State Summary | Address |
  4. +-------------+-------------------------+-------------+
  5. | blog | 0 of 0 units in-service | |
  6. +-------------+-------------------------+-------------+

Application code

This document will not focus on how to write a blog with Rails, you can clonethe entire source direct from GitHub:https://github.com/tsuru/tsuru-ruby-sample. Here is what we did for theproject:

  • Create the project (rails new blog)
  • Generate the scaffold for Post (rails generate scaffold Post title:string body:text)

Git deployment

When you create a new app, tsuru will display the Git remote that you shoulduse. You can always get it using the command app-info:

  1. $ tsuru app-info --app blog
  2. Application: blog
  3. Repository: git@192.168.50.4.nip.io:blog.git
  4. Platform: ruby
  5. Teams: admin
  6. Address: blog.192.168.50.4.nip.io
  7. Owner: admin@example.com
  8. Team owner: admin
  9. Deploys: 0
  10. Pool: theonepool
  11.  
  12. App Plan:
  13. +---------------+--------+------+-----------+---------+
  14. | Name | Memory | Swap | Cpu Share | Default |
  15. +---------------+--------+------+-----------+---------+
  16. | autogenerated | 0 MB | 0 MB | 100 | false |
  17. +---------------+--------+------+-----------+---------+

The Git remote will be used to deploy your application using Git. You can justpush to tsuru remote and your project will be deployed:

  1. $ git push git@192.168.50.4.nip.io:blog.git master
  2. Counting objects: 86, done.
  3. Delta compression using up to 4 threads.
  4. Compressing objects: 100% (75/75), done.
  5. Writing objects: 100% (86/86), 29.75 KiB, done.
  6. Total 86 (delta 2), reused 0 (delta 0)
  7. remote: Cloning into '/home/application/current'...
  8. remote: requirements.apt not found.
  9. remote: Skipping...
  10. remote: /home/application/current /
  11. remote: Fetching gem metadata from https://rubygems.org/.........
  12. remote: Fetching gem metadata from https://rubygems.org/..
  13. #####################################
  14. # OMIT (see below) #
  15. #####################################
  16. remote: ---> App will be restarted, please check its log for more details...
  17. remote:
  18. To git@192.168.50.4.nip.io:blog.git
  19. * [new branch] master -> master

If you get a “Permission denied (publickey).”, make sure you’re member of ateam and have a public key added to tsuru. To add a key, use the command key-add:

  1. $ tsuru key-add mykey ~/.ssh/id_rsa.pub

You can use git remote add to avoid typing the entire remote url every timeyou want to push:

  1. $ git remote add tsuru git@192.168.50.4.nip.io:blog.git

Then you can run:

  1. $ git push tsuru master
  2. Everything up-to-date

And you will be also able to omit the —app flag from now on:

  1. $ tsuru app-info
  2. Application: blog
  3. Repository: git@192.168.50.4.nip.io:blog.git
  4. Platform: ruby
  5. Teams: admin
  6. Address: blog.192.168.50.4.nip.io
  7. Owner: admin@example.com
  8. Team owner: admin
  9. Deploys: 0
  10. Pool: theonepool
  11. Units: 1
  12. +------------+---------+
  13. | Unit | State |
  14. +------------+---------+
  15. | eab5151eff | started |
  16. +------------+---------+
  17.  
  18. App Plan:
  19. +---------------+--------+------+-----------+---------+
  20. | Name | Memory | Swap | Cpu Share | Default |
  21. +---------------+--------+------+-----------+---------+
  22. | autogenerated | 0 MB | 0 MB | 100 | false |
  23. +---------------+--------+------+-----------+---------+

Listing dependencies

In the last section we omitted the dependencies step of deploy. In tsuru, anapplication can have two kinds of dependencies:

  • Operating system dependencies, represented by packages in the package managerof the underlying operating system (e.g.: yum and apt-get);
  • Platform dependencies, represented by packages in the package manager of theplatform/language (in Ruby, bundler).
    All apt-get dependencies must be specified in a requirements.apt file,located in the root of your application, and ruby dependencies must be locatedin a file called Gemfile, also in the root of the application. Since wewill use MySQL with Rails, we need to install mysql package using gem,and this package depends on an apt-get package: libmysqlclient-dev, sohere is how requirements.apt looks like:
  1. libmysqlclient-dev

And here is Gemfile:

  1. source 'https://rubygems.org'
  2.  
  3. gem 'rails', '3.2.13'
  4. gem 'mysql'
  5. gem 'sass-rails', '~> 3.2.3'
  6. gem 'coffee-rails', '~> 3.2.1'
  7. gem 'therubyracer', platforms: 'ruby'
  8. gem 'uglifier', '>= 1.0.3'
  9. gem 'jquery-rails'

You can see the complete output of installing these dependencies below:

  1. $ git push tsuru master
  2. #####################################
  3. # OMIT #
  4. #####################################
  5. remote: Reading package lists...
  6. remote: Building dependency tree...
  7. remote: Reading state information...
  8. remote: The following extra packages will be installed:
  9. remote: libmysqlclient18 mysql-common
  10. remote: The following NEW packages will be installed:
  11. remote: libmysqlclient-dev libmysqlclient18 mysql-common
  12. remote: 0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
  13. remote: Need to get 2360 kB of archives.
  14. remote: After this operation, 9289 kB of additional disk space will be used.
  15. remote: Get:1 http://archive.ubuntu.com/ubuntu/ quantal/main mysql-common all 5.5.27-0ubuntu2 [13.7 kB]
  16. remote: Get:2 http://archive.ubuntu.com/ubuntu/ quantal/main libmysqlclient18 amd64 5.5.27-0ubuntu2 [949 kB]
  17. remote: Get:3 http://archive.ubuntu.com/ubuntu/ quantal/main libmysqlclient-dev amd64 5.5.27-0ubuntu2 [1398 kB]
  18. remote: Fetched 2360 kB in 2s (1112 kB/s)
  19. remote: Selecting previously unselected package mysql-common.
  20. remote: (Reading database ... 41063 files and directories currently installed.)
  21. remote: Unpacking mysql-common (from .../mysql-common_5.5.27-0ubuntu2_all.deb) ...
  22. remote: Selecting previously unselected package libmysqlclient18:amd64.
  23. remote: Unpacking libmysqlclient18:amd64 (from .../libmysqlclient18_5.5.27-0ubuntu2_amd64.deb) ...
  24. remote: Selecting previously unselected package libmysqlclient-dev.
  25. remote: Unpacking libmysqlclient-dev (from .../libmysqlclient-dev_5.5.27-0ubuntu2_amd64.deb) ...
  26. remote: Setting up mysql-common (5.5.27-0ubuntu2) ...
  27. remote: Setting up libmysqlclient18:amd64 (5.5.27-0ubuntu2) ...
  28. remote: Setting up libmysqlclient-dev (5.5.27-0ubuntu2) ...
  29. remote: Processing triggers for libc-bin ...
  30. remote: ldconfig deferred processing now taking place
  31. remote: /home/application/current /
  32. remote: Fetching gem metadata from https://rubygems.org/..........
  33. remote: Fetching gem metadata from https://rubygems.org/..
  34. remote: Using rake (10.1.0)
  35. remote: Using i18n (0.6.1)
  36. remote: Using multi_json (1.7.8)
  37. remote: Using activesupport (3.2.13)
  38. remote: Using builder (3.0.4)
  39. remote: Using activemodel (3.2.13)
  40. remote: Using erubis (2.7.0)
  41. remote: Using journey (1.0.4)
  42. remote: Using rack (1.4.5)
  43. remote: Using rack-cache (1.2)
  44. remote: Using rack-test (0.6.2)
  45. remote: Using hike (1.2.3)
  46. remote: Using tilt (1.4.1)
  47. remote: Using sprockets (2.2.2)
  48. remote: Using actionpack (3.2.13)
  49. remote: Using mime-types (1.23)
  50. remote: Using polyglot (0.3.3)
  51. remote: Using treetop (1.4.14)
  52. remote: Using mail (2.5.4)
  53. remote: Using actionmailer (3.2.13)
  54. remote: Using arel (3.0.2)
  55. remote: Using tzinfo (0.3.37)
  56. remote: Using activerecord (3.2.13)
  57. remote: Using activeresource (3.2.13)
  58. remote: Using coffee-script-source (1.6.3)
  59. remote: Using execjs (1.4.0)
  60. remote: Using coffee-script (2.2.0)
  61. remote: Using rack-ssl (1.3.3)
  62. remote: Using json (1.8.0)
  63. remote: Using rdoc (3.12.2)
  64. remote: Using thor (0.18.1)
  65. remote: Using railties (3.2.13)
  66. remote: Using coffee-rails (3.2.2)
  67. remote: Using jquery-rails (3.0.4)
  68. remote: Installing libv8 (3.11.8.17)
  69. remote: Installing mysql (2.9.1)
  70. remote: Using bundler (1.3.5)
  71. remote: Using rails (3.2.13)
  72. remote: Installing ref (1.0.5)
  73. remote: Using sass (3.2.10)
  74. remote: Using sass-rails (3.2.6)
  75. remote: Installing therubyracer (0.11.4)
  76. remote: Installing uglifier (2.1.2)
  77. remote: Your bundle is complete!
  78. remote: Gems in the groups test and development were not installed.
  79. remote: It was installed into ./vendor/bundle
  80. #####################################
  81. # OMIT #
  82. #####################################
  83. To git@192.168.50.4.nip.io:blog.git
  84. 9515685..d67c3cd master -> master

Running the application

As you can see, in the deploy output there is a step described as “Restartingyour app”. In this step, tsuru will restart your app if it’s running, or startit if it’s not. But how does tsuru start an application? That’s very simple, ituses a Procfile (a concept stolen from Foreman). In this Procfile, you describehow your application should be started. Here is how the Procfile should look like:

  1. web: bundle exec rails server -p $PORT -e production

Now we commit the file and push the changes to tsuru Git server, runninganother deploy:

  1. $ git add Procfile
  2. $ git commit -m "Procfile: added file"
  3. $ git push tsuru master
  4. #####################################
  5. # OMIT #
  6. #####################################
  7. remote: ---> App will be restarted, please check its log for more details...
  8. remote:
  9. To git@192.168.50.4.nip.io:blog.git
  10. d67c3cd..f2a5d2d master -> master

Now that the app is deployed, you can access it from your browser, getting theIP or host listed in app-list and opening it. For example,in the list below:

  1. $ tsuru app-list
  2. +-------------+-------------------------+---------------------+
  3. | Application | Units State Summary | Address |
  4. +-------------+-------------------------+---------------------+
  5. | blog | 1 of 1 units in-service | blog.cloud.tsuru.io |
  6. +-------------+-------------------------+---------------------+

Deployment hooks

It would be boring to manually run rake db:migrate after every deployment.So we can configure an automatic hook to always run before or afterthe app restarts.

tsuru parses a file called tsuru.yaml and runs restart hooks. As theextension suggests, this is a YAML file, that contains a list of commands thatshould run before and after the restart. Here is our example of tsuru.yaml:

  1. hooks:
  2. restart:
  3. before:
  4. - RAILS_ENV=production bundle exec rake db:migrate

For more details, check the hooks documentation.

tsuru will look for the file in the root of the project. Let’s commit anddeploy it:

  1. $ git add tsuru.yaml
  2. $ git commit -m "tsuru.yaml: added file"
  3. $ git push tsuru master
  4. #####################################
  5. # OMIT #
  6. #####################################
  7. To git@192.168.50.4.nip.io:blog.git
  8. a780de9..1b675b8 master -> master

It is necessary to compile de assets before the app restart. To do it we canuse the rake assets:precompile command. Then let’s add the command tocompile the assets in tsuru.yaml:

  1. hooks:
  2. build:
  3. - RAILS_ENV=production bundle exec rake assets:precompile
  1. $ git add tsuru.yaml
  2. $ git commit -m "tsuru.yaml: added file"
  3. $ git push tsuru master
  4. #####################################
  5. # OMIT #
  6. #####################################
  7. To git@192.168.50.4.nip.io:blog.git
  8. a780de9..1b675b8 master -> master

It’s done! Now we have a Rails project deployed on tsuru.

Now we can access your blog app in the URL returned in app-info.

Going further

For more information, you can dig into the tsuru docs, orread the complete instructions on how to use the tsuru command.

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