Skip to content

Instantly share code, notes, and snippets.

@kostecky
Last active March 26, 2018 20:10
Show Gist options
  • Save kostecky/5a4195ebf78c7f4b4b77 to your computer and use it in GitHub Desktop.
Save kostecky/5a4195ebf78c7f4b4b77 to your computer and use it in GitHub Desktop.
Capistrano rolling deploy with haproxy
# Create a map of the deploy server to the haproxy label
server 'A.A.A.A', proxy_label: 'server-01'
server 'B.B.B.B', proxy_label: 'server-02'
# Define our puppet proxy server(s) and role
server 'C.C.C.C', no_release: true, roles: [:proxy]
# We need to override deploy:symlink:linked_dirs
# No way around it - override deploy:symlink:release and rewrite it for rolling deploys
Rake::Task["deploy:symlink:release"].clear_actions
namespace :deploy do
namespace :symlink do
task :release do
on release_roles(:all), :in => :sequence do |release_host|
unless release_host.properties.proxy_label.to_s.empty?
on roles(:proxy) do |proxy_host|
# Put server in maintenance if there is a proxy_label
execute :haproxyctl, 'disable', 'all', release_host.properties.proxy_label
# Determine if there are connections to the puppetmaster and block until they finish
puppet_compiling = true
while puppet_compiling do
proxy_stat = capture(:haproxyctl, 'show', 'stat')
CSV.parse(proxy_stat, headers: true) do |row|
debug("row[1]: #{row[1]} and proxy_label #{release_host.properties.proxy_label}")
if row[1] == release_host.properties.proxy_label
debug("Current connections for #{release_host.properties.proxy_label}: #{row[4]}")
if row[4] == '0'
puppet_compiling = false
end
end
end
sleep(1)
end
end
end
# Link new release to current
tmp_current_path = release_path.parent.join(current_path.basename)
execute :ln, '-s', release_path, tmp_current_path
execute :mv, tmp_current_path, current_path.parent
# Take server out of maintenance
unless release_host.properties.proxy_label.to_s.empty?
on roles(:proxy) do |proxy_host|
execute(:haproxyctl, 'enable', 'all', release_host.properties.proxy_label)
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment