Ruby On Rails: Deploy on Engineyard server

Platform as a service (PaaS) is a category of cloud computing services that provides a platform allowing customers to develop, run and manage Web applications without the complexity of building and maintaining the infrastructure typically associated with developing and launching an app.[1]

Features of Engineyard PaaS[2]

Managed Application Runtime


Engine Yard provides user with a pre-integrated, pre-tested Ruby on Rails technology stack, including web, application and database servers, built-in monitoring and process management, a Rails-optimized linux distribution, in-memory caches and more. AWS helps Engine Yard provide the flexibility, scale, global reach, and rapid time to market that Engine Yard’s customers require. [3]

Monitoring & Alerting

Engine Yard can track the resource (storage, CPU and memory) utilization alerts users when they need additional capacity, or when the users’ applications are behaving poorly.

Seamless Application Capacity Management

When users add an additional instance to applications, Engine Yard can automatically deploy with the correct application configuration.

Things you need

– Engine Yard Account

I am assuming that you have already setup you Engineyard account. You have created your application and initialized an environment i.e. staging/production. If you are having trouble to setup your deployment  server visit this link and to create an environment visit this link.

Firstly, you need to boot your server and then there are various ways you can deploy your ruby projects to Engineyard VPC.

GUI based deploy

Click deploy to copy the RSA public key and create a new SSH key in your github/other account settings

Click ‘Deploy Key’  >  Copy the SSH key > Create a new SSH key in your github/other account settings

EY Dashboard snapshot

Here you can specify the repo-branch from where EY is supposed to pull your source codes.

Click the ‘Deploy’ button to deploy the application. It may take few minutes depending upon the server you choose. Single instance sever like mine is the slowest.

CLI Based Deploy

Engineyard Command Line Interface

Install

Install engineyard like any other ruby gem:

$ gem install engineyard

Initialize a repository for deployment on Engine Yard Cloud.

$ ey init

This command writes or updates an ey.yml file which explains options available for customizing the deployment process.

Configuration

The ey.yml file allows options to be saved for each environment to which an application is deployed.

A typical Rails application will have a config/ey.yml like this:

---
# This is all you need for a typical rails application.
defaults:
  migrate: true
  migration_command: rake db:migrate
  precompile_assets: true

Now you need to Share your SSH public key with you Engineyard account

Share SSH Key

Open you public key file inside ~/.ssh folder ~/.ssh/id_rsa.pub (normally, may be different in your case) and copy the content and create a new SSH key in EY

Click the 'Add a new SSH public key' button to create new SSH key. This key is used to make SSH connection to the server during deployment

Click the ‘Add a new SSH public key’ button to create new SSH key. This key is used to make SSH connection to the server during deployment

Paster the content of ~/.ssh/id_rsa.pub or any .pub file in the text area.

Copy the content of ~/.ssh/id_rsa.pub or any .pub file to the text area.

Click the 'Apply' button to make the newly added SSH keys work.

Click the ‘Apply’ button to make the newly added SSH keys work.

Now you can use deploy command

# You must be inside your local repo
#   i.e. 
#   $ cd /path/to/your/repo/
#   Your local repo and EY Cloud must have common remote repo

$ ey deploy -e=your_app_environment
# Example
#   $ ey deploy -e=your_app_staging

Deployment will be done for simple application.

What Engineyard already does?

– installed MySql server and client and created database.yml files inside  /data/[your_app]/shared/config/database.yml

– installs ruby, rails

# in app_root/deploy/before_compile_assets.rb
on_app_servers_and_utilities do
  # Copy the yaml files from `shared/config` to `current/config`
  ['application.yml', 'database.yml'].each do |file_name|
    run "ln -nfs #{config.shared_path}/config/#{file_name} #{config.release_path}/config/#{file_name}"
  end
end

Now, if you have some background processors needed like sidekiq, resque you need to restart the sidekiq/resque server every time your deploy some new code. And if unfortunately your sidekiq/resque server crashes then you need to restart them manually. Therefore to make that task easy we can use deploy hook  deploy/before_restart.rb to make sure these sever restart after your application server restarts.

# deploy/before_restart.rb
on_app_servers_and_utilities do
  worker_count = 1 # please replace your own value
  (0...worker_count).each do |i|
    sudo "/engineyard/bin/sidekiq #{config.app} stop #{config.framework_env} #{i}"
  end
  # Wait 20s for sidekiq to process or send back to redis-server
  sudo "sleep 20s ; monit start all -g #{config.app}_sidekiq"
end

EY Cloud Recipes[4]

The ey-cloud-recipes repository is a collection of chef cookbooks that setup and configure commonly used tools for ruby applications, as well as cookbooks that can be used to modify parts of the EY Cloud environment.

Quick Start Guide[5]

  1. Clone this repository
  2. Uncomment the recipes that you wish to use in cookbooks/main/recipes/default.rb
  3. Make any changes that are mentioned in the individual cookbook’s readme.md file
  4. Install the engineyard gem, if you haven’t already (gem install engineyard).
  5. Upload your recipes to EY Cloud using ey recipes upload -e ENVIRONMENT, where ENVIRONMENT is the name of your environment.
  6. Run your recipes on the environment using ey recipes apply -e ENVIRONMENT
When uploading of recipes is completed, you can see the message in the dashboard.

When uploading of recipes is completed, you can see the message in the dashboard.

Seeding Data

If you have some rows of records to set after your server is setup, you have to seed data you set in /db/seed.rb. You can set to run the command in any deploy hook however, there is no any hook that executes only on our will. So, we need to execute the command using SSH connection.

$ cd path/to/your/app/in/localrepo
$ ey ssh
# You get logged in into the server via SSH
user@server.ip $ cd /data/[app_name]/current
user@server.ip /data/[app_name]/current $ bundle exec rake db:seed
# If Mysql2::Error: Duplicate entry occurs, do following
user@server.ip /data/[app_name]/current $ bundle exec rails c
# Delete records from tables you want using Model.delete_all
user@server.ip /data/[app_name]/current $ bundle exec rake db:seed

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s