Monday, June 22, 2015

Deploy Rails 4.1 App with Sunzi and Capistrano 3 Notes from Screencast

1. Ignore database.yml and secrets.yml by adding it to .gitignore
2. gem install sunzi (Does not have to be in Gemfile)
3. cd config
4. sunzi create 
5. In a temp directory : git clone git@github.com:crslade/sunzi-recipes.git sunzi-template
6. In the project directory

cd config/sunzi/
cp ~/temp/sunzi-template/files/* files
cp ~/temp/sunzi-template/recipes/* recipes
cp ~/temp/sunzi-template/sunzi.yml .
cp ~/temp/sunzi-template/install.sh .

7. sunzi.yml has password. Do not checkin into git. Same thing for deploy_key.

Add sunzi.yml and deploy_key to config/sunzi/.gitignore

# Ignore sensitive data
sunzi.yml
files/deploy_key

8. [todo/config/sunzi]

cp sunzi.yml sunzi.sample.yml
cp files/deploy_key files/deploy_key.sample (Deploy user can login without password)

9. cp ~/.ssh/id_rsa.pub files/deploy_key 

10. define database name, password, deploy_user = deploy, app_name=todo in sunzi.yml

11. Manual Steps. Some of these can be automated. Since it is one time thing, it is ok to do it manually.

Login to VPS. Create a linode instance by selecting the plan, location. Add the public ssh key of laptop so that you can login to the instance as a root user.


   $cat ~/.ssh/id_rsa.pub 
   $root@192.155.81.222 
   #mkdir .ssh
   #paste id_rsa.pub content in .ssh/authorized_keys
   #chown -R root:root .ssh
   #chmod 700 .ssh
   #chmod 600 .ssh/authorized_keys
   #exit
   $ssh root@192.155.81.222 
   should not prompt for any password
   
   AUTOMATE THIS TASK LATER.
   
   
12. Copy the IP address from your Linode dashboard and ssh root@ip-address 

You should be able to login into the instance without providing any password.

13. sunzi compile

14.  sunzi deploy ip-address

This will take 10 to 15 minutes.

15. Hit the IP address on the browser. You will see the nginx static page.

16. ssh deploy@ip-address should take into the instance without requiring any password.

17. github.com/talkingquickly/capistrano-3-rails-template copy the clone url and download this project.

[temp]$ git clone url cap-template

18. 

[todo] Gemfile

# Use unicorn as the app server
gem 'unicorn'

# Use Capistrano for deployment
group :development do
 gem 'capistrano-rails'
 gem 'capistrano-bundler'
 gem 'capistrano-rbenv', "~> 2.0"
end

Refer the : https://github.com/crslade/todo-sunzi-deploy or temp/todo-sunzi-deploy

bundle install

19. 

bundle exec cap install

Capfile :

# Load DSL and Setup Up Stages
require 'capistrano/setup'

# Includes default deployment tasks
require 'capistrano/deploy'

# Includes tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
#   https://github.com/capistrano/rvm
#   https://github.com/capistrano/rbenv
#   https://github.com/capistrano/chruby
#   https://github.com/capistrano/bundler
#   https://github.com/capistrano/rails
#
# require 'capistrano/rvm'
require 'capistrano/rbenv'
# require 'capistrano/chruby'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'

# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
Dir.glob('lib/capistrano/**/*.rb').each { |r| import r }

20. In project root directory:

cp ~/temp/cap-template/lib/capistrano/*.rb lib/capistrano
cp ~/temp/cap-template/lib/capistrano/tasks/* lib/capistrano/tasks
cp ~/temp/cap-template/config/deploy.rb config/deploy.rb
cp ~/temp/cap-template/config/deploy/production.rb config/deploy/production.rb
mkdir config/deploy/shared
cp ~/temp/cap-template/config/deploy/shared/* config/deploy/shared

21. Edit deploy.rb

application name
repo url
ruby version

linked_files secrets.yml
config_files secrets.yml

22. 

Create deploy/shared/secrets.yml.erb

Copy secrets.yml into the erb file and retain only production.

23. Find out how to setup staging server. Leave staging.rb as it is for now.

24. In deploy/production.rb

Specify the server_name as IP address or as domain name if DNS is already setup. The IP also goes into server line.

25. cap production deploy:setup_config

26. ssh deploy@ip-add

cd apps/todo_prod/shared/config

cp secrets.sample.yml to secrets.yml
cp database.sample.yml database.yml

THIS STEP CAN BE DONE USING SSHKIT SCRIPT OR WRAP SSHKIT TASK IN A RAKE TASK.

27. 

commit all changes to git and push it to bitbucket.

28.

cap production deploy

29.

Reload the webpage. The application should be running.

30. To troubleshoot:

Unicorn log files. Output of deployment. nginx log files and app log files.