CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/scripts/service_hub.py
Views: 687
1
#!/usr/bin/env python
2
3
import os, socket, time
4
5
os.chdir(os.environ['SALVUS_ROOT'])
6
7
8
def cmd(s):
9
print(s)
10
if os.WEXITSTATUS(os.system(s)):
11
raise RuntimeError
12
13
14
def hub(command, server_id):
15
cmd("hub {command} {args} ".format(command=command,
16
args=hub_args(server_id)))
17
18
19
def hub_args(server_id):
20
if server_id != '':
21
port = 5000 + 2 * int(server_id)
22
proxy_port = port + 1
23
else:
24
if args.port:
25
port = int(args.port)
26
elif args.port == -1:
27
port = 5000
28
else:
29
port = 0
30
if args.proxy_port:
31
proxy_port = int(args.proxy_port)
32
elif args.proxy_port == -1:
33
proxy_port = 5001
34
else:
35
proxy_port = 0
36
if args.share_port:
37
share_port = int(args.share_port)
38
else:
39
share_port = 0
40
41
agent_port = int(args.agent_port) if args.agent_port else 0
42
43
s = "--host={hostname} --websocket-server --agent_port {agent_port} {mentions} --share_path {share_path} ".format(
44
hostname=args.hostname,
45
server_id=server_id,
46
agent_port=agent_port,
47
mentions="--mentions" if args.mentions else "",
48
share_path=args.share_path)
49
50
if args.database_nodes:
51
s += ' --database_nodes {database_nodes} '.format(
52
database_nodes=args.database_nodes)
53
54
if args.kucalc:
55
s += ' --kucalc '
56
57
if args.lti:
58
s += ' --lti '
59
60
if args.landing:
61
s += ' --landing'
62
63
if args.dev:
64
s += ' --dev '
65
66
if args.single:
67
s += ' --single '
68
69
70
if args.personal:
71
s += ' --personal '
72
73
if args.update:
74
s += ' --update '
75
76
if args.test:
77
s += ' --test '
78
79
if args.foreground:
80
s += ' --foreground '
81
else:
82
logpath = "%s/../logs" % os.environ['SALVUS_ROOT']
83
pidpath = "%s/../pids" % os.environ['SALVUS_ROOT']
84
if not os.path.exists(logpath):
85
os.makedirs(logpath)
86
if not os.path.exists(pidpath):
87
os.makedirs(pidpath)
88
logfile = "%s/hub%s.log" % (logpath, server_id)
89
pidfile = "%s/hub%s.pid" % (pidpath, server_id)
90
s += " --logfile {logfile} --pidfile {pidfile} ".format(
91
logfile=logfile, pidfile=pidfile)
92
93
if server_id:
94
s += ' --id ' + server_id
95
96
return s
97
98
99
def start_hub(server_id):
100
if args.foreground:
101
hub('', server_id)
102
else:
103
hub('start', server_id)
104
105
106
def stop_hub(server_id):
107
hub('stop', server_id)
108
109
110
def restart_hub(server_id):
111
hub('stop', server_id)
112
time.sleep(1)
113
hub('start', server_id)
114
115
116
def gap():
117
print("waiting %s seconds before restarting next hub" % args.gap)
118
time.sleep(args.gap)
119
120
121
def start(args):
122
for server_id in args.id.split(','):
123
start_hub(server_id)
124
125
126
def stop(args):
127
for server_id in args.id.split(','):
128
stop_hub(server_id)
129
130
131
def restart(args):
132
v = args.id.split(',')
133
for i, server_id in enumerate(v):
134
restart_hub(server_id)
135
if i < len(v) - 1:
136
gap()
137
138
139
if __name__ == "__main__":
140
141
import argparse
142
parser = argparse.ArgumentParser(description="Control hub servers")
143
subparsers = parser.add_subparsers(help='sub-command help')
144
145
parser.add_argument(
146
"--id",
147
help="comma separated list ids of servers to start/stop",
148
dest="id",
149
default="",
150
type=str)
151
152
parser.add_argument('--database_nodes',
153
help="",
154
dest='database_nodes',
155
default='')
156
157
parser.add_argument('--foreground',
158
help="foreground",
159
dest='foreground',
160
action="store_const",
161
const=True,
162
default=False)
163
164
parser.add_argument('--dev',
165
help="dev",
166
dest='dev',
167
action="store_const",
168
const=True,
169
default=False)
170
171
parser.add_argument('--kucalc',
172
help="kucalc",
173
dest='kucalc',
174
action="store_const",
175
const=True,
176
default=False)
177
178
parser.add_argument('--lti',
179
help="lti",
180
dest='lti',
181
action="store_const",
182
const=True,
183
default=False)
184
185
parser.add_argument('--landing',
186
help="landing",
187
dest='landing',
188
action="store_const",
189
const=True,
190
default=False)
191
192
parser.add_argument('--single',
193
help="single",
194
dest='single',
195
action="store_const",
196
const=True,
197
default=False)
198
199
parser.add_argument('--personal',
200
help="personal",
201
dest='personal',
202
action="store_const",
203
const=True,
204
default=False)
205
206
parser.add_argument('--update',
207
help="update",
208
dest='update',
209
action="store_const",
210
const=True,
211
default=False)
212
213
parser.add_argument('--test',
214
help="test",
215
dest='test',
216
action="store_const",
217
const=True,
218
default=False)
219
220
parser.add_argument('--agent_port', dest='agent_port', type=int, default=0)
221
222
parser.add_argument('--mentions',
223
help="mentions",
224
dest='mentions',
225
action="store_const",
226
const=True,
227
default=False)
228
229
parser.add_argument('--share_path',
230
dest='share_path',
231
type=str,
232
default='')
233
234
parser.add_argument(
235
"--hostname",
236
help="hostname to listen on [default: hostname of computer]",
237
dest="hostname",
238
default=socket.gethostname(),
239
type=str)
240
241
parser.add_argument(
242
"--gap",
243
help=
244
"time (in seconds) to wait before restarting each hub [default: 10]",
245
dest="gap",
246
default=10,
247
type=int)
248
249
parser_stop = subparsers.add_parser('stop', help='stop the hubs')
250
parser_stop.set_defaults(func=stop)
251
252
parser_start = subparsers.add_parser('start', help='start the hubs')
253
parser_start.set_defaults(func=start)
254
255
parser_restart = subparsers.add_parser('restart', help='restart the hubs')
256
parser_restart.set_defaults(func=restart)
257
258
args = parser.parse_args()
259
args.func(args)
260
261