forked from WIPACrepo/pyglidein
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_util.py
117 lines (100 loc) · 3.37 KB
/
client_util.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
"""Utilities used by client.py and ssh_helper.py"""
from __future__ import absolute_import, division, print_function
import logging
import threading
import urllib2
import ast
from util import json_encode, json_decode
logger = logging.getLogger('client_util')
class Client(object):
"""Raw JSONRPC client object"""
cid = 0
cidlock = threading.RLock()
def __init__(self, timeout=60.0, address=None, ssl_options=None):
if address is None:
raise Exception('need a valid address')
# save timeout
self._timeout = timeout
# save address
self._address = address
# save ssl_options
self._sslopts = ssl_options
@classmethod
def newid(cls):
cls.cidlock.acquire()
cid = cls.cid
cls.cid += 1
cls.cidlock.release()
return cid
def request(self, methodname, kwargs):
"""Send request to RPC Server"""
# check method name for bad characters
if methodname[0] == '_':
logger.warning('cannot use RPC for private methods')
raise Exception('Cannot use RPC for private methods')
# translate request to json
body = json_encode({'jsonrpc': '2.0', 'method': methodname,
'params': kwargs, 'id': Client.newid()})
headers = {'Content-type':'application/json'}
request = urllib2.Request(self._address, data=body, headers=headers)
# make request to server
try:
response = urllib2.urlopen(request, timeout=self._timeout)
except Exception:
logger.warn('error making jsonrpc request', exc_info=True)
raise
# translate response from json
try:
cb_data = response.read()
data = json_decode(cb_data)
except Exception:
try:
logger.info('json data: %r', cb_data)
except Exception:
pass
raise
if 'error' in data:
try:
raise Exception('Error %r: %r %r'%data['error'])
except Exception:
raise Exception('Error %r'%data['error'])
if 'result' in data:
return data['result']
else:
return None
def get_state(address):
"""Getting the server state directly from remote queue"""
c = Client(address=address)
try:
return c.request('get_state', {})
except Exception:
logger.warn('error getting state', exc_info=True)
def monitoring(address,info=None):
"""Sending monitoring information back"""
if info is None:
info = {}
c = Client(address=address)
try:
return c.request('monitoring', info)
except Exception:
logger.warn('error getting state', exc_info=True)
def config_options_dict(config):
"""
Parsing config file
Args:
config: Pythong config parser object
Returns:
A dict with the different sections of the config file
and the literal values of the configuraton objects
"""
config_dict = {}
for section in config.sections():
config_dict[section] = {}
for option in config.options(section):
val = config.get(section, option)
try:
val = ast.literal_eval(val)
except Exception:
pass
config_dict[section][option] = val
return config_dict