This repository was archived by the owner on Jan 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathinstall.py
More file actions
executable file
·171 lines (131 loc) · 3.89 KB
/
Copy pathinstall.py
File metadata and controls
executable file
·171 lines (131 loc) · 3.89 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#! /usr/bin/env python3
import locale
import sys
import os
import yaml
import yamlordereddictloader
from dialog import Dialog
import urllib.request
from subprocess import Popen, PIPE
locale.setlocale(locale.LC_ALL, '')
config_file = './mikado.conf'
d = Dialog(dialog="dialog", autowidgetsize=True)
d.set_background_title("Mikado")
with open("./scripts/install.yaml", 'r') as stream:
try:
yamlconfig = yaml.load(stream, Loader=yamlordereddictloader.Loader)
except yaml.YAMLError as exc:
print(exc)
dialogs = yamlconfig.get('dialogs')
def d_optional():
"""
Component selector
"""
return d.checklist(
dialogs.get('optional').get('msg'),
choices=[(a, "", False) for a in dialogs.get('optional').get('components')],
title=dialogs.get('optional').get('title')
)
def d_inputs(fields):
"""
Input dialog generator
"""
output = dict()
for field in fields:
output[field] = dict()
for fd in dialogs.get(field):
def_value = dialogs.get(field).get(fd).get('default', '')
# set default CIDR helper
if def_value == '@@@cidr@@@':
def_value = "{}/32".format(
urllib.request.urlopen(
'http://ifconfig.io/ip'
).read().decode('ascii').strip()
)
c, output[field][fd] = d.inputbox(
dialogs.get(field).get(fd).get('msg'),
init=def_value
)
if c != d.OK:
return False
return output
def d_error(msg):
"""
Error dialog
"""
d.msgbox(dialogs.get('errors').get(msg))
sys.exit(1)
def d_info(dialog):
"""
Info dialog
"""
d.msgbox(dialogs.get(dialog))
def write_config(config):
"""
config file handler
"""
output = ""
c = [c for c in config.values()]
for service in c:
for k, v in service.items():
output += "export {}=\"{}\"\n".format(k, v)
with open(config_file, 'w') as out:
out.write(output + '\n')
def execute(cmd):
"""
shell executor
"""
os.environ["CONTINUE"] = "y"
cmd = cmd.split(' ')
with Popen(cmd, stdout=PIPE, bufsize=1, universal_newlines=True) as p:
for line in p.stdout:
print(line, end='')
def create_tf(domain, fastly=None, statuscake=None):
"""
Create the initial terraform config for the domain
"""
if fastly is None and statuscake is None:
file = './examples/basic.tf'
if fastly is None and statuscake:
file = './examples/basic-with-statuscake.tf'
if fastly and statuscake is None:
file = './examples/basic-with-fastly.tf'
else:
file = './examples/basic-with-fastly-statuscake.tf'
output = ""
with open(file, 'r') as stream:
data = stream.read()
cnf = data.replace('###DOMAIN###', domain)
with open('terraform/{}.tf'.format(domain), 'w') as out:
out.write(cnf + '\n')
def install():
# Display welcome screen
d_info('welcome')
c, optional_fileds = d_optional()
# get required data and validate it
base_data = d_inputs(['Mikado'])
if len([a for a in base_data.get('Mikado').values() if not a]):
return d_error('missing_data')
pass
optional_data = d_inputs(optional_fileds)
config = base_data.copy()
config.update(optional_data)
write_config(config)
d_info('tfbase')
# build base infra
execute('make apply')
d_info('amibuild')
execute('make build-ami')
execute('make deploy-ami')
d_info('tfmikado')
create_tf(
domain=config.get('Mikado').get('domain'),
fastly=len(list(filter(None, config.get('Datadog', {}).values()))),
statuscake=len(list(filter(None, config.get('Datadog', {}).values())))
)
execute('make apply')
d_info('success')
def main():
install()
if __name__ == '__main__':
main()