3 This module exposes an XMLRPC interface that allows PlanetLab users to
4 create/destroy slivers with delegated instantiation, start and stop
5 slivers, make resource loans, and examine resource allocations. The
6 XMLRPC is provided on a localhost-only TCP port as well as via a Unix
7 domain socket that is accessible by ssh-ing into a delegate account
8 with the forward_api_calls shell.
11 import SimpleXMLRPCServer
30 UNIX_ADDR = '/tmp/sliver_mgr.api'
32 deliver_ticket = None # set in sm.py:start()
37 def export_to_api(nargs):
39 nargs_dict[method.__name__] = nargs
40 api_method_dict[method.__name__] = method
47 """Help(): get help"""
48 return ''.join([method.__doc__ + '\n' for method in api_method_dict.itervalues()])
52 """Ticket(tkt): deliver a ticket"""
54 data = ticket.verify(tkt)
55 name = data['slivers'][0]['name']
58 logger.log('Ticket delivered for %s' % name)
59 Create(database.db.get(name))
60 except Exception, err:
61 raise xmlrpclib.Fault(102, 'Ticket error: ' + str(err))
66 """GetXIDs(): return an dictionary mapping slice names to XIDs"""
67 return dict([(pwent[0], pwent[2]) for pwent in pwd.getpwall() if pwent[6] == sliver_vs.Sliver_VS.SHELL])
71 """GetSSHKeys(): return an dictionary mapping slice names to SSH keys"""
73 for rec in database.db.itervalues():
75 keydict[rec['name']] = rec['keys']
80 """Create(sliver_name): create a non-PLC-instantiated sliver"""
81 if rec['instantiation'] == 'delegated': accounts.get(rec['name']).ensure_created(rec)
85 """Destroy(sliver_name): destroy a non-PLC-instantiated sliver"""
86 if rec['instantiation'] == 'delegated': accounts.get(rec['name']).ensure_destroyed()
90 """Start(sliver_name): run start scripts belonging to the specified sliver"""
91 accounts.get(rec['name']).start()
95 """Stop(sliver_name): kill all processes belonging to the specified sliver"""
96 accounts.get(rec['name']).stop()
99 def GetEffectiveRSpec(rec):
100 """GetEffectiveRSpec(sliver_name): return the RSpec allocated to the specified sliver, including loans"""
101 return rec.get('_rspec', {}).copy()
105 """GetRSpec(sliver_name): return the RSpec allocated to the specified sliver, excluding loans"""
106 return rec.get('rspec', {}).copy()
110 """GetLoans(sliver_name): return the list of loans made by the specified sliver"""
111 return rec.get('_loans', [])[:]
113 def validate_loans(obj):
114 """Check that <obj> is a valid loan specification."""
115 def validate_loan(obj): return (type(obj)==list or type(obj)==tuple) and len(obj)==3 and type(obj[0])==str and type(obj[1])==str and obj[1] in database.LOANABLE_RESOURCES and type(obj[2])==int and obj[2]>=0
116 return type(obj)==list and False not in map(validate_loan, obj)
119 def SetLoans(rec, loans):
120 """SetLoans(sliver_name, loans): overwrite the list of loans made by the specified sliver"""
121 if not validate_loans(loans): raise xmlrpclib.Fault(102, 'Invalid argument: the second argument must be a well-formed loan specification')
122 rec['_loans'] = loans
126 class APIRequestHandler(SimpleXMLRPCServer.SimpleXMLRPCRequestHandler):
127 # overriding _dispatch to achieve this effect is officially deprecated,
128 # but I can't figure out how to get access to .request without
129 # duplicating SimpleXMLRPCServer code here, which is more likely to
130 # change than the deprecated behavior is to be broken
132 @database.synchronized
133 def _dispatch(self, method_name_unicode, args):
134 method_name = str(method_name_unicode)
135 try: method = api_method_dict[method_name]
137 api_method_list = api_method_dict.keys()
138 api_method_list.sort()
139 raise xmlrpclib.Fault(100, 'Invalid API method %s. Valid choices are %s' % (method_name, ', '.join(api_method_list)))
140 expected_nargs = nargs_dict[method_name]
141 if len(args) != expected_nargs:
142 raise xmlrpclib.Fault(101, 'Invalid argument count: got %d, expecting %d.' % (len(args),
145 # Figure out who's calling.
146 # XXX - these ought to be imported directly from some .h file
148 sizeof_struct_ucred = 12
149 ucred = self.request.getsockopt(socket.SOL_SOCKET, SO_PEERCRED, sizeof_struct_ucred)
150 xid = struct.unpack('3i', ucred)[2]
151 caller_name = pwd.getpwuid(xid)[0]
152 if method_name not in ('ReCreate', 'Help', 'Ticket', 'GetXIDs', 'GetSSHKeys'):
153 target_name = args[0]
154 target_rec = database.db.get(target_name)
156 if not (target_rec and target_rec['type'].startswith('sliver.')):
157 raise xmlrpclib.Fault(102, 'Invalid argument: the first argument must be a sliver name.')
158 if not (caller_name, method_name) in target_rec['delegations']:
159 # or (caller_name == 'utah_elab_delegate' and target_name.startswith('utah_'))):
160 raise xmlrpclib.Fault(108, 'Permission denied.')
162 result = method(target_rec, *args[1:])
163 else: result = method(*args)
164 if result == None: result = 1
167 class APIServer_INET(SocketServer.ThreadingMixIn, SimpleXMLRPCServer.SimpleXMLRPCServer): allow_reuse_address = True
169 class APIServer_UNIX(APIServer_INET): address_family = socket.AF_UNIX
172 """Start two XMLRPC interfaces: one bound to localhost, the other bound to a Unix domain socket."""
173 serv1 = APIServer_INET(('127.0.0.1', API_SERVER_PORT), requestHandler=APIRequestHandler, logRequests=0)
174 tools.as_daemon_thread(serv1.serve_forever)
175 try: os.unlink(UNIX_ADDR)
177 if e.errno != errno.ENOENT: raise
178 serv2 = APIServer_UNIX(UNIX_ADDR, requestHandler=APIRequestHandler, logRequests=0)
179 tools.as_daemon_thread(serv2.serve_forever)
180 os.chmod(UNIX_ADDR, 0666)