Mercurial > public > mercurial-scm > hg-stable
annotate contrib/automation/hgautomation/ssh.py @ 43076:2372284d9457
formatting: blacken the codebase
This is using my patch to black
(https://github.com/psf/black/pull/826) so we don't un-wrap collection
literals.
Done with:
hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S
# skip-blame mass-reformatting only
# no-check-commit reformats foo_bar functions
Differential Revision: https://phab.mercurial-scm.org/D6971
author | Augie Fackler <augie@google.com> |
---|---|
date | Sun, 06 Oct 2019 09:45:02 -0400 |
parents | 65b3ef162b39 |
children | 24ee91ba9aa8 |
rev | line source |
---|---|
42312
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
1 # ssh.py - Interact with remote SSH servers |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
2 # |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
3 # Copyright 2019 Gregory Szorc <gregory.szorc@gmail.com> |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
4 # |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
6 # GNU General Public License version 2 or any later version. |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
7 |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
8 # no-check-code because Python 3 native. |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
9 |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
10 import socket |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
11 import time |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
12 import warnings |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
13 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42312
diff
changeset
|
14 from cryptography.utils import CryptographyDeprecationWarning |
42312
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
15 import paramiko |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
16 |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
17 |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
18 def wait_for_ssh(hostname, port, timeout=60, username=None, key_filename=None): |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
19 """Wait for an SSH server to start on the specified host and port.""" |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42312
diff
changeset
|
20 |
42312
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
21 class IgnoreHostKeyPolicy(paramiko.MissingHostKeyPolicy): |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
22 def missing_host_key(self, client, hostname, key): |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
23 return |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
24 |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
25 end_time = time.time() + timeout |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
26 |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
27 # paramiko triggers a CryptographyDeprecationWarning in the cryptography |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
28 # package. Let's suppress |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
29 with warnings.catch_warnings(): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42312
diff
changeset
|
30 warnings.filterwarnings( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42312
diff
changeset
|
31 'ignore', category=CryptographyDeprecationWarning |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42312
diff
changeset
|
32 ) |
42312
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
33 |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
34 while True: |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
35 client = paramiko.SSHClient() |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
36 client.set_missing_host_key_policy(IgnoreHostKeyPolicy()) |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
37 try: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42312
diff
changeset
|
38 client.connect( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42312
diff
changeset
|
39 hostname, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42312
diff
changeset
|
40 port=port, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42312
diff
changeset
|
41 username=username, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42312
diff
changeset
|
42 key_filename=key_filename, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42312
diff
changeset
|
43 timeout=5.0, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42312
diff
changeset
|
44 allow_agent=False, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42312
diff
changeset
|
45 look_for_keys=False, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42312
diff
changeset
|
46 ) |
42312
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
47 |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
48 return client |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
49 except socket.error: |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
50 pass |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
51 except paramiko.AuthenticationException: |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
52 raise |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
53 except paramiko.SSHException: |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
54 pass |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
55 |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
56 if time.time() >= end_time: |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
57 raise Exception('Timeout reached waiting for SSH') |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
58 |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
59 time.sleep(1.0) |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
60 |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
61 |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
62 def exec_command(client, command): |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
63 """exec_command wrapper that combines stderr/stdout and returns channel""" |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
64 chan = client.get_transport().open_session() |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
65 |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
66 chan.exec_command(command) |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
67 chan.set_combine_stderr(True) |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
68 |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
69 stdin = chan.makefile('wb', -1) |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
70 stdout = chan.makefile('r', -1) |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
71 |
65b3ef162b39
automation: initial support for running Linux tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
72 return chan, stdin, stdout |