| Class | VirtP2V::Connection |
| In: |
lib/virt-p2v/connection.rb
lib/virt-p2v/connection.rb |
| Parent: | Object |
# File lib/virt-p2v/connection.rb, line 37
37: def initialize(hostname, username, password)
38: @mutex = Mutex.new
39: @connection_listeners = []
40:
41: @hostname = hostname
42: @username = username
43: @password = password
44:
45: @buffer = ""
46:
47: @session = nil
48: @channel = nil
49: end
# File lib/virt-p2v/connection.rb, line 37
37: def initialize(hostname, username, password)
38: @mutex = Mutex.new
39: @connection_listeners = []
40:
41: @hostname = hostname
42: @username = username
43: @password = password
44:
45: @buffer = ""
46:
47: @session = nil
48: @channel = nil
49: end
# File lib/virt-p2v/connection.rb, line 110
110: def close
111: unless @channel.nil?
112: @channel.close
113: @channel = nil
114: end
115: unless @session.nil?
116: @session.close
117: @session = nil
118: end
119:
120: @buffer = ''
121: end
# File lib/virt-p2v/connection.rb, line 110
110: def close
111: unless @channel.nil?
112: @channel.close
113: @channel = nil
114: end
115: unless @session.nil?
116: @session.close
117: @session = nil
118: end
119:
120: @buffer = ''
121: end
# File lib/virt-p2v/connection.rb, line 51
51: def connect(&cb)
52: run(cb) {
53: begin
54: @session = Libssh2.connect(@hostname, @username, @password) \
55: if @session.nil?
56:
57: @channel = @session.exec('LANG=C virt-p2v-server') \
58: if @channel.nil?
59: #rescue Libssh2::Session::InvalidHostnameError
60: # raise InvalidHostnameError
61: #rescue Libssh2::Session::InvalidCredentialsError
62: # raise InvalidCredentialsError
63: #rescue => ex
64: # raise RemoteError.new(ex.message)
65: end
66:
67: begin
68: line = @channel.read(64)
69:
70: if line !~ /^VIRT_P2V_SERVER /
71: raise RemoteError.new("Unexpected response: #{line}")
72: end
73: rescue Libssh2::Channel::ApplicationError => ex
74: if ex.message =~ /command not found/
75: raise RemoteError.new(
76: "virt-p2v-server is not installed on #{@hostname}")
77: else
78: raise RemoteError.new(ex.message)
79: end
80: end
81:
82: begin
83: i = 0;
84: listener_result = lambda { |result|
85: if result.kind_of?(Exception)
86: cb.call(result)
87: else
88: i += 1
89: if i == @connection_listeners.length
90: cb.call(true)
91: else
92: Gtk.queue {
93: @connection_listeners[i].call(listener_result)
94: }
95: end
96: end
97: }
98: Gtk.queue { @connection_listeners[0].call(listener_result) }
99: rescue => ex
100: @channel.close unless @channel.nil?
101: raise ex
102: end
103: }
104: end
# File lib/virt-p2v/connection.rb, line 51
51: def connect(&cb)
52: run(cb) {
53: begin
54: @session = Libssh2.connect(@hostname, @username, @password) \
55: if @session.nil?
56:
57: @channel = @session.exec('LANG=C virt-p2v-server') \
58: if @channel.nil?
59: #rescue Libssh2::Session::InvalidHostnameError
60: # raise InvalidHostnameError
61: #rescue Libssh2::Session::InvalidCredentialsError
62: # raise InvalidCredentialsError
63: #rescue => ex
64: # raise RemoteError.new(ex.message)
65: end
66:
67: begin
68: line = @channel.read(64)
69:
70: if line !~ /^VIRT_P2V_SERVER /
71: raise RemoteError.new("Unexpected response: #{line}")
72: end
73: rescue Libssh2::Channel::ApplicationError => ex
74: if ex.message =~ /command not found/
75: raise RemoteError.new(
76: "virt-p2v-server is not installed on #{@hostname}")
77: else
78: raise RemoteError.new(ex.message)
79: end
80: end
81:
82: begin
83: i = 0;
84: listener_result = lambda { |result|
85: if result.kind_of?(Exception)
86: cb.call(result)
87: else
88: i += 1
89: if i == @connection_listeners.length
90: cb.call(true)
91: else
92: Gtk.queue {
93: @connection_listeners[i].call(listener_result)
94: }
95: end
96: end
97: }
98: Gtk.queue { @connection_listeners[0].call(listener_result) }
99: rescue => ex
100: @channel.close unless @channel.nil?
101: raise ex
102: end
103: }
104: end
# File lib/virt-p2v/connection.rb, line 191
191: def container(type, &cb)
192: raise NotConnectedError if @channel.nil?
193:
194: run(cb) {
195: @channel.write("CONTAINER #{type}\n")
196: result = parse_return
197:
198: Gtk.queue { cb.call(result) }
199: }
200: end
# File lib/virt-p2v/connection.rb, line 191
191: def container(type, &cb)
192: raise NotConnectedError if @channel.nil?
193:
194: run(cb) {
195: @channel.write("CONTAINER #{type}\n")
196: result = parse_return
197:
198: Gtk.queue { cb.call(result) }
199: }
200: end
# File lib/virt-p2v/connection.rb, line 158
158: def convert(&cb)
159: raise NotConnectedError if @channel.nil?
160:
161: run(cb) {
162: @channel.write("CONVERT\n")
163: result = parse_return
164:
165: Gtk.queue { cb.call(result) }
166: }
167: end
# File lib/virt-p2v/connection.rb, line 158
158: def convert(&cb)
159: raise NotConnectedError if @channel.nil?
160:
161: run(cb) {
162: @channel.write("CONVERT\n")
163: result = parse_return
164:
165: Gtk.queue { cb.call(result) }
166: }
167: end
# File lib/virt-p2v/connection.rb, line 123
123: def lang(lang, &cb)
124: raise NotConnectedError if @channel.nil?
125:
126: run(cb) {
127: @channel.write("LANG #{lang}\n")
128: result = parse_return
129:
130: Gtk.queue { cb.call(result) }
131: }
132: end
# File lib/virt-p2v/connection.rb, line 123
123: def lang(lang, &cb)
124: raise NotConnectedError if @channel.nil?
125:
126: run(cb) {
127: @channel.write("LANG #{lang}\n")
128: result = parse_return
129:
130: Gtk.queue { cb.call(result) }
131: }
132: end
# File lib/virt-p2v/connection.rb, line 169
169: def list_profiles(&cb)
170: raise NotConnectedError if @channel.nil?
171:
172: run(cb) {
173: @channel.write("LIST_PROFILES\n")
174: result = parse_return
175:
176: Gtk.queue { cb.call(result) }
177: }
178: end
# File lib/virt-p2v/connection.rb, line 169
169: def list_profiles(&cb)
170: raise NotConnectedError if @channel.nil?
171:
172: run(cb) {
173: @channel.write("LIST_PROFILES\n")
174: result = parse_return
175:
176: Gtk.queue { cb.call(result) }
177: }
178: end
# File lib/virt-p2v/connection.rb, line 134
134: def metadata(meta, &cb)
135: raise NotConnectedError if @channel.nil?
136:
137: run(cb) {
138: payload = YAML::dump(meta)
139: @channel.write("METADATA #{payload.length}\n");
140: @channel.write(payload)
141: result = parse_return
142:
143: Gtk.queue { cb.call(result) }
144: }
145: end
# File lib/virt-p2v/connection.rb, line 134
134: def metadata(meta, &cb)
135: raise NotConnectedError if @channel.nil?
136:
137: run(cb) {
138: payload = YAML::dump(meta)
139: @channel.write("METADATA #{payload.length}\n");
140: @channel.write(payload)
141: result = parse_return
142:
143: Gtk.queue { cb.call(result) }
144: }
145: end
# File lib/virt-p2v/connection.rb, line 33
33: def on_connect(&cb)
34: @connection_listeners << cb
35: end
# File lib/virt-p2v/connection.rb, line 33
33: def on_connect(&cb)
34: @connection_listeners << cb
35: end
# File lib/virt-p2v/connection.rb, line 147
147: def path(length, path, &cb)
148: raise NotConnectedError if @channel.nil?
149:
150: run(cb) {
151: @channel.write("PATH #{length} #{path}\n")
152: result = parse_return
153:
154: Gtk.queue { cb.call(result) }
155: }
156: end
# File lib/virt-p2v/connection.rb, line 147
147: def path(length, path, &cb)
148: raise NotConnectedError if @channel.nil?
149:
150: run(cb) {
151: @channel.write("PATH #{length} #{path}\n")
152: result = parse_return
153:
154: Gtk.queue { cb.call(result) }
155: }
156: end
# File lib/virt-p2v/connection.rb, line 202
202: def send_data(io, length, progress, &completion)
203: raise NotConnectedError if @channel.nil?
204:
205: run(completion) {
206: @channel.write("DATA #{length}\n")
207: @channel.send_data(io) { |total|
208: Gtk.queue { progress.call(total) }
209: }
210:
211: result = parse_return
212:
213: Gtk.queue { progress.call(length); completion.call(result) }
214: }
215: end
# File lib/virt-p2v/connection.rb, line 202
202: def send_data(io, length, progress, &completion)
203: raise NotConnectedError if @channel.nil?
204:
205: run(completion) {
206: @channel.write("DATA #{length}\n")
207: @channel.send_data(io) { |total|
208: Gtk.queue { progress.call(total) }
209: }
210:
211: result = parse_return
212:
213: Gtk.queue { progress.call(length); completion.call(result) }
214: }
215: end
# File lib/virt-p2v/connection.rb, line 180
180: def set_profile(profile, &cb)
181: raise NotConnectedError if @channel.nil?
182:
183: run(cb) {
184: @channel.write("SET_PROFILE #{profile}\n")
185: result = parse_return
186:
187: Gtk.queue { cb.call(result) }
188: }
189: end