-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate.py
67 lines (56 loc) · 2.31 KB
/
generate.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
import yaml, json, argparse, os
from jupyter_server.auth.security import passwd
from pwgen import pwgen
# Function to create hosts entries
def create_hosts_info(n, config):
hosts_infos = []
# Generate host entries
for i in range(1, n+1):
# Placeholder for password and hashed password generation
pw = pwgen(10, symbols=False, capitalize=False)
hashed_pw = passwd(pw)
hosts_infos_entry = {
'subdomain': f'{i:03d}.{config["hostname"]}',
'pass': pw,
'hashed_pass': hashed_pw,
}
hosts_infos.append(hosts_infos_entry)
return hosts_infos
def main():
parser = argparse.ArgumentParser(description="Hostname generation with password and hashed passwords.")
parser.add_argument("-n","--num", type=int, required=True, help="The number of hosts to generate")
args = parser.parse_args()
with open('config.yaml', 'r') as file:
config = yaml.safe_load(file)
host_infos = create_hosts_info(args.num, config)
# Write to YAML file for ansible
with open('ansible/config.yaml', 'w') as file:
ansible_cfg = {
'main_domain': config["domain"],
'notebooks': host_infos,
'admins': config["admins"],
'acme_email': config["email"]
}
yaml.dump(ansible_cfg, file, indent=2, default_style="'", default_flow_style=False, sort_keys=False)
# Write to json file for terraform
with open('terraform/config.tfvars.json','w') as file:
tf_config = {
'domain': config["domain"],
'hcloud_token': config["hcloud_token"],
'name': config["hostname"],
'ssh_key_location': config["ssh_key_location"]
}
json.dump(tf_config, file, indent=2, sort_keys=False)
with open('ansible/hosts','w') as file:
private_key_file = os.path.splitext(config["ssh_key_location"])[0]
ansible_hosts_data = {
'servers': {
'hosts': {
f'{config["hostname"]}.{config["domain"]}': {'ansible_user': 'alpaca', 'ansible_ssh_private_key_file': private_key_file}
}
}
}
yaml.dump(ansible_hosts_data, file, indent=2, sort_keys=False, )
if __name__ == "__main__":
main()
# Call the function to create the files