rails Rake and mysql ssh port forwarding.

Posted by rube_noob on Stack Overflow See other posts from Stack Overflow or by rube_noob
Published on 2009-12-17T09:33:16Z Indexed on 2010/06/15 2:02 UTC
Read the original article Hit count: 286

Hello, I need to create a rake task to do some active record operations via a ssh tunnel.

The rake task is run on a remote windows machine so I would like to keep things in ruby. This is my latest attempt.

  desc "Syncronizes the tablets DB with the Server"
      task(:sync => :environment) do
        require 'rubygems'
        require 'net/ssh'

        begin
        Thread.abort_on_exception = true
        tunnel_thread = Thread.new do
          Thread.current[:ready] = false
          hostname = 'host'
          username = 'tunneluser'

          Net::SSH.start(hostname, username) do|ssh|
            ssh.forward.local(3333, "mysqlhost.com", 3306)
              Thread.current[:ready] = true
              puts "ready thread"
              ssh.loop(0) { true }
        end
        end

        until tunnel_thread[:ready] == true do
        end
        puts "tunnel ready"
        Importer.sync

        rescue StandardError => e    
          puts "The Database Sync Failed."
        end
  end

The task seems to hang at "tunnel ready" and never attempts the sync.

I have had success when running first a rake task to create the tunnel and then running the rake sync in a different terminal. I want to combine these however so that if there is an error with the tunnel it will not attempt the sync.

This is my first time using ruby Threads and Net::SSH forwarding so I am not sure what is the issue here.

Any Ideas!?

Thanks

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about threads