21 from select
import select
29 from threading
import Thread
32 APP_PATH = os.path.abspath(os.path.dirname(__file__))
35 logger = logging.getLogger(__name__)
36 logger.setLevel(settings.LOG_LEVEL)
38 fh = logging.FileHandler(os.path.join(settings.LOG_DIR,
'vmm.log'))
39 fh.setLevel(settings.LOG_LEVEL)
42 ch = logging.StreamHandler(sys.stdout)
43 ch.setLevel(settings.LOG_LEVEL)
48 dev.find(name=
"keyboard")
50 raise Exception(
"No such device")
52 logger.info(
'Setting keyboard to %s bus=0x%x, vendor=0x%x, product=0x%x, version=0x%x' % (dev.name, dev.idbus, dev.idvendor, dev.idproduct, dev.idversion))
55 verify = os.path.isdir(settings.CA_DIR)
60 actions = __import__(
'actions')
61 VERSION = actions.VERSION
66 def rpc(cmd, params=None):
67 logger.debug(
"RPC: %s, PARAMS: %s" % (cmd, params))
71 params[
'version'] = VERSION
73 params = {
'version': VERSION}
75 r = requests.get(
'%s/%s' % (settings.CTX_ADDRESS, cmd), verify=verify, params=params)
76 logger.debug(
'status: %s, ok: %s, text: %s' % (r.status_code, r.ok, r.text))
78 logger.error(
'requests error %s' % e)
84 r = json.loads(r.text)
87 if r[
'status'] !=
'ok':
88 logger.error(
'RPC Error: %s' % r[
'status'])
89 if 'actions_file' in r:
90 f = file(os.path.join(APP_PATH,
'actions.py'),
'w')
91 f.write(r[
'actions_file'])
93 if 'actions' in sys.modules:
97 actions = __import__(
'actions')
98 VERSION = actions.VERSION
103 ca_bundle = open(
'ca_bundle.crt',
'wb')
105 if not os.path.isdir(settings.CA_DIR):
106 logger.info(
'certificates directory does not exits, every request will be accepted')
109 for crt
in os.listdir(settings.CA_DIR):
110 f = open(os.path.join(settings.CA_DIR, crt),
'r')
111 ca_bundle.writelines(f.readlines())
122 if r[
'status'] !=
'ok':
123 logger.error(
'Hello failed, no acceptance from server (err %s), exiting program...' % (r[
'status']))
131 logger.error(
'error while connecting to ctx server (will renew...) %s' % (e))
137 logger.info(
'Start thread')
141 r =
rpc(
'get_command')
142 if not r
or r[
'status'] !=
'ok':
149 logger.info(
"Requested command: %s(%s)" % (r[
'name'], r[
'args']))
152 error_msg =
'ERROR: No command parameter'
153 logger.error(error_msg)
154 rpc(
'finish_command', {
'status':
'failed',
'command_id': command_id,
155 'error_message': error_msg})
157 call_action = getattr(actions, r[
'name'],
None)
159 rpc(
'finish_command', {
'status':
'failed',
'command_id': command_id,
160 'error_message':
'Invalid action'})
162 args = json.loads(r[
'args'])
165 response = call_action(**args)
166 logger.info(
"response: %s" % str(response))
167 rpc(
'finish_command', dict({
'status':
'finished',
'command_id': command_id,
'returns': response}))
169 error_msg =
'ERROR: UNDEFINED ERROR %s' % unicode(e)
170 logger.exception(error_msg)
171 rpc(
'finish_command', {
'status':
'failed',
'command_id': command_id,
172 'error_message': error_msg})
174 logger.exception(
'General exception: %s' % e)
183 logger.info(
'Waiting for keyboard')
185 r, w, x = select([dev], [], [])
186 for event
in dev.readall():
187 if event.code == 113
and event.value == 1:
188 logger.info(
"Key received")