-
Notifications
You must be signed in to change notification settings - Fork 3
/
hosts.py
234 lines (183 loc) · 6.18 KB
/
hosts.py
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
# -*- coding: UTF-8 -*-
# Copyright (C) 2009-2010 Juan David Ibáñez Palomar <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import from the Standard Library
from contextlib import closing
from getpass import getpass
from os.path import basename, expanduser
import socket
from stat import S_ISDIR
from sys import stdout
# Import from paramiko
from paramiko import AutoAddPolicy, SSHClient, PasswordRequiredException
# Add log
import paramiko
paramiko.util.log_to_file(expanduser("~/.usine/paramiko.log"))
# Import from itools
from itools.core import get_pipe
from itools.log import log_info
"""
This module provides a common interface to access the localhost and to
access a remote host (through paramiko). The common API is:
- run: to execute a command
- put: to copy a file
"""
###########################################################################
# Local host
###########################################################################
class LocalHost(object):
cwd = None
def chdir(self, cwd):
self.cwd = expanduser(cwd)
def run(self, command, cwd=None):
# Change dir
if cwd:
self.chdir(cwd)
# Format command
if type(command) is str:
command = expanduser(command)
command_str = command
command = command.split()
else:
command_str = ' '.join(command)
# Print
log_info('%s $ %s' % (self.cwd, command_str))
# Call
return get_pipe(command, cwd=self.cwd)
def put(self, source, target):
raise NotImplementedError
###########################################################################
# Remote host
###########################################################################
def read_from_channel(recv):
buffer = []
try:
data = recv(1024)
while data:
buffer.append(data)
data = recv(1024)
except socket.timeout:
pass
return ''.join(buffer)
def run_with_shell(channel, cwd, command):
command = 'cd %s\n%s\necho EOF\n' % (cwd, command)
# Call
channel.invoke_shell()
channel.settimeout(0.0)
channel.send(command)
while True:
data = read_from_channel(channel.recv)
# Check EOF
if not data or data.endswith('EOF\n'):
data = data[:-4]
stdout.write(data)
stdout.flush()
break
stdout.write(data)
stdout.flush()
# Error
data = read_from_channel(channel.recv_stderr)
if data:
stdout.write(data)
stdout.flush()
def run_without_shell(channel, cwd, command):
command = 'cd %s && %s' % (cwd, command)
# Call
channel.exec_command(command)
status = channel.recv_exit_status()
if status:
print 'ERROR'
data = channel.recv_stderr(512)
while data:
stdout.write(data)
stdout.flush()
data = channel.recv_stderr(512)
else:
data = channel.recv(512)
while data:
stdout.write(data)
stdout.flush()
data = channel.recv(512)
class RemoteHost(object):
def __init__(self, host, user, shell):
host, port = host.split(':')
self.host = host
self.port = int(port)
self.user = user
self.shell = shell # True or False
# Connection
self.ssh = None
def chdir(self, cwd):
self.cwd = cwd
@property
def transport(self):
if self.ssh is None:
log_info('Connect %s@%s:%s' % (self.user, self.host, self.port))
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(AutoAddPolicy())
try:
ssh.connect(self.host, self.port, self.user)
except PasswordRequiredException:
password = getpass('Enter passphrase for key: ')
ssh.connect(self.host, self.port, self.user, password)
self.ssh = ssh
return self.ssh.get_transport()
def close(self):
if self.ssh:
self.ssh.close()
self.ssh = None
def run(self, command, cwd=None, quiet=False):
# Change dir
if cwd:
self.chdir(cwd)
# Print
if quiet is False:
log_info('%s@%s %s $ %s' % (self.user, self.host, self.cwd, command))
channel = self.transport.open_channel('session')
try:
if self.shell:
run_with_shell(channel, self.cwd, command)
else:
run_without_shell(channel, self.cwd, command)
finally:
channel.close()
def put(self, source, target):
ftp = self.transport.open_sftp_client()
with closing(ftp) as ftp:
target = target.replace('~', ftp.normalize('.'))
statinfo = ftp.stat(target)
if S_ISDIR(statinfo.st_mode):
target = '%s/%s' % (target, basename(source))
try:
statinfo = ftp.stat(target)
except IOError:
msg = 'PUT %s -> %s@%s:%s'
log_info(msg % (source, self.user, self.host, target))
ftp.put(source, target)
else:
filename = basename(source)
log_info('[INFO] %s already uploaded, skipping.' % filename)
# Singleton
local = LocalHost()
# Cache
remote_hosts = {}
def get_remote_host(host, user, shell):
key = (host, user, shell)
remote_host = remote_hosts.get(key)
if not remote_host:
remote_host = RemoteHost(host, user, shell)
remote_hosts[key] = remote_host
return remote_host