Skip to content

Instantly share code, notes, and snippets.

@botanicus
Created January 20, 2015 07:35
Show Gist options
  • Save botanicus/cb859f38a8c753124dac to your computer and use it in GitHub Desktop.
Save botanicus/cb859f38a8c753124dac to your computer and use it in GitHub Desktop.
Script for QA. It will snapshot your production, start given number of Terminals and reset the app to the given Git ref.
#!/usr/bin/env ruby
require 'terminal.com/api'
require 'date'
# Usage: ./qa.rb [production Terminal container_key] [how many Terminals to spin up] [Git ref]
def usage
abort("Usage: #{$0} [container_key] [x] [y]")
end
container_key = ARGV.shift || usage
terminal_count = ARGV.shift.to_i || usage
git_ref = ARGV.shift || usage
api = Terminal::API.new('73449ae08fce0d0cdf1d4dc6134b3c049538254a4cc7bc69e9e6a6c84c3823e4', 'k1voWgemOZ9Idhm2yF3wbCQs1ch3HpFV')
# https://www.terminal.com/api/docs#snapshot-terminal
puts "~ Taking a snapshot of the production environment."
response = api.snapshot_terminal(container_key, title: "Backup #{Time.now.strftime('%Y-%m-%d-%H-%M')}")
request_id = response['request_id']
snapshot_id = loop do
print "."
response = Terminal.request_progress(request_id)
snapshot_id = response['result']
if snapshot_id
break snapshot_id
elsif response['status'] == 'failed'
abort "\nError occurred when trying to snapshot the production Terminal: #{response.inspect}"
end
sleep 1
end
puts "\n~ Snapshot is ready, spinning up Terminals ..."
# https://www.terminal.com/api/docs#start-snapshot
startup_script = DATA.read.gsub(/%GIT_REF%/, git_ref)
terminal_count.times.map do |i|
name = "QA #{git_ref} #{i}"
puts "~ Spinning up Terminal #{name}"
api.start_snapshot(snapshot_id, name: name, temporary: true, startup_script: startup_script)
end
__END__
cd /var/www/expense-tracker/server
stop expense-tracker
git checkout %GIT_REF%
start expense-tracker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment