forked from gromacs/copernicus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpc-server
executable file
·273 lines (235 loc) · 8.88 KB
/
cpc-server
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
#!/usr/bin/env python
# This file is part of Copernicus
# http://www.copernicus-computing.org/
#
# Copyright (C) 2011, Sander Pronk, Iman Pouya, Erik Lindahl, and others.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# This is the main executable, from which all actions are launched.
import sys
import copy
import os
import shutil
from getpass import getpass
import cpc.client
import cpc.network.server
from cpc.server.state import database
import cpc.worker
import cpc.util
import cpc.util.log
import cpc.server.tracking
import cpc.util.conf.conf_base
from cpc.util.exception import ClientError
from cpc.util.conf.server_conf import ServerConf, SetupError
from cpc.util.conf.server_conf import initiateServerSetup
from cpc.util import cmd_line_utils
from cpc.util.version import __version__
def print_usage():
print "Usage: cpc-server start [-dev] [-no-server-verification]"
print " cpc-server config-list"
print " cpc-server config-values"
print " cpc-server config param value"
print " cpc-server setup [-force] [(-host) | (-servername relative-dir)]"\
" project-directory"
print " cpc-server create-connection-bundle|bundle [-o destination] "
print "Other commands "
print " cpc-server version"
print ""
print "Common options:"
print " cpc-server [-c servername]"
sys.exit(1)
# make a copy for later
args = copy.copy(sys.argv)
if len(args) < 2:
print_usage()
# remove the 0th argument
args.pop(0)
servername = None
debug = None
# first parse common options
while len(args) > 0:
if args[0][0] != '-':
break
elif args[0] == '-c':
option = args.pop(0)
if len(args) < 1:
sys.stdout.write(
"ERROR: no value specified for global option '%s'\n" %
option)
print_usage()
servername = args.pop(0)
servername = os.path.join(
cpc.util.conf.conf_base.findAndCreateGlobalDir(), servername)
elif args[0] == '-d':
args.pop(0)
debug = cpc.util.log.MODE_DEBUG
#can only handle the flag here and we do not want to completely rewrite the cmd line parsing logic at the moment
elif args[0]=='-h':
print_usage()
exit(0)
else:
sys.stdout.write("ERROR: no command or faulty global option '%s'\n" %
args[0])
print_usage()
def getArg(argnr, name):
try:
ret = args[argnr]
except IndexError:
raise ClientError("Missing argument: %s" % name)
return ret
cmd = args[0]
try:
if cmd == "help":
cmd_line_utils.printLogo()
cmd_line_utils.printAuthors()
print_usage()
elif cmd == "version":
cmd_line_utils.printLogo()
cmd_line_utils.printAuthors()
cf = ServerConf(confdir=servername)
print("Server version: %s"%__version__)
print("Server id: %s"%cf.getServerId())
sys.exit(0)
elif cmd == "start":
cf = ServerConf(confdir=servername)
serverMode = cf.getMode()
doFork = True
if len(args) > 1:
if args[1] == "-dev":
#dev mode should always at least print out debug log output
if serverMode == "prod":
serverMode = "debug"
cpc.util.log.initServerLogToStdout(serverMode)
doFork = False
if '-no-server-verification' in args:
cf.setServerVerification(False)
print "Server will allow requests from connected servers on the client port. " \
"This means that the connecting servers will not be athuenticated in any way. " \
"Only use this setting in cases where you have openssl incompatibilites between servers."
print "Starting server.."
doProfile = cf.getProfiling()
if doProfile:
try:
import yappi
yappi.start()
except:
pass
cpc.network.server.runServer(serverMode, doFork)
sys.exit(0)
elif cmd == "setup":
# the -c option will be ignored here.
rundir = None
altHostName = None
forceReset = False
hostConfDir = False
altDir = False
rootpass = None
args.pop(0)
i = 0
while i < len(args):
arg = args[i]
if arg == '-force':
forceReset = True
elif arg == '-host':
if(altDir):
print "cannot use -host in combination with -servername "
sys.exit(1)
hostConfDir = True
elif arg == '-p':
rootpass = args[i + 1]
i += 1
elif arg == '-servername':
if(hostConfDir):
print "cannot use -servername in combination with -host"
sys.exit(1)
altDir = True
altHostName = args[i + 1]
i += 1
else:
if rundir is None:
rundir = arg
else:
raise ClientError("Unknown option: %s" % arg)
i += 1
if rundir is None:
raise ClientError(
"Missing run directory: the base directory for project output")
if(not forceReset):
forceReset = cmd_line_utils.checkServerConfExistAndAskToRemove(hostConfDir,altHostName)
initiateServerSetup(rundir, forceReset, hostConfDir,
altHostName)
if rootpass is None:
#setup database
rootpass = getpass("\nA user named cpc-admin will be created.\nPlease enter a password for the cpc-admin account:")
try:
database.setupDatabase(rootpass)
except Exception as e:
raise SetupError("Failed to initialize database: %s"%e)
sys.exit(1)
#direct invocation of method not feasible since a conf object is a singleton,
# client conf and server conf cannot be instantiated at the same time
cmd = "cpcc add-server localhost %s"%ServerConf().getClientSecurePort()
os.system(cmd)
print "\nInstallation succeeded.\nYou can now start the server with cpc-server start"
print "After starting the the server you can login to the server using the cpc-admin user (cpcc login cpc-admin)"
sys.exit(0)
if (cmd == "config-list" or
cmd == "config-values" or
cmd == "config"):
cf = ServerConf(confdir=servername)
# initialize the client log
cpc.util.log.initClientLog(debug)
# initialize the tracker
cpc.server.tracking.tracker.initTracker()
if cmd == "config-list":
#print '\n'
conf = ServerConf()
configs = conf.getUserSettableConfigs()
cmd_line_utils.printSortedConfigListDescriptions(configs)
elif cmd == "config-values":
#print '\n'
conf = ServerConf()
configs = conf.getUserSettableConfigs()
cmd_line_utils.printSortedConfigListValues(configs)
elif cmd == "config":
param = getArg(1, "parameter name")
value = getArg(2, "parameter value")
conf = ServerConf()
conf.set(param, value)
elif cmd == "bundle" or cmd == "create-connection-bundle":
cf = ServerConf(confdir=servername)
connectionBundle = cmd_line_utils.initiateWorkerSetup()
try:
if len(args)==3 and args[1]=='-o':
filename = args[2]
else:
filename = "client.cnx"
if(os.path.exists(filename)):
shutil.copyfile(filename,filename+".bak")
print("found a connection bundle backed it up as %s.bak"%filename)
file = open(filename,"w")
file.write(connectionBundle.toJson())
file.close()
print "wrote connection bundle to %s"%filename
except Exception as e:
print "could not write the connection bundle please ensure" \
" that you have write access in the current directory"
else:
print "ERROR: unknown command '%s'" % cmd
except ClientError as e:
print("ERROR: %s" % str(e))
except cpc.util.CpcError as e:
print("ERROR: %s" % str(e))
except IOError as e:
raise e