This repository has been archived by the owner on Mar 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgenerate-release-PDF.sh
executable file
·267 lines (225 loc) · 7.36 KB
/
generate-release-PDF.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
# This script is to automate the Docker instance for M.A.R.S. on Linux(debian-based) & Max OS/X
# GLOBAL VARS
MARS_LINK='https://github.com/chaoss/MARS'
CUR_DIR='automation'
DOCKERHUB_IMG_NAME='ritikmalik/mars-image:latest'
DOCKER_IMG_NAME='chaoss-mars'
DOCKER_CONTAINER_NAME='mars-container'
OS_type=''
# Define colors
RED='\033[0;91m'
GREEN='\033[0;92m'
BLUE='\033[0;94m'
CYAN='\033[0;96m'
YELLOW='\033[0;93m'
NC='\033[0m' # No Color
### function definations begins
greetings() {
echo -e "${RED}"
echo -e ' ___---___ '
echo -e ' .-- --. '
echo -e ' ./ () * .-. \. '
echo -e ' / o . ( ) \ '
echo -e ' / . "-" \ '
echo -e ' | () . ° o . * | '
echo -e ' | ╔╦╗ ╔═╗ ╦═╗ ╔═╗ | '
echo -e ' | o ║║║ ╠═╣ ╠╦╝ ╚═╗ .| '
echo -e ' | ╩ ╩ ╩ ╩ ╩╚═ ╚═╝ | '
echo -e ' | . .--. O ° | '
echo -e ' \ ° | | o . / '
echo -e ' \ `.__.` . / '
echo -e ' `\ o () /` '
echo -e ' `--___ ___--` '
echo -e ' --- '
echo -e ' Metrics Automated Release System'
echo -e "${YELLOW}======================================"
echo -e "${CYAN}"
echo -e "Latest Release: 17th Aug, 2021"
echo -e "Current Version: v1.0"
echo -e "GitHub: https://github.com/chaoss/MARS\n"
echo -e "${YELLOW}======================================"
echo -e
}
# set -x
check_platform() {
# OS detections step
platform="$(uname -s)"
case "${platform}" in
Linux*) machine='Linux' ;;
Darwin*) machine='Mac OS/X' ;;
CYGWIN*) machine='Cygwin' ;;
MINGW*) machine='MinGw' ;;
*) machine="UNKNOWN:${platform}" ;;
esac
if [ "${machine}" = "Linux" ]; then
echo -e "${BLUE}[Check 0/5]:${CYAN} Checking user platform...${GREEN}${machine}"
elif [ "${machine}" = "Mac OS/X" ]; then
echo -e "${BLUE}[Check 0/4]:${CYAN} Checking user platform...${GREEN}${machine}"
else
echo -e "${RED}[Error]: ${machine} is not yet supported on M.A.R.S."
exit 1
fi
# return the machine type as param - OS_type
eval $1='${machine}'
}
check_docker_installed() {
# check if Docker is installed
if [[ $(which docker) && $(docker --version) ]]; then
echo -e "${BLUE}[Check ${1}/${2}]:${CYAN} Checking if Docker is installed...${GREEN}Yes"
else
echo -e "${RED}[Error]: Docker not found, please install Docker."
echo
echo -e "${CYAN}Refer to official doc: https://docs.docker.com/engine/install/${NC}"
echo
exit 1
fi
}
check_user_docker_grp() {
# check if user is in Docker group
if [ $(grep /etc/group -e "docker" | awk -F ':' '{ print $4 }') = $USER ]; then
echo -e "${BLUE}[Check ${1}/${2}]:${CYAN} Checking if ${USER} is in Docker group...${GREEN}Yes"
else
echo -e "${RED}[Error]: Please add your user to Docker group${NC}"
echo
echo -e "Run the following commands:\n"
echo -e "$ sudo usermod -a -G docker $USER"
echo -e "$ reboot"
echo
exit 1
fi
}
check_docker_running() {
# check if docker is running
if docker info >/dev/null 2>&1; then
echo -e "${BLUE}[Check ${1}/${2}]:${CYAN} Checking if Docker is running...${GREEN}Yes"
else
echo -e "${RED}[Error]: Docker does not seem to be running, run it first and retry${NC}"
echo
exit 1
fi
}
check_cur_dir() {
# check if current directory == ${CUR_DIR}
if [ "$(pwd | awk -F '/' '{ print $NF }')" = "${CUR_DIR}" ]; then
echo -e "${BLUE}[Check ${1}/${2}]:${CYAN} Checking for correct directory...${GREEN}Yes"
else
echo -e "${RED}[Error]: Please run this script in '${CUR_DIR}' directory${NC}"
echo
exit 1
fi
}
check_dockerfile() {
# check if Dockerfile exist for user platform in passive_user_input
if [ "${3}" = "Linux" ]; then
FILE='passive_user_input/Dockerfile_Linux'
if [[ -f "$FILE" ]]; then
echo -e "${BLUE}[Check ${1}/${2}]:${CYAN} Checking for Dockerfile...${GREEN}Yes"
cp ${FILE} Dockerfile
else
echo -e "${RED}[Error]: $FILE not found in 'passive_user_input' for ${3}${NC}"
echo
exit 1
fi
else
FILE='passive_user_input/Dockerfile_Mac'
if [[ -f "$FILE" ]]; then
echo -e "${BLUE}[Check ${1}/${2}]:${CYAN} Checking for Dockerfile...${GREEN}Yes"
cp ${FILE} Dockerfile
else
echo -e "${RED}[Error]: $FILE not found in 'passive_user_input' for ${3}${NC}"
echo
exit 1
fi
fi
echo -e "\n${CYAN}Selecting Dockerfile for ${GREEN}${3}${CYAN} platform${NC}"
}
check_exit() {
# check exit code of command, exit if non 0
echo
if [ $1 -eq 0 ]; then
echo -e "${GREEN}[+] Command executed with exit code $1"
else
echo -e "${RED}[Error]: Process stopped with exit code $1${NC}"
echo
exit 1
fi
}
### function definations ends
# main()
greetings
check_platform "OS_type"
# The number of checks are dynamic to OS type,
# 1 extra check for linux - "user should be in Docker grp"
# Pass the current check number and total checks as params
if [ "${OS_type}" = "Linux" ]; then
check_docker_installed "1" "5"
check_user_docker_grp "2" "5"
check_docker_running "3" "5"
check_cur_dir "4" "5"
check_dockerfile "5" "5" "Linux"
else
check_docker_installed "1" "4"
check_docker_running "2" "4"
check_cur_dir "3" "4"
check_dockerfile "4" "4" "Mac OS/X"
fi
echo
echo -e "${GREEN}Passed all checks successfully..."
echo
# Checks over, start main stuff
# build the docker image -> ${DOCKER_IMG_NAME}
echo -e "${CYAN}Building the '${DOCKER_IMG_NAME}' image"
echo -e "--------------------------------${NC}"
echo
# build Docker image according to user platform
if [ "${OS_type}" = "Linux" ]; then
docker build -t ${DOCKER_IMG_NAME} \
--build-arg USER_ID=$(id -u) --build-arg GROUP_ID=$(id -g) .
check_exit $?
else
docker build -t ${DOCKER_IMG_NAME} .
check_exit $?
fi
# remove dangling images (if any)
echo
echo -e "${CYAN}Removing dangling images (if any)"
echo -e "---------------------------------${NC}"
echo
docker rmi $(docker images | grep none | awk '{ print $3 }') 2>/dev/null
echo
echo -e "${GREEN}Done"
echo
# remove mars-container if already exist
echo -e "${CYAN}Removing ${DOCKER_CONTAINER_NAME} (if already exist)...${NC}"
docker rm -f ${DOCKER_CONTAINER_NAME} &>/dev/null && echo 'Removed old container'
echo
echo -e "${GREEN}Done"
echo
# run docker image with bind mount
echo
echo -e "${CYAN}Running the ${DOCKER_CONTAINER_NAME}"
echo -e "--------------------------${NC}"
docker run --name ${DOCKER_CONTAINER_NAME} -it \
--mount type=bind,source=$(pwd),target=/MARS ${DOCKER_IMG_NAME}
# remove container
echo
echo -e "${CYAN}Removing the ${DOCKER_CONTAINER_NAME}"
echo -e "---------------------------${NC}"
echo
docker rm ${DOCKER_CONTAINER_NAME}
check_exit $?
# remove Dockerfile
echo
echo -e "${NC}Removing redundant Dockerfile"
rm Dockerfile
# echo -e "\n${CYAN}Changing permissions from root user to ${YELLOW}${SUDO_USER}"
# chown -R ${SUDO_USER}:${SUDO_USER} .
# Display output
echo
echo -e "${NC}Unless you plan to rerun the script in future, you can safetly delete the image:${CYAN}"
echo
echo -e "$ docker rmi ${DOCKER_IMG_NAME}${NC}"
echo
# echo -e "${GREEN}Logs are saved in ${YELLOW}logs.txt"
# echo -e "${GREEN}Ouput PDF is saved in ${YELLOW}output/${GREEN} directory with format: ${YELLOW}'Output-YYYY-MM-DD.pdf'\n"