Class: DbTunnel::DbSync
- Inherits:
-
Object
- Object
- DbTunnel::DbSync
- Defined in:
- lib/db_tunnel/db_sync.rb
Instance Attribute Summary collapse
-
#tunnel_host ⇒ Object
Returns the value of attribute tunnel_host.
Instance Method Summary collapse
- #execute(from:, to:) ⇒ Object
-
#initialize(tunnel_host: nil) ⇒ DbSync
constructor
A new instance of DbSync.
- #tunnel_check ⇒ Object
- #tunnel_off ⇒ Object
- #tunnel_on ⇒ Object
Constructor Details
#initialize(tunnel_host: nil) ⇒ DbSync
Returns a new instance of DbSync.
5 6 7 |
# File 'lib/db_tunnel/db_sync.rb', line 5 def initialize(tunnel_host: nil) self.tunnel_host = tunnel_host || ENV["TUNNEL_HOST"] end |
Instance Attribute Details
#tunnel_host ⇒ Object
Returns the value of attribute tunnel_host.
3 4 5 |
# File 'lib/db_tunnel/db_sync.rb', line 3 def tunnel_host @tunnel_host end |
Instance Method Details
#execute(from:, to:) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/db_tunnel/db_sync.rb', line 34 def execute(from:, to:) cmd = clone_cmd(from: from, to: to) puts "[DbSync#execute] #{cmd}" # execute cmd and stream output Open3.popen3(cmd) do |stdout, stderr, status, thread| while (line = stderr.gets) puts(line) end end end |
#tunnel_check ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/db_tunnel/db_sync.rb', line 19 def tunnel_check sql = "SELECT COUNT(*) FROM users;" puts "Checking DB access: #{sql}" cmd = "psql #{tunnel_staging_db_url} -c '#{sql}'" # puts cmd system cmd end |
#tunnel_off ⇒ Object
28 29 30 31 32 |
# File 'lib/db_tunnel/db_sync.rb', line 28 def tunnel_off cmd = "lsof -ti:#{tunnel_port} | xargs kill -9" puts "Killing SSH tunnel: #{cmd}" system cmd end |
#tunnel_on ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/db_tunnel/db_sync.rb', line 9 def tunnel_on crdes, db_host_db_name = stag_db_url.split("@") db_host, db_name = db_host_db_name.split("/") print "Preparing SSH tunnel localhost:#{tunnel_port} through #{tunnel_host}... " cmd = %( ssh -f -N -L #{tunnel_port}:#{db_host} #{tunnel_username}@#{tunnel_host} ) result = system(cmd) puts result ? "Succeeded." : "Failed." end |