Changeset 37776ea
- Timestamp:
- Mar 2, 2010 7:31:35 AM (15 years ago)
- Branches:
- axis_example, compt_changes, info-ops, master, version-3.01, version-3.02
- Children:
- 6f82229
- Parents:
- 6d07908
- Location:
- fedd/federation
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fedd/federation/proxy_emulab_segment.py
r6d07908 r37776ea 10 10 import signal 11 11 12 from proxy_segment import proxy_segment 12 13 from service_error import service_error 13 14 14 class proxy_emulab_segment: 15 class ssh_cmd_timeout(RuntimeError): pass 16 15 class start_segment(proxy_segment): 17 16 def __init__(self, log=None, keyfile=None, debug=False): 18 self.log = log or logging.getLogger(\ 19 'fedd.access.proxy_emulab_segment') 20 self.ssh_privkey_file = keyfile 21 self.debug = debug 22 self.ssh_exec="/usr/bin/ssh" 23 self.scp_exec = "/usr/bin/scp" 24 self.ssh_cmd_timeout = proxy_emulab_segment.ssh_cmd_timeout 25 26 def scp_file(self, file, user, host, dest=""): 27 """ 28 scp a file to the remote host. If debug is set the action is only 29 logged. 30 """ 31 32 scp_cmd = [self.scp_exec, '-o', 'IdentitiesOnly yes', 33 '-o', 'StrictHostKeyChecking no', '-i', 34 self.ssh_privkey_file, file, 35 "%s@%s:%s" % (user, host, dest)] 36 rv = 0 37 38 try: 39 dnull = open("/dev/null", "w") 40 except IOError: 41 self.log.debug("[ssh_file]: failed to open " + \ 42 "/dev/null for redirect") 43 dnull = Null 44 45 self.log.debug("[scp_file]: %s" % " ".join(scp_cmd)) 46 if not self.debug: 47 rv = subprocess.call(scp_cmd, stdout=dnull, 48 stderr=dnull, close_fds=True) 49 50 return rv == 0 51 52 def ssh_cmd(self, user, host, cmd, wname=None, timeout=None): 53 """ 54 Run a remote command on host as user. If debug is set, the action 55 is only logged. Commands are run without stdin, to avoid stray 56 SIGTTINs. 57 """ 58 sh_str = ("%s -n -o 'IdentitiesOnly yes' -o " + \ 59 "'StrictHostKeyChecking no' -i %s %s@%s %s") % \ 60 (self.ssh_exec, self.ssh_privkey_file, 61 user, host, cmd) 62 63 try: 64 dnull = open("/dev/null", "w") 65 except IOError: 66 self.log.debug("[ssh_cmd]: failed to open /dev/null " + \ 67 "for redirect") 68 dnull = Null 69 70 self.log.debug("[ssh_cmd]: %s" % sh_str) 71 if not self.debug: 72 if dnull: 73 sub = subprocess.Popen(sh_str, shell=True, stdout=dnull, 74 stderr=dnull, close_fds=True) 75 else: 76 sub = subprocess.Popen(sh_str, shell=True, close_fds=True) 77 if timeout: 78 i = 0 79 rv = sub.poll() 80 while i < timeout: 81 if rv is not None: break 82 else: 83 time.sleep(1) 84 rv = sub.poll() 85 i += 1 86 else: 87 self.log.debug("Process exceeded runtime: %s" % sh_str) 88 os.kill(sub.pid, signal.SIGKILL) 89 raise self.ssh_cmd_timeout(); 90 return rv == 0 91 else: 92 return sub.wait() == 0 93 else: 94 if timeout == 0: 95 self.log.debug("debug timeout raised on %s " % sh_str) 96 raise self.ssh_cmd_timeout() 97 else: 98 return True 99 100 class start_segment(proxy_emulab_segment): 101 def __init__(self, log=None, keyfile=None, debug=False): 102 proxy_emulab_segment.__init__(self, log=log, 103 keyfile=keyfile, debug=debug) 17 proxy_segment.__init__(self, log=log, keyfile=keyfile, debug=debug) 104 18 self.null = """ 105 19 set ns [new Simulator] … … 295 209 return True 296 210 297 class stop_segment(proxy_ emulab_segment):211 class stop_segment(proxy_segment): 298 212 def __init__(self, log=None, keyfile=None, debug=False): 299 proxy_emulab_segment.__init__(self, 300 log=log, keyfile=keyfile, debug=debug) 213 proxy_segment.__init__(self, log=log, keyfile=keyfile, debug=debug) 301 214 302 215 def __call__(self, parent, user, pid, eid):
Note: See TracChangeset
for help on using the changeset viewer.