Revision 04ccf5e9 daemons/ganeti-rapi
b/daemons/ganeti-rapi | ||
---|---|---|
177 | 177 |
return result |
178 | 178 |
|
179 | 179 |
|
180 |
def ParseOptions(): |
|
181 |
"""Parse the command line options. |
|
182 |
|
|
183 |
@return: (options, args) as from OptionParser.parse_args() |
|
180 |
def CheckRAPI(options, args): |
|
181 |
"""Initial checks whether to run or exit with a failure |
|
184 | 182 |
|
185 | 183 |
""" |
186 |
parser = optparse.OptionParser(description="Ganeti Remote API", |
|
187 |
usage="%prog [-d]", |
|
188 |
version="%%prog (ganeti) %s" % |
|
189 |
constants.RAPI_VERSION) |
|
190 |
parser.add_option("-d", "--debug", dest="debug", |
|
191 |
help="Enable some debug messages", |
|
192 |
default=False, action="store_true") |
|
193 |
parser.add_option("--no-ssl", dest="ssl", |
|
194 |
help="Do not secure HTTP protocol with SSL", |
|
195 |
default=True, action="store_false") |
|
196 |
parser.add_option("-K", "--ssl-key", dest="ssl_key", |
|
197 |
help="SSL key", |
|
198 |
default=constants.RAPI_CERT_FILE, type="string") |
|
199 |
parser.add_option("-C", "--ssl-cert", dest="ssl_cert", |
|
200 |
help="SSL certificate", |
|
201 |
default=constants.RAPI_CERT_FILE, type="string") |
|
202 |
parser.add_option("-f", "--foreground", dest="fork", |
|
203 |
help="Don't detach from the current terminal", |
|
204 |
default=True, action="store_false") |
|
205 |
parser.add_option("-b", "--bind", dest="bind_address", |
|
206 |
help="Bind address", |
|
207 |
default="", metavar="ADDRESS") |
|
208 |
|
|
209 |
options, args = parser.parse_args() |
|
210 |
|
|
211 | 184 |
if len(args) != 0: |
212 |
print >> sys.stderr, "Usage: %s [-d]" % sys.argv[0] |
|
185 |
print >> sys.stderr, "Usage: %s [-f] [-d] [-p port] [-b ADDRESS]" % \ |
|
186 |
sys.argv[0] |
|
213 | 187 |
sys.exit(constants.EXIT_FAILURE) |
214 | 188 |
|
215 |
if options.ssl and not (options.ssl_cert and options.ssl_key): |
|
216 |
print >> sys.stderr, ("For secure mode please provide " |
|
217 |
"--ssl-key and --ssl-cert arguments") |
|
218 |
sys.exit(constants.EXIT_FAILURE) |
|
189 |
if options.ssl: |
|
190 |
if not (options.ssl_cert and options.ssl_key): |
|
191 |
print >> sys.stderr, ("For secure mode please provide " |
|
192 |
"--ssl-key and --ssl-cert arguments") |
|
193 |
sys.exit(constants.EXIT_FAILURE) |
|
194 |
for fname in (options.ssl_cert, options.ssl_key): |
|
195 |
if not os.path.isfile(fname): |
|
196 |
print >> sys.stderr, "config %s not there, will not run." % fname |
|
197 |
sys.exit(constants.EXIT_FAILURE) |
|
219 | 198 |
|
220 |
return options, args
|
|
199 |
ssconf.CheckMaster(options.debug)
|
|
221 | 200 |
|
222 | 201 |
|
223 |
def main():
|
|
224 |
"""Main function.
|
|
202 |
def ExecRAPI(options, args):
|
|
203 |
"""Main RAPI function, executed with the pidfile held.
|
|
225 | 204 |
|
226 | 205 |
""" |
227 |
options, args = ParseOptions() |
|
228 |
daemon_name = constants.RAPI |
|
229 |
|
|
230 |
if options.fork: |
|
231 |
utils.CloseFDs() |
|
232 |
|
|
206 |
# Read SSL certificate |
|
233 | 207 |
if options.ssl: |
234 |
# Read SSL certificate |
|
235 |
try: |
|
236 |
ssl_params = http.HttpSslParams(ssl_key_path=options.ssl_key, |
|
237 |
ssl_cert_path=options.ssl_cert) |
|
238 |
except Exception, err: |
|
239 |
sys.stderr.write("Can't load the SSL certificate/key: %s\n" % (err,)) |
|
240 |
sys.exit(constants.EXIT_FAILURE) |
|
208 |
ssl_params = http.HttpSslParams(ssl_key_path=options.ssl_key, |
|
209 |
ssl_cert_path=options.ssl_cert) |
|
241 | 210 |
else: |
242 | 211 |
ssl_params = None |
243 | 212 |
|
244 |
ssconf.CheckMaster(options.debug) |
|
245 |
port = utils.GetDaemonPort(constants.RAPI) |
|
213 |
mainloop = daemon.Mainloop() |
|
214 |
server = RemoteApiHttpServer(mainloop, options.bind_address, options.port, |
|
215 |
ssl_params=ssl_params, ssl_verify_peer=False, |
|
216 |
request_executor_class=JsonErrorRequestExecutor) |
|
217 |
server.Start() |
|
218 |
try: |
|
219 |
mainloop.Run() |
|
220 |
finally: |
|
221 |
server.Stop() |
|
246 | 222 |
|
247 |
if options.fork: |
|
248 |
utils.Daemonize(logfile=constants.DAEMONS_LOGFILES[daemon_name]) |
|
249 | 223 |
|
250 |
utils.SetupLogging(constants.DAEMONS_LOGFILES[daemon_name], debug=options.debug,
|
|
251 |
stderr_logging=not options.fork)
|
|
224 |
def main():
|
|
225 |
"""Main function.
|
|
252 | 226 |
|
253 |
utils.WritePidFile(daemon_name) |
|
254 |
try: |
|
255 |
mainloop = daemon.Mainloop() |
|
256 |
server = RemoteApiHttpServer(mainloop, options.bind_address, port, |
|
257 |
ssl_params=ssl_params, ssl_verify_peer=False, |
|
258 |
request_executor_class= |
|
259 |
JsonErrorRequestExecutor) |
|
260 |
server.Start() |
|
261 |
try: |
|
262 |
mainloop.Run() |
|
263 |
finally: |
|
264 |
server.Stop() |
|
265 |
finally: |
|
266 |
utils.RemovePidFile(daemon_name) |
|
227 |
""" |
|
228 |
parser = optparse.OptionParser(description="Ganeti Remote API", |
|
229 |
usage="%prog [-f] [-d] [-p port] [-b ADDRESS]", |
|
230 |
version="%%prog (ganeti) %s" % constants.RAPI_VERSION) |
|
231 |
parser.add_option("--no-ssl", dest="ssl", |
|
232 |
help="Do not secure HTTP protocol with SSL", |
|
233 |
default=True, action="store_false") |
|
234 |
parser.add_option("-K", "--ssl-key", dest="ssl_key", |
|
235 |
help="SSL key", |
|
236 |
default=constants.RAPI_CERT_FILE, type="string") |
|
237 |
parser.add_option("-C", "--ssl-cert", dest="ssl_cert", |
|
238 |
help="SSL certificate", |
|
239 |
default=constants.RAPI_CERT_FILE, type="string") |
|
240 |
|
|
241 |
dirs = [(val, constants.RUN_DIRS_MODE) for val in constants.SUB_RUN_DIRS] |
|
242 |
dirs.append((constants.LOG_OS_DIR, 0750)) |
|
243 |
daemon.GenericMain(constants.RAPI, parser, dirs, CheckRAPI, ExecRAPI) |
|
267 | 244 |
|
268 | 245 |
|
269 | 246 |
if __name__ == '__main__': |
Also available in: Unified diff