-
Notifications
You must be signed in to change notification settings - Fork 0
/
garbo.py
73 lines (52 loc) · 1.9 KB
/
garbo.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
"""
...
"""
import argparse
import logging
import yaml
from garbo import config, utils
from garbo.discovery import aws
from garbo.storage.dummy import dump_graph, load_graph
from garbo.storage.d3js import D3JSForce
__author__ = 'nati'
def _get_parsed_arguments():
parser = argparse.ArgumentParser()
parser.add_argument('--applications', '-a', default=False,
help='YAML file containing Core Resources per application')
parser.add_argument('--discovery', '-d', action='store_true', default=False,
help='Perform a discovery (don\'t use stored graph). default: False')
parser.add_argument('--gen-d3js', '-g', action='store_true', default=False,
help='Generate a json file for D3JS Directed Force Graph. default: False')
return parser.parse_args()
def _gen_d3js(graph):
# TODO: move to d3js.py ?
fdg = D3JSForce()
for item in graph:
fdg.add_item(item)
fdg.export('d3js/garbo.json')
def main():
# Load configuration and parse arguments
config.load()
logging.basicConfig(level=logging.INFO)
args = _get_parsed_arguments()
if args.discovery:
# Perform discovery into selected storage
graph = list(aws.collect_all())
dump_graph(graph)
else:
# Read resources and relations from storage
graph = load_graph()
unused_graph = []
if args.applications:
# Read applications file
with open(args.applications) as applications_file:
applications = yaml.load(applications_file)
root_resources = [r for app in applications for r in applications[app]]
# Perform mark & Sweep
unused_graph = utils.mark_and_sweep(graph, root_resources)
out_graph = unused_graph if args.applications else graph
if args.gen_d3js:
# Generate a graph
_gen_d3js(out_graph)
if __name__ == '__main__':
main()