This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
/
connection.py
233 lines (186 loc) · 7.27 KB
/
connection.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import socket
import sys
import json
import requests
import traceback
from dashi import DashiConnection
from dashi.bootstrap import DEFAULT_EXCHANGE
from dashi.exceptions import NotFoundError
from ceiclient.exception import CeiClientError
PYON_RETRIES = 5
class CeiConnection(object):
"""Abstract class defining the interface to talk with CEI services"""
def call(self, service, operation, **kwargs):
pass
class DashiCeiConnection(CeiConnection):
_name = 'ceiclient'
def __init__(self, broker, username, password, exchange=None, timeout=None, port=5672, ssl=False, sysname=None):
self.amqp_broker = broker
self.amqp_username = username
self.amqp_password = password
self.amqp_port = port
self.sysname = sysname
self.amqp_exchange = exchange or DEFAULT_EXCHANGE
self.timeout = timeout
self.dashi_connection = DashiConnection(self._name,
'amqp://%s:%s@%s:%s//' % (
self.amqp_username,
self.amqp_password, self.amqp_broker,
self.amqp_port),
self.amqp_exchange, ssl=ssl, sysname=self.sysname)
def call(self, service, operation, **kwargs):
try:
return self.dashi_connection.call(service, operation, self.timeout, **kwargs)
except socket.timeout as e:
raise CeiClientError("timed out")
except socket.error as e:
raise CeiClientError(e)
def fire(self, service, operation, **kwargs):
try:
return self.dashi_connection.fire(service, operation, **kwargs)
except socket.timeout as e:
raise CeiClientError("timed out")
except socket.error as e:
raise CeiClientError(e)
def disconnect(self):
self.dashi_connection.disconnect()
class PyonCeiConnection(CeiConnection):
_name = 'ceiclient'
def __init__(self, broker, username, password, vhost='/',
sysname=None, timeout=None, port=5672, ssl=False):
try:
from pyon.net.messaging import make_node
from pyon.net.endpoint import RPCClient
from pyon.util.containers import get_default_sysname
import pyon.core.exception as pyonexception
except ImportError:
raise CeiClientError("Pyon isn't available in your environment")
self.pyonexception = pyonexception
self.RPCClient = RPCClient
self.connection_params = {
'host': broker,
'username': username,
'password': password,
'vhost': vhost,
'port': port
}
self.timeout = timeout
self.sysname = sysname or get_default_sysname()
node, ioloop = make_node(connection_params=self.connection_params,
timeout=self.timeout)
interceptor_config = {
'interceptors': {
'encode': {
'class': 'pyon.core.interceptor.encode.EncodeInterceptor'
}
},
'stack': {
'message_incoming': ['encode'],
'message_outgoing': ['encode']
}
}
node.setup_interceptors(interceptor_config)
self.pyon_node = node
self.pyon_ioloop = ioloop
def call(self, service, operation, retry=PYON_RETRIES, **kwargs):
pyonex = self.pyonexception
to_name = (self.sysname, service)
client = self.RPCClient(node=self.pyon_node, to_name=to_name)
for i in range(0, retry):
try:
ret = client.request(kwargs, op=operation)
break
except pyonex.IonException, e:
if e.status_code in (pyonex.TIMEOUT, pyonex.SERVER_ERROR,
pyonex.SERVICE_UNAVAILABLE):
print >> sys.stderr, "Problem calling Pyon, retry %s" % i
print >> sys.stderr, traceback.format_exc()
continue
else:
raise
else:
raise CeiClientError("Tried %s times to do %s. Giving up." % (retry, operation))
return ret
def fire(self, service, operation, **kwargs):
to_name = (self.sysname, service)
client = self.RPCClient(node=self.pyon_node, to_name=to_name)
return client.request(kwargs, op=operation)
def disconnect(self):
self.pyon_node.stop_node()
self.pyon_ioloop.kill()
class PyonHTTPGateWayCeiConnection(CeiConnection):
def __init__(self, hostname, timeout=None, port=5001, ssl=False):
self.hostname = hostname
self.timeout = timeout
self.port = port
self.ssl = ssl
if self.ssl:
self.scheme = "https"
else:
self.scheme = "http"
self.url = "%s://%s:%s" % (self.scheme, self.hostname, self.port)
def _make_url(self, service, operation, call_type=None):
if call_type is None:
call_type = 'service'
elif call_type == 'agent':
call_type = 'agent'
operation = 'execute_agent'
elif call_type == 'agent_resource':
call_type = 'agent'
operation = 'execute_resource'
return "%s/ion-%s/%s/%s" % (self.url, call_type, service, operation)
def _make_parameters(self, service, operation, params, call_type=None):
if call_type is None:
call_type = 'service'
if call_type == 'service':
payload = {
'serviceRequest': {
'serviceName': service,
'serviceOp': operation,
'params': params
}
}
elif call_type == 'agent':
params['command'] = {
'type_': 'AgentCommand',
'command': operation
}
payload = {
'agentRequest': {
'agentId': service,
'agentOp': 'execute_agent',
'params': params
}
}
elif call_type == 'agent_resource':
params['command'] = {
'type_': 'AgentCommand',
'command': operation
}
payload = {
'agentRequest': {
'agentId': service,
'agentOp': 'execute_resource',
'params': params
}
}
params = {'payload': json.dumps(payload)}
return params
def call(self, service, operation, retry=PYON_RETRIES, call_type=None, **kwargs):
url = self._make_url(service, operation, call_type=call_type)
params = self._make_parameters(service, operation, kwargs, call_type=call_type)
result = requests.post(url, data=params)
result_json = result.json()
try:
return result_json['data']['GatewayResponse']
except KeyError:
print result_json
error = result_json['data']['GatewayError']
if error.get('Exception') == 'NotFound':
raise NotFoundError("%s: %s" % (error['Exception'], error['Message']))
else:
raise CeiClientError("%s: %s" % (error['Exception'], error['Message']))
def fire(self, service, operation, **kwargs):
pass
def disconnect(self):
pass