-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathes.py
More file actions
executable file
·81 lines (57 loc) · 1.85 KB
/
es.py
File metadata and controls
executable file
·81 lines (57 loc) · 1.85 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import argparse
from elasticsearch import Elasticsearch
import yaml
from action import ACTIONS
def parse_conf(path):
try:
with open(path) as fd:
conf = yaml.safe_load(fd)
if not conf:
raise ValueError('Empty file')
except IOError:
print '{} not found on disk.'.format(path)
except ValueError as exc:
print 'Invalid conf: {}'.format(exc)
else:
return conf
def initialize_es_connexion(conf, profile):
try:
p = conf[profile]
http_auth = '{}:{}'.format(p.get('username'), p.get('password'))
es = Elasticsearch(host=p.get('host'),
port=p.get('port'),
use_ssl=p.get('use_ssl'),
http_auth=http_auth)
assert es.ping()
except KeyError:
print 'The profile {} does not exist'.format(profile)
except AssertionError:
print 'The cluster {} is not pingable'.format(p.get('host'))
else:
return es
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-p',
'--profile',
required=True,
help='Elasticsearch profile from config.yaml')
parser.add_argument('-c',
'--conf',
required=False,
default="./config.yaml",
help='Location of the yaml conf file')
subparsers = parser.add_subparsers(help="actions")
for action_cls in ACTIONS:
action_cls.parser(subparsers)
args = parser.parse_args()
conf = parse_conf(args.conf)
if not conf:
return
es = initialize_es_connexion(conf, args.profile)
if not es:
return
args.action_cls(es).do(args)
if __name__ == '__main__':
main()