Class | VirtP2V::Connection |
In: |
lib/virt-p2v/connection.rb
lib/virt-p2v/connection.rb |
Parent: | Object |
connected | [R] | |
connected | [R] |
# File lib/virt-p2v/connection.rb, line 44 44: def initialize(hostname, username, password, &cb) 45: @mutex = Mutex.new 46: @connection_listeners = [] 47: 48: # Always send our version number on connection 49: @connection_listeners << Proc.new { |cb| 50: self.version { |result| cb.call(result) } 51: } 52: 53: run(cb) { 54: error = nil 55: begin 56: @ssh = Net::SSH.start(hostname, username, :password => password) 57: rescue SocketError, Errno::EHOSTUNREACH => ex 58: raise InvalidHostnameError 59: raise ex 60: rescue Net::SSH::AuthenticationFailed => ex 61: raise InvalidCredentialsError 62: raise ex 63: end 64: 65: @buffer = "" 66: @connected = false 67: 68: Gtk.queue { cb.call(true) } 69: } 70: end
# File lib/virt-p2v/connection.rb, line 44 44: def initialize(hostname, username, password, &cb) 45: @mutex = Mutex.new 46: @connection_listeners = [] 47: 48: # Always send our version number on connection 49: @connection_listeners << Proc.new { |cb| 50: self.version { |result| cb.call(result) } 51: } 52: 53: run(cb) { 54: error = nil 55: begin 56: @ssh = Net::SSH.start(hostname, username, :password => password) 57: rescue SocketError, Errno::EHOSTUNREACH => ex 58: raise InvalidHostnameError 59: raise ex 60: rescue Net::SSH::AuthenticationFailed => ex 61: raise InvalidCredentialsError 62: raise ex 63: end 64: 65: @buffer = "" 66: @connected = false 67: 68: Gtk.queue { cb.call(true) } 69: } 70: end
# File lib/virt-p2v/connection.rb, line 123 123: def close 124: @connected = false 125: @buffer = "" 126: @ch.close 127: end
# File lib/virt-p2v/connection.rb, line 123 123: def close 124: @connected = false 125: @buffer = "" 126: @ch.close 127: end
# File lib/virt-p2v/connection.rb, line 72 72: def connect(&cb) 73: run(cb) { 74: @ch = @ssh.open_channel do |ch| 75: ch.exec("virt-p2v-server") do |ch, success| 76: raise RemoteError, 77: "could not execute a remote command" unless success 78: 79: ch.on_data do |ch, data| 80: @buffer << data 81: end 82: 83: # If we get anything on stderr, raise it as a RemoteError 84: ch.on_extended_data do |ch, type, data| 85: close 86: raise RemoteError, data 87: end 88: 89: # Clean up local resources if we get eof from the other end 90: ch.on_eof do |ch| 91: close 92: end 93: 94: @connected = true 95: end 96: 97: end 98: 99: # Wait until we're connected 100: @ssh.loop do 101: !@connected 102: end 103: 104: i = 0; 105: listener_result = lambda { |result| 106: if result.kind_of?(Exception) 107: cb.call(result) 108: else 109: i += 1 110: if i == @connection_listeners.length 111: cb.call(true) 112: else 113: Gtk.queue { 114: @connection_listeners[i].call(listener_result) 115: } 116: end 117: end 118: } 119: Gtk.queue { @connection_listeners[0].call(listener_result) } 120: } 121: end
# File lib/virt-p2v/connection.rb, line 72 72: def connect(&cb) 73: run(cb) { 74: @ch = @ssh.open_channel do |ch| 75: ch.exec("virt-p2v-server") do |ch, success| 76: raise RemoteError, 77: "could not execute a remote command" unless success 78: 79: ch.on_data do |ch, data| 80: @buffer << data 81: end 82: 83: # If we get anything on stderr, raise it as a RemoteError 84: ch.on_extended_data do |ch, type, data| 85: close 86: raise RemoteError, data 87: end 88: 89: # Clean up local resources if we get eof from the other end 90: ch.on_eof do |ch| 91: close 92: end 93: 94: @connected = true 95: end 96: 97: end 98: 99: # Wait until we're connected 100: @ssh.loop do 101: !@connected 102: end 103: 104: i = 0; 105: listener_result = lambda { |result| 106: if result.kind_of?(Exception) 107: cb.call(result) 108: else 109: i += 1 110: if i == @connection_listeners.length 111: cb.call(true) 112: else 113: Gtk.queue { 114: @connection_listeners[i].call(listener_result) 115: } 116: end 117: end 118: } 119: Gtk.queue { @connection_listeners[0].call(listener_result) } 120: } 121: end
# File lib/virt-p2v/connection.rb, line 208 208: def container(type, &cb) 209: raise NotConnectedError unless @connected 210: 211: run(cb) { 212: @ch.send_data("CONTAINER #{type}\n") 213: result = parse_return 214: 215: Gtk.queue { cb.call(result) } 216: } 217: end
# File lib/virt-p2v/connection.rb, line 208 208: def container(type, &cb) 209: raise NotConnectedError unless @connected 210: 211: run(cb) { 212: @ch.send_data("CONTAINER #{type}\n") 213: result = parse_return 214: 215: Gtk.queue { cb.call(result) } 216: } 217: end
# File lib/virt-p2v/connection.rb, line 175 175: def convert(&cb) 176: raise NotConnectedError unless @connected 177: 178: run(cb) { 179: @ch.send_data("CONVERT\n") 180: result = parse_return 181: 182: Gtk.queue { cb.call(result) } 183: } 184: end
# File lib/virt-p2v/connection.rb, line 175 175: def convert(&cb) 176: raise NotConnectedError unless @connected 177: 178: run(cb) { 179: @ch.send_data("CONVERT\n") 180: result = parse_return 181: 182: Gtk.queue { cb.call(result) } 183: } 184: end
# File lib/virt-p2v/connection.rb, line 140 140: def lang(lang, &cb) 141: raise NotConnectedError unless @connected 142: 143: run(cb) { 144: @ch.send_data("LANG #{lang}\n") 145: result = parse_return 146: 147: Gtk.queue { cb.call(result) } 148: } 149: end
# File lib/virt-p2v/connection.rb, line 140 140: def lang(lang, &cb) 141: raise NotConnectedError unless @connected 142: 143: run(cb) { 144: @ch.send_data("LANG #{lang}\n") 145: result = parse_return 146: 147: Gtk.queue { cb.call(result) } 148: } 149: end
# File lib/virt-p2v/connection.rb, line 186 186: def list_profiles(&cb) 187: raise NotConnectedError unless @connected 188: 189: run(cb) { 190: @ch.send_data("LIST_PROFILES\n") 191: result = parse_return 192: 193: Gtk.queue { cb.call(result) } 194: } 195: end
# File lib/virt-p2v/connection.rb, line 186 186: def list_profiles(&cb) 187: raise NotConnectedError unless @connected 188: 189: run(cb) { 190: @ch.send_data("LIST_PROFILES\n") 191: result = parse_return 192: 193: Gtk.queue { cb.call(result) } 194: } 195: end
# File lib/virt-p2v/connection.rb, line 151 151: def metadata(meta, &cb) 152: raise NotConnectedError unless @connected 153: 154: run(cb) { 155: payload = YAML::dump(meta) 156: @ch.send_data("METADATA #{payload.length}\n"); 157: @ch.send_data(payload) 158: result = parse_return 159: 160: Gtk.queue { cb.call(result) } 161: } 162: end
# File lib/virt-p2v/connection.rb, line 151 151: def metadata(meta, &cb) 152: raise NotConnectedError unless @connected 153: 154: run(cb) { 155: payload = YAML::dump(meta) 156: @ch.send_data("METADATA #{payload.length}\n"); 157: @ch.send_data(payload) 158: result = parse_return 159: 160: Gtk.queue { cb.call(result) } 161: } 162: end
# File lib/virt-p2v/connection.rb, line 40 40: def on_connect(&cb) 41: @connection_listeners << cb 42: end
# File lib/virt-p2v/connection.rb, line 40 40: def on_connect(&cb) 41: @connection_listeners << cb 42: end
# File lib/virt-p2v/connection.rb, line 164 164: def path(length, path, &cb) 165: raise NotConnectedError unless @connected 166: 167: run(cb) { 168: @ch.send_data("PATH #{length} #{path}\n") 169: result = parse_return 170: 171: Gtk.queue { cb.call(result) } 172: } 173: end
# File lib/virt-p2v/connection.rb, line 164 164: def path(length, path, &cb) 165: raise NotConnectedError unless @connected 166: 167: run(cb) { 168: @ch.send_data("PATH #{length} #{path}\n") 169: result = parse_return 170: 171: Gtk.queue { cb.call(result) } 172: } 173: end
# File lib/virt-p2v/connection.rb, line 219 219: def send_data(io, length, progress, &completion) 220: raise NotConnectedError unless @connected 221: 222: run(completion) { 223: @ch.send_data("DATA #{length}\n") 224: total = 0 225: buffer = '' 226: begin 227: # This loop is in the habit of hanging in Net::SSH when sending 228: # a chunk larger than about 2M. Putting the 1 second wait 229: # timeout here kickstarts it if it stops. 230: @ssh.loop(1) { 231: if io.eof? || total == length then 232: false 233: else 234: if @ch.remote_window_size > 0 then 235: out = length - total 236: out = @ch.remote_window_size \ 237: if out > @ch.remote_window_size 238: 239: io.read(out, buffer) 240: @ch.send_data(buffer) 241: 242: total += buffer.length 243: 244: # Send a progress callback 245: Gtk.queue { progress.call(total) } 246: end 247: 248: true 249: end 250: } 251: rescue => ex 252: Gtk.queue { completion.call(ex) } 253: end 254: 255: result = parse_return 256: 257: Gtk.queue { completion.call(result) } 258: } 259: end
# File lib/virt-p2v/connection.rb, line 219 219: def send_data(io, length, progress, &completion) 220: raise NotConnectedError unless @connected 221: 222: run(completion) { 223: @ch.send_data("DATA #{length}\n") 224: total = 0 225: buffer = '' 226: begin 227: # This loop is in the habit of hanging in Net::SSH when sending 228: # a chunk larger than about 2M. Putting the 1 second wait 229: # timeout here kickstarts it if it stops. 230: @ssh.loop(1) { 231: if io.eof? || total == length then 232: false 233: else 234: if @ch.remote_window_size > 0 then 235: out = length - total 236: out = @ch.remote_window_size \ 237: if out > @ch.remote_window_size 238: 239: io.read(out, buffer) 240: @ch.send_data(buffer) 241: 242: total += buffer.length 243: 244: # Send a progress callback 245: Gtk.queue { progress.call(total) } 246: end 247: 248: true 249: end 250: } 251: rescue => ex 252: Gtk.queue { completion.call(ex) } 253: end 254: 255: result = parse_return 256: 257: Gtk.queue { completion.call(result) } 258: } 259: end
# File lib/virt-p2v/connection.rb, line 197 197: def set_profile(profile, &cb) 198: raise NotConnectedError unless @connected 199: 200: run(cb) { 201: @ch.send_data("SET_PROFILE #{profile}\n") 202: result = parse_return 203: 204: Gtk.queue { cb.call(result) } 205: } 206: end
# File lib/virt-p2v/connection.rb, line 197 197: def set_profile(profile, &cb) 198: raise NotConnectedError unless @connected 199: 200: run(cb) { 201: @ch.send_data("SET_PROFILE #{profile}\n") 202: result = parse_return 203: 204: Gtk.queue { cb.call(result) } 205: } 206: end
# File lib/virt-p2v/connection.rb, line 129 129: def version(&cb) 130: raise NotConnectedError unless @connected 131: 132: run(cb) { 133: @ch.send_data("VERSION 0\n") 134: result = parse_return 135: 136: Gtk.queue { cb.call(result) } 137: } 138: end