-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_config.py
executable file
·118 lines (100 loc) · 3.85 KB
/
create_config.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
#!/usr/bin/env python3
# Originally written by Claudio Kopper
# Modifications by Jamie Rajewski
import os
import json
import subprocess
from string import Template
def create_ssh_config(data, outfile):
# Add universal options here first
result = "Host *\n IdentitiesOnly yes\n\n"
ssh_username = data['ssh-username']['value']
ssh_keyfile = data['ssh-key-file']['value']
bastion_host_public = data['bastion-address-public']['value']
bastion_hostname = "illume-bastion-v2"
# Substitute vars in template
filein = open("packer/bootstrap/ssh/ssh.cfg.template.per_host")
src = Template(filein.read())
del filein
d = {
'hostname':bastion_hostname,
'host_ip':bastion_host_public,
'ssh_username':ssh_username,
'ssh_keyfile':ssh_keyfile,
'proxy_jump': 'None',
}
result += src.substitute(d) + "\n"
for idx, address in enumerate(data['illume-proxy-addresses']['value']):
d = {
'hostname':"illume-proxy-{:02d}-v2".format(idx+1),
'host_ip':address,
'ssh_username':ssh_username,
'ssh_keyfile':ssh_keyfile,
'proxy_jump':bastion_hostname,
}
result += src.substitute(d) + "\n"
for idx, address in enumerate(data['illume-control-addresses']['value']):
d = {
'hostname':"illume-control-{:02d}-v2".format(idx+1),
'host_ip':address,
'ssh_username':ssh_username,
'ssh_keyfile':ssh_keyfile,
'proxy_jump':bastion_hostname,
}
result += src.substitute(d) + "\n"
for instance in data['illume-worker-addresses']['value']:
d = {
'hostname':instance,
'host_ip':data['illume-worker-addresses']['value'][instance],
'ssh_username':ssh_username,
'ssh_keyfile':ssh_keyfile,
'proxy_jump':bastion_hostname,
}
result += src.substitute(d) + "\n"
for idx, address in enumerate(data['illume-ingress-addresses']['value']):
d = {
'hostname':"illume-ingress-{:02d}-v2".format(idx+1),
'host_ip':address,
'ssh_username':ssh_username,
'ssh_keyfile':ssh_keyfile,
'proxy_jump':bastion_hostname,
}
result += src.substitute(d) + "\n"
for idx, address in enumerate(data['illume-openLDAP-addresses']['value']):
d = {
'hostname':"illume-openLDAP-{:02d}-v2".format(idx+1),
'host_ip':address,
'ssh_username':ssh_username,
'ssh_keyfile':ssh_keyfile,
'proxy_jump':bastion_hostname,
}
result += src.substitute(d) + "\n"
for idx, address in enumerate(data['illume-phpLDAPadmin-addresses']['value']):
d = {
'hostname':"illume-phpLDAPadmin-{:02d}-v2".format(idx+1),
'host_ip':address,
'ssh_username':ssh_username,
'ssh_keyfile':ssh_keyfile,
'proxy_jump':bastion_hostname,
}
result += src.substitute(d) + "\n"
for idx, address in enumerate(data['illume-monitor-addresses']['value']):
d = {
'hostname':"illume-monitor-{:02d}-v2".format(idx+1),
'host_ip':address,
'ssh_username':ssh_username,
'ssh_keyfile':ssh_keyfile,
'proxy_jump':bastion_hostname,
}
result += src.substitute(d) + "\n"
text_file = open(outfile, "w")
text_file.write(result)
text_file.close()
def main():
# get data from terraform output for injection in template
tf_output= subprocess.Popen("terraform output -state=terraform/terraform.tfstate -json", shell=True, stdout=subprocess.PIPE).stdout.read()
data = json.loads(tf_output)
# Write ssh config (for localhost)
create_ssh_config(data, outfile="ssh.cfg")
if __name__ == '__main__':
main()