-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeploy.sh
202 lines (170 loc) Β· 5.62 KB
/
deploy.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
#!/bin/bash
set -e
# Configuration variables
NON_INTERACTIVE=false
ACTION=""
ENV_TYPE=""
RECONFIGURE=false
COMPOSE_FOLDER="$PWD/docker"
CONFIG_DIR="$COMPOSE_FOLDER/.env"
COMPOSE_DEV="docker-compose-dev.yml"
COMPOSE_PROD="docker-compose.yml"
DOCKER_COMPOSE=$(command -v docker-compose || echo "docker compose")
# Header
echo "π Ngen Deployment Script"
cd $COMPOSE_FOLDER || { echo "β Error: Docker Compose folder not found. This script must be run from the project root."; exit 1; }
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--dev|--prod)
ENV_TYPE="${1#--}"
shift
;;
--non-interactive)
NON_INTERACTIVE=true
shift
;;
start|stop|reconfigure)
ACTION=$1
shift
;;
*)
echo "β οΈ Unknown option: $1"
exit 1
;;
esac
done
echo ""
echo "Configuration file: ${ENV_FILE:-Not set}"
echo "Docker Compose executable: $DOCKER_COMPOSE"
echo "Docker Compose folder: $COMPOSE_FOLDER"
echo "Non-interactive mode: $NON_INTERACTIVE"
echo ""
check_env_mode() {
if [ -z "$ENV_TYPE" ]; then
if [ "$NON_INTERACTIVE" = true ]; then
echo "β οΈ Running in non-interactive mode. Please specify --dev or --prod."
exit 1
fi
echo "πΉ Select the environment mode:"
echo " 1) Development"
echo " 2) Production"
read -p "Enter the number of your choice: " env_choice
case "$env_choice" in
1) ENV_TYPE="dev" ;;
2) ENV_TYPE="prod" ;;
*) echo "β Invalid choice. Exiting..."; exit 1 ;;
esac
fi
ENV_FILE="${CONFIG_DIR}/ngen.${ENV_TYPE}.env"
EXAMPLE_FILE="${CONFIG_DIR}/ngen.${ENV_TYPE}.env.example"
echo "Environment selected: $ENV_TYPE"
echo ""
}
configure_env_mode() {
echo "π Configuring Ngen ${ENV_TYPE} environment..."
if [ -f "$ENV_FILE" ]; then
echo "β οΈ Configuration file already exists: ${ENV_FILE}"
echo "Use 'bash deploy.sh reconfigure --$ENV_TYPE' to reset the configuration."
exit 1
fi
if [ ! -f "$EXAMPLE_FILE" ]; then
echo "β Error: Missing example file ${EXAMPLE_FILE}. Cannot proceed."
exit 1
fi
tmp_file="${ENV_FILE}.tmp"
> "$tmp_file"
while IFS= read -r line; do
if [[ "$line" == \#* ]] || [[ -z "$line" ]]; then
echo "$line"
echo "$line" >> "$tmp_file"
continue
fi
IFS='=' read -r key current_value <<< "$line"
key=$(echo "$key" | xargs)
current_value=$(echo "$current_value" | xargs)
if [ "$NON_INTERACTIVE" = true ]; then
new_value="$current_value"
else
read -r -p "Modify ${key}? [Current: ${current_value}] (Enter to keep): " new_value < /dev/tty
fi
echo "${key}=${new_value:-$current_value}" >> "$tmp_file"
done < "$EXAMPLE_FILE"
mv "$tmp_file" "$ENV_FILE"
echo "β
Configuration completed: ${ENV_FILE}"
echo ""
}
stop_containers() {
echo "π Stopping all Ngen environments..."
echo "πΉ Trying to stop development environment..."
$DOCKER_COMPOSE -f $COMPOSE_DEV down -v || true
echo ""
echo "πΉ Trying to stop production environment..."
$DOCKER_COMPOSE -f $COMPOSE_PROD down -v || true
echo "Done."
echo ""
}
start_containers() {
echo "βΆοΈ Starting Ngen in ${ENV_TYPE} mode..."
if [ "$ENV_TYPE" = "dev" ]; then
COMPOSE_FILE=$COMPOSE_DEV
ALT_COMPOSE_FILE=$COMPOSE_PROD
ALT_MODE="Production"
ALT_ENV_FILE="${CONFIG_DIR}/ngen.prod.env"
else
COMPOSE_FILE=$COMPOSE_PROD
ALT_COMPOSE_FILE=$COMPOSE_DEV
ALT_MODE="Development"
ALT_ENV_FILE="${CONFIG_DIR}/ngen.dev.env"
fi
echo "πΉ ${ENV_TYPE^} mode selected. Using $COMPOSE_FILE"
if [ -f "$ALT_ENV_FILE" ]; then
echo "Removing containers from ${ALT_MODE} mode to avoid conflicts..."
$DOCKER_COMPOSE -f "$ALT_COMPOSE_FILE" down -v
else
echo "No environment file found for ${ALT_MODE}. Skipping container removal."
fi
if $DOCKER_COMPOSE -f $COMPOSE_FILE ps | grep -q 'Up'; then
echo "β οΈ System is already running. Stop it before starting again."
exit 1
fi
echo "Launching Ngen in ${ENV_TYPE} mode..."
$DOCKER_COMPOSE -f $COMPOSE_FILE up -d
}
print_service_urls() {
if [ "$ENV_TYPE" = "dev" ]; then
echo ""
echo "π Ngen services are now running. By default config access with the following URLs:"
echo "πΉ Ngen Frontend: http://ipaddress:3000"
echo "πΉ Ngen API: http://ipaddress:8000"
echo ""
else
echo ""
echo "π Ngen services are now running. By default config access with the following URLs:"
echo "πΉ Ngen Frontend: https://ipaddress/"
echo "πΉ Ngen API: https://ipaddress/api"
echo ""
fi
}
# Execution flow
if [ "$ACTION" = "stop" ]; then
stop_containers
elif [ "$ACTION" = "start" ]; then
check_env_mode
if [ -f "$ENV_FILE" ]; then
echo "β
Using existing configuration file: ${ENV_FILE}"
else
echo "Configuration file not found for ${ENV_TYPE}. Creating a new one..."
configure_env_mode
fi
start_containers
print_service_urls
elif [ "$ACTION" = "reconfigure" ]; then
check_env_mode
rm -f "$ENV_FILE"
configure_env_mode
echo "β
Configuration completed: ${ENV_FILE}"
echo "Use 'bash deploy.sh start --$ENV_TYPE' to start Ngen"
echo "Use 'bash deploy.sh stop --$ENV_TYPE' to stop Ngen"
fi
exit 0