-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
207 lines (178 loc) · 5.92 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
203
204
205
206
207
#!/bin/bash
# Express setup of GZCTF
# for Ubuntu 2x.xx LTS
# by Cyr1s https://github.com/Cyr1s-dev
#
# Version 0.1 (2024-12-19)
#
# Usage: just run deploy.sh :)
#
# get user name
BASE_USER="$(who am i | awk '{print $1}')"
if [ -z "$BASE_USER" ]; then
BASE_USER="root"
fi
# check for root
IAM=$(whoami)
if [ ${IAM} != "root" ]; then
echo "You must be root to use this script"
exit 1
fi
# check for updates
echo "Updating system packages..."
apt-get update
if [ $? -ne 0 ]; then
echo "System update failed"
exit 1
fi
apt-get upgrade -y
if [ $? -ne 0 ]; then
echo "System upgrade failed"
exit 1
fi
# Check network connectivity
echo "Checking network connectivity..."
ping -c 4 8.8.8.8
if [ $? -ne 0 ]; then
echo "Network is not reachable."
exit 1
fi
echo "Checking Docker website connectivity..."
curl -I https://www.docker.com
if [ $? -ne 0 ]; then
echo "Cannot connect to Docker website."
exit 1
fi
# Install Docker
echo "Installing Docker..."
apt install docker.io docker-compose -y
if [ $? -ne 0 ]; then
echo "Docker installation failed"
exit 1
fi
echo "Docker installed successfully!"
docker --version
docker-compose --version
# Set GZCTF installation directory
echo -n "Please enter installation directory (default /home/$BASE_USER/GZCTF): "
read install_dir
if [ -z "$install_dir" ]; then
install_dir="/home/$BASE_USER/GZCTF"
fi
# Create installation directory
echo "Creating directory: $install_dir"
mkdir -p "$install_dir"
if [ $? -ne 0 ]; then
echo "Directory creation failed"
exit 1
fi
original_dir="$(pwd)"
# Copy appsettings.json and docker-compose.yml to the installation directory
cp "$original_dir/appsettings.json" "$install_dir"
cp "$original_dir/docker-compose.yml" "$install_dir"
cd "$install_dir"
echo "Switched to directory: $(pwd)"
# Get user input for PostgreSQL password, GZCTF public entry, and admin password
echo -n "Please enter PostgreSQL password (default: Admin123): "
read -s postgres_password
echo
if [ -z "$postgres_password" ]; then
postgres_password="Admin123"
fi
# Export PostgreSQL password to avoid manual input
export PGPASSWORD="$postgres_password"
# Automatically detect server IP
public_entry=$(hostname -I | awk '{print $1}')
if [ -z "$public_entry" ]; then
echo "Failed to detect server IP. Please enter manually."
while true; do
echo -n "Please enter GZCTF server ip: "
read public_entry
# Regex match for IPv4 address
if [[ "$public_entry" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
# Check if each segment is between 0 and 255
IFS='.' read -r -a octets <<< "$public_entry"
valid=true
for octet in "${octets[@]}"; do
if (( octet < 0 || octet > 255 )); then
valid=false
break
fi
done
if $valid; then
break
else
echo "Invalid IP address. Each octet must be between 0 and 255. Please try again."
fi
else
echo "Invalid IP address format. Please enter in the format X.X.X.X where X is between 0 and 255."
fi
done
else
echo "Detected server IP: $public_entry"
fi
while true; do
echo -n "Please enter GZCTF admin password (must contain uppercase, lowercase letters, and numbers): "
read -s gzctf_admin_password
echo
if [[ ${#gzctf_admin_password} -ge 8 && "$gzctf_admin_password" =~ [A-Z] && "$gzctf_admin_password" =~ [a-z] && "$gzctf_admin_password" =~ [0-9] ]]; then
break
else
echo "Password does not meet the requirements. Please try again."
fi
done
# Update appsettings.json with user input
sed -i "s/\"Password\": \"Admin123\"/\"Password\": \"$postgres_password\"/" "$install_dir/appsettings.json"
sed -i "s/\"PublicEntry\": \"127.0.0.1\"/\"PublicEntry\": \"$public_entry\"/" "$install_dir/appsettings.json"
# Update docker-compose.yml with user input
sed -i "s/POSTGRES_PASSWORD=Admin123/POSTGRES_PASSWORD=$postgres_password/" "$install_dir/docker-compose.yml"
sed -i "s/GZCTF_ADMIN_PASSWORD=Admin123/GZCTF_ADMIN_PASSWORD=$gzctf_admin_password/" "$install_dir/docker-compose.yml"
echo "Configuration updated successfully!"
# Switch to installation directory and execute docker-compose
cd "$install_dir"
sudo docker-compose up -d
# Get PostgreSQL container IP
db_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' gzctf_db_1)
if [ -z "$db_ip" ]; then
echo "Failed to retrieve PostgreSQL container IP"
exit 1
fi
echo "PostgreSQL container IP: $db_ip"
# Install PostgreSQL client
echo "Installing PostgreSQL client..."
sudo apt install postgresql-client -y
if [ $? -ne 0 ]; then
echo "PostgreSQL client installation failed"
exit 1
fi
# Add multiple log checks before waiting
echo "Checking PostgreSQL container logs for startup confirmation..."
docker logs gzctf_db_1 --tail 20
# Wait for PostgreSQL database to be ready
echo "Waiting for PostgreSQL database to be ready..."
until psql -h "$db_ip" -p 5432 -U postgres -d gzctf -c "\q" > /dev/null 2>&1; do
echo "PostgreSQL is not ready yet. Waiting..."
sleep 5
# Add a log check
docker logs gzctf_db_1 --tail 5
done
echo "PostgreSQL database is ready."
# Connect to PostgreSQL database
echo "Connecting to PostgreSQL database..."
psql -h "$db_ip" -p 5432 -U postgres -d gzctf -c "\q"
if [ $? -ne 0 ]; then
echo "Failed to connect to PostgreSQL database"
exit 1
fi
echo "Successfully connected to PostgreSQL database."
# Update AspNetUsers role
echo "Updating AspNetUsers role in PostgreSQL database..."
psql -h "$db_ip" -p 5432 -U postgres -d gzctf -c "UPDATE \"AspNetUsers\" SET \"Role\"=3 WHERE \"UserName\"='admin';"
if [ $? -ne 0 ]; then
echo "Failed to update AspNetUsers role in PostgreSQL database"
exit 1
fi
echo "AspNetUsers role updated successfully."
# information
echo "PostgreSQL password: $postgres_password"
echo "GZCTF admin password: $gzctf_admin_password"