4 # Copyright (C) 2006, 2007 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 """ Ganeti Remote API master script.
29 from ganeti import constants
30 from ganeti import utils
31 from ganeti.rapi import RESTHTTPServer
35 """Parse the command line options.
38 (options, args) as from OptionParser.parse_args()
41 parser = optparse.OptionParser(description="Ganeti Remote API",
42 usage="%prog [-d] [-p port]",
43 version="%%prog (ganeti) %s" %
44 constants.RAPI_VERSION)
45 parser.add_option("-d", "--debug", dest="debug",
46 help="Enable some debug messages",
47 default=False, action="store_true")
48 parser.add_option("-p", "--port", dest="port",
49 help="Port to run API (%s default)." %
51 default=constants.RAPI_PORT, type="int")
52 parser.add_option("-S", "--https", dest="ssl",
53 help="Secure HTTP protocol with SSL",
54 default=False, action="store_true")
55 parser.add_option("-K", "--ssl-key", dest="ssl_key",
57 default=None, type="string")
58 parser.add_option("-C", "--ssl-cert", dest="ssl_cert",
59 help="SSL certificate",
60 default=None, type="string")
61 parser.add_option("-f", "--foreground", dest="fork",
62 help="Don't detach from the current terminal",
63 default=True, action="store_false")
65 options, args = parser.parse_args()
67 if len(args) != 1 or args[0] not in ("start", "stop"):
68 print >> sys.stderr, "Usage: %s [-d] [-p port] start|stop\n" % sys.argv[0]
72 if not (options.ssl_cert and options.ssl_key):
73 print >> sys.stderr, ("For secure mode please provide "
74 "--ssl-key and --ssl-cert arguments")
81 """Map network port to PID.
84 port: A port number to map.
90 _NET_STAT = ['/proc/net/tcp', '/proc/net/udp']
95 for file in _NET_STAT:
99 for line in f.readlines()[1:]:
101 inode2port[long(d[9])] = int(d[1].split(':')[1], 16)
104 except EnvironmentError:
105 # Nothing can be done
108 fdlist = glob.glob('/proc/[0-9]*/fd/*')
111 pid = int(fd.split('/')[2])
112 inode = long(os.stat(fd)[1])
113 if inode in inode2port:
114 port2pid[inode2port[inode]] = pid
115 except EnvironmentError:
116 # Nothing can be done
120 return port2pid[port]
125 def StartAPI(options):
135 utils.Daemonize(logfile=constants.LOG_RAPISERVER)
136 RESTHTTPServer.start(options)
139 def StopAPI(options):
142 pid = Port2PID(options.port)
144 print ("Stopping Ganeti-RAPI PID: %d, port: %d... "
145 % (pid, options.port)),
149 print >> sys.stderr, "Unable to locate running Ganeti-RAPI on port: %d" \
152 except Exception, ex:
153 print >> sys.stderr, ex
163 options, args = ParseOptions()
164 if args[0] == "start":
165 result = StartAPI(options)
167 result = StopAPI(options)
171 if __name__ == '__main__':