Last active
January 25, 2016 01:04
-
-
Save zedalaye/c176a43a1d00d677ab39 to your computer and use it in GitHub Desktop.
Synchronize database between 2 apps at Scalingo
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
#!/usr/bin/env ruby | |
require 'uri' | |
require 'pty' | |
require 'expect' | |
if ARGV.length == 2 | |
app = ARGV[0] | |
dump = ARGV[1] | |
else | |
puts "Syntax: #{__FILE__} <scalingo app name> <database dump>" | |
exit | |
end | |
abort('Crash Protection : you cannot run this script on production applications') if app =~ /production/ | |
puts "Stopping application..." | |
puts `scalingo --app #{app} scale web:0 workers:0 clock:0` | |
puts "Recreate PostgreSQL addon..." | |
addons = `scalingo --app #{app} addons` | |
addon_line = addons.split("\n").grep(/PostgreSQL/).first | |
if addon_line | |
puts " > Remove actual PostgreSQL addon" | |
addon_id = addon_line.split('|')[2].strip | |
PTY.spawn("scalingo --app #{app} addons-remove #{addon_id}") do |reader, writer| | |
reader.expect(/To confirm, type the ID of the addon/, 5) # cont. in 5s if input doesn't match | |
writer.puts(addon_id) | |
puts reader.gets | |
end | |
end | |
puts " > Create new PostgreSQL addon" | |
puts `scalingo --app #{app} addons-add scalingo-postgresql free` | |
puts "Starting the tunnel..." | |
master, slave = PTY.open | |
read, write = IO.pipe | |
tunnel_pid = spawn("scalingo -app #{app} db-tunnel SCALINGO_POSTGRESQL_URL", :in=>read, :out=>slave) | |
read.close # we dont need the read | |
slave.close # or the slave | |
line, host, port = master.expect(/You can access your database on '(\d+\.\d+\.\d+\.\d+):(\d+)'/, 5) | |
puts line | |
at_exit { | |
puts "Close the tunnel" | |
Process.kill('TERM', tunnel_pid) | |
} | |
puts "Find Database connection params" | |
env = `scalingo --app #{app} env` | |
url = env.split("\n").grep(/^SCALINGO_POSTGRESQL_URL=/).first.split('=')[1].strip | |
uri = URI.parse(url) | |
db_name = uri.path[1..-1] | |
puts "Restoring database..." | |
puts "PGPASSWORD=#{uri.password} pg_restore -U #{uri.user} -h #{host} -p #{port} -d #{db_name} -O #{dump}" | |
puts `PGPASSWORD=#{uri.password} pg_restore -U #{uri.user} -h #{host} -p #{port} -d #{db_name} -O #{dump}` | |
puts "Patching database for use in testing environment..." | |
psql_cmd="PGPASSWORD=#{uri.password} psql -U #{uri.user} -h 127.0.0.1 -p #{port} -d #{db_name} -c" | |
puts `#{psql_cmd} \"update COMPANY_URLS set main=false where COMPANY_ID=2\"` | |
puts `#{psql_cmd} \"insert into COMPANY_URLS(company_id, url, subdomain, main) values(2, '#{app}.scalingo.io', false, true)\"` | |
puts "Migrate database..." | |
puts `scalingo --app #{app} run rake db:migrate` | |
puts "Restart application..." | |
puts `scalingo --app #{app} scale web:1:S` | |
puts 'Done!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment