forked from ungleich/python-oca
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
141 lines (113 loc) · 4.4 KB
/
__init__.py
File metadata and controls
141 lines (113 loc) · 4.4 KB
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
# -*- coding: UTF-8 -*-
import http.client
import os
import re
import socket
import xmlrpc.client
from .cluster import Cluster, ClusterPool
from .datastore import Datastore, DatastorePool
from .exceptions import OpenNebulaException
from .group import Group, GroupPool
from .host import Host, HostPool
from .image import Image, ImagePool
from .template import VmTemplate, VmTemplatePool
from .user import User, UserPool
from .vm import VirtualMachine, VirtualMachinePool
from .vn import VirtualNetwork, VirtualNetworkPool
CONNECTED = -3
ALL = -2
CONNECTED_AND_GROUP = -1
class TimeoutHTTPConnection(http.client.HTTPConnection):
def connect(self):
http.client.HTTPConnection.connect(self)
self.sock.settimeout(self.timeout)
class TimeoutHTTP(http.client.HTTPConnection):
_connection_class = TimeoutHTTPConnection
def set_timeout(self, timeout):
self._conn.timeout = timeout
class ProxiedTransport(xmlrpc.client.Transport):
def __init__(self, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, *args, **kwargs):
xmlrpc.client.Transport.__init__(self, *args, **kwargs)
self.timeout = timeout
def set_proxy(self, proxy):
self.proxy = proxy
def make_connection(self, host):
self.realhost = host
h = http.client.HTTPConnection(self.proxy)
return h
def send_request(self, connection, handler, request_body):
connection.putrequest("POST", 'http://%s%s' % (self.realhost, handler))
def send_host(self, connection, host):
connection.putheader('Host', self.realhost)
class Client(object):
"""
The client class, represents the connection with the core and handles the
xml-rpc calls(see http://www.opennebula.org/documentation:rel3.2:api)
"""
DEFAULT_ONE_AUTH = "~/.one/one_auth"
ONE_AUTH_RE = re.compile('^(.+?):(.+)$')
DEFAULT_ONE_ADDRESS = "http://localhost:2633/RPC2"
def __init__(self, secret=None, address=None, proxy=None):
if secret:
one_secret = secret
elif "ONE_AUTH" in os.environ and os.environ["ONE_AUTH"]:
one_auth = os.path.expanduser(os.environ["ONE_AUTH"])
with open(one_auth) as f:
one_secret = f.read().strip()
elif os.path.exists(os.path.expanduser(self.DEFAULT_ONE_AUTH)):
with open(os.path.expanduser(self.DEFAULT_ONE_AUTH)) as f:
one_secret = f.read().strip()
else:
raise OpenNebulaException('ONE_AUTH file not present')
one_secret = self.ONE_AUTH_RE.match(one_secret)
if one_secret:
user = one_secret.groups()[0]
password = one_secret.groups()[1]
else:
raise OpenNebulaException("Authorization file malformed")
self.one_auth = '{0}:{1}'.format(user, password)
if address:
self.one_address = address
elif "ONE_XMLRPC" in os.environ and os.environ["ONE_XMLRPC"]:
self.one_address = os.environ["ONE_XMLRPC"]
else:
self.one_address = self.DEFAULT_ONE_ADDRESS
if proxy:
p = ProxiedTransport(timeout=100)
p.set_proxy(proxy)
self.server = xmlrpc.client.ServerProxy(self.one_address, transport=p)
else:
self.server = xmlrpc.client.ServerProxy(self.one_address)
def call(self, function, *args):
"""
Calls rpc function.
Arguments
``function``
OpenNebula xmlrpc funtcion name (without preceding 'one.')
``args``
function arguments
"""
try:
func = getattr(self.server.one, function)
ret = func(self.one_auth, *args)
try:
is_success, data, return_code = ret
except ValueError:
data = ''
is_success = False
except socket.error as e:
# connection error
raise e
if not is_success:
raise OpenNebulaException(data)
return data
def version(self):
"""
Get the version of the connected OpenNebula server.
"""
return self.call('system.version')
__all__ = [Client, OpenNebulaException, Host, HostPool, VirtualMachine,
VirtualMachinePool, User, UserPool,
Image, ImagePool, VirtualNetwork, VirtualNetworkPool,
Group, GroupPool, VmTemplate, VmTemplatePool, ALL, CONNECTED,
Cluster, ClusterPool, Datastore, DatastorePool]