-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
273 lines (218 loc) · 8.06 KB
/
setup.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import argparse
from glob import glob
from subprocess import call
def read_folder(folder):
files = glob(folder + '/*.md')
for i in range(len(files)):
files[i] = files[i][(len(folder) + 1):-3]
return files
def read_file(path):
file = open(path, "r")
content = []
# save file into array
for line in file:
content.append(line)
return content
def write_file(path, content, test_mode=True):
# \033[1;32m = [0 none 1 bold; 0 none 31 red 32 green 33 yellow
print('\033[1;32mConfiguring:\033[1;33m', path, '\033[0;0m')
# do not actually save while in test mode
if test_mode:
return False
file = open(path, "w")
# write content into file
for line in content:
file.write(line)
file.close()
def execute_block(title, content, test_mode=True):
# \033[1;32m = [0 none 1 bold; 0 none 31 red 32 green 33 yellow
print('\033[1;32mConfiguring:\033[1;31m', title, '\033[0;0m')
for line in content:
# do not execute commands while in test mode
if test_mode:
print(line, end='')
else:
call(line, shell=True)
# print blank line to separate output of commands
print()
def find_char(line, chars=' \t'):
flag = False
for pos in range(len(line)):
for char in chars:
if flag and line[pos] == char:
return pos
else:
flag = True
return -1
def find_line(content, keyword):
keyword = keyword.strip()
# exit if the keyword is too short
if (len(keyword) < 2):
return -2
for line_num in range(len(content)):
line = content[line_num].strip()
if keyword[0] == '#' and keyword[1] != ' ':
if line[1:] == keyword[1:]:
return line_num
elif line[0:] == keyword[1:]:
return line_num
elif line[0:(len(keyword) + 1)] == ('#' + keyword):
return line_num
elif line[0:(len(keyword) + 2)] == ('//' + keyword):
return line_num
elif line[0:len(keyword)] == keyword:
return line_num
return -1
def modify_block(path, content, exact = False, test_mode = True):
# read the target file and modify in memory
target_content = read_file(path)
# create print buffer
print_buffer = '';
for line in content:
if exact:
pos = len(line) - 1
else:
pos = find_char(line)
# do not allow single character keywords
if pos > 1:
line_num = find_line(target_content, line[0:pos])
# if the line is not found, add it to bottom of file
if line_num == -1:
print_buffer += '\033[0;33m + ' + line[:-1] + '\033[0;0m\n'
target_content.append(line)
continue
# skip operation if the lines are the same
if target_content[line_num] == line:
continue
# print and highlight changes
print_buffer += '\033[0;0m - ' + target_content[line_num][:-1] + '\033[0;0m\n'
print_buffer += '\033[0;33m + ' + line[:-1] + '\033[0;0m\n'
# modify the line in the file
target_content[line_num] = line
write_file(path, target_content, test_mode)
# output the print buffer
print(print_buffer)
def process(content, test_mode = False):
# define variables
execute_flag = False
modify_flag = False
i = 0
for line in content:
if execute_flag == False and modify_flag == False and (line == '```\n' or line == '```yaml\n'):
modify_flag = i
elif line == '```bash\n':
execute_flag = i
elif line == '```\n':
if execute_flag != False:
if content[execute_flag - 1][0:1] == '!':
i += 1
continue
# use line above start as title, remove newline
# remove start indicator, only pass commands
execute_block(
content[execute_flag - 1][:-1],
content[(execute_flag + 1):i],
test_mode
)
elif content[modify_flag - 1][0:1] == '*':
# immediatly write the file
write_file(
content[modify_flag - 1][1:-2],
content[(modify_flag + 1):i],
test_mode
)
elif content[modify_flag - 1][0:1] == '!':
i += 1
continue
else:
# use exact pattern matching if * indicated
if (content[modify_flag - 1][-2] == '*'):
# use line above as path, remove newline
modify_block(
content[modify_flag - 1][:-2],
content[(modify_flag + 1):i],
True,
test_mode
)
else:
# use line above as path, remove newline
modify_block(
content[modify_flag - 1][:-1],
content[(modify_flag + 1):i],
False,
test_mode
)
# reset flag variables
execute_flag = False
modify_flag = False
i += 1
if __name__ == '__main__':
# create a parser for getting the distro, service(s), and host
parser = argparse.ArgumentParser(
description='Deploy a service based on configurations specified in ' +
'markdown format.'
)
parser.add_argument(
'--distro',
'-d',
default='ubuntu',
help='Run the basic distro configuration first',
choices=read_folder('distro')
)
parser.add_argument(
'--service',
'-s',
help='Specify a list of services to install and configure',
choices=read_folder('service'),
nargs='*'
)
parser.add_argument(
'--host',
'-H',
help='Custom settings for the host specified',
choices=read_folder('host')
)
parser.add_argument(
'--test',
'-t',
help='Simulate commands and file changes',
default='true',
choices=['true', 'false']
)
# run configurations based on the selections
args = parser.parse_args()
# set test mode or not
if args.test == 'false':
test_mode = False
else:
test_mode = True
if type(args.distro) == str:
# \033[1;32m = [0 none 1 bold; 0 none 31 red 32 green 33 yellow
print('\033[1;31mDistro:\033[1;31m', args.distro, '\033[0;0m')
print('\033[1;31m==================================\033[0;0m')
process(read_file('distro/' + args.distro + '.md'), test_mode)
print()
if type(args.service) == list:
for path in args.service:
# \033[1;32m = [0 none 1 bold; 0 none 31 red 32 green 33 yellow
print('\033[1;31mService:\033[1;31m', path, '\033[0;0m')
print('\033[1;31m===========================\033[0;0m')
process(read_file('service/' + path + '.md'), test_mode)
print()
if type(args.host) == str:
# \033[1;32m = [0 none 1 bold; 0 none 31 red 32 green 33 yellow
print('\033[1;31mHost:\033[1;31m', args.host, '\033[0;0m')
print('\033[1;31m==============================\033[0;0m')
process(read_file('host/' + args.host + '.md'), test_mode)
print()
if type(args.service) == list:
valid_services = read_folder('service')
for path in args.service:
path += '-defer'
for service in valid_services:
if path == service:
# \033[1;32m = [0 none 1 bold; 0 none 31 red 32 green 33 yellow
print('\033[1;31mService:\033[1;31m', path, '\033[0;0m')
print('\033[1;31m===========================\033[0;0m')
process(read_file('service/' + path + '.md'), test_mode)
print()