-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathutils.py
More file actions
297 lines (243 loc) · 9.52 KB
/
utils.py
File metadata and controls
297 lines (243 loc) · 9.52 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# -*- coding: utf-8 -*-
'''
python-capture-agent
~~~~~~~~~~~~~~~~~~~~
:copyright: 2014-2017, Lars Kiesow <lkiesow@uos.de>
:license: LGPL – see license.lgpl for more details.
'''
from pyca.config import config
from pyca import db
from datetime import datetime
from dateutil.tz import tzutc
import errno
import json
import logging
import os
import os.path
import pycurl
import time
from io import BytesIO as bio
from urllib.parse import quote as urlquote
logger = logging.getLogger(__name__)
def http_request(url, post_data=None, timeout=None):
'''Make an HTTP request to a given URL with optional parameters.
'''
logger.debug('Requesting URL: %s', url)
buf = bio()
curl = pycurl.Curl()
curl.setopt(curl.URL, url.encode('ascii', 'ignore'))
# Use cookies if configured
if config('server', 'cookiefile'):
curl.setopt(curl.COOKIEJAR, config('server', 'cookiefile'))
curl.setopt(curl.COOKIEFILE, config('server', 'cookiefile'))
# More verbose curl calls in debug mode
if logger.getEffectiveLevel() == logging.DEBUG:
curl.setopt(pycurl.VERBOSE, True)
# Disable HTTPS verification methods if insecure is set
if config('server', 'insecure'):
curl.setopt(curl.SSL_VERIFYPEER, 0)
curl.setopt(curl.SSL_VERIFYHOST, 0)
if config('server', 'certificate'):
# Make sure verification methods are turned on
curl.setopt(curl.SSL_VERIFYPEER, 1)
curl.setopt(curl.SSL_VERIFYHOST, 2)
# Import your certificates
curl.setopt(pycurl.CAINFO, config('server', 'certificate'))
if config('ingest', 'upload_rate') > 0:
curl.setopt(
curl.MAX_SEND_SPEED_LARGE,
config('ingest', 'upload_rate'))
curl.setopt(curl.CONNECTTIMEOUT, config('http', 'connection_timeout'))
if timeout is not None:
curl.setopt(curl.TIMEOUT, timeout)
else:
curl.setopt(curl.TIMEOUT, config('http', 'timeout'))
if post_data:
curl.setopt(curl.HTTPPOST, post_data)
curl.setopt(curl.WRITEFUNCTION, buf.write)
logger.debug('Using authentication method %s',
config('server')['auth_method'])
if config('server')['auth_method'] == 'digest':
curl.setopt(curl.HTTPHEADER, ['X-Requested-Auth: Digest'])
curl.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_DIGEST)
curl.setopt(pycurl.USERPWD, ':'.join([config('server', 'username'),
config('server', 'password')]))
curl.setopt(curl.FAILONERROR, True)
curl.setopt(curl.FOLLOWLOCATION, True)
curl.perform()
curl.close()
result = buf.getvalue()
buf.close()
return result
def get_service(service_type):
'''Get available service endpoints for a given service type from the
Opencast ServiceRegistry.
'''
override = config('server', 'service_overrides', service_type)
if override:
logger.info('Overriding endpoint for %s: %s', service_type, override)
return [override]
# Get available services from Opencast
endpoint = '/services/available.json?serviceType=' + str(service_type)
url = config('server', 'url') + endpoint
response = http_request(url).decode('utf-8')
services = json.loads(response).get('services', {}).get('service', [])
services = ensurelist(services)
endpoints = [service['host'] + service['path'] for service in services
if service['online'] and service['active']]
for endpoint in endpoints:
logger.info(u'Endpoint for %s: %s', service_type, endpoint)
return endpoints
def timestamp():
'''Get current unix timestamp
'''
return int(datetime.now(tzutc()).timestamp())
def try_mkdir(directory):
'''Try to create a directory. Pass without error if it already exists.
'''
try:
os.mkdir(directory)
except OSError as err:
if err.errno != errno.EEXIST:
raise err
def service(service_name, force_update=False):
'''Get the location of a given service from Opencast and add it to the
current configuration.
:param service_name: Name of the service type to request locations for
:param force_update: Force an update, possibly wait for a service to
become available if none is available right now.
:return: List of service locations
'''
service_id = f'org.opencastproject.{service_name}'
service_url = config('services', service_id)
logger.debug('Cached service URLs for %s: %s', service_name, service_url)
if service_url and not force_update:
return service_url
# Get service from Opencast server
config('services')[service_id] = []
while not config('services', service_id) and not terminate():
try:
config('services')[service_id] = get_service(service_id)
logger.debug('Updates service URL for %s: %s',
service_name,
config('services', service_id))
except pycurl.error:
logger.exception('Could not get %s endpoint. Retry in 5s',
service_name)
time.sleep(5.0)
return config('services', service_id)
def ensurelist(x):
'''Ensure an element is a list.'''
return x if type(x) is list else [x]
def register_ca(status='idle'):
'''Register this capture agent at the Matterhorn admin server so that it
shows up in the admin interface.
:param status: Current status of the capture agent
'''
# If this is a backup CA we don't tell the Matterhorn core that we are
# here. We will just run silently in the background:
if config('agent', 'backup_mode'):
return
service_endpoint = service('capture.admin')
if not service_endpoint:
logger.warning('Missing endpoint for updating agent status.')
return
params = [('address', config('ui', 'url')), ('state', status)]
name = urlquote(config('agent', 'name').encode('utf-8'), safe='')
url = f'{service_endpoint[0]}/agents/{name}'
try:
response = http_request(url, params).decode('utf-8')
if response:
logger.info(response)
except pycurl.error as e:
logger.warning('Could not set agent state to %s: %s', status, e)
def recording_state(recording_id, status):
'''Send the state of the current recording to the Matterhorn core.
:param recording_id: ID of the current recording
:param status: Status of the recording
'''
# If this is a backup CA we do not update the recording state since the
# actual CA does that and we want to interfere. We will just run silently
# in the background:
if config('agent', 'backup_mode'):
return
params = [('state', status)]
url = service('capture.admin')[0]
url += f'/recordings/{recording_id}'
try:
result = http_request(url, params).decode('utf-8')
logger.info(result)
except pycurl.error as e:
logger.warning('Could not set recording state to %s: %s', status, e)
@db.with_session
def update_event_status(dbs, event, status):
'''Update the status of a particular event in the database.
'''
dbs.query(db.RecordedEvent).filter(db.RecordedEvent.start == event.start)\
.update({'status': status})
event.status = status
dbs.commit()
@db.with_session
def set_service_status(dbs, service, status):
'''Update the status of a particular service in the database.
'''
srv = db.ServiceStates()
srv.type = service
srv.status = status
dbs.merge(srv)
dbs.commit()
def set_service_status_immediate(service, status):
'''Update the status of a particular service in the database and send an
immediate signal to Opencast.
'''
set_service_status(service, status)
update_agent_state()
@db.with_session
def get_service_status(dbs, service):
'''Update the status of a particular service in the database.
'''
srvs = dbs.query(db.ServiceStates).filter(db.ServiceStates.type == service)
if srvs.count():
return srvs[0].status
return db.ServiceStatus.STOPPED
def update_agent_state():
'''Update the current agent state in opencast.
'''
status = 'idle'
# Determine reported agent state with priority list
if get_service_status(db.Service.SCHEDULE) == db.ServiceStatus.STOPPED:
status = 'offline'
elif get_service_status(db.Service.CAPTURE) == db.ServiceStatus.BUSY:
status = 'capturing'
elif get_service_status(db.Service.INGEST) == db.ServiceStatus.BUSY:
status = 'uploading'
register_ca(status=status)
def update_agent_config():
'''Update the current agent configuration in opencast.
'''
if config('agent', 'backup_mode'):
return
service_endpoint = service('capture.admin')
if not service_endpoint:
logger.warning('Missing endpoint for updating agent status.')
return
inputs = ",".join(config('agent', 'inputs'))
params = [(
'configuration',
'{\'capture.device.names\': \'' + inputs + '\'}'
)]
name = urlquote(config('agent', 'name').encode('utf-8'), safe='')
url = f'{service_endpoint[0]}/agents/{name}/configuration'
try:
response = http_request(url, params).decode('utf-8')
if response:
logger.info(response)
except pycurl.error as e:
logger.warning('Could not set configuration of agent %s: %s', name, e)
def terminate(shutdown=None):
'''Mark process as to be terminated.
'''
global _terminate
if shutdown is not None:
_terminate = shutdown
return '_terminate' in globals() and _terminate