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
|
#!/usr/bin/python
"""
func
Copyright 2007, Red Hat, Inc
see AUTHORS
This software may be freely redistributed under the terms of the GNU
general public license.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
"""
import SimpleXMLRPCServer
import os
import subprocess
import socket
SERVE_ON = (None,None)
# FIXME: logrotate
from codes import *
import config_data
import logger
import module_loader
import utils
#from busrpc.services import RPCDispatcher
#from busrpc.config import DeploymentConfig
from rhpl.translate import _, N_, textdomain, utf8
I18N_DOMAIN = "vf_server"
class XmlRpcInterface(object):
def __init__(self, modules={}):
"""
Constructor sets up SQLAlchemy (database ORM) and logging.
"""
config_obj = config_data.Config()
self.config = config_obj.get()
self.tables = {}
self.tokens = []
self.modules = modules
self.logger = logger.Logger().logger
self.__setup_handlers()
def __setup_handlers(self):
"""
Add RPC functions from each class to the global list so they can be called.
FIXME: eventually calling most functions should go from here through getattr.
"""
self.handlers = {}
for x in self.modules.keys():
try:
self.modules[x].register_rpc(self.handlers)
self.logger.debug("adding %s" % x)
except AttributeError, e:
self.logger.warning("module %s could not be loaded, it did not have a register_rpc method" % modules[x])
# FIXME: find some more elegant way to surface the handlers?
# FIXME: aforementioned login/session token requirement
def get_dispatch_method(self, method):
if method in self.handlers:
return FuncApiMethod(self.logger, method,
self.handlers[method])
else:
self.logger.info("Unhandled method call for method: %s " % method)
raise InvalidMethodException
def _dispatch(self, method, params):
"""
the SimpleXMLRPCServer class will call _dispatch if it doesn't
find a handler method
"""
return self.get_dispatch_method(method)(*params)
class BusRpcWrapper:
def __init__(self, config):
self.rpc_interface = None
def __getattr__(self, name):
if self.rpc_interface == None:
self.rpc_interface = XmlRpcInterface()
return self.rpc_interface.get_dispatch_method(name)
def __repr__(self):
return ("<BusRpcWrapper>")
class FuncApiMethod:
def __init__(self, logger, name, method):
self.logger = logger
self.__method = method
self.__name = name
def __log_exc(self):
"""
Log an exception.
"""
(t, v, tb) = sys.exc_info()
self.logger.info("Exception occured: %s" % t )
self.logger.info("Exception value: %s" % v)
self.logger.info("Exception Info:\n%s" % string.join(traceback.format_list(traceback.extract_tb(tb))))
def __call__(self, *args):
self.logger.debug("(X) -------------------------------------------")
try:
rc = self.__method(*args)
except FuncException, e:
self.__log_exc()
rc = e
except:
self.logger.debug("Not a virt-factory specific exception")
self.__log_exc()
raise
rc = rc.to_datastruct()
self.logger.debug("Return code for %s: %s" % (self.__name, rc))
return rc
def serve(websvc):
"""
Code for starting the XMLRPC service.
FIXME: make this HTTPS (see RRS code) and make accompanying Rails changes..
"""
server =FuncXMLRPCServer(('', 51234))
server.register_instance(websvc)
server.serve_forever()
def serve_qpid(config_path, register_with_bridge=False, is_bridge_server=False):
"""
Code for starting the QPID RPC service.
"""
config = DeploymentConfig(config_path)
dispatcher = RPCDispatcher(config, register_with_bridge, is_bridge_server=is_bridge_server)
try:
dispatcher.start()
except KeyboardInterrupt:
dispatcher.stop()
print "Exiting..."
class FuncXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer):
def __init__(self, args):
self.allow_reuse_address = True
SimpleXMLRPCServer.SimpleXMLRPCServer.__init__(self, args)
def main(argv):
"""
Start things up.
"""
for arg in sys.argv:
if arg == "import" or arg == "--import":
prov_obj = provisioning.Provisioning()
prov_obj.init(None, {})
return
elif arg == "sync" or arg == "--sync":
prov_obj = provisioning.Provisioning()
prov_obj.sync(None, {}) # just for testing
return
modules = module_loader.load_modules()
print "modules", modules
websvc = XmlRpcInterface(modules=modules)
if "qpid" in sys.argv or "--qpid" in sys.argv:
if "daemon" in sys.argv or "--daemon" in sys.argv:
utils.daemonize("/var/run/vf_server_qpid.pid")
else:
print "serving...\n"
serve_qpid("/etc/virt-factory/qpid.conf")
else:
if "daemon" in sys.argv or "--daemon" in sys.argv:
utils.daemonize("/var/run/vf_server.pid")
else:
print "serving...\n"
# daemonize only if --daemonize, because I forget to type "debug" -- MPD
serve(websvc)
# FIXME: upgrades? database upgrade logic would be nice to have here, as would general creation (?)
# FIXME: command line way to add a distro would be nice to have in the future, rsync import is a bit heavy handed.
# (and might not be enough for RHEL, but is good for Fedora/Centos)
if __name__ == "__main__":
textdomain(I18N_DOMAIN)
main(sys.argv)
|