Skip to content
Snippets Groups Projects
Commit 857539fd authored by mads's avatar mads
Browse files

Improved error handling

parent 4497a8fe
No related branches found
No related tags found
No related merge requests found
...@@ -24,7 +24,10 @@ class Resource(object): ...@@ -24,7 +24,10 @@ class Resource(object):
def ok2submit(self): def ok2submit(self):
"""Always ok to have min_cpu cpus and ok to have more if there are min_free free cpus""" """Always ok to have min_cpu cpus and ok to have more if there are min_free free cpus"""
total, free, user = self.check_resources() try:
total, free, user = self.check_resources()
except:
return False
if user < self.min_cpu: if user < self.min_cpu:
return True return True
...@@ -75,9 +78,7 @@ class SSHPBSClusterResource(Resource, SSHClient): ...@@ -75,9 +78,7 @@ class SSHPBSClusterResource(Resource, SSHClient):
cpu_free, nodeSum = pbswrap.count_cpus(users, host, pbsnodes) cpu_free, nodeSum = pbswrap.count_cpus(users, host, pbsnodes)
return nodeSum['used_cpu'] + cpu_free, cpu_free, cpu_user return nodeSum['used_cpu'] + cpu_free, cpu_free, cpu_user
except IOError as e: except Exception as e:
raise e
except:
raise EnvironmentError("check resources failed") raise EnvironmentError("check resources failed")
def jobids(self, jobname_prefix): def jobids(self, jobname_prefix):
......
...@@ -25,6 +25,8 @@ class SSHClient(object): ...@@ -25,6 +25,8 @@ class SSHClient(object):
self.key = key self.key = key
self.disconnect = 0 self.disconnect = 0
self.client = None self.client = None
self.sftp = None
self.transport = None
if key is not None: if key is not None:
self.key = paramiko.RSAKey.from_private_key(StringIO(key), password=passphrase) self.key = paramiko.RSAKey.from_private_key(StringIO(key), password=passphrase)
...@@ -33,7 +35,8 @@ class SSHClient(object): ...@@ -33,7 +35,8 @@ class SSHClient(object):
def __enter__(self): def __enter__(self):
self.disconnect += 1 self.disconnect += 1
if self.client is None: if self.client is None or self.client._transport is None or self.client._transport.is_active() is False:
self.close()
self.connect() self.connect()
return self.client return self.client
...@@ -44,9 +47,7 @@ class SSHClient(object): ...@@ -44,9 +47,7 @@ class SSHClient(object):
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(self.host, self.port, username=self.username, password=self.password, pkey=self.key, timeout=self.TIMEOUT) self.client.connect(self.host, self.port, username=self.username, password=self.password, pkey=self.key, timeout=self.TIMEOUT)
assert self.client is not None assert self.client is not None
self.transport = paramiko.Transport((self.host, self.port)) self.sftp = paramiko.SFTPClient.from_transport(self.client._transport)
self.transport.connect(username=self.username, password=self.password)
self.sftp = paramiko.SFTPClient.from_transport(self.transport)
return self return self
def __exit__(self, *args): def __exit__(self, *args):
...@@ -85,11 +86,12 @@ class SSHClient(object): ...@@ -85,11 +86,12 @@ class SSHClient(object):
print (ret) print (ret)
def close(self): def close(self):
if self.client is not None: for x in ["sftp", "client" ]:
self.client.close() try:
self.client = None getattr(self, x).close()
self.sftp.close() setattr(self, x, None)
self.transport.close() except:
pass
self.disconnect = False self.disconnect = False
def file_exists(self, filename): def file_exists(self, filename):
...@@ -180,6 +182,7 @@ class SharedSSHClient(SSHClient): ...@@ -180,6 +182,7 @@ class SharedSSHClient(SSHClient):
while self.next != threading.currentThread(): while self.next != threading.currentThread():
time.sleep(1) time.sleep(1)
SSHClient.__enter__(self)
return self.client return self.client
def __exit__(self, *args): def __exit__(self, *args):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment