Deploy Application via Chef

After creating the Ruby on Rails application mentioned in my blog, the next task was to deploy the application to a hosted web server.

The traditional way would be to write rake tasks and trigger them manually or using a CI build/deploy. Well chef provides another easy solution to deploy an application. The recipe below performs all the tasks of deploying an application. The before_migrate script contains the tasks that need to be performed before the application kicks in. The migration_command is the final command that is run to mark deployment successful. deploy_revision is used to make sure that the recipe would only be executed once the git head changes in the repository from the last deployed version. symlink_before_migrate can be used to create symlinks to the yml files used in the configuring the application.

The deploy cookbook for a basic app looks like below


app_user="user"

deploy_dir="/opt/application_deploy"

user  app_user

directory "/home/#{app_user}/.ssh" do

mode "0700"

owner app_user

group app_user

end


cookbook_file "/home/#{app_user}/.ssh/deploy" do

source "deploy"

mode "0600"

owner app_user

group app_user

end


cookbook_file"/home/#{app_user}/.ssh/config" do

source "ssh_config"

mode "0644"

owner app_user

group app_user

end


directory deploy_dir do

recursive true

owner app_user

group app_user

mode "755"

end


%w{config log}.each do |dir|

directory "#{deploy_dir}/shared/#{dir}" do

owner app_user

group app_user

mode "755"

recursive true

end

end


gem_package "bundler"


cookbook_file "#{deploy_dir}/shared/config/database.yml" do

source "database.yml"

mode "0644"

owner app_user

group app_user

end

deploy_revision deploy_dir do

repo "<Git hub repository ssh url>"

environment "production"

revision "HEAD"

user app_user

symlink_before_migrate "config/database.yml" => "config/database.yml"

migration_command "bundle exec rails server -d -e production"

migrate true

action :deploy

restart_command "touch tmp/restart.txt"

before_migrate do

execute "bundle install" do

command "bundle install --path .bundle"

cwd  "#{release_path}"

user app_user

end


execute "Create db" do

command 'bundle exec rake db:create RAILS_ENV="production" '

cwd "#{release_path}"

user app_user

end


execute "Migrate db" do

command 'bundle exec rake db:migrate RAILS_ENV="production"'

cwd  "#{release_path}"

user app_user

end

end

end


Comments

Popular posts from this blog

To DR or Not To DR

High Availability NAT for AWS VPC with Multiple Private Subnets.

Load Balancer with SSL offloading - nginx + HAProxy