-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathnet_report.py
More file actions
executable file
·74 lines (59 loc) · 2.19 KB
/
net_report.py
File metadata and controls
executable file
·74 lines (59 loc) · 2.19 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
#!/usr/bin/env python
import json
from himlarcli import utils
from himlarcli.keystone import Keystone
from himlarcli.mail import Mail
from himlarcli.nova import Nova
from himlarcli.parser import Parser
from himlarcli.printer import Printer
utils.is_virtual_env()
parser = Parser()
options = parser.parse_args()
printer = Printer(options.format)
kc = Keystone(options.config, debug=options.debug)
kc.set_domain(options.domain)
kc.set_dry_run(options.dry_run)
logger = kc.get_logger()
# Region
if hasattr(options, "region"):
regions = kc.find_regions(region_name=options.region)
else:
regions = kc.find_regions()
def get_network_list():
networks = []
for region in regions:
logger.debug("=> region %s", region)
nc = Nova(options.config, debug=options.debug, log=logger, region=region)
instances = nc.get_all_instances()
for i in instances:
instance = {"id": i.id}
# instance["power_state"] = getattr(i, "OS-EXT-STS:power_state")
instance["status"] = i.status.lower()
# print(dir(i))
for k, v in i.networks.items():
instance["network"] = {"type": k, "IP": v}
networks.append(instance)
return networks
def action_list():
networks = get_network_list()
pretty = json.dumps(networks, indent=2)
print(pretty)
def action_mail():
networks = get_network_list()
# Set common mail parameters
mail = utils.get_client(Mail, options, logger)
fromaddr = "support@nrec.no"
bccaddr = 'iaas-logs@usit.uio.no'
attachment_payload = json.dumps(networks, indent=2)
body_content = f"Dump of used NREC IP addresses in {', '.join(regions)} attached."
msg = mail.create_mail_with_txt_attachment(
options.subject, body_content, attachment_payload, "resources.json", fromaddr
)
mail.send_mail(options.email, msg, fromaddr, bcc=bccaddr, msgid="report")
logger.debug("=> send mail to %s", options.email)
# Run local function with the same name as the action (Note: - => _)
action = locals().get("action_" + options.action.replace("-", "_"))
if action is not None:
action()
else:
utils.sys_error(f"Function action_{options.action}() not implemented")