forked from elastic/ecs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
107 lines (88 loc) · 4.13 KB
/
generator.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
import argparse
import glob
import os
import yaml
import time
from generators import asciidoc_fields
from generators import beats
from generators import csv_generator
from generators import es_template
from generators import ecs_helpers
from generators import intermediate_files
from schema import loader
from schema import cleaner
from schema import finalizer
from schema import subset_filter
from schema import exclude_filter
def main():
args = argument_parser()
ecs_generated_version = read_version(args.ref)
print('Running generator. ECS version ' + ecs_generated_version)
# default location to save files
out_dir = 'generated'
docs_dir = 'docs'
if args.out:
default_dirs = False
out_dir = os.path.join(args.out, out_dir)
docs_dir = os.path.join(args.out, docs_dir)
else:
default_dirs = True
ecs_helpers.make_dirs(out_dir)
# To debug issues in the gradual building up of the nested structure, insert
# statements like this after any step of interest.
# ecs_helpers.yaml_dump('ecs.yml', fields)
# Detect usage of experimental changes to tweak artifact version label
if args.include and loader.EXPERIMENTAL_SCHEMA_DIR in args.include:
ecs_generated_version += "+exp"
print('Experimental ECS version ' + ecs_generated_version)
fields = loader.load_schemas(ref=args.ref, included_files=args.include)
cleaner.clean(fields, strict=args.strict)
finalizer.finalize(fields)
fields = subset_filter.filter(fields, args.subset, out_dir)
fields = exclude_filter.exclude(fields, args.exclude)
nested, flat = intermediate_files.generate(fields, os.path.join(out_dir, 'ecs'), default_dirs)
if args.intermediate_only:
exit()
csv_generator.generate(flat, ecs_generated_version, out_dir)
es_template.generate(nested, ecs_generated_version, out_dir, args.mapping_settings)
es_template.generate_legacy(flat, ecs_generated_version, out_dir, args.template_settings, args.mapping_settings)
beats.generate(nested, ecs_generated_version, out_dir)
if args.include or args.subset or args.exclude:
exit()
ecs_helpers.make_dirs(docs_dir)
asciidoc_fields.generate(nested, ecs_generated_version, docs_dir)
def argument_parser():
parser = argparse.ArgumentParser()
parser.add_argument('--ref', action='store', help='Loads fields definitions from `./schemas` subdirectory from specified git reference. \
Note that "--include experimental/schemas" will also respect this git ref.')
parser.add_argument('--include', nargs='+',
help='include user specified directory of custom field definitions')
parser.add_argument('--exclude', nargs='+',
help='exclude user specified subset of the schema')
parser.add_argument('--subset', nargs='+',
help='render a subset of the schema')
parser.add_argument('--out', action='store', help='directory to output the generated files')
parser.add_argument('--template-settings', action='store',
help='index template settings to use when generating elasticsearch template')
parser.add_argument('--mapping-settings', action='store',
help='mapping settings to use when generating elasticsearch template')
parser.add_argument('--strict', action='store_true',
help='enforce strict checking at schema cleanup')
parser.add_argument('--intermediate-only', action='store_true',
help='generate intermediary files only')
args = parser.parse_args()
# Clean up empty include of the Makefile
if args.include and [''] == args.include:
args.include.clear()
return args
def read_version(ref=None):
if ref:
print('Loading schemas from git ref ' + ref)
tree = ecs_helpers.get_tree_by_ref(ref)
return tree['version'].data_stream.read().decode('utf-8').rstrip()
else:
print('Loading schemas from local files')
with open('version', 'r') as infile:
return infile.read().rstrip()
if __name__ == '__main__':
main()