Friday, 20 February 2015

General settings for deployment in deploy.rb Capistrano

Here some general settings and use of them in capistrano deployment.

1. set_scm: Set protocol to copy code from your repository to server.
   set: 'git' // git protocol will used for deployment.

2. Set repository: set path of your git repository.
3. deploy_via : This is very important setting. There are two ways to set this variable
  3.1 Set to copy
         set :deploy_via, :copy
 this will copy all code with all meta data of repository like branch info n all.This will be a slow process because capistrano will perform a scp from your repository to server.

 3.2 Set to export: 
       set :deploy_via, :export

This is faster a bit because it will clone repository to the server it leave metadata of the repository.

4. Set branch: Set default branch to be deployed.There are many ways you can set it like:

 4.1 Set it manually: Just hard code the branch name.  
        set :branch ,'branch_name'
  4.2 Ask before deployment: Use Capistrano::CLI.ask to take branch name at deployment time.

set :branch do
default_branch = Capistrano::CLI.ask('Enter branch name')
end

4.3 Use current branch: You can use current working branch like:
  set :branch do
   default_branch = `git rev-parse --abbrev-ref HEAD`.chomp
  end

These are some common settings to keep in mind before writing a deployment script in capistrano.

Enjoy!!!!!

Friday, 13 February 2015

Working with SAML(Security Assertion Markup Language) in rails by 'ruby-saml' gem

Security Assertion Markup Language (SAML, pronounced sam-el) is an XML-based, open-standard data format for exchanging authentication and authorization data between parties, in particular, between an identity provider and a service provider. SAML is a product of the OASIS Security Services Technical Committee. SAML dates from 2001; the most recent major update of SAML was published in 2005, but protocol enhancements have steadily been added through additional, optional standards.

Here is an example of implementing saml-sso in rails applications by using 'ruby-saml':

1. Install gem:

     gem install 'ruby-saml' or whrite gem 'ruby-saml' in your app's gem file.

2. Create a controller named 'saml'.

3. Configure settings:

 3.a create a yml file inside app/config/saml_settings.yml.
 3.b add the following code inside the yml file

idp:
  sso_target_url: idp_provider's_saml_auth_path
  slo_target_url: idp_provider's_saml_logout_path

so:
  assertion_consumer_service_url: Service_providers_consume_path
  issuer: service_provider's_path
  assertion_consumer_logout_service_url: service_provider's_saml_logout_path

4.Load all saml settings in your saml_controller.

5.Send login request to idp:

 def init  request = OneLogin::RubySaml::Authrequest.new
  redirect_to(request.create(saml_settings))
end

6.Handle Idp response for login:

def consume  response = OneLogin::RubySaml::Response.new(params[:SAMLResponse])
  response.settings = saml_settings
  if response.is_valid? && user = User.find_or_create_by(email: response.name_id)
    session[:user_id] = user.id
    redirect_to root_url
  else    redirect_to login_path
  end
end

the above example is for implementing single sign on in a rails app.

Friday, 6 February 2015

Templating in Rails by using .docx and unoconv

In practice we often use to convert docx files in several forms as like PDF and doc file by using unoconv utility in Ubuntu.

There is a general issue with  converting docx files having image in that file i.e image background will be change.while converting docx file to doc unoconv change image property. if you set image background to no fill in docx then it will be 'white background' in doc file.


To resolve this issue you can do the following steps:
1. Extract .docx file and go to 'test_docx_files/word/media' folder.
2. Put your image inside  'test_docx_files/word/media' folder.
3. Convert your image to '.emf' file and rename to to 'image1.emf'.
4. Come to  'test_docx_files' and run following command:

`zip -r ../hest1.docx .`

this will create a .docx file named 'test1.docx' outside of the 'test_docx_files' folder.
5. convert this .docx file to .doc file.

Now you can see your image is having same property as like in .docx file.