/
cli.py
395 lines (313 loc) · 10.5 KB
/
cli.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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# -*- coding: utf-8 -*-
from __future__ import print_function
import ckantoolkit as tk
import click
import ckanext.harvest.utils as utils
from ckanext.harvest.logic import HarvestJobExists
def get_commands():
return [harvester]
@click.group()
def harvester():
"""Harvests remotely mastered metadata.
"""
pass
@harvester.group()
def source():
"""Manage harvest sources
"""
pass
@source.command()
@click.argument(u"name")
@click.argument(u"url")
@click.argument(u"type")
@click.argument(u"title", required=False)
@click.argument(u"active", type=tk.asbool, default=True)
@click.argument(u"owner_org", required=False)
@click.argument(u"frequency", default=u"MANUAL")
@click.argument(u"config", required=False)
def create(name, url, type, title, active, owner_org, frequency, config):
"""Create new harvest source.
"""
try:
result = utils.create_harvest_source(
name, url, type, title, active, owner_org, frequency, config
)
except tk.ValidationError as e:
tk.error_shout(u"Validation error:")
for field, err in e.error_summary.items():
tk.error_shout("\t{}: {}".format(field, err))
raise click.Abort()
click.echo(result)
@source.command()
@click.argument(u"id", metavar=u"SOURCE_ID_OR_NAME")
@click.pass_context
def show(ctx, id):
"""Shows a harvest source.
"""
flask_app = ctx.meta["flask_app"]
try:
with flask_app.test_request_context():
result = utils.show_harvest_source(id)
except tk.ObjectNotFound:
tk.error_shout(u"Source <{}> not found.".format(id))
raise click.Abort()
click.echo(result)
@source.command()
@click.argument(u"id", metavar=u"SOURCE_ID_OR_NAME")
@click.pass_context
def remove(ctx, id):
"""Remove (deactivate) a harvester source, whilst leaving any related
datasets, jobs and objects.
"""
flask_app = ctx.meta["flask_app"]
with flask_app.test_request_context():
utils.remove_harvest_source(id)
click.secho("Removed harvest source: {0}".format(id), fg="green")
@source.command()
@click.argument(u"id", metavar=u"SOURCE_ID_OR_NAME")
@click.pass_context
def clear(ctx, id):
"""Clears all datasets, jobs and objects related to a harvest source,
but keeps the source itself.
"""
flask_app = ctx.meta["flask_app"]
with flask_app.test_request_context():
utils.clear_harvest_source(id)
click.secho("Cleared harvest source: {0}".format(id), fg="green")
@source.command()
@click.argument(u"id", metavar=u"SOURCE_ID_OR_NAME", required=False)
@click.option(
"-k",
"--keep-current",
default=False
)
@click.pass_context
def clear_history(ctx, id, keep_current):
"""If no source id is given the history for all harvest sources
(maximum is 1000) will be cleared.
Clears all jobs and objects related to a harvest source, but keeps
the source itself. The datasets imported from the harvest source
will NOT be deleted!!! If a source id is given, it only clears
the history of the harvest source with the given source id.
"""
flask_app = ctx.meta["flask_app"]
with flask_app.test_request_context():
result = utils.clear_harvest_source_history(id, bool(keep_current))
click.secho(result, fg="green")
@harvester.command()
@click.argument("all", required=False)
@click.pass_context
def sources(ctx, all):
"""Lists harvest sources.
If 'all' is defined, it also shows the Inactive sources
"""
flask_app = ctx.meta["flask_app"]
with flask_app.test_request_context():
result = utils.list_sources(bool(all))
click.echo(result)
@harvester.command()
@click.argument("id", metavar="SOURCE_ID_OR_NAME")
@click.pass_context
def job(ctx, id):
"""Create new harvest job and runs it (puts it on the gather queue).
"""
flask_app = ctx.meta["flask_app"]
with flask_app.test_request_context():
try:
result = utils.create_job(id)
except HarvestJobExists as e:
tk.error_shout(e)
ctx.abort()
click.echo(result)
@harvester.command()
@click.pass_context
def jobs(ctx):
"""Lists harvest jobs.
"""
flask_app = ctx.meta["flask_app"]
with flask_app.test_request_context():
result = utils.list_jobs()
click.echo(result)
@harvester.command()
@click.argument("id", metavar="SOURCE_OR_JOB_ID")
@click.pass_context
def job_abort(ctx, id):
"""Marks a job as "Aborted" so that the source can be restarted afresh.
It ensures that the job's harvest objects status are also marked
finished. You should ensure that neither the job nor its objects
are currently in the gather/fetch queues.
"""
flask_app = ctx.meta["flask_app"]
with flask_app.test_request_context():
try:
result = utils.abort_job(id)
except tk.ObjectNotFound:
tk.error_shout(u"Job not found.")
ctx.abort()
click.echo(result)
@harvester.command()
@click.argument("life_span", default=False, required=False)
@click.option(
"-i",
"--include",
default=False,
help="""If source_id provided as included, then only it's failed jobs will be aborted.
You can use comma as a separator to provide multiple source_id's""",
)
@click.option(
"-e",
"--exclude",
default=False,
help="""If source_id provided as excluded, all sources failed jobs, except for that
will be aborted. You can use comma as a separator to provide multiple source_id's""",
)
@click.pass_context
def abort_failed_jobs(ctx, life_span, include, exclude):
"""Abort all jobs which are in a "limbo state" where the job has
run with errors but the harvester run command will not mark it
as finished, and therefore you cannot run another job.
"""
flask_app = ctx.meta["flask_app"]
with flask_app.test_request_context():
result = utils.abort_failed_jobs(life_span, include, exclude)
click.echo(result)
@harvester.command()
def purge_queues():
"""Removes all jobs from fetch and gather queue.
"""
utils.purge_queues()
@harvester.command()
def gather_consumer():
"""Starts the consumer for the gathering queue.
"""
utils.gather_consumer()
@harvester.command()
def fetch_consumer():
"""Starts the consumer for the fetching queue.
"""
utils.fetch_consumer()
@harvester.command()
@click.pass_context
def run(ctx):
"""Starts any harvest jobs that have been created by putting them onto
the gather queue.
Also checks running jobs - if finished it changes their status to
Finished.
"""
flask_app = ctx.meta["flask_app"]
with flask_app.test_request_context():
utils.run_harvester()
@harvester.command()
@click.pass_context
@click.argument("id", metavar="SOURCE_ID_OR_NAME")
@click.argument("force-import", required=False, metavar="GUID")
def run_test(ctx, id, force_import=None):
"""Runs a harvest - for testing only.
This does all the stages of the harvest (creates job, gather,
fetch, import) without involving the web UI or the queue
backends. This is useful for testing a harvester without having to
fire up gather/fetch_consumer processes, as is done in production.
"""
if force_import:
force_import = force_import.split('=')[-1]
flask_app = ctx.meta["flask_app"]
with flask_app.test_request_context():
utils.run_test_harvester(id, force_import)
@harvester.command("import")
@click.pass_context
@click.argument("id", metavar="SOURCE_ID_OR_NAME", required=False)
@click.option(
"-j",
"--no-join-datasets",
is_flag=True,
help="Do not join harvest objects to existing datasets",
)
@click.option(
"-o",
"--harvest-object-id",
help="Id of the harvest object to which perform the import stage",
)
@click.option(
"-p",
"--package-id",
help="Id of the package whose harvest object to perform the import stage for",
)
@click.option(
"-g",
"--guid",
help="Guid of the harvest object to which perform the import stage for",
)
@click.option(
"--segments",
help="""A string containing hex digits that represent which of
the 16 harvest object segments to import. e.g. 15af will run segments 1,5,a,f""",
)
def import_stage(
ctx, id, no_join_datasets, harvest_object_id, guid, package_id, segments
):
"""Perform the import stage with the last fetched objects, for a
certain source or a single harvest object.
Please note that no objects will be fetched from the remote
server. It will only affect the objects already present in the
database.
To import a particular harvest source, specify its id as an argument.
To import a particular harvest object use the -o option.
To import a particular guid use the -g option.
To import a particular package use the -p option.
You will need to specify the -j flag in cases where the datasets
are not yet created (e.g. first harvest, or all previous harvests
have failed)
The --segments flag allows to define a string containing hex
digits that represent which of the 16 harvest object segments to
import. e.g. 15af will run segments 1,5,a,f
"""
flask_app = ctx.meta["flask_app"]
with flask_app.test_request_context():
try:
utils.import_stage(
id,
no_join_datasets,
harvest_object_id,
guid,
package_id,
segments,
)
except tk.ObjectNotFound:
tk.error_shout(u"Source <{}> not found.".format(id))
@harvester.command()
@click.pass_context
def clean_harvest_log(ctx):
"""Clean-up mechanism for the harvest log table.
You can configure the time frame through the configuration
parameter `ckan.harvest.log_timeframe`. The default time frame is 30
days
"""
flask_app = ctx.meta["flask_app"]
with flask_app.test_request_context():
utils.clean_harvest_log()
@harvester.command("job-all")
@click.pass_context
def job_all(ctx):
"""Create new harvest jobs for all active sources.
"""
flask_app = ctx.meta["flask_app"]
with flask_app.test_request_context():
result = utils.job_all()
click.echo(result)
@harvester.command()
@click.pass_context
def reindex(ctx):
"""Reindexes the harvest source datasets.
"""
flask_app = ctx.meta["flask_app"]
with flask_app.test_request_context():
utils.reindex()
@harvester.command("harvesters_info")
@click.pass_context
def harvesters_info(ctx):
"""
"""
flask_app = ctx.meta["flask_app"]
with flask_app.test_request_context():
result = utils.harvesters_info()
click.echo(result)