Created
August 7, 2013 14:49
-
-
Save spangenberg/6174721 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'aws-sdk' | |
require 'net/http' | |
require 'uri' | |
namespace :deploy do | |
desc 'Deploy to OpsWorks' | |
task :opsworks do | |
unless ENV['VERSION'] | |
puts 'Please provide a version' | |
exit(1) | |
end | |
opsworks = AWS::OpsWorks.new.client | |
s3 = AWS::S3.new | |
stacks = opsworks.describe_stacks[:stacks] | |
stack = stacks.select { |s| s[:name] == 'Stack' }[0] | |
apps = opsworks.describe_apps(stack_id: stack[:stack_id])[:apps] | |
app = apps.select { |a| a[:name] == 'App' }[0] | |
bucket = s3.buckets['deployment-artifacts'] | |
obj = bucket.objects["#{ENV['VERSION']}.tar.gz"] | |
opsworks.update_app(app_id: app[:app_id], app_source: { url: obj.url_for(:read).to_s.split('?')[0] }) | |
instances = opsworks.describe_instances(stack_id: stack[:stack_id])[:instances] | |
created_deployment = opsworks.create_deployment(stack_id: stack[:stack_id], app_id: app[:app_id], instance_ids: instances.map { |i| i[:instance_id] }, command: { name: 'deploy' }, comment: "Release: #{ENV['VERSION']}") | |
deployment = nil | |
begin | |
sleep 10 if deployment | |
deployments = opsworks.describe_deployments(deployment_ids: [created_deployment[:deployment_id]])[:deployments] | |
deployment = deployments.select { |d| d[:deployment_id] == created_deployment[:deployment_id] }[0] | |
end while deployment[:status] == 'running' | |
if deployment[:status] == 'successful' | |
puts 'Successfully deployed to OpsWorks' | |
exit(0) | |
else | |
puts "Deployment went wrong: #{deployment.inspect}" | |
commands = opsworks.describe_commands(deployment_id: created_deployment[:deployment_id])[:commands] | |
commands.select { |c| c[:status] == 'failed' }.each do |command| | |
response = Net::HTTP.get_response(URI.parse(command[:log_url])) | |
puts "Failed command: #{command.inspect}\nLog:\n#{response.body}" | |
end | |
exit(1) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment