-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdiscover_test.py
113 lines (83 loc) · 2.77 KB
/
discover_test.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
from flask import json
from itertools import groupby
import logging
import sys
from rubick.common import MarkedIssue, Inspection
from rubick.discovery import OpenstackDiscovery
import rubick.inspections
# Silence PEP8 "unused import"
assert rubick.inspections
import rubick.schemas
assert rubick.schemas
from rubick.json import openstack_for_json
from rubick.model import DirectoryResource
def indent_prefix(indent=0):
s = ''
if indent > 0:
for i in xrange(0, indent):
s += ' '
return s
def print_issue(issue, indent=0):
prefix = indent_prefix(indent)
if hasattr(issue, 'mark'):
print(
'%s[%s] %s (line %d column %d)' %
(prefix, issue.type, issue.message,
issue.mark.line + 1, issue.mark.column + 1))
else:
print('%s[%s] %s' % (prefix, issue.type, issue.message))
def print_issues(issues, indent=0):
issue_source_f = lambda i: i.mark.source if isinstance(
i, MarkedIssue) else None
source_groupped_issues = groupby(
sorted(issues, key=issue_source_f), key=issue_source_f)
for source, issues in source_groupped_issues:
if source:
print('%sFile %s' % (indent_prefix(indent), source))
for issue in sorted(issues, key=lambda i: i.mark.line):
print_issue(issue, indent + 1)
else:
for issue in issues:
print_issue(issue, indent)
def print_service(service):
print(' ' + service.name)
print_issues(service.all_issues, indent=3)
def print_path(path):
if isinstance(path, DirectoryResource):
print(' ' + path.path + '/')
else:
print(' ' + path.path)
print_issues(path.all_issues, indent=3)
def print_host(host):
print(host)
print_issues(host.issues, indent=1)
print(' Services:')
for service in host.components:
print_service(service)
print(' Filesystem:')
for path in sorted(host.filesystem.values(), key=lambda f: f.path):
print_path(path)
def print_openstack(openstack):
print_issues(openstack.issues)
for host in openstack.hosts:
print_host(host)
def main():
logging.basicConfig(level=logging.WARNING)
logging.getLogger('rubick').setLevel(logging.DEBUG)
discovery = OpenstackDiscovery()
try:
with open('test_rsa') as f:
private_key = f.read()
except Exception:
private_key = sys.stdin.read()
openstack = discovery.discover(
['172.18.65.179'],
private_key=private_key)
all_inspections = Inspection.all_inspections()
for inspection in all_inspections:
x = inspection()
x.inspect(openstack)
print_openstack(openstack)
# print(json.dumps(openstack_for_json(openstack)))
if __name__ == '__main__':
main()