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
33 RELEASE_VERSION = 0.01
38 """Parse the command line options.
41 (options, args) as from OptionParser.parse_args()
44 parser = optparse.OptionParser(description="Ganeti Remote API",
45 usage="%prog [-d] [-p port]",
46 version="%%prog (ganeti) %s" % RELEASE_VERSION)
48 parser.add_option("-d", "--debug", dest="debug",
49 help="Enable some debug messages",
50 default=False, action="store_true")
51 parser.add_option("-p", "--port", dest="port",
52 help="Port to run API",
53 default=API_PORT, type="int")
54 parser.add_option("-S", "--https", dest="ssl",
55 help="Secure HTTP protocol with SSL",
56 default=False, action="store_true")
57 parser.add_option("-K", "--ssl-key", dest="ssl_key",
59 default=None, type="string")
60 parser.add_option("-C", "--ssl-cert", dest="ssl_cert",
61 help="SSL certificate",
62 default=None, type="string")
63 parser.add_option("-f", "--foreground", dest="fork",
64 help="Don't detach from the current terminal",
65 default=True, action="store_false")
67 options, args = parser.parse_args()
69 if len(args) != 1 or args[0] not in ("start", "stop"):
70 print >> sys.stderr, "Usage: %s [-d] [-p port] start|stop\n" % sys.argv[0]
74 if not (options.ssl_cert and options.ssl_key):
75 print >> sys.stderr, ("For secure mode please provide "
76 "--ssl-key and --ssl-cert arguments")
83 """Map network port to PID.
86 port: A port number to map.
92 _NET_STAT = ['/proc/net/tcp', '/proc/net/udp']
97 for file in _NET_STAT:
101 for line in f.readlines()[1:]:
103 inode2port[long(d[9])] = int(d[1].split(':')[1], 16)
106 except EnvironmentError:
107 # Nothing can be done
110 fdlist = glob.glob('/proc/[0-9]*/fd/*')
113 pid = int(fd.split('/')[2])
114 inode = long(os.stat(fd)[1])
115 if inode in inode2port:
116 port2pid[inode2port[inode]] = pid
117 except EnvironmentError:
118 # Nothing can be done
122 return port2pid[port]
127 def StartAPI(options):
137 utils.Daemonize(logfile=constants.LOG_RAPISERVER)
138 RESTHTTPServer.start(options)
141 def StopAPI(options):
144 pid = Port2PID(options.port)
146 print ("Stopping Ganeti-RAPI PID: %d, port: %d... "
147 % (pid, options.port)),
151 print >> sys.stderr, "Unable to locate running Ganeti-RAPI on port: %d" \
154 except Exception, ex:
155 print >> sys.stderr, ex
165 options, args = ParseOptions()
166 if args[0] == "start":
167 result = StartAPI(options)
169 result = StopAPI(options)
173 if __name__ == '__main__':