-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconnection.py
More file actions
271 lines (228 loc) · 9.6 KB
/
connection.py
File metadata and controls
271 lines (228 loc) · 9.6 KB
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
# Copyright 2012 University of Pittsburgh
# Copyright 2012 Pittsburgh Supercomputing Center
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
'''
Created on Feb, 8, 2013
@author: Shawn Brown
'''
import os,sys
import paramiko
import select
import datetime,dateutil.parser
import time
import math
from multiprocessing import Process,Queue
import simWS
from logger import Log
class SSHConn:
def __init__(self, logger_, machineName_="olympus.psc.edu", debug_=False):
# Machine Parameters
self.logger = logger_
if machineName_ not in simWS.configuration['machines'].keys():
self.logger.update('CONF_MACHINE_FIND_FAILED')
self._machine = machineName_
self._configuration = simWS.configuration['machines'][self._machine]
self._simConfigs = simWS.configuration['simulators']
self._username = self._configuration['username']
self._password = self._configuration['password']
self._privateKeyFile = self._configuration['privateKeyFile']
self._localConfiguration = simWS.configuration['local']
self._ssh = None
self._transport = None
self._sftp = None
self.isConnectedSSH = False
self.isConnectedSFTP = False
self.name = "SSH Connection: %s"%self._machine
self.debug = debug_
### Flags for internals
self._runPBS = False
self._numUNKNOWN = 0
if self._configuration['queueType'] == 'PBS':
self._runPBS = True
self._remoteDir = self._configuration['remoteDir']
if self._remoteDir == "$SCRATCH":
try:
if self.debug: print 'On connection %s getting scratch directory'%self.name
self._remoteDir = self._getScratchDir()
if self.debug: print 'Got %s as scratch directory on connection %s'%(self._remoteDir,self.name)
self.logger.update('SSH_SCRATCH_DIR_SUCCESS')
except Exception as e:
self.logger.update('SSH_SCRATCH_DIR_FAILED',message="%s"%str(e))
raise e
self._remoteTmpDir = None
self._runScript = None
self._runScriptName = None
self._directRunDirectory = None
self._startTiming = False
def _connect(self,blocking=True):
### open SSH connection
timeout = 3000
count = 0
while True:
if (self.isConnectedSSH is False and self.isConnectedSFTP is False):
break
if count > timeout:
self.logger.update("SSH_CONN_TIMEOUT")
self._close()
time.sleep(0.5)
count += 1
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
if self.debug:
print 'Establishing SSH connection %s'%(self.name)
try:
self.ssh.connect(self._machine,
username=self._username,
password=self._password,
key_filename=self._privateKeyFile)
self.isConnectedSSH = True
self.logger.update('SSH_CONN_ESTABLISHED')
except Exception as e:
self.logger.update('SSH_CONN_FAILED', message="%s" % str(e))
raise e
if self.debug: print 'SSH connection %s established'%(self.name)
### open SFTP connection
if self.debug: print 'Establishing SFTP connection %s'%(self.name)
try:
self.transport = paramiko.Transport((self._machine,22))
if self._privateKeyFile:
privateKey = paramiko.RSAKey.from_private_key_file(self._privateKeyFile)
self.transport.connect(username=self._username,
pkey = privateKey)
else:
self.transport.connect(username=self._username,
password=self._password)
self.sftp = paramiko.SFTPClient.from_transport(self.transport)
self.isConnectedSFTP = True
self.logger.update('SFTP_CONN_ESTABLISHED')
except Exception as e:
self.logger.update('SFTP_CONN_FAILED',message="%s"%str(e))
raise e
if self.debug: print 'SFTP connection %s established'%self.name
def _close(self):
try:
self.ssh.close()
self.isConnectedSSH = False
self.logger.update("SSH_CONN_CLOSED")
except:
self.logger.update("SSH_CONN_CLOSE_FAILED")
raise
try:
self.sftp.close()
self.transport.close()
self.isConnectedSFTP = False
self.logger.update("SFTP_CONN_CLOSED")
except:
self.logger.update("SFTP_CONN_CLOSE_FAILED")
raise
def _getScratchDir(self):
remote_command = 'echo $SCRATCH'
try:
returnVal = None
while returnVal is None:
returnVal = self._executeCommand(remote_command)
scratchString = ""
scratchString = returnVal.strip()
self.logger.update("SSH_GETSCRATCH_SUCCESS",message="%s"%scratchString)
except Exception as e:
self.logger.update("SSH_GETSCRATCH_FAILED",message="%s"%str(e))
raise e
return scratchString
def _executeCommand(self,command):
stdin=None
stdout=None
stderr=None
returnValue=None
try:
self._connect()
if self.debug: print "Excecuting %s"%command
stdin,stdout,stderr = self.ssh.exec_command(command)
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
rl,wl,xl = select.select([stdout.channel],[],[],0.0)
if len(rl) > 0:
returnValue = stdout.channel.recv(1024)
self._close()
self.logger.update('SSH_EXECUTE_SUCCESS',message='Command = %s'%command)
except Exception as e:
self.logger.update('SSH_EXECUTE_FAILED',message='Command = %s,%s'%(command,str(e)))
print "There was an error exectuting the command on connections %s:"%self.name
print "%s"%command
print "stderr returned: %s"%str(stderr)
raise e
return returnValue
def _mkdir(self,remoteDirectoryName):
if self.debug: print "Making %s directory on %s"%(remoteDirectoryName,self.name)
if self._remoteDir is not None:
remoteDirectoryName = self._remoteDir + "/" + remoteDirectoryName
if self.debug: print "Parsed remote directory is %s"%remoteDirectoryName
try:
self._connect()
self.sftp.mkdir(remoteDirectoryName)
self.logger.update('SSH_MKDIR_SUCCESS',message='%s'%remoteDirectoryName)
self._close()
except Exception as e:
print e
self.logger.update('SSH_MKDIR_FAILED',message='%s'%remoteDirectoryName)
raise e
# def _sendStringToFile(self,content,remoteFileName=None):
#
# if remoteFileName is None:
# remoteFileName = localFileName
#
# if self._remoteDir is not None:
# remoteFileName = self._remoteDir + "/" + remoteFileName
#
# if self.isConnectedSSH is False:
# self.logger.update("SSH_SENDSTRTOFILE_BEFORE_ESTB")
# raise RuntimeError("Trying to send a string to a file through %s before openning the SSH connection"\
# %self.name)
#
# try:
# command = "echo \"%s\" > %s"%(content,remoteFileName)
# #command = "echo \"%s\""%(content)
# print "command = " + command
# self._executeCommand(command)
# self.logger.update("SSH_SENDSTRTOFILE_SUCCESS",message="STRING->%s"%(remoteFileName))
# except Exception as e:
# self.logger.update("SSH_SENDSTRTOFILE_FAILED",message="STRING->%s: %s"%(remoteFileName,str(e)))
# raise
def sendFile(self,localFileName,remoteFileName=None):
if remoteFileName is None:
remoteFileName = localFileName
if self._remoteDir is not None:
remoteFileName = self._remoteDir + "/" + remoteFileName
try:
self._connect()
self.sftp.put(localFileName,remoteFileName)
self.logger.update("SSH_SENDFILE_SUCCESS",message="%s->%s"%(localFileName,remoteFileName))
self._close()
except Exception as e:
self.logger.update("SSH_SENDFILE_FAILED",message="%s->%s: %s"%(localFileName,remoteFileName,str(e)))
raise
def main():
import random
import time
connections = {}
#sys.exit()
### Test on Blacklight
logger = Log(logFileName_='./test.log')
for i in range(0,1):
tempId = random.randint(0,100000)
#if i < 2:
connections[tempId] = SSHConn(logger,machineName_='fe-sandbox.psc.edu',debug_=True)
connections[tempId]._mkdir(simWS.configuration['simulators']['test']['runDirPrefix']+"."+str(tempId))
### Main Hook
if __name__=="__main__":
main()