forked from autotest/virt-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote.py
826 lines (725 loc) · 32.2 KB
/
remote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
"""
Functions and classes used for logging into guests and transferring files.
"""
import logging, time, re, os, shutil, tempfile
import aexpect, utils_misc, rss_client
from autotest.client.shared import error
import data_dir
class LoginError(Exception):
def __init__(self, msg, output):
Exception.__init__(self, msg, output)
self.msg = msg
self.output = output
def __str__(self):
return "%s (output: %r)" % (self.msg, self.output)
class LoginAuthenticationError(LoginError):
pass
class LoginTimeoutError(LoginError):
def __init__(self, output):
LoginError.__init__(self, "Login timeout expired", output)
class LoginProcessTerminatedError(LoginError):
def __init__(self, status, output):
LoginError.__init__(self, None, output)
self.status = status
def __str__(self):
return ("Client process terminated (status: %s, output: %r)" %
(self.status, self.output))
class LoginBadClientError(LoginError):
def __init__(self, client):
LoginError.__init__(self, None, None)
self.client = client
def __str__(self):
return "Unknown remote shell client: %r" % self.client
class SCPError(Exception):
def __init__(self, msg, output):
Exception.__init__(self, msg, output)
self.msg = msg
self.output = output
def __str__(self):
return "%s (output: %r)" % (self.msg, self.output)
class SCPAuthenticationError(SCPError):
pass
class SCPAuthenticationTimeoutError(SCPAuthenticationError):
def __init__(self, output):
SCPAuthenticationError.__init__(self, "Authentication timeout expired",
output)
class SCPTransferTimeoutError(SCPError):
def __init__(self, output):
SCPError.__init__(self, "Transfer timeout expired", output)
class SCPTransferFailedError(SCPError):
def __init__(self, status, output):
SCPError.__init__(self, None, output)
self.status = status
def __str__(self):
return ("SCP transfer failed (status: %s, output: %r)" %
(self.status, self.output))
def handle_prompts(session, username, password, prompt, timeout=10, debug=False):
"""
Log into a remote host (guest) using SSH or Telnet. Wait for questions
and provide answers. If timeout expires while waiting for output from the
child (e.g. a password prompt or a shell prompt) -- fail.
@brief: Log into a remote host (guest) using SSH or Telnet.
@param session: An Expect or ShellSession instance to operate on
@param username: The username to send in reply to a login prompt
@param password: The password to send in reply to a password prompt
@param prompt: The shell prompt that indicates a successful login
@param timeout: The maximal time duration (in seconds) to wait for each
step of the login procedure (i.e. the "Are you sure" prompt, the
password prompt, the shell prompt, etc)
@raise LoginTimeoutError: If timeout expires
@raise LoginAuthenticationError: If authentication fails
@raise LoginProcessTerminatedError: If the client terminates during login
@raise LoginError: If some other error occurs
"""
password_prompt_count = 0
login_prompt_count = 0
while True:
try:
match, text = session.read_until_last_line_matches(
[r"[Aa]re you sure", r"[Pp]assword:\s*$", r"[Ll]ogin:\s*$",
r"[Cc]onnection.*closed", r"[Cc]onnection.*refused",
r"[Pp]lease wait", r"[Ww]arning", prompt],
timeout=timeout, internal_timeout=0.5)
if match == 0: # "Are you sure you want to continue connecting"
if debug:
logging.debug("Got 'Are you sure...', sending 'yes'")
session.sendline("yes")
continue
elif match == 1: # "password:"
if password_prompt_count == 0:
if debug:
logging.debug("Got password prompt, sending '%s'", password)
session.sendline(password)
password_prompt_count += 1
continue
else:
raise LoginAuthenticationError("Got password prompt twice",
text)
elif match == 2: # "login:"
if login_prompt_count == 0 and password_prompt_count == 0:
if debug:
logging.debug("Got username prompt; sending '%s'", username)
session.sendline(username)
login_prompt_count += 1
continue
else:
if login_prompt_count > 0:
msg = "Got username prompt twice"
else:
msg = "Got username prompt after password prompt"
raise LoginAuthenticationError(msg, text)
elif match == 3: # "Connection closed"
raise LoginError("Client said 'connection closed'", text)
elif match == 4: # "Connection refused"
raise LoginError("Client said 'connection refused'", text)
elif match == 5: # "Please wait"
if debug:
logging.debug("Got 'Please wait'")
timeout = 30
continue
elif match == 6: # "Warning added RSA"
if debug:
logging.debug("Got 'Warning added RSA to known host list")
continue
elif match == 7: # prompt
if debug:
logging.debug("Got shell prompt -- logged in")
break
except aexpect.ExpectTimeoutError, e:
raise LoginTimeoutError(e.output)
except aexpect.ExpectProcessTerminatedError, e:
raise LoginProcessTerminatedError(e.status, e.output)
def remote_login(client, host, port, username, password, prompt, linesep="\n",
log_filename=None, timeout=10):
"""
Log into a remote host (guest) using SSH/Telnet/Netcat.
@param client: The client to use ('ssh', 'telnet' or 'nc')
@param host: Hostname or IP address
@param port: Port to connect to
@param username: Username (if required)
@param password: Password (if required)
@param prompt: Shell prompt (regular expression)
@param linesep: The line separator to use when sending lines
(e.g. '\\n' or '\\r\\n')
@param log_filename: If specified, log all output to this file
@param timeout: The maximal time duration (in seconds) to wait for
each step of the login procedure (i.e. the "Are you sure" prompt
or the password prompt)
@raise LoginBadClientError: If an unknown client is requested
@raise: Whatever handle_prompts() raises
@return: A ShellSession object.
"""
if client == "ssh":
cmd = ("ssh -o UserKnownHostsFile=/dev/null "
"-o PreferredAuthentications=password -p %s %s@%s" %
(port, username, host))
elif client == "telnet":
cmd = "telnet -l %s %s %s" % (username, host, port)
elif client == "nc":
cmd = "nc %s %s" % (host, port)
else:
raise LoginBadClientError(client)
logging.debug("Login command: '%s'", cmd)
session = aexpect.ShellSession(cmd, linesep=linesep, prompt=prompt)
try:
handle_prompts(session, username, password, prompt, timeout)
except Exception:
session.close()
raise
if log_filename:
session.set_output_func(utils_misc.log_line)
session.set_output_params((log_filename,))
session.set_log_file(log_filename)
return session
def wait_for_login(client, host, port, username, password, prompt, linesep="\n",
log_filename=None, timeout=240, internal_timeout=10):
"""
Make multiple attempts to log into a remote host (guest) until one succeeds
or timeout expires.
@param timeout: Total time duration to wait for a successful login
@param internal_timeout: The maximal time duration (in seconds) to wait for
each step of the login procedure (e.g. the "Are you sure" prompt
or the password prompt)
@see: remote_login()
@raise: Whatever remote_login() raises
@return: A ShellSession object.
"""
logging.debug("Attempting to log into %s:%s using %s (timeout %ds)",
host, port, client, timeout)
end_time = time.time() + timeout
while time.time() < end_time:
try:
return remote_login(client, host, port, username, password, prompt,
linesep, log_filename, internal_timeout)
except LoginError, e:
logging.debug(e)
time.sleep(2)
# Timeout expired; try one more time but don't catch exceptions
return remote_login(client, host, port, username, password, prompt,
linesep, log_filename, internal_timeout)
def _remote_scp(session, password_list, transfer_timeout=600, login_timeout=20):
"""
Transfer file(s) to a remote host (guest) using SCP. Wait for questions
and provide answers. If login_timeout expires while waiting for output
from the child (e.g. a password prompt), fail. If transfer_timeout expires
while waiting for the transfer to complete, fail.
@brief: Transfer files using SCP, given a command line.
@param session: An Expect or ShellSession instance to operate on
@param password_list: Password list to send in reply to the password prompt
@param transfer_timeout: The time duration (in seconds) to wait for the
transfer to complete.
@param login_timeout: The maximal time duration (in seconds) to wait for
each step of the login procedure (i.e. the "Are you sure" prompt or
the password prompt)
@raise SCPAuthenticationError: If authentication fails
@raise SCPTransferTimeoutError: If the transfer fails to complete in time
@raise SCPTransferFailedError: If the process terminates with a nonzero
exit code
@raise SCPError: If some other error occurs
"""
password_prompt_count = 0
timeout = login_timeout
authentication_done = False
scp_type = len(password_list)
while True:
try:
match, text = session.read_until_last_line_matches(
[r"[Aa]re you sure", r"[Pp]assword:\s*$", r"lost connection"],
timeout=timeout, internal_timeout=0.5)
if match == 0: # "Are you sure you want to continue connecting"
logging.debug("Got 'Are you sure...', sending 'yes'")
session.sendline("yes")
continue
elif match == 1: # "password:"
if password_prompt_count == 0:
logging.debug("Got password prompt, sending '%s'" %
password_list[password_prompt_count])
session.sendline(password_list[password_prompt_count])
password_prompt_count += 1
timeout = transfer_timeout
if scp_type == 1:
authentication_done = True
continue
elif password_prompt_count == 1 and scp_type == 2:
logging.debug("Got password prompt, sending '%s'" %
password_list[password_prompt_count])
session.sendline(password_list[password_prompt_count])
password_prompt_count += 1
timeout = transfer_timeout
authentication_done = True
continue
else:
raise SCPAuthenticationError("Got password prompt twice",
text)
elif match == 2: # "lost connection"
raise SCPError("SCP client said 'lost connection'", text)
except aexpect.ExpectTimeoutError, e:
if authentication_done:
raise SCPTransferTimeoutError(e.output)
else:
raise SCPAuthenticationTimeoutError(e.output)
except aexpect.ExpectProcessTerminatedError, e:
if e.status == 0:
logging.debug("SCP process terminated with status 0")
break
else:
raise SCPTransferFailedError(e.status, e.output)
def remote_scp(command, password_list, log_filename=None, transfer_timeout=600,
login_timeout=20):
"""
Transfer file(s) to a remote host (guest) using SCP.
@brief: Transfer files using SCP, given a command line.
@param command: The command to execute
(e.g. "scp -r foobar root@localhost:/tmp/").
@param password_list: Password list to send in reply to a password prompt.
@param log_filename: If specified, log all output to this file
@param transfer_timeout: The time duration (in seconds) to wait for the
transfer to complete.
@param login_timeout: The maximal time duration (in seconds) to wait for
each step of the login procedure (i.e. the "Are you sure" prompt
or the password prompt)
@raise: Whatever _remote_scp() raises
"""
logging.debug("Trying to SCP with command '%s', timeout %ss",
command, transfer_timeout)
if log_filename:
output_func = utils_misc.log_line
output_params = (log_filename,)
else:
output_func = None
output_params = ()
session = aexpect.Expect(command,
output_func=output_func,
output_params=output_params)
try:
_remote_scp(session, password_list, transfer_timeout, login_timeout)
finally:
session.close()
def scp_to_remote(host, port, username, password, local_path, remote_path,
limit="", log_filename=None, timeout=600):
"""
Copy files to a remote host (guest) through scp.
@param host: Hostname or IP address
@param username: Username (if required)
@param password: Password (if required)
@param local_path: Path on the local machine where we are copying from
@param remote_path: Path on the remote machine where we are copying to
@param limit: Speed limit of file transfer.
@param log_filename: If specified, log all output to this file
@param timeout: The time duration (in seconds) to wait for the transfer
to complete.
@raise: Whatever remote_scp() raises
"""
if (limit):
limit = "-l %s" % (limit)
command = ("scp -v -o UserKnownHostsFile=/dev/null "
"-o PreferredAuthentications=password -r %s "
"-P %s %s %s@\[%s\]:%s" %
(limit, port, local_path, username, host, remote_path))
password_list = []
password_list.append(password)
return remote_scp(command, password_list, log_filename, timeout)
def scp_from_remote(host, port, username, password, remote_path, local_path,
limit="", log_filename=None, timeout=600, ):
"""
Copy files from a remote host (guest).
@param host: Hostname or IP address
@param username: Username (if required)
@param password: Password (if required)
@param local_path: Path on the local machine where we are copying from
@param remote_path: Path on the remote machine where we are copying to
@param limit: Speed limit of file transfer.
@param log_filename: If specified, log all output to this file
@param timeout: The time duration (in seconds) to wait for the transfer
to complete.
@raise: Whatever remote_scp() raises
"""
if (limit):
limit = "-l %s" % (limit)
command = ("scp -v -o UserKnownHostsFile=/dev/null "
"-o PreferredAuthentications=password -r %s "
"-P %s %s@\[%s\]:%s %s" %
(limit, port, username, host, remote_path, local_path))
password_list = []
password_list.append(password)
remote_scp(command, password_list, log_filename, timeout)
def scp_between_remotes(src, dst, port, s_passwd, d_passwd, s_name, d_name,
s_path, d_path, limit="", log_filename=None,
timeout=600):
"""
Copy files from a remote host (guest) to another remote host (guest).
@param src/dst: Hostname or IP address of src and dst
@param s_name/d_name: Username (if required)
@param s_passwd/d_passwd: Password (if required)
@param s_path/d_path: Path on the remote machine where we are copying
from/to
@param limit: Speed limit of file transfer.
@param log_filename: If specified, log all output to this file
@param timeout: The time duration (in seconds) to wait for the transfer
to complete.
@return: True on success and False on failure.
"""
if (limit):
limit = "-l %s" % (limit)
command = ("scp -v -o UserKnownHostsFile=/dev/null -o "
"PreferredAuthentications=password -r %s -P %s"
" %s@\[%s\]:%s %s@\[%s\]:%s" %
(limit, port, s_name, src, s_path, d_name, dst, d_path))
password_list = []
password_list.append(s_passwd)
password_list.append(d_passwd)
return remote_scp(command, password_list, log_filename, timeout)
def nc_copy_between_remotes(src, dst, s_port, s_passwd, d_passwd,
s_name, d_name, s_path, d_path,
c_type="ssh", c_prompt="\n",
d_port="8888", d_protocol="udp", timeout=10,
check_sum=True):
"""
Copy files from a remote host (guest) to another remote host (guest) using
netcat. now this method only support linux
@param src/dst: Hostname or IP address of src and dst
@param s_name/d_name: Username (if required)
@param s_passwd/d_passwd: Password (if required)
@param s_path/d_path: Path on the remote machine where we are copying
@param c_type: Login method to remote host(guest).
@param c_prompt : command line prompt of remote host(guest)
@param d_port: the port data transfer
@param d_protocol : nc protocol use (tcp or udp)
@param timeout: If a connection and stdin are idle for more than timeout
seconds, then the connection is silently closed.
@return: True on success and False on failure.
"""
s_session = remote_login(c_type, src, s_port, s_name, s_passwd, c_prompt)
d_session = remote_login(c_type, dst, s_port, d_name, d_passwd, c_prompt)
s_session.cmd("iptables -I INPUT -p %s -j ACCEPT" % d_protocol)
d_session.cmd("iptables -I OUTPUT -p %s -j ACCEPT" % d_protocol)
logging.info("Transfer data using netcat from %s to %s" % (src, dst))
cmd = "nc"
if d_protocol == "udp":
cmd += " -u"
cmd += " -w %s" % timeout
s_session.sendline("%s -l %s < %s" % (cmd, d_port, s_path))
d_session.sendline("echo a | %s %s %s > %s" % (cmd, src, d_port, d_path))
if check_sum:
if (s_session.cmd("md5sum %s" % s_path).split()[0] !=
d_session.cmd("md5sum %s" % d_path).split()[0]):
return False
return True
def udp_copy_between_remotes(src, dst, s_port, s_passwd, d_passwd,
s_name, d_name, s_path, d_path,
c_type="ssh", c_prompt="\n",
d_port="9000", timeout=600):
"""
Copy files from a remote host (guest) to another remote host (guest) by
udp.
@param src/dst: Hostname or IP address of src and dst
@param s_name/d_name: Username (if required)
@param s_passwd/d_passwd: Password (if required)
@param s_path/d_path: Path on the remote machine where we are copying
@param c_type: Login method to remote host(guest).
@param c_prompt : command line prompt of remote host(guest)
@param d_port: the port data transfer
@param timeout: data transfer timeout
"""
s_session = remote_login(c_type, src, s_port, s_name, s_passwd, c_prompt)
d_session = remote_login(c_type, dst, s_port, d_name, d_passwd, c_prompt)
def send_cmd_safe(session, cmd, timeout=360):
logging.debug("Sending command: %s", cmd)
session.sendline(cmd)
output = ""
got_prompt = False
start_time = time.time()
# Wait for shell prompt until timeout.
while ((time.time() - start_time) < timeout and not got_prompt):
time.sleep(0.2)
session.sendline()
try:
output += session.read_up_to_prompt()
got_prompt = True
except aexpect.ExpectTimeoutError:
pass
return output
def get_abs_path(session, filename, extension):
"""
return file path drive+path
"""
cmd_tmp = "wmic datafile where \"Filename='%s' and "
cmd_tmp += "extension='%s'\" get drive^,path"
cmd = cmd_tmp % (filename, extension)
info = session.cmd_output(cmd, timeout=360).strip()
drive_path = re.search(r'(\w):\s+(\S+)', info, re.M)
if not drive_path:
raise error.TestError("Not found file %s.%s in your guest"
% (filename, extension))
return ":".join(drive_path.groups())
def get_file_md5(session, file_path):
"""
Get files md5sums
"""
if c_type == "ssh":
md5_cmd = "md5sum %s" % file_path
md5_reg = r"(\w+)\s+%s.*" % file_path
else:
drive_path = get_abs_path(session, "md5sums", "exe")
filename = file_path.split("\\")[-1]
md5_reg = r"%s\s+(\w+)" % filename
md5_cmd = '%smd5sums.exe %s | find "%s"' % (drive_path, file_path,
filename)
o = session.cmd_output(md5_cmd)
file_md5 = re.findall(md5_reg, o)
if not o:
raise error.TestError("Get file %s md5sum error" % file_path)
return file_md5
def server_alive(session):
if c_type == "ssh":
check_cmd = "ps aux"
else:
check_cmd = "tasklist"
o = session.cmd_output(check_cmd)
if not o:
raise error.TestError("Can not get the server status")
if "sendfile" in o.lower():
return True
return False
def start_server(session):
if c_type == "ssh":
start_cmd = "sendfile %s &" % d_port
else:
drive_path = get_abs_path(session, "sendfile", "exe")
start_cmd = "start /b %ssendfile.exe %s" % (drive_path,
d_port)
send_cmd_safe(session, start_cmd)
if not server_alive(session):
raise error.TestError("Start udt server failed")
def start_client(session):
if c_type == "ssh":
client_cmd = "recvfile %s %s %s %s" % (src, d_port,
s_path, d_path)
else:
drive_path = get_abs_path(session, "recvfile", "exe")
client_cmd_tmp = "%srecvfile.exe %s %s %s %s"
client_cmd = client_cmd_tmp % (drive_path, src, d_port,
s_path.split("\\")[-1],
d_path.split("\\")[-1])
send_cmd_safe(session, client_cmd, timeout)
def stop_server(session):
if c_type == "ssh":
stop_cmd = "killall sendfile"
else:
stop_cmd = "taskkill /F /IM sendfile.exe"
if server_alive(session):
send_cmd_safe(session, stop_cmd)
try:
src_md5 = get_file_md5(s_session, s_path)
if not server_alive(s_session):
start_server(s_session)
start_client(d_session)
dst_md5 = get_file_md5(d_session, d_path)
if src_md5 != dst_md5:
err_msg = "Files md5sum mismatch, file %s md5sum is '%s', "
err_msg = "but the file %s md5sum is %s"
raise error.TestError(err_msg % (s_path, src_md5,
d_path, dst_md5))
finally:
stop_server(s_session)
s_session.close()
d_session.close()
def copy_files_to(address, client, username, password, port, local_path,
remote_path, limit="", log_filename=None,
verbose=False, timeout=600):
"""
Copy files to a remote host (guest) using the selected client.
@param client: Type of transfer client
@param username: Username (if required)
@param password: Password (if requried)
@param local_path: Path on the local machine where we are copying from
@param remote_path: Path on the remote machine where we are copying to
@param address: Address of remote host(guest)
@param limit: Speed limit of file transfer.
@param log_filename: If specified, log all output to this file (SCP only)
@param verbose: If True, log some stats using logging.debug (RSS only)
@param timeout: The time duration (in seconds) to wait for the transfer to
complete.
@raise: Whatever remote_scp() raises
"""
if client == "scp":
scp_to_remote(address, port, username, password, local_path,
remote_path, limit, log_filename, timeout)
elif client == "rss":
log_func = None
if verbose:
log_func = logging.debug
c = rss_client.FileUploadClient(address, port, log_func)
c.upload(local_path, remote_path, timeout)
c.close()
def copy_files_from(address, client, username, password, port, remote_path,
local_path, limit="", log_filename=None,
verbose=False, timeout=600):
"""
Copy files from a remote host (guest) using the selected client.
@param client: Type of transfer client
@param username: Username (if required)
@param password: Password (if requried)
@param remote_path: Path on the remote machine where we are copying from
@param local_path: Path on the local machine where we are copying to
@param address: Address of remote host(guest)
@param limit: Speed limit of file transfer.
@param log_filename: If specified, log all output to this file (SCP only)
@param verbose: If True, log some stats using logging.debug (RSS only)
@param timeout: The time duration (in seconds) to wait for the transfer to
complete.
@raise: Whatever remote_scp() raises
"""
if client == "scp":
scp_from_remote(address, port, username, password, remote_path,
local_path, limit, log_filename, timeout)
elif client == "rss":
log_func = None
if verbose:
log_func = logging.debug
c = rss_client.FileDownloadClient(address, port, log_func)
c.download(remote_path, local_path, timeout)
c.close()
class RemoteFile(object):
"""
Class to handle the operations of file on remote host or guest.
"""
def __init__(self, address, client, username, password, port,
remote_path, limit="", log_filename=None,
verbose=False, timeout=600):
"""
Initialization of RemoteFile class.
@param address: Address of remote host(guest)
@param client: Type of transfer client
@param username: Username (if required)
@param password: Password (if requried)
@param remote_path: Path of file which we want to edit on remote.
@param limit: Speed limit of file transfer.
@param log_filename: If specified, log all output to this file(SCP only)
@param verbose: If True, log some stats using logging.debug (RSS only)
@param timeout: The time duration (in seconds) to wait for the
transfer tocomplete.
"""
self.address = address
self.client = client
self.username = username
self.password = password
self.port = port
self.remote_path = remote_path
self.limit = limit
self.log_filename = log_filename
self.verbose = verbose
self.timeout = timeout
#Get a local_path and all actions is taken on it.
filename = os.path.basename(self.remote_path)
#Get a local_path.
tmp_dir = data_dir.get_tmp_dir()
local_file = tempfile.NamedTemporaryFile(prefix=("%s_" % filename),
dir=tmp_dir)
self.local_path = local_file.name
local_file.close()
#Get a backup_path.
back_dir = data_dir.get_backing_data_dir()
backup_file = tempfile.NamedTemporaryFile(prefix=("%s_" % filename),
dir=tmp_dir)
self.backup_path = backup_file.name
backup_file.close()
#Get file from remote.
self._pull_file()
#Save a backup.
shutil.copy(self.local_path, self.backup_path)
def __del__(self):
"""
Called when the instance is about to be destroyed.
"""
self._reset_file()
if os.path.exists(self.backup_path):
os.remove(self.backup_path)
if os.path.exists(self.local_path):
os.remove(self.local_path)
def _pull_file(self):
"""
Copy file from remote to local.
"""
if self.client == "test":
shutil.copy(self.remote_path, self.local_path)
else:
copy_files_from(self.address, self.client, self.username,
self.password, self.port, self.remote_path,
self.local_path, self.limit, self.log_filename,
self.verbose, self.timeout)
def _push_file(self):
"""
Copy file from local to remote.
"""
if self.client == "test":
shutil.copy(self.local_path, self.remote_path)
else:
copy_files_to(self.address, self.client, self.username,
self.password, self.port, self.local_path,
self.remote_path, self.limit, self.log_filename,
self.verbose, self.timeout)
def _reset_file(self):
"""
Copy backup from local to remote.
"""
if self.client == "test":
shutil.copy(self.backup_path, self.remote_path)
else:
copy_files_to(self.address, self.client, self.username,
self.password, self.port, self.backup_path,
self.remote_path, self.limit, self.log_filename,
self.verbose, self.timeout)
def _read_local(self):
"""
Read file on local_path.
@return: string list got from readlines().
"""
local_file = open(self.local_path, "r")
lines = local_file.readlines()
local_file.close()
return lines
def _write_local(self, lines):
"""
Write file on local_path. Call writelines method of File.
"""
local_file = open(self.local_path, "w")
local_file.writelines(lines)
local_file.close()
def add(self, line_list):
"""
Append lines in line_list into file on remote.
"""
lines = self._read_local()
for line in line_list:
lines.append("\n%s" % line)
self._write_local(lines)
self._push_file()
def sub(self, pattern2repl_dict):
"""
Replace the string which match the pattern
to the value contained in pattern2repl_dict.
"""
lines = self._read_local()
for pattern, repl in pattern2repl_dict.items():
for index in range(len(lines)):
line = lines[index]
lines[index] = re.sub(pattern, repl, line)
self._write_local(lines)
self._push_file()
def remove(self, pattern_list):
"""
Remove the lines in remote file which matchs a pattern
in pattern_list.
"""
lines = self._read_local()
for pattern in pattern_list:
for index in range(len(lines)):
line = lines[index]
if re.match(pattern, line):
lines.remove(line)
#Check this line is the last one or not.
if (not line.endswith('\n') and (index >0)):
lines[index-1] = lines[index-1].rstrip("\n")
self._write_local(lines)
self._push_file()