codedecoder

breaking into the unknown…

multibox multiserver deployment with mina

1 Comment

I have explained how to deploy your rails app using mina in this post. Here, I will explain how to do deployment on multiple servers say demo, staging, testing, production etc. The logic is simple, we need to laod the configuration dynamically depending on the server we want to deploy. two things provided by mina is helpful. first it allow you to define your own task with task keyword and invoke other methods with invoke keyword. we will make use of both of them. we will separate all setings from deploy.rb file to a separate file, say deploy_conf.yml. The steps are almost same as I have explained here for single deployment. So go to the last post for step by step explaination

STEP 1: create a file deploy_conf.yml in config folder

This file will contain all the configuration settings for different server. Say, my demo and staging server content in it is as below

staging:
    domain: trantor@192.168.173.52
    deploy_to: /var/www/my_staging_app.com
    repository: git@github.com:skycorporation/my_app.git
    branch: master
    rails_env: staging

demo:
    domain: trantor@192.168.173.52
    deploy_to: /var/www/my_demo_app.com
    repository: git@github.com:skycorporation/my_app.git
    branch: master
    rails_env: demo

STEP 2: Setting the Staging server.

STEP 3: Create empty directory on server manually which required by mina as prequisite.  you should repeat this on each server

STEP 4: install and init mina

STEP 5: write the deployment code to deploy.rb file. Iam showing here only the lines which is added to code from this post

….

require ‘mina/rvm’ #from last post…add below code after it

require “yaml” #need to parse the yml file containing our configuration

YAML.load(File.open(‘config/deploy_conf.yml’)).keys.each do |server|
#we will loop over each server in deploy_conf.yml file and dynamically generate setup and deploy task for that
  desc %{Set up #{server} for deployment}
  task “setup_#{server}” => :environment do # for say demo server, this line is like task setup_demo
    load_config(server) # this will call load_config method below which will the load the configuration for that server
    invoke :setup # will invoke the setup method
  end

  desc %{deploy to #{server} server}
  task “deploy_to_#{server}” => :environment do # for say demo server, this line is like task deploy_to_demo
    load_config(server) # this will call load_config method below which will the load the configuration for that server
    invoke :deploy # will invoke the deploy method
  end
end

def load_config( server)
  thirdpillar_config =YAML.load(File.open(‘config/deploy_conf.yml’)) #this will load the yml file and return a hash for each server
  puts “———-> configuring #{server} server”
  set :domain, thirdpillar_config[server][‘domain’] #retriving the domain value for current server
  set :deploy_to, thirdpillar_config[server][‘deploy_to’] #retriving the deploy_to value for current server
  set :repository, thirdpillar_config[server][‘repository’]
  set :branch, thirdpillar_config[server][‘branch’]
  set :rails_env, thirdpillar_config[server][‘rails_env’]
end

set :shared_paths, [‘config/database.yml’]  #from last post, copy remaining code from there below

……

STEP 6: run mina setup_staging on the terminal

$mina setup_staging #this will call your custom method for setting up server

STEP  7: run mina deploy_to_staging on the terminal

$mina deploy_to_staging #this will call your custom method for deploying to staging server

Similarly, running mina deploy_to_demo will deploy to your demo server.

Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

One thought on “multibox multiserver deployment with mina

  1. Pingback: Rails auto deployment với capistrano – Appconus Blog

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