-
Notifications
You must be signed in to change notification settings - Fork 3
/
builder.sh
executable file
·267 lines (216 loc) · 9.11 KB
/
builder.sh
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
#!/bin/bash
# Tool to create wazuh-install.sh, wazuh-cert-tool.sh
# and wazuh-passwords-tool.sh
# Copyright (C) 2015, Wazuh Inc.
#
# This program is a free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License (version 2) as published by the FSF - Free Software
# Foundation.
readonly base_path_builder="$(dirname "$(readlink -f "$0")")"
readonly resources_installer="${base_path_builder}/install_functions"
readonly resources_config="${base_path_builder}/config"
readonly resources_certs="${base_path_builder}/cert_tool"
readonly resources_passwords="${base_path_builder}/passwords_tool"
readonly resources_common="${base_path_builder}/common_functions"
readonly resources_download="${base_path_builder}/downloader"
source_branch="5.0.0"
function getHelp() {
echo -e ""
echo -e "NAME"
echo -e " $(basename "$0") - Builds the Wazuh installation assistant and tools."
echo -e ""
echo -e "SYNOPSIS"
echo -e " $(basename "$0") [-v] -i | -c | -p"
echo -e ""
echo -e "DESCRIPTION"
echo -e " -i, --installer"
echo -e " Builds the unattended installer single file wazuh-install.sh"
echo -e ""
echo -e " -c, --cert-tool"
echo -e " Builds the certificate creation tool wazuh-cert-tool.sh"
echo -e ""
echo -e " -p, --password-tool"
echo -e " Builds the password creation and modification tool wazuh-password-tool.sh"
echo -e ""
echo -e " -h, --help"
echo -e " Shows help."
exit 1
}
function buildInstaller() {
output_script_path="${base_path_builder}/wazuh-install.sh"
## Create installer script
echo -n > "${output_script_path}"
## License
echo "#!/bin/bash
# Wazuh installer
# Copyright (C) 2015, Wazuh Inc.
#
# This program is a free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License (version 2) as published by the FSF - Free Software
# Foundation." >> "${output_script_path}"
echo >> "${output_script_path}"
grep -Ev '^#|^\s*$' ${resources_common}/commonVariables.sh >> "${output_script_path}"
grep -Ev '^#|^\s*$' ${resources_installer}/installVariables.sh >> "${output_script_path}"
echo >> "${output_script_path}"
## Configuration files as variables
configuration_files=($(find "${resources_config}" -type f))
config_file_name=($(eval "echo "${configuration_files[@]}" | sed 's|${resources_config}||g;s|/|_|g;s|.yml||g'"))
for index in "${!config_file_name[@]}"; do
echo "config_file${config_file_name[$index]}=\"$(cat "${configuration_files[$index]}" | sed 's|\"|\\\"|g;s|\$|\\\$|g')\"" >> "${output_script_path}"
echo >> "${output_script_path}"
done
## Sigint trap
echo "trap installCommon_cleanExit SIGINT" >> "${output_script_path}"
## JAVA_HOME
echo "export JAVA_HOME=\"/usr/share/wazuh-indexer/jdk/\"" >> "${output_script_path}"
## Functions for all install function modules
install_modules=($(find "${resources_installer}" -type f))
install_modules_names=($(eval "echo \"${install_modules[*]}\" | sed 's,${resources_installer}/,,g'"))
for i in "${!install_modules[@]}"; do
if [ "${install_modules_names[$i]}" != "installVariables.sh" ]; then
echo "# ------------ ${install_modules_names[$i]} ------------ " >> "${output_script_path}"
sed -n '/^function [a-zA-Z_]\(\)/,/^}/p' ${install_modules[$i]} >> "${output_script_path}"
echo >> "${output_script_path}"
fi
done
## dist-detect.sh
checkDistDetectURL
echo "function dist_detect() {" >> "${output_script_path}"
curl -s "https://raw.githubusercontent.com/wazuh/wazuh/${source_branch}/src/init/dist-detect.sh" | sed '/^#/d' >> "${output_script_path}"
echo "}" >> "${output_script_path}"
## Common functions
sed -n '/^function [a-zA-Z_]\(\)/,/^}/p' "${resources_common}/common.sh" >> "${output_script_path}"
## Certificate tool library functions
sed -n '/^function [a-zA-Z_]\(\)/,/^}/p' "${resources_certs}/certFunctions.sh" >> "${output_script_path}"
## Passwords tool library functions
sed -n '/^function [a-zA-Z_]\(\)/,/^}/p' "${resources_passwords}/passwordsFunctions.sh" >> "${output_script_path}"
## Main function and call to it
echo >> "${output_script_path}"
echo "main \"\$@\"" >> "${output_script_path}"
}
function buildPasswordsTool() {
output_script_path="${base_path_builder}/wazuh-passwords-tool.sh"
## Create installer script
echo -n > "${output_script_path}"
## License
echo "#!/bin/bash
# Wazuh installer
# Copyright (C) 2015, Wazuh Inc.
#
# This program is a free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License (version 2) as published by the FSF - Free Software
# Foundation." >> "${output_script_path}"
## Common and Passwords tool variables
grep -Ev '^#|^\s*$' ${resources_common}/commonVariables.sh >> "${output_script_path}"
grep -Ev '^#|^\s*$' "${resources_passwords}/passwordsVariables.sh" >> "${output_script_path}"
echo >> "${output_script_path}"
## Functions for all password function modules
passwords_modules=($(find "${resources_passwords}" -type f))
passwords_modules_names=($(eval "echo "${passwords_modules[@]}" | sed 's,${resources_passwords}/,,g'"))
for i in "${!passwords_modules[@]}"; do
if [ "${passwords_modules[$i]}" != "passwordsVariables.sh" ]; then
echo "# ------------ ${passwords_modules_names[$i]} ------------ " >> "${output_script_path}"
sed -n '/^function [a-zA-Z_]\(\)/,/^}/p' "${passwords_modules[$i]}" >> "${output_script_path}"
echo >> "${output_script_path}"
fi
done
## Common functions
sed -n '/^function [a-zA-Z_]\(\)/,/^}/p' "${resources_common}/common.sh" >> "${output_script_path}"
## Call to main function
echo >> "${output_script_path}"
echo "main \"\$@\"" >> "${output_script_path}"
}
function buildCertsTool() {
output_script_path="${base_path_builder}/wazuh-certs-tool.sh"
## Create installer script
echo -n > "${output_script_path}"
## License
echo "#!/bin/bash
# Wazuh installer
# Copyright (C) 2015, Wazuh Inc.
#
# This program is a free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License (version 2) as published by the FSF - Free Software
# Foundation." >> "${output_script_path}"
## Common and Certs tool variables
grep -Ev '^#|^\s*$' ${resources_common}/commonVariables.sh >> "${output_script_path}"
grep -Ev '^#|^\s*$' "${resources_certs}/certVariables.sh" >> "${output_script_path}"
echo >> "${output_script_path}"
## Functions for all certs tool function modules
certs_modules=($(find "${resources_certs}" -type f))
certs_modules_names=($(eval "echo "${certs_modules[@]}" | sed 's,${resources_certs}/,,g'"))
for i in "${!certs_modules[@]}"; do
if [ "${certs_modules[$i]}" != "certVariables.sh" ]; then
echo "# ------------ ${certs_modules_names[$i]} ------------ " >> "${output_script_path}"
sed -n '/^function [a-zA-Z_]\(\)/,/^}/p' "${certs_modules[$i]}" >> "${output_script_path}"
echo >> "${output_script_path}"
fi
done
## Common functions
sed -n '/^function [a-zA-Z_]\(\)/,/^}/p' "${resources_common}/common.sh" >> "${output_script_path}"
## Call to main function
echo >> "${output_script_path}"
echo "main \"\$@\"" >> "${output_script_path}"
}
function builder_main() {
umask 066
if [ $# -eq 0 ]; then
getHelp
fi
while [ -n "${1}" ]
do
case "${1}" in
"-i"|"--installer")
installer=1
shift 1
;;
"-c"|"--cert-tool")
certTool=1
shift 1
;;
"-p"|"--password-tool")
passwordsTool=1
shift 1
;;
"-h"|"--help")
getHelp
;;
*)
echo "Unknow option: \"${1}\""
getHelp
esac
done
if [ -n "${installer}" ]; then
buildInstaller
chmod 500 ${output_script_path}
fi
if [ -n "${passwordsTool}" ]; then
buildPasswordsTool
chmod 500 ${output_script_path}
fi
if [ -n "${certTool}" ]; then
buildCertsTool
chmod 644 ${output_script_path}
fi
}
function checkDistDetectURL() {
urls=("https://raw.githubusercontent.com/wazuh/wazuh/${source_branch}/src/init/dist-detect.sh"
"https://raw.githubusercontent.com/wazuh/wazuh/master/src/init/dist-detect.sh")
for url in "${urls[@]}"; do
eval "curl -s -o /dev/null '${url}' --retry 5 --retry-delay 5 --max-time 300 --fail"
e_code="${PIPESTATUS[0]}"
if [ "${e_code}" -eq 0 ]; then
source_branch=$(echo "${url}" | awk -F'/' '{print $(NF-3)}')
break
fi
done
if [ "${e_code}" -ne 0 ]; then
echo -e "Error: Could not get the dist-detect file."
exit 1
fi
}
builder_main "$@"