-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxen_api_rpc.py
executable file
·128 lines (98 loc) · 3.78 KB
/
xen_api_rpc.py
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
from SimpleXMLRPCServer import SimpleXMLRPCServer
from security_util import expose, requires_user_privilege, requires_authentication_only, \
requires_admin_privilege, is_exposed, is_authorized
from xen_api import XenAPI
import SocketServer
import ConfigParser
config = ConfigParser.ConfigParser()
# TODO change this to a common config file on a shared location
config.read("/home/vital/config.ini")
# TODO Security to check if requesting user has privilege and callback security model
# TODO or default security model
class XenAPIExposer:
""" This class exposes the actual xen_api with a remote RPC interface """
def __init__(self):
self.prefix = 'xenapi'
def _dispatch(self, method, params):
""" This method receives all the calls to the xen_api."""
# check if method starts with correct prefix
if not method.startswith(self.prefix + "."):
raise Exception('method "%s" is not supported' % method)
method_name = method.partition('.')[2]
func = getattr(self, method_name)
if not is_exposed(func):
raise Exception('method "%s" is not supported' % method)
is_authorized(func, params[0], params[1])
return func(*params)
# TODO - add functionality to check if a user has access to the VM specified
# TODO - TBD on all functions
@expose
@requires_authentication_only
def start_vm(self, user, passwd, vm_name, vm_options):
return XenAPI().start_vm(vm_name, vm_options)
@expose
@requires_authentication_only
def stop_vm(self, user, passwd, vm_name):
XenAPI().stop_vm(vm_name)
@expose
@requires_authentication_only
def save_vm(self, user, passwd, vm_name):
XenAPI().save_vm(vm_name)
@expose
@requires_authentication_only
def restore_vm(self, user, passwd, vm_name, base_vm):
XenAPI().restore_vm(vm_name, base_vm)
@expose
@requires_authentication_only
def list_all_vms(self, user, passwd):
return XenAPI().list_all_vms()
@expose
@requires_authentication_only
def get_dom_details(self, user, passwd):
return XenAPI().get_dom_details()
@expose
@requires_authentication_only
def list_vm(self, user, passwd, vm_name):
return XenAPI().list_vm(vm_name)
@expose
@requires_authentication_only
def setup_vm(self, user, passwd, vm_name, base_vm, vif=None):
XenAPI().setup_vm(vm_name, base_vm, vif)
@expose
@requires_authentication_only
def cleanup_vm(self, user, passwd, vm_name):
XenAPI().cleanup_vm(vm_name)
@expose
@requires_authentication_only
def kill_zombie_vm(self, user, passwd, vm_id):
XenAPI().kill_zombie_vm(vm_id)
@expose
@requires_authentication_only
def create_bridge(self, user, passwd, name):
XenAPI().create_bridge(name)
@expose
@requires_authentication_only
def remove_bridge(self, user, passwd, name):
XenAPI().remove_bridge(name)
@expose
@requires_authentication_only
def vm_exists(self, user, passwd, name):
return XenAPI().vm_exists(name)
@expose
@requires_authentication_only
def bridge_exists(self, user, passwd, name):
return XenAPI().bridge_exists(name)
@expose
@requires_authentication_only
def is_bridge_up(self, user, passwd, name):
return XenAPI().is_bridge_up(name)
# allows RPC module to handle concurrent requests
class SimpleThreadedXMLRPCServer(SocketServer.ThreadingMixIn, SimpleXMLRPCServer):
pass
server = SimpleThreadedXMLRPCServer((config.get("XenAPI", "IP_ADDRESS"), int(config.get("XenAPI", "PORT"))), logRequests=True, allow_none=True)
server.register_instance(XenAPIExposer())
try:
print 'Use Control-C to exit'
server.serve_forever()
except KeyboardInterrupt:
print 'Exiting'