Skip to content

Instantly share code, notes, and snippets.

@amolpujari
Last active December 16, 2015 22:29
Show Gist options
  • Save amolpujari/5507227 to your computer and use it in GitHub Desktop.
Save amolpujari/5507227 to your computer and use it in GitHub Desktop.
chef-solo rails deploy git submodule checkout branch sha
# solo.rb
require File.expand_path('../lib/global_constants.rb', __FILE__)
require File.expand_path('../lib/git_addition.rb', __FILE__)
root = File.absolute_path(File.dirname(__FILE__))
file_cache_path root
cookbook_path [ root + '/cookbooks', root + '/custom_cookbooks']
data_bag_path root + '/data_bags'
node_path root + '/nodes'
role_path root + '/roles'
#log_level :debug
#log_location STDOUT
#verbose_logging false
# lib/git_addition.rb
require 'chef/log'
require 'chef/provider'
require 'chef/mixin/shell_out'
require 'fileutils'
require 'shellwords'
class Chef
class Provider
class Git < Chef::Provider
def fetch_updates
#setup_remote_tracking_branches if @new_resource.remote != 'origin'
converge_by("fetch updates for #{@new_resource.remote}") do
fetch_command = "rm Gemfile.lock; git fetch #{@new_resource.remote} && git fetch #{@new_resource.remote} --tags && git checkout #{@new_resource.branch} && git reset --hard #{@new_resource.sha}"
Chef::Log.debug "Fetching updates from #{new_resource.remote} and resetting to revision #{target_revision}"
shell_out!(fetch_command, run_options(:cwd => @new_resource.destination))
end
end
def checkout
sha_ref = target_revision
converge_by("checkout ref #{sha_ref} branch #{@new_resource.branch}") do
shell_out!("rm Gemfile.lock; git fetch -u && git checkout #{@new_resource.branch} && git reset --hard #{@new_resource.sha} ", run_options(:cwd => @new_resource.destination))
Chef::Log.info "#{@new_resource} checked out branch: #{@new_resource.branch} reference: #{sha_ref}"
end
end
def target_revision
@target_revision ||= begin
if sha_hash?(@new_resource.sha)
@target_revision = @new_resource.sha
else
@target_revision = remote_resolve_reference
end
end
end
alias :revision_slug :target_revision
end
end
end
class Chef
class Resource
class Deploy < Chef::Resource
attr_accessor :sha
def sha(arg=nil)
set_or_return(
:sha,
arg,
:kind_of => [ String ]
)
end
end
end
end
# main recipe
app_root = "#{$pw_home_dir}/myapp-deploy"
current_path = "#{app_root}/current"
cache_path = "#{app_root}/shared/cached-copy"
package "g++"
package "nodejs"
package "mysql-client"
package "libmysqlclient-dev"
rvm_ruby node.rvm_ruby
rvm_default_ruby node.rvm_ruby
deploy_revision app_root do
action $chef_rails_deploy
user node.user
group node.group
repo node.repo
branch ($cfg["myapp_branch"] or node.myapp_branch)
sha ($cfg["myapp_sha"] or node.myapp_sha)
env = { "RAILS_ENV" => $rails_env }
Chef::Mixin::DeepMerge.merge(node.environment, env)
environment env
before_migrate do
myapp_config cache_path
myapp_checkout cache_path
rvm_shell "bundle" do
ruby_string node.rvm_ruby
cwd cache_path
user node.user
group node.group
code %{ bundle --path #{cache_path} --gemfile #{cache_path}/Gemfile }
end
end
purge_before_symlink %w{tmp}
create_dirs_before_symlink %w{uploads tmp}
symlinks {}
symlink_before_migrate({
"log" => "log",
"tmp" => "tmp",
"public" => "public",
"cached-copy/config/database.yml" => "config/database.yml",
"cached-copy/config/config.yml" => "config/config.yml"
})
migrate true
migration_command "echo 'skipping RAILS_ENV=#{$rails_env} bundle exec rake db:migrate --trace' "
shallow_clone true # TBD
keep_releases 10
before_restart do
myapp_checkout "#{app_root}/current"
#assets ($cfg["myapp-revision"] or "HEAD") do
# app_path cache_path
#end
if production?
#rvm_gem "passenger" do
# options " -y "
#end
# follow myapp/installprofile.apache
#restart_command "touch tmp/restart.txt"
end
end
end
# definitions/submodule.rb
define :submodule, :name=>nil do
return if rollback?
rvm_shell "submodule #{params[:name]}" do
ruby_string node.rvm_ruby
cwd params[:name]
user node.user
group node.group
code %{ git submodule init && git submodule update }
end
end
# definitions/checkout.rb
define :checkout, :name=>nil, :cwd=>nil, :branch=>nil, :sha=>nil do
rvm_shell "#{params[:name]} #{params[:branch]} #{params[:sha]} => #{params[:cwd]}" do
ruby_string node.rvm_ruby
cwd params[:cwd]
user node.user
group node.group
code %{ git checkout #{params[:branch]} && git pull && git reset --hard #{params[:sha]} }
end
end
# definitions/myapp_checkout.rb
define :myapp_checkout, :name=>nil do
return if rollback?
path = params[:name]
submodule path
checkout "sm1" do
cwd "#{path}/vendor/plugins/sm1"
branch ($cfg["myapp_sm1_branch"] or node.myapp_sm1_branch)
sha ($cfg["myapp_sm1_sha"] or node.myapp_sm1_sha)
end
checkout "sm2" do
cwd "#{path}/vendor/plugins/sm2"
branch ($cfg["myapp_sm2_branch"] or node.myapp_sm2_branch)
sha ($cfg["myapp_sm2_sha"] or node.myapp_sm2_sha)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment