-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-entrypoint.sh
142 lines (118 loc) · 4.39 KB
/
docker-entrypoint.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
#!/bin/bash
error='\033[37m\033[41m'
success='\033[30m\033[42m'
warning='\033[30m\033[43m'
info='\033[37m\033[44m'
blue='\033[34m'
yellow='\033[33m'
green='\033[32m'
clr='\033[0m'
verbose=0
parse_options() {
while [[ "$#" -gt 0 ]]; do
case $1 in
-v|--verbose) verbose=1 ;;
esac
shift
done
}
spinner() {
chars="◐◓◑◒"
while :; do
for ((i = 0; i < ${#chars}; i++)); do
sleep 0.5
echo -en "$1 ${chars:$i:1}" "\r"
done
done
}
execute() {
if [ $verbose -eq 1 ] ; then
"$@"
else
"$@" > /dev/null 2>&1
fi
}
set_app_name() {
APP_NAME="sword"
echo "$1" | grep -Pq '^(?!-+)[\w_-]+$'
RESULT=$?
if [ $# -gt 0 ] || [ $RESULT -eq 0 ]; then
APP_NAME=$1
fi
}
set_free_port() {
FREE_PORT_SITE=$(comm -23 <({ echo 443; seq 8080 8100; }) <(nmap --min-hostgroup 100 -p 443,8080-8100 -sS -n -T4 host.docker.internal | grep 'open' | awk '{print $1}' | cut -d'/' -f1) | head -n 1)
sudo -u appuser sed -i "s/443:443/$FREE_PORT_SITE:443/g" docker-compose.yml
URL_SITE=https://$APP_NAME.localhost
URL_MAIL=https://$APP_NAME-mail.localhost
if [ "$FREE_PORT_SITE" -ne 443 ]; then
URL_SITE=$URL_SITE:$FREE_PORT_SITE
URL_MAIL=$URL_MAIL:$FREE_PORT_SITE
fi
}
install() {
mkdir -p /app/"$APP_NAME"
if [ -z "$(ls -A /app/"$APP_NAME"/)" ]; then
cd /srv/files/ || return 1
cp -R . /app/"$APP_NAME"/
cd /app/"$APP_NAME"/ || return 1
sed -i "s@APP_NAME=sword@APP_NAME=$APP_NAME@g" .env
sed -i "/PROJECT_DIR=/d" .env
echo "PROJECT_DIR=$APP_NAME" >> .env
sed -i "s@- ./@- $HOST_PWD/$APP_NAME/@g" docker-compose.yml
fi
chown -R appuser:appgroup /app/"$APP_NAME"
cd /app/"$APP_NAME"/ || return 1
set_free_port
docker compose up -d
chown -R appuser:appgroup .
setfacl -R -m u:appuser:rwX -m u:www-data:rwX -m u:82:rwX .
setfacl -dR -m u:appuser:rwX -m u:www-data:rwX -m u:82:rwX .
}
main() {
parse_options "$@"
set_app_name "$@"
tput civis
echo ''
echo -e "$success $clr"
echo -e "$success Sword installation in progress $clr"
echo -e "$success $clr"
echo ''
echo "Creating new project in ./$APP_NAME directory."
spinner 'Please wait while preparing your project...' &
PID=$!
execute install
RESULT=$?
kill $PID
tput cnorm
if [ $RESULT -eq 0 ]; then
echo ''
echo ''
echo -e "$info $clr"
echo -e "$info What's next? $clr"
echo -e "$info $clr"
echo ''
echo -e " * ${blue}Read$clr the documentation at ${yellow}https://getsword.com$clr"
echo ''
echo -e " * ${blue}Go$clr to ${yellow}$URL_SITE$clr to access your website"
echo ''
echo -e " * ${blue}Go$clr to ${yellow}$URL_MAIL$clr to check mails sent by Symfony or WordPress"
else
if [ $verbose -eq 0 ]; then
echo ''
echo ''
echo -e "$error $clr"
echo -e "$error An error occurred during installation. What to do next? $clr"
echo -e "$error $clr"
echo ''
echo -e " * ${blue}Remove$clr the Docker environment with ${yellow}cd $APP_NAME && docker compose down$clr"
echo ''
echo -e " * ${blue}Remove$clr the project with ${yellow}rm -rf $HOST_PWD/$APP_NAME$clr"
echo ''
echo -e " * ${blue}Relaunch$clr installation with ${yellow}-v${clr} or ${yellow}--verbose${clr} at the end of the command line"
echo ''
echo -e " * Alternatively, try a manual installation. Check the documentation at ${yellow}https://getsword.com$clr"
fi
fi
}
main "$@"