Engineyard : Deployment : Passing multiple Environment variables to deploy hooks

Engineyard has made it easier to send environment variables to deploy hook when you execute the deployment command from client console. Environment variable is not the exact terminology to be used. EY sends it as configuration hash.  You may want/need to send environment variables in situations such as:

  • When you want to detect that this is the attempt when you want your seeds ( Data defined in seed.rb) to be committed to database.
  • When you want specific services or packages like Elastic Search to reindex the records

ey deploy -e my_app_staging --branch=test --config=initial:true reindex:job

In this example I am passing environment variables/configs initial and reindex with corresponding values. To access these values in hooks we use config object which will be available inside any helper_methods such as

  • on_app_master(&block)
  • on_app_servers(&block)
  • on_app_servers_and_utilities(&block), etc

Example:

Auto Reindexing of multiple models/classes for ElasticSearch

# Reindexing of Models for Elastic Search
# In our case Elastic Search is running in App Master
on_app_servers do
  # reusing a block block so that any changes in this module
  #   would have effect on the whole deployment hooks
  # In short: DRYing up the codes
  require "#{config.release_path}/deploy/color_wrapper.rb"
  # Sometimes the order seems off so try
  puts_c SEPARATOR_SYMBOL * SEPARATOR_WIDTH
  puts_c "Checking for option passed from EY client for Reindexing of Models"
  run "cd #{config.release_path}"
  if config[:reindex] == 'all'
    puts_c "Reindexing all Models"
    run "bundle exec rake searchkick:reindex:all"
  elsif config[:reindex] && config[:reindex] != 'all'
    models = config[:reindex].split('|')
    models.each do |klass|
      puts_c "Reindexing the class: #{klass.capitalize}"
      run "bundle exec rake searchkick:reindex CLASS=#{klass.capitalize}"
    end
  else
    puts_c "Skipping the Reindexing process"
  end
  puts_c SEPARATOR_SYMBOL * SEPARATOR_WIDTH
  $stderr.flush
end

You might wonder what is puts_c

# colorize the rendered string
# @param text [String] Text to be wrapped with colorizing string
SEPARATOR_SYMBOL = '~'
DEFAULT_COLOR = 'green'
SEPARATOR_WIDTH = 120
def puts_c(text, color = DEFAULT_COLOR)
 case color
 when 'green'
   $stderr.puts "33[32m#{text}33[0m"
 when 'blue'
   $stderr.puts "33[34m#{text}33[0m"
 when 'red'
   $stderr.puts "33[31m#{text}33[0m"
 when 'brown'
   $stderr.puts "33[33m#{text}33[0m"
 else
   $stderr.puts "33[32m#{text}33[0m"
 end
end
$stderr.puts is method to echo 
You can research on this.

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