-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.sh
executable file
·157 lines (137 loc) · 2.86 KB
/
run.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
#!/bin/bash
# run.sh v0.2.1
# 2024-10-19
APP_NAME='flightbox'
RED='\033[0;31m'
YELLOW='\033[0;33m'
GREEN='\033[0;32m'
RESET='\033[0m'
PRO=production
DEV=dev
error_close() {
echo -e "\n${RED}ERROR - EXITED: ${YELLOW}$1${RESET}\n"
exit 1
}
# $1 string - question to ask
# Ask a yes no question, only accepts `y` or `n` as a valid answer, returns 0 for yes, 1 for no
ask_yn() {
while true; do
printf "\n%b%s? [y/N]:%b " "${GREEN}" "$1" "${RESET}"
read -r answer
if [[ "$answer" == "y" ]]; then
return 0
elif [[ "$answer" == "n" ]]; then
return 1
else
echo -e "${RED}\nPlease enter 'y' or 'n'${RESET}"
fi
done
}
if ! [ -x "$(command -v dialog)" ]; then
error_close "dialog is not installed"
fi
# $1 any variable name
# $2 variable name
check_variable() {
if [ -z "$1" ]; then
error_close "Missing variable $2"
fi
}
check_variable "$APP_NAME" "\$APP_NAME"
# Get the directory of the script
APP_DIR=$(dirname "$(readlink -f "$0")")
DOCKER_DIR="${APP_DIR}/docker"
dev_up() {
cd "${DOCKER_DIR}" || error_close "${DOCKER_DIR} doesn't exist"
echo "starting containers: ${API}"
docker compose -f dev.docker-compose.yml up --force-recreate --build -d "${APP_NAME}"
}
dev_down() {
cd "${DOCKER_DIR}" || error_close "${DOCKER_DIR} doesn't exist"
docker compose -f dev.docker-compose.yml down
}
production_up() {
cd "${DOCKER_DIR}" || error_close "${DOCKER_DIR} doesn't exist"
docker compose -f docker-compose.yml up -d
}
production_rebuild() {
cd "${DOCKER_DIR}" || error_close "${DOCKER_DIR} doesn't exist"
docker compose -f docker-compose.yml up -d --build
}
production_down() {
cd "${DOCKER_DIR}" || error_close "${DOCKER_DIR} doesn't exist"
docker compose -f docker-compose.yml down
}
git_pull_branch() {
git checkout -- .
git checkout main
git pull origin main
git fetch --tags
latest_tag=$(git tag | sort -V | tail -n 1)
git checkout -b "$latest_tag"
}
pull_branch() {
GIT_CLEAN=$(git status --porcelain)
if [ -n "$GIT_CLEAN" ]; then
echo -e "\n${RED}GIT NOT CLEAN${RESET}\n"
printf "%s\n" "${GIT_CLEAN}"
fi
if [[ -n "$GIT_CLEAN" ]]; then
if ! ask_yn "Happy to clear git state"; then
exit
fi
fi
git_pull_branch
main
}
main() {
echo "in main"
cmd=(dialog --backtitle "Start ${MONO_NAME} containers" --radiolist "choose environment" 14 80 16)
options=(
1 "${DEV} up" off
2 "${DEV} down" off
3 "${PRO} up" off
4 "${PRO} down" off
5 "${PRO} rebuild" off
6 "pull & branch" off
)
choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
exitStatus=$?
clear
if [ $exitStatus -ne 0 ]; then
exit
fi
for choice in $choices; do
case $choice in
0)
exit
;;
1)
dev_up
break
;;
2)
dev_down
break
;;
3)
echo "production up: ${APP_NAME}"
production_up
break
;;
4)
production_down
break
;;
5)
production_rebuild
break
;;
6)
pull_branch
break
;;
esac
done
}
main