-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate_workshop_spreadsheet.py
executable file
·146 lines (127 loc) · 3.85 KB
/
generate_workshop_spreadsheet.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
"""
Author: Brandon Blinderman
Email: [email protected]
Date: 2023-03-20
This script generates a CSV file of attendees for the Splunk Observability Workshop.
Usage:
./generate_workshop_spreadsheet.py -m <path/to/members.txt> -ip <path/to/ec2_ips.json> -r <realm> -p <ec2_password>
"""
import argparse
import csv
import json
import os
import sys
from time import sleep
from typing import List
argparser = argparse.ArgumentParser()
argparser.add_argument(
"-m",
"--members",
help="Text file containing email addresses, one per line. Default: 'members.txt'",
default="members.txt",
)
argparser.add_argument(
"-ip", "--ips", help="Text file containing EC2 IP addresses, one per line"
)
argparser.add_argument(
"-r",
"--realm",
help="Splunk/SignalFX realm. Default: us1",
required=False,
default="us1",
)
argparser.add_argument(
"-p",
"--password",
help="The EC2 instance password, randomly generated by Terraform.",
required=True,
)
argparser.description = (
"Generate a CSV file of attendees for the Splunk Observability Workshop"
)
args = argparser.parse_args()
email_list = args.members
ec2_ips = args.ips
sfx_realm = args.realm
ec2_password = args.password
OUTPUT_FILENAME = "Workshop_Attendees.csv"
def sort_emails(email_list: str) -> List[str]:
"""
import 'members.txt', put into a list, and sort alphabetically.
Remove empty lines and commas and trailing spaces.
"""
with open(email_list, encoding="utf-8") as f:
emails = f.read().splitlines()
emails = [email.replace(",", "").strip() for email in emails if email]
emails.sort()
return emails
def extract_names(emails: List[str]) -> List[str]:
"""extract names/usernames from emails"""
return [email.split("@")[0] for email in emails]
def load_ips(ec2_ips: str) -> List[str]:
"""import json file containing EC2 IPs"""
ips = []
with open(ec2_ips, encoding="utf-8") as f:
ips = json.load(f)
ips.sort()
return ips
def write_csv(
users: List[str],
emails: List[str],
ips: List[str],
sfx_realm: str,
ec2_password: str,
) -> None:
"""writes CSV file using 'members.txt' and 'ec2_ips.json'"""
with open(
os.path.join(os.getcwd(), OUTPUT_FILENAME),
"w",
encoding="utf-8",
newline="",
) as f:
writer = csv.writer(f)
writer.writerow(
[
"User",
"Email",
"IP address (EC2)",
"SSH Info",
"Password",
"Browser Access",
"Splunk Observability URL",
]
)
num_users = len(users)
num_ips = len(ips)
for i in range(max(num_users, num_ips)):
user = users[i] if i < num_users else ""
email = emails[i] if i < num_users else ""
ip = ips[i] if i < num_ips else ""
ssh_info = f"ssh ubuntu@{ip}" if ip else ""
ec2_password = ec2_password if ip else ""
browser_access = f"http://{ip}:6501" if ip else ""
sfx_url = f"https://app.{sfx_realm}.signalfx.com" if ip else ""
writer.writerow(
[
user,
email,
ip,
ssh_info,
ec2_password,
browser_access,
sfx_url,
]
)
if __name__ == "__main__":
if not args.ips:
argparser.print_help()
sys.exit(1)
else:
emails = sort_emails(email_list)
users = extract_names(emails)
ips = load_ips(ec2_ips)
print(f"Generating CSV file for {len(emails)} attendees...")
sleep(1)
write_csv(users, emails, ips, sfx_realm, ec2_password)
print(f"CSV file saved to: {sys.path[0]}/{OUTPUT_FILENAME}")