-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathhost_dumper.py
54 lines (48 loc) · 2.15 KB
/
host_dumper.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
#!/usr/bin/env python
import argparse
import pprint
from app import create_app
from app.common import inventory_config
from app.culling import Timestamps
from app.environment import RuntimeEnvironment
from app.models import Host
from app.serialization import serialize_host
application = create_app(RuntimeEnvironment.COMMAND)
parser = argparse.ArgumentParser(
description="Util that dumps a host from the hosts table. The db configuration is read from the environment. "
"This util is expected to be used within the image/pod."
)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--id", help="search for a host using id")
group.add_argument("--hostname", help="search for a host using display_name, fqdn")
group.add_argument("--insights_id", help="search for a host using insights_id")
group.add_argument("--org_id", help="dump all hosts associated with the org_id")
parser.add_argument("--no-pp", help="enable pretty printing", action="store_true")
args = parser.parse_args()
with application.app.app_context():
# query_results = Host.query.filter().all()
# print(query_results)
if args.id:
host_id_list = [args.id]
print("looking up host using id")
query_results = Host.query.filter(Host.id.in_(host_id_list)).all()
elif args.hostname:
print("looking up host using display_name, fqdn")
query_results = Host.query.filter(
Host.display_name.comparator.contains(args.hostname)
| Host.canonical_facts["fqdn"].astext.contains(args.hostname)
).all()
elif args.insights_id:
print("looking up host using insights_id")
query_results = Host.query.filter(
Host.canonical_facts.comparator.contains({"insights_id": args.insights_id})
).all()
elif args.org_id:
query_results = Host.query.filter(Host.org_id == args.org_id).all()
staleness_timestamps = Timestamps.from_config(inventory_config())
json_host_list = [serialize_host(host, staleness_timestamps) for host in query_results]
if args.no_pp:
print(json_host_list)
else:
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(json_host_list)