-
Notifications
You must be signed in to change notification settings - Fork 40
/
p.haul-wrap
executable file
·129 lines (103 loc) · 3.81 KB
/
p.haul-wrap
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
#!/usr/bin/env python
import os
import socket
import argparse
import thread
# Usage
# p.haul-wrap service
# p.haul-wrap client <destination> <type> <id>
#
# p.haul-wrap is a helper script which perform primitive connections
# establishment and call p.haul or p.haul-service specifying created
# connections via command line arguments. Use it exclusively for testing
# purposes!
#
# E.g.
# p.haul-wrap service
# p.haul-wrap client 10.0.0.1 vz 100
#
default_rpc_port = 12345
default_service_bind_addr = "0.0.0.0"
def start_web_gui(partner, rpc_port):
"""Start web gui if requested"""
import webgui.p_haul_web_gui
thread.start_new_thread(webgui.p_haul_web_gui.start_web_gui,
(partner, rpc_port))
def run_phaul_service(args, unknown_args):
"""Run p.haul-service"""
if args.web_gui:
start_web_gui(args.web_partner, args.bind_port)
print "Waiting for connection..."
# Establish connection
host = args.bind_addr, args.bind_port
server_sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_sk.bind(host)
server_sk.listen(8)
connection_sks = [None, None]
while True:
for i in range(len(connection_sks)):
connection_sks[i], dummy = server_sk.accept()
# Organize p.haul-service args
target_args = [args.path]
target_args.extend(unknown_args)
target_args.extend(["--fdrpc", str(connection_sks[0].fileno()),
"--fdmem", str(connection_sks[1].fileno())])
# Call p.haul-service
cmdline = " ".join(target_args)
print "Exec p.haul-service: {0}".format(cmdline)
if args.one_shot:
os.system(cmdline)
return
else:
thread.start_new_thread(os.system, tuple([cmdline]))
def run_phaul_client(args, unknown_args):
"""Run p.haul"""
print "Establish connection..."
# Establish connection
dest_host = args.to, args.port
connection_sks = [None, None]
for i in range(len(connection_sks)):
connection_sks[i] = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection_sks[i].connect(dest_host)
# Organize p.haul args
target_args = [args.path]
target_args.extend(unknown_args)
target_args.extend(["--to", args.to,
"--fdrpc", str(connection_sks[0].fileno()),
"--fdmem", str(connection_sks[1].fileno())])
# Call p.haul
print "Exec p.haul: {0}".format(" ".join(target_args))
os.system(" ".join(target_args))
# Initialize arguments parser
parser = argparse.ArgumentParser("Process HAULer wrap")
subparsers = parser.add_subparsers(title="Subcommands")
# Initialize service mode arguments parser
service_parser = subparsers.add_parser("service", help="Service mode")
service_parser.set_defaults(func=run_phaul_service)
service_parser.add_argument("--bind-addr", help="IP to bind to", type=str,
default=default_service_bind_addr)
service_parser.add_argument("--bind-port", help="Port to bind to", type=int,
default=default_rpc_port)
service_parser.add_argument("--path", help="Path to p.haul-service script",
default=os.path.join(os.path.dirname(__file__), "p.haul-service"))
service_parser.add_argument("--one-shot",
help="Do not run in loop to accept multiple connections",
default=False, action='store_true')
service_parser.add_argument("--web-gui",
help="Start web gui", default = False, action = 'store_true')
service_parser.add_argument("--web-partner",
help="Start web gui", type = str, default = None)
# Initialize client mode arguments parser
client_parser = subparsers.add_parser("client", help="Client mode")
client_parser.set_defaults(func=run_phaul_client)
client_parser.add_argument("to", help="IP where to haul")
client_parser.add_argument("--port", help="Port where to haul", type=int,
default=default_rpc_port)
client_parser.add_argument("--path", help="Path to p.haul script",
default=os.path.join(os.path.dirname(__file__), "p.haul"))
# Parse arguments and run wrap in specified mode
args, unknown_args = parser.parse_known_args()
try:
args.func(args, unknown_args)
except KeyboardInterrupt:
pass