forked from Axonius/axonius_api_client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
offline_axonius_api_client_creator.sh
executable file
·389 lines (303 loc) · 8.84 KB
/
offline_axonius_api_client_creator.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#!/bin/bash
# Copyright (C) Axonius
# Script Provided without warranty
# Script info
SCRIPT_FULL_PATH="$(realpath "${0}")"
SCRIPT_NAME="$(basename "${SCRIPT_FULL_PATH%.*}")"
SCRIPT_VERSION="1.0"
# Misc commands
MAKE_LOG_WORKING_DIRECTORY="mkdir -p ${LOG_FILE_DATED}"
SET_YELLOW_PROMPT="tput setaf 3"
SET_NORMAL_PROMPT="tput setaf 9"
CURRENT_DIRECTORY="$(pwd)"
BUILD_DIRECTORY=$CURRENT_DIRECTORY/${SCRIPT_NAME}_build
# Messages
BR="echo"
INSTANCE_ALREADY_RUNNING="An instance of ${SCRIPT_NAME} is already running.\n"
RUN_AS_ROOT_MESSAGE="${SCRIPT_NAME} must be run as root."
SCRIPT_COMPLETE_MESSAGE="Complete."
declare -i IS_REDHAT
IS_REDHAT=$(rpm -qf /etc/redhat-release | grep -c redhat)
# Installer script
IFS='' read -r -d '' INSTALLER <<"EOF"
#!/bin/bash
# Copyright (C) Axonius
# Script Provided without warranty
# Script info
SCRIPT_FULL_PATH="$(realpath "${0}")"
SCRIPT_NAME="$(basename "${SCRIPT_FULL_PATH%.*}")"
SCRIPT_VERSION="1.0"
# Misc commands
MAKE_LOG_WORKING_DIRECTORY="mkdir -p ${LOG_FILE_DATED}"
SET_YELLOW_PROMPT="tput setaf 3"
SET_NORMAL_PROMPT="tput setaf 9"
# Messages
BR="echo"
INSTANCE_ALREADY_RUNNING="An instance of ${SCRIPT_NAME} is already running.\n"
RUN_AS_ROOT_MESSAGE="${SCRIPT_NAME} must be run as root."
SCRIPT_COMPLETE_MESSAGE="Complete."
# Variables
PIP_CMD=
declare -i PIP_EXISTS
PIP_EXISTS=$(command -v pip3 | wc -l)
### Helper Functions
die() {
declare -i EXIT_CODE
EXIT_CODE=${1}
[ "${EXIT_CODE}" == "" ] && exit 1 || exit "${EXIT_CODE}"
}
function catch() {
${BR}
${SET_NORMAL_PROMPT}
rm -f "/var/tmp/${SCRIPT_NAME}.lock"
${DELETE_LOG_WORKING_DIRECTORY}
die 255
}
function continueExitDialog() {
PS3="Select the item number: "
${SET_YELLOW_PROMPT}
echo "${1}"
${BR}
select action in Continue Exit
do
case "${action}" in
Continue)
${BR}
break;;
Exit)
die 0;;
*)
echo "Invalid selection";;
esac
done
${SET_NORMAL_PROMPT} # Set prompt to normal
}
function help() {
declare MSG
MSG=$(cat <<-END
Usage: ${SCRIPT_NAME} [OPTION...]
Installer for the offline install of the Axonius API Client.
General options.
-h, --help\t\t\tShow this message
-v, --version\t\t\tDisplay version information
END
)
echo -e "${MSG}"
}
function version() {
declare MSG
MSG=$(cat <<-END
${SCRIPT_NAME} v${SCRIPT_VERSION}
Copyright (C) 2021 Axonius
Provided "As-is", and without warranty.
END
)
echo -e "${MSG}"
}
function incorrectOption() {
declare MSG
MSG=$(cat <<-END
${SCRIPT_NAME}: invalid option "${1}"
Try '${SCRIPT_NAME} --help' for more information.
END
)
echo -e "${MSG}"
echo
}
### Main Body
if [ ! ${#} -eq 0 ]; then
for arg in "${@}"; do
case $arg in
-h | --help) # display Help
help # Call the help function above
die 0;;
-v | --version)
version # Call the version function above
die 0;;
*) # incorrect option
incorrectOption "${arg}" # Call incorrectOption above
die 0;;
esac
done
fi
# Create lock to ensure only one instance runs at a time, limit resource concurrency issues
exec 100>/var/tmp/"${SCRIPT_NAME}".lock || die 255
if ! flock -n 100; then
${SET_YELLOW_PROMPT} # Set text yellow text
echo -e "${INSTANCE_ALREADY_RUNNING}"
${SET_NORMAL_PROMPT} # Set prompt to normal
die 255
fi
# Cleanup on exit. In order; blank line, set text normal, delete lock file, exit abnormally
trap 'catch' SIGINT SIGTERM ERR EXIT
# Ensure the script is ran as root
if [[ ${EUID} -ne 0 ]]; then
${SET_YELLOW_PROMPT}
echo "${RUN_AS_ROOT_MESSAGE}"
die 255
fi
if [ "${PIP_EXISTS}" -eq "1" ]; then
PIP_CMD=pip3
else
echo "Please install pip3 and run this script again."
die 255
fi
tar xzvf openssl.tar.gz -C /
${PIP_CMD} install --no-index --find-links wheel_files cryptography axonius-api-client
# Done, tell user where to find the output
printf "%s" "${SCRIPT_COMPLETE_MESSAGE}"
EOF
### Helper Functions
die() {
declare -i EXIT_CODE
EXIT_CODE=${1}
[ "${EXIT_CODE}" == "" ] && exit 1 || exit "${EXIT_CODE}"
}
function catch() {
${BR}
${SET_NORMAL_PROMPT}
rm -f "/var/tmp/${SCRIPT_NAME}.lock"
#rm -rf ${BUILD_DIRECTORY}
cd ${CURRENT_DIRECTORY}
}
function continueExitDialog() {
PS3="Select the item number: "
${SET_YELLOW_PROMPT}
echo "${1}"
${BR}
select action in Continue Exit
do
case "${action}" in
Continue)
${BR}
break;;
Exit)
die 0;;
*)
echo "Invalid selection";;
esac
done
${SET_NORMAL_PROMPT} # Set prompt to normal
}
function help() {
declare MSG
MSG=$(cat <<-END
Usage: ${SCRIPT_NAME} [OPTION...]
Creates install package for offline install of the Axonius API Client.
General options.
-h, --help\t\t\tShow this message
-v, --version\t\t\tDisplay version information
END
)
echo -e "${MSG}"
}
function version() {
declare MSG
MSG=$(cat <<-END
${SCRIPT_NAME} v${SCRIPT_VERSION}
Copyright (C) 2021 Axonius
Provided "As-is", and without warranty.
END
)
echo -e "${MSG}"
}
function incorrectOption() {
declare MSG
MSG=$(cat <<-END
${SCRIPT_NAME}: invalid option "${1}"
Try '${SCRIPT_NAME} --help' for more information.
END
)
echo -e "${MSG}"
echo
}
function run() {
# Declare local vars
declare IFS
declare FULL_COMMAND
declare EXE
declare ARGS
declare RESULT
IFS=' '
FULL_COMMAND="${*:1:$#}"
EXE="${1}"
ARGS="${*:2:$#-1}"
RESULT=0
# Log the command that is about to run
echo "RUNNING COMMAND: ${FULL_COMMAND}"
echo "###"
# Run the command and output both stderr and stdin to the log file
# Word-splitting ARGS on purpose, suppressing shellcheck warning on next line
# shellcheck disable=SC2086
"${EXE}" ${ARGS}
RESULT="${?}"
if [ ! "${RESULT}" -eq "0" ]; then
echo "Critical Error, ${FULL_COMMAND} failed with an exit value of ${RESULT}." | tee -a "${LOG_FILE}"
fi
echo "###"
}
### Main Body
if [ ! ${#} -eq 0 ]; then
for arg in "${@}"; do
case $arg in
-h | --help) # display Help
help # Call the help function above
die 0;;
-v | --version)
version # Call the version function above
die 0;;
*) # incorrect option
incorrectOption "${arg}" # Call incorrectOption above
die 0;;
esac
done
fi
# Create lock to ensure only one instance runs at a time, limit resource concurrency issues
exec 100>/var/tmp/"${SCRIPT_NAME}".lock || die 255
if ! flock -n 100; then
${SET_YELLOW_PROMPT} # Set text yellow text
echo -e "${INSTANCE_ALREADY_RUNNING}"
${SET_NORMAL_PROMPT} # Set prompt to normal
die 255
fi
# Cleanup on exit. In order; blank line, set text normal, delete lock file, exit abnormally
trap 'catch' SIGINT SIGTERM ERR EXIT
# Ensure the script is ran as root
if [[ ${EUID} -ne 0 ]]; then
${SET_YELLOW_PROMPT}
echo "${RUN_AS_ROOT_MESSAGE}"
die 255
fi
mkdir -p ${BUILD_DIRECTORY} && cd ${BUILD_DIRECTORY}
yum groupinstall -y "Development Tools"
if [ "${IS_REDHAT}" -eq "1" ]; then
yum -y --enablerepo=rhel-7-server-optional-rpms install python3 python3-devel libffi-devel wget curl
else
yum install -y python3 python3-devel libffi-devel wget curl
fi
wget -O rustup.sh https://sh.rustup.rs
sh rustup.sh -y
source $HOME/.cargo/env
set -e
OPENSSL_VERSION="1.1.1j"
CWD=$(pwd)
python3 -m venv py36-venv
source py36-venv/bin/activate
pip3 install --trusted-host pypi.org --trusted-host files.pythonhosted.org -U setuptools wheel
pip3 install --trusted-host pypi.org --trusted-host files.pythonhosted.org -U setuptools_rust
curl -k -O https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz
tar xvf openssl-${OPENSSL_VERSION}.tar.gz
cd openssl-${OPENSSL_VERSION}
mkdir -p /opt/openssl_${OPENSSL_VERSION}
./config no-shared no-ssl2 no-ssl3 -fPIC --prefix=/opt/openssl_${OPENSSL_VERSION}/openssl
make && make install
CFLAGS="-I/opt/openssl_${OPENSSL_VERSION}/openssl/include" LDFLAGS="-L/opt/openssl_${OPENSSL_VERSION}/openssl/lib" pip3 wheel --trusted-host pypi.org --trusted-host files.pythonhosted.org --no-binary :all: cryptography
mkdir -p offline_axonius_api_client_installer/wheel_files
mv *.whl offline_axonius_api_client_installer/wheel_files
pip3 download -d offline_axonius_api_client_installer/wheel_files axonius-api-client==4.2.2
echo "${INSTALLER}" > offline_axonius_api_client_installer/offline_axonius_api_client_installer.sh
chmod u+x offline_axonius_api_client_installer/offline_axonius_api_client_installer.sh
tar czvf offline_axonius_api_client_installer/openssl.tar.gz -P /opt/openssl_${OPENSSL_VERSION}
tar czvf ${CURRENT_DIRECTORY}/offline_axonius_api_client_installer.tar.gz offline_axonius_api_client_installer
# Done, tell user where to find the output
printf "\n%s" "${SCRIPT_COMPLETE_MESSAGE}"