forked from onyxfish/relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relay.py
300 lines (235 loc) · 7.68 KB
/
relay.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python
import ConfigParser
import json
from optparse import OptionParser
import os
import shlex
import socket
import subprocess
import sys
import termios
import tty
import paramiko
LIB_PATH = '/usr/local/lib/relay'
CONF_PATH = os.path.expanduser('~/.relay.conf')
env = {}
def _parse_options():
"""
Read configuration and parse command line options.
"""
if not os.path.exists(CONF_PATH):
sys.exit('Relay config file does not exist at %s!' % CONF_PATH)
config = ConfigParser.ConfigParser()
config.read(CONF_PATH)
parser = OptionParser(
usage=("relay [options] <command>[:arg1,arg2,...] ...")
)
parser.add_option('--user',
dest='relay_user',
default=config.get('relay', 'user'),
help='User to connect to the relay server as'
)
parser.add_option('--server',
dest='relay_server',
default=config.get('relay', 'server'),
help='Hostname or IP of the relay server'
)
parser.add_option('--private-key',
dest='pair_private_key',
default=os.path.expanduser(config.get('relay', 'private_key')),
help='Absolute path to the private SSH key'
)
parser.add_option('--public-key',
dest='pair_public_key',
default=os.path.expanduser(config.get('relay', 'public_key')),
help='Absolute path to the public SSH key'
)
parser.add_option('--pair-user',
dest='pair_user',
default=config.get('relay', 'pair_user'),
help='Username of the pair programmer account on each developer computer'
)
parser.add_option('--ports-json',
dest='ports_json',
default=os.path.expanduser(config.get('relay', 'ports_json')),
help='Absolute path to the port mapping file'
)
parser.add_option('--bash-profile',
dest='bash_profile',
default=os.path.expanduser(config.get('relay', 'bash_profile')),
help='Absolute path to a file containing bash aliases to be installed when creating the pair programmer user'
)
parser.add_option('--verbose',
action='store_true',
dest='verbose',
default=False,
help='Display verbose output'
)
opts, args = parser.parse_args()
opts.lib_path = LIB_PATH
return parser, opts, args
def _parse_arguments(arguments):
"""
Convert fabric-style arguments.
Partially lifted from fabric: https://github.com/fabric/fabric
"""
cmds = []
for cmd in arguments:
args = []
kwargs = {}
if ':' in cmd:
cmd, argstr = cmd.split(':', 1)
for pair in argstr.split(','):
args.append(pair)
cmds.append((cmd, args, kwargs))
return cmds
def run(cmd):
"""
Run a shell command using subprocess.
"""
cmd = str(cmd)
if env['verbose']:
sys.stdout.write('--> %s\n' % cmd)
cmd_list = shlex.split(cmd)
p = subprocess.Popen(
cmd_list,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
return p.communicate()
def posix_shell(chan):
"""
POSIX shell implementation for proxied SSH.
Lifted from Paramiko examples.
"""
import select
oldtty = termios.tcgetattr(sys.stdin)
try:
tty.setraw(sys.stdin.fileno())
tty.setcbreak(sys.stdin.fileno())
chan.settimeout(0.0)
while True:
r, w, e = select.select([chan, sys.stdin], [], [])
if chan in r:
try:
x = chan.recv(1024)
if len(x) == 0:
print '\r\n*** EOF\r\n',
break
sys.stdout.write(x)
sys.stdout.flush()
except socket.timeout:
pass
if sys.stdin in r:
x = sys.stdin.read(1)
if len(x) == 0:
break
chan.send(x)
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
def _main():
"""
Execute a series of arbitrary commands.
Partially lifted from fabric: https://github.com/fabric/fabric
"""
try:
parser, options, arguments = _parse_options()
env.update(vars(options))
arguments = parser.largs
if not (arguments):
parser.print_help()
sys.exit(0)
commands_to_run = _parse_arguments(arguments)
for name, args, kwargs in commands_to_run:
try:
func = globals()[name]
except KeyError:
sys.stderr.write('Command %s does not exist' % name)
sys.exit(1)
func(*args, **kwargs)
except SystemExit:
raise
except KeyboardInterrupt:
sys.stdout.write('\nQuit\n')
sys.exit(0)
"""
UTILS
"""
def user(username):
"""
Set programmer to use for connection.
"""
if env['verbose']:
sys.stdout.write('User: %s\n' % username)
with open(env['ports_json']) as f:
user_port_maps = json.load(f)
env['port_map'] = user_port_maps[username]
"""
SETUP
"""
def install_bash_profile():
"""
Install bash profile aliases for local user.
"""
run('cat %(bash_profile)s >> ~/.bash_profile' % env)
def create_pairprogrammer_osx():
"""
Create OSX pair programming user.
"""
#sys.stdout.write('Creating user "%(pair_users"' % env)
run('sudo dscl . -create /Users/%(pair_user)s' % env)
run('sudo dscl . -create /Users/%(pair_user)s UserShell /bin/bash' % env)
run('sudo dscl . -create /Users/%(pair_user)s RealName "%(pair_user)s"' % env)
run('sudo dscl . -create /Users/%(pair_user)s UniqueID "1667"' % env)
run('sudo dscl . -create /Users/%(pair_user)s PrimaryGroupID 20' % env)
run('sudo mkdir -p /Users/%(pair_user)s' % env)
run('sudo dscl . -create /Users/%(pair_user)s NFSHomeDirectory /Users/%(pair_user)s' % env)
run('sudo dscl . -append /Groups/com.apple.access_ssh GroupMembership %(pair_user)s' % env)
run('sudo mkdir -p /Users/%(pair_user)s/.ssh/' % env)
run('sudo cp %(pair_public_key)s /Users/%(pair_user)s/.ssh/authorized_keys' % env)
run('sudo cp %(bash_profile)s /Users/%(pair_user)s/.bash_profile' % env)
def setup():
install_bash_profile()
create_pairprogrammer_osx()
"""
SHARING
"""
def share(local_port):
"""
Share a local port with remote users.
"""
env['local_port'] = local_port
try:
env['remote_port'] = env['port_map'][local_port]
except KeyError:
sys.exit('Port mapping does not exist for port %s' % local_port)
sys.stdout.write('Sharing local port %(local_port)s on remote port %(remote_port)s\n' % env)
run('chmod 600 %(pair_private_key)s' % env)
run('ssh -i %(pair_private_key)s -N -R 0.0.0.0:%(remote_port)s:localhost:%(local_port)s %(relay_user)s@%(relay_server)s' % env)
def ssh():
"""
SSH to remote user.
"""
env['remote_port'] = env['port_map']['22']
sys.stdout.write('Connecting to SSH session on remote port %(remote_port)s\n' % env)
run('chmod 600 %(pair_private_key)s' % env)
client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect(
hostname=env['relay_server'],
port=int(env['remote_port']),
username=env['pair_user'],
key_filename=env['pair_private_key']
)
channel = client.invoke_shell()
posix_shell(channel)
def web():
"""
Open web browser at remote user's development server.
"""
env['remote_port'] = env['port_map']['8000']
sys.stdout.write('Launching browser on remote port %(remote_port)s\n' % env)
run('open http://%(relay_server)s:%(remote_port)s' % env)
if __name__ == '__main__':
_main()