/
manage.py
executable file
·187 lines (148 loc) · 4.71 KB
/
manage.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
#!/usr/bin/env python
from copy import deepcopy
from datetime import datetime
import json
from glob import glob
import gzip
from hashlib import sha1
import os
import requests
import sys
import time
import logging
from time import sleep
import click
from click.core import Command
from click.decorators import _make_command
from elasticsearch.exceptions import NotFoundError
from binoas.utils import load_config, parse_frequency
from binoas.es import setup_elasticsearch
from binoas.db import setup_db
from binoas.digest import Digest
from binoas.transformers import JSONPathPostTransformer
logging.basicConfig(
format='%(asctime)s.%(msecs)s:%(name)s:%(thread)d:%(levelname)s:%(process)d:%(message)s',
level=logging.INFO)
def command(name=None, cls=None, **attrs):
"""
Wrapper for click Commands, to replace the click.Command docstring with the
docstring of the wrapped method (i.e. the methods defined below). This is
done to support the autodoc in Sphinx, and the correct display of docstrings
"""
if cls is None:
cls = Command
def decorator(f):
r = _make_command(f, name, attrs, cls)
r.__doc__ = f.__doc__
return r
return decorator
def _create_path(path):
if not os.path.exists(path):
click.secho('Creating path "%s"' % path, fg='green')
os.makedirs(path)
return path
@click.group()
@click.version_option()
def cli():
"""Binoas"""
@cli.group()
def elasticsearch():
"""Manage Elasticsearch"""
@cli.group()
def digest():
"""Manage digests"""
@cli.group()
def database():
"""Manage database"""
@cli.group()
def transform():
"""Transform documents (for testing)"""
@command('put_template')
@click.option('--template_file', default='mappings/template.json',
type=click.File('rb'), help='Path to JSON file containing the template.')
def es_put_template(template_file):
"""
Put a template into Elasticsearch. A template contains settings and mappings
that should be applied to multiple indices. Check ``mappings/template.json``
for an example.
:param template_file: Path to JSON file containing the template. Defaults to ``mappings/template.json``.
"""
config = load_config()
es = setup_elasticsearch(config)
click.echo('Putting ES template: %s' % template_file.name)
template = json.load(template_file)
template_file.close()
es.indices.put_template('binoas_template', template)
@command('cleanup')
def es_cleanup():
"""
Delete documents beyond retention time
"""
config = load_config()
es = setup_elasticsearch(config)
for application in config['binoas']['applications']:
seconds = parse_frequency(
config['binoas']['applications'][application].get(
'retention', '1h'))
click.echo('Cleaning up %s (%s)' % (application, seconds,))
es_query = {
"query": {
"range": {
"modified": {
"lt": "now-%ss/s" % (seconds,),
}
}
}
}
index_name = 'binoas_%s' % (application,)
try:
res = es.delete_by_query(
index=index_name, doc_type='item', body=es_query)
except NotFoundError:
res = None
print(res)
@command('make')
@click.option('--frequency', default='1h',
type=str, help='The frequency for the digest')
def digest_make(frequency):
"""
Make a digest.
"""
config = load_config()
es = setup_elasticsearch(config)
digest = Digest(config)
for application in config['binoas']['applications']:
click.echo('Making digest for %s and frequency %s' % (
application, frequency))
digest.make(application, frequency)
sleep(5)
@command('rollback')
def database_rollback():
"""
Do a database rollback.
"""
config = load_config()
session = setup_db(config)
session.rollback()
@command('json')
@click.option('--json_file', default='mappings/template.json',
type=click.File('rb'), help='Path to JSON file containing the message.')
def transform_json(json_file):
"""
Transform an incoming json document according to the contents.
"""
config = load_config()
template = json.load(json_file)
json_file.close()
trf = JSONPathPostTransformer(config)
res = trf.transform(template)
print(json.dumps(res))
# Register commands explicitly with groups, so we can easily use the docstring
# wrapper
elasticsearch.add_command(es_put_template)
elasticsearch.add_command(es_cleanup)
digest.add_command(digest_make)
database.add_command(database_rollback)
transform.add_command(transform_json)
if __name__ == '__main__':
cli()