[37776ea] | 1 | #!/usr/local/bin/python |
---|
| 2 | |
---|
| 3 | import sys, os |
---|
| 4 | import re |
---|
| 5 | |
---|
| 6 | import tempfile |
---|
| 7 | import subprocess |
---|
| 8 | import logging |
---|
| 9 | import time |
---|
| 10 | import signal |
---|
| 11 | |
---|
| 12 | from service_error import service_error |
---|
| 13 | |
---|
[06c1dba] | 14 | class ssh_emulab_segment: |
---|
[5bf359d] | 15 | """ |
---|
| 16 | Base class for segment starter classes that access their underlying testebd |
---|
| 17 | remotely using ssh. It is promarily a code repository for commonly used |
---|
| 18 | ssh commands for moving code and logging in. |
---|
| 19 | """ |
---|
[37776ea] | 20 | class ssh_cmd_timeout(RuntimeError): pass |
---|
| 21 | |
---|
| 22 | def __init__(self, log=None, keyfile=None, debug=False): |
---|
[5bf359d] | 23 | """ |
---|
| 24 | log is the logging.log to print messages to, keyfile is the private key |
---|
| 25 | for ssh interactions and if debug is true, commands are not executed |
---|
| 26 | using ssh. |
---|
| 27 | """ |
---|
[c7141dc] | 28 | self.log = log or logging.getLogger('fedd.access.ssh_emulab_segment') |
---|
[37776ea] | 29 | self.ssh_privkey_file = keyfile |
---|
| 30 | self.debug = debug |
---|
| 31 | self.ssh_exec="/usr/bin/ssh" |
---|
| 32 | self.scp_exec = "/usr/bin/scp" |
---|
[c7141dc] | 33 | self.ssh_cmd_timeout = ssh_emulab_segment.ssh_cmd_timeout |
---|
[37776ea] | 34 | |
---|
| 35 | def scp_file(self, file, user, host, dest=""): |
---|
| 36 | """ |
---|
| 37 | scp a file to the remote host. If debug is set the action is only |
---|
| 38 | logged. |
---|
| 39 | """ |
---|
| 40 | |
---|
| 41 | scp_cmd = [self.scp_exec, '-o', 'IdentitiesOnly yes', |
---|
[e777dab] | 42 | '-o', 'StrictHostKeyChecking no', '-o', 'ForwardX11 no','-i', |
---|
[37776ea] | 43 | self.ssh_privkey_file, file, |
---|
| 44 | "%s@%s:%s" % (user, host, dest)] |
---|
| 45 | rv = 0 |
---|
| 46 | |
---|
| 47 | try: |
---|
| 48 | dnull = open("/dev/null", "w") |
---|
[d3c8759] | 49 | except EnvironmentError: |
---|
[37776ea] | 50 | self.log.debug("[ssh_file]: failed to open " + \ |
---|
| 51 | "/dev/null for redirect") |
---|
| 52 | dnull = Null |
---|
| 53 | |
---|
| 54 | self.log.debug("[scp_file]: %s" % " ".join(scp_cmd)) |
---|
| 55 | if not self.debug: |
---|
| 56 | rv = subprocess.call(scp_cmd, stdout=dnull, |
---|
| 57 | stderr=dnull, close_fds=True) |
---|
| 58 | |
---|
| 59 | return rv == 0 |
---|
| 60 | |
---|
[8fbef04] | 61 | def scp_file_from(self, file, user, host, dest=""): |
---|
| 62 | """ |
---|
| 63 | scp a file to the remote host. If debug is set the action is only |
---|
| 64 | logged. |
---|
| 65 | """ |
---|
| 66 | |
---|
| 67 | scp_cmd = [self.scp_exec, '-o', 'IdentitiesOnly yes', |
---|
| 68 | '-o', 'StrictHostKeyChecking no', '-o', 'ForwardX11 no','-i', |
---|
| 69 | self.ssh_privkey_file, "%s@%s:%s" % (user, host, file), dest] |
---|
| 70 | rv = 0 |
---|
| 71 | |
---|
| 72 | try: |
---|
| 73 | dnull = open("/dev/null", "w") |
---|
| 74 | except EnvironmentError: |
---|
| 75 | self.log.debug("[ssh_file]: failed to open " + \ |
---|
| 76 | "/dev/null for redirect") |
---|
| 77 | dnull = Null |
---|
| 78 | |
---|
| 79 | self.log.debug("[scp_file]: %s" % " ".join(scp_cmd)) |
---|
| 80 | if not self.debug: |
---|
| 81 | rv = subprocess.call(scp_cmd, stdout=dnull, |
---|
| 82 | stderr=dnull, close_fds=True) |
---|
| 83 | |
---|
| 84 | return rv == 0 |
---|
| 85 | |
---|
[37776ea] | 86 | def ssh_cmd(self, user, host, cmd, wname=None, timeout=None): |
---|
| 87 | """ |
---|
| 88 | Run a remote command on host as user. If debug is set, the action |
---|
| 89 | is only logged. Commands are run without stdin, to avoid stray |
---|
| 90 | SIGTTINs. |
---|
| 91 | """ |
---|
| 92 | sh_str = ("%s -n -o 'IdentitiesOnly yes' -o " + \ |
---|
[e777dab] | 93 | "'StrictHostKeyChecking no' -o 'ForwardX11 no' " + |
---|
| 94 | "-i %s %s@%s %s") % \ |
---|
[37776ea] | 95 | (self.ssh_exec, self.ssh_privkey_file, |
---|
| 96 | user, host, cmd) |
---|
| 97 | |
---|
| 98 | try: |
---|
| 99 | dnull = open("/dev/null", "w") |
---|
[d3c8759] | 100 | except EnvironmentError: |
---|
[37776ea] | 101 | self.log.debug("[ssh_cmd]: failed to open /dev/null " + \ |
---|
| 102 | "for redirect") |
---|
| 103 | dnull = Null |
---|
| 104 | |
---|
| 105 | self.log.debug("[ssh_cmd]: %s" % sh_str) |
---|
| 106 | if not self.debug: |
---|
| 107 | if dnull: |
---|
| 108 | sub = subprocess.Popen(sh_str, shell=True, stdout=dnull, |
---|
| 109 | stderr=dnull, close_fds=True) |
---|
| 110 | else: |
---|
| 111 | sub = subprocess.Popen(sh_str, shell=True, close_fds=True) |
---|
| 112 | if timeout: |
---|
| 113 | i = 0 |
---|
| 114 | rv = sub.poll() |
---|
| 115 | while i < timeout: |
---|
| 116 | if rv is not None: break |
---|
| 117 | else: |
---|
| 118 | time.sleep(1) |
---|
| 119 | rv = sub.poll() |
---|
| 120 | i += 1 |
---|
| 121 | else: |
---|
| 122 | self.log.debug("Process exceeded runtime: %s" % sh_str) |
---|
| 123 | os.kill(sub.pid, signal.SIGKILL) |
---|
| 124 | raise self.ssh_cmd_timeout(); |
---|
| 125 | return rv == 0 |
---|
| 126 | else: |
---|
| 127 | return sub.wait() == 0 |
---|
| 128 | else: |
---|
| 129 | if timeout == 0: |
---|
| 130 | self.log.debug("debug timeout raised on %s " % sh_str) |
---|
| 131 | raise self.ssh_cmd_timeout() |
---|
| 132 | else: |
---|
| 133 | return True |
---|
| 134 | |
---|