📜 ⬆️ ⬇️

The easiest deploy application on Ruby on Rails


Six months ago, I wrote a Deploy application post on RoR 4 using Capistrano 3 . Time passed, I received a lot of positive reviews, but there were also negative ones. From them it was possible to understand the following:

I thought and wrote gem 'capistrano3-ubuntu-server-config' , which completely sets up your “clean” Ubuntu server. All you have to do with your hands is to create a new user and give him visudo rights (and you don’t need to give him the rights to passwordless sudo). He can:

You can run the configuration wizard, which will know what needs to be done from the above and will ask all the settings in advance so that you can go and drink coffee, or you can run individual tasks. This gem will be useful not only to Rails developers, but to everyone who uses Capistrano for deployment.

This article will cover the following topics:


gem 'capistrano3-ubuntu-server-config'

What can do this gem, I have already told. Let's go directly to work with him. Imagine that we have a clean web server on Ubuntu (I tested on Ubuntu 14.04). We need to do only two simple things ourselves: create a new user with sudo rights and provide passwordless access from your local machine to the server via SSH. Let's start with the first, on the server, logged in as root perform:
 adduser deployer echo "deployer ALL=(ALL) ALL" >> /etc/sudoers 

Instead of deployer there can be any username. It would not hurt to even change the password of the root user with the passwd command.

Now we will provide passwordless login from the local machine to the server via ssh. To do this, execute it on the local machine (where depoyer is the user name, 111.111.111.111 is the server address):
 ssh-copy-id deployer@111.111.111.111 

That's where the server setup is complete. In an ideal situation, you no longer have to log in via ssh to the server. To view logs, I recommend gem 'tail' .
Let's start using my gem'a. Add to Gemfile:
 group :development do gem 'capistrano' gem 'capistrano3-ubuntu-server-prepare' end 

We execute bundle install, cap install, we add a line
 require 'capistrano3/ubuntu-server-prepare' 
in capfile.
')
Almost everything is ready to go. Except for one thing: to configure Nginx and Redis, my script takes .conf files from the config / production / nginx and config / production / redis folders. To quickly copy my configuration files to these folders, just run:
 rake ubuntu_server_prepare:copy_config 

Bonus also get customized Unicorn config. There are two files in the nginx folder: nginx.conf and nginx_with_pagespeed.conf. The second is used when selecting the pagespeed installation in the configurator.

Attention! my nginx and unicorn configuration file! configured on a Rails application that is in '/ var / www / application / current'. Change all paths in these files or just add a line.
 set :application, 'application' 
in your deploy.rb for deployment to this folder.

In 'config / deploy / production.rb', you need to register your server, and also make sure that the line in Capfile
 require 'capistrano/rvm' 
was commented out .

Now we proceed to the most delicious:
 cap production ubuntu_server_prepare 

The configurator will ask you a lot of questions, having received the answers to which, will be engaged in setting up the server. This process is moderately long, so you can go to drink coffee.

It is possible to run individual tasks, for example, by running
 cap production ubuntu_server_prepare:nginx_conf 
You copy the nginx.conf configuration file to the server and reboot nginx. This is convenient to quickly change the config: changed something right in the project folder and sent it to the server with one command.

gem 'capistrano3-git-push'

A small task for Capistrano 3, performing
 git add -A git commit -m "#{}" git push 
only in case there are changes. If you enter “skip” in the field for requesting a message about a commit, nothing will be done, which is convenient when you need to deploy, but you do not need to upload changes to the repository.
Connect is easy. In the gemfile:
 group :development do gem 'capistrano3-git-push' end 

In Capfile:
 require 'capistrano3/git-push' 

In deploy.rb:
 before :deploy, 'git:push' 


My current configuration is Capistrano

If you recall my previous article , then my deploy.rb was just huge. Now my configuration is simple to madness.
Gemfile
 group :development do gem 'capistrano' gem 'capistrano-rails' gem 'capistrano-bundler' gem 'capistrano3-unicorn' gem 'capistrano-rvm' gem 'capistrano3-ubuntu-server-prepare' gem 'capistrano3-delayed-job' end group :production do gem 'unicorn' end 

Capfile
 # Load DSL and set up stages require 'capistrano/setup' require 'capistrano/deploy' require 'capistrano3/ubuntu-server-prepare' require 'capistrano3/unicorn' require 'capistrano3/git-push' require 'capistrano/rvm' require 'capistrano/bundler' require 'capistrano/rails' 

deploy.rb
 set :application, 'application' set :repo_url, "#{__}" set :unicorn_config_path, "#{current_path}/config/production/unicorn/unicorn.rb" set :linked_dirs, fetch(:linked_dirs, []).push('bin', 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system') #      deploy.rb,     namespace :deploy do task :setup do before "deploy:migrate", :create_db invoke :deploy end task :create_db do on roles(:all) do within release_path do with rails_env: fetch(:rails_env) do execute :rake, "db:create" end end end end task :restart do invoke 'unicorn:legacy_restart' end end before :deploy, 'git:push' before 'deploy:setup', 'git:push' 


That is, you first need to do everything that I described at the beginning of the article, then once
 cap production deploy:setup 
to create a database. All subsequent times we perform
 cap production deploy 


It is probably worth explaining what is happening here.
What's going on here?
 set :unicorn_config_path, "#{current_path}/config/production/unicorn/unicorn.rb" 
Sets the location of the unicorn config for the gem 'capistrano3-unicorn'.

 set :linked_dirs, fetch(:linked_dirs, []).push('bin', 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system') 
Creates symlinks to specified folders from current folder to shared folder.

 task :setup do before "deploy:migrate", :create_db invoke :deploy end 
Calls: create_db, before executing 'db: migrate' during the first deploy (setup:).

 task :create_db do on roles(:all) do within release_path do with rails_env: fetch(:rails_env) do execute :rake, "db:create" end end end end 
The same task: create_db, which calls 'rake db: create' on the first delay.

  task :restart do invoke 'unicorn:legacy_restart' end 
restart unicorn for every delay.


PS If you use resque, you should look at the gem 'capistrano-resque' , if you use delayedjob, you should look at the gem 'capistrano3-delayed-job' .

Source: https://habr.com/ru/post/246011/


All Articles