Skip to content

Instantly share code, notes, and snippets.

@gmacdougall
Created May 8, 2017 18:03
Show Gist options
  • Save gmacdougall/b36bc7eac004f11f61385b8968326b7d to your computer and use it in GitHub Desktop.
Save gmacdougall/b36bc7eac004f11f61385b8968326b7d to your computer and use it in GitHub Desktop.
require 'aws-sdk'
require 'timeout'
def wait_for_completion(opsworks, result)
status = 'running'
Timeout::timeout(300) do
while (status == 'running') do
sleep(5)
response = opsworks.describe_deployments(
deployment_ids: [result.deployment_id]
)
status = response.deployments.first.status
end
end
fail 'Timed out after 300s' if status == 'running'
end
{
'stack' => 'stack-uuid-goes-here',
}.each do |dir, stack_id|
puts '=' * 50
puts "Starting #{dir}"
puts '=' * 50
Dir.chdir 'ftp'
filename = "#{dir}-cookbooks"
`berks package #{filename}.tar.gz`
s3 = Aws::S3::Client.new
File.open("#{filename}.tar.gz", 'rb') do |file|
s3.put_object(
bucket: 'bucket-name-goes-here',
body: file,
key: filename,
)
end
puts "Uploaded #{filename} to S3"
opsworks = Aws::OpsWorks::Client.new
opsworks.update_stack(
stack_id: stack_id,
custom_json: File.read('stack.json')
)
puts 'Updated stack JSON'
result = opsworks.create_deployment(
stack_id: stack_id,
command: {
name: 'update_custom_cookbooks'
},
)
puts 'Updating custom cookbooks'
wait_for_completion(opsworks, result)
puts 'Custom cookbooks update complete'
result = opsworks.create_deployment(
stack_id: stack_id,
command: {
name: 'setup'
},
)
puts 'Running opsworks setup'
wait_for_completion(opsworks, result)
puts 'Opsworks setup complete'
puts 'Done'
puts '=' * 50
Dir.chdir '..'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment