forked from rhuanpk/ncd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·179 lines (161 loc) · 4.98 KB
/
setup.sh
File metadata and controls
executable file
·179 lines (161 loc) · 4.98 KB
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
#!/bin/bash
setup-domains-array(){
read -p '* Domains (space separated): ' -a DOMAINS
for domain in "${DOMAINS[@]}"; do
CONCAT+="'$domain' "
done
echo "DOMAINS=(${CONCAT% })" > "$SOURCE_DOMAINS"
}
echo '### Nginx & Certbot with Docker - Setup'
echo '>> Cloning base repository...'
git clone -q 'https://github.com/rhuanpk/ncd.git'
sed -i 's/^git clone/#&/' "$0"
cd './ncd/'
for status in `git status --porcelain | sed -n 's/^.\(.\).*$/\1/;{/^\(M\|\?\)/p}' | sort -u`; do
[[ "$status" = '?' && -d './project/certbot/' && -d './project/nginx/conf/' ]] && {
read -p '* Certs already exists, gens in different way (delete existing)? (y/N) '
[ "${REPLY,,}" = 'y' ] && {
sudo -k 2>&-
read -sp '* User password: '; echo
if ! OUTPUT="`echo -e "${REPLY}\n" | sudo -Sv 2>&1`" && [[ ! "$OUTPUT" =~ incorrect\ password ]]; then
while :; do
unset EXITS
echo -n '* [ROOT] '
if EXITS=$(
su - -c " \
cd \"`pwd`\"; \
for file in \`find ./project/certbot/ ./project/nginx/conf/ -type d -user 0\`; do \
chown -R 1000:1000 \"\$file\"; \
echo \"#@\$?@#\"; \
done \
"
); then
break
fi
[[ "`sed -n 's/^.*#@\([[:digit:]]\+\)@#.*$/\1/pg' <<< "$EXITS" | tr -d '\n'`" =~ [^0] ]] && HAS_ERROR=true
done
else
while :; do
IS_SUDO=true
unset EXITS
for file in `find ./project/certbot/ ./project/nginx/conf/ -type d -user 0`; do
sudo chown -R 1000:1000 "$file"
EXITS+="$?"
done
[[ "$EXITS" =~ [^0] ]] && HAS_ERROR=true
if ! "${HAS_ERROR:-false}"; then break; fi
done
fi
git clean -f 'project/certbot/' 'project/nginx/conf/'
if "${HAS_ERROR:-false}"; then
echo '! Some error occurred, try on your own: git clean -f ./'
exit 1
fi
}
}
[ "$status" = 'M' ] && {
read -p '* Config files are modified, reset? (Y/n) '
[ "${REPLY,,}" != 'n' ] && {
git restore --worktree ./
git clean -f 'source/'
}
}
done
SOURCE_CERT='./source/cert'
SOURCE_DOMAINS='./source/domains'
SOURCE_GROUPS='./source/groups'
SOURCE_SINGLE='./source/single'
SOURCE_STAGING='./source/staging'
NGINX_FOLDER='./project/nginx'
read -p '* Is production setup? (y/N) '
[ "${REPLY,,}" != 'y' ] && echo "IS_STAGING='true'" > "$SOURCE_STAGING"
. "$SOURCE_STAGING" 2>&-
[ -z "$IS_STAGING" ] && {
setup-domains-array
} || {
read -p '* Is local test? (Y/n) '
[ "${REPLY,,}" != 'n' ] && {
echo '>> Setting up local domains...'
echo "DOMAINS=('ncd.xyz' 'www.ncd.xyz')" > "$SOURCE_DOMAINS"
. "$SOURCE_DOMAINS"
sudo -k 2>&-
read -sp '* User password: '; echo
if ! OUTPUT="`echo -e "${REPLY}\n" | sudo -Sv 2>&1`" && [[ ! "$OUTPUT" =~ incorrect\ password ]]; then
while :; do
echo -n '* [ROOT] '
if su - -c "echo $'\n# Only tests\n127.0.0.1\tncd.xyz\n127.0.0.1\twww.ncd.xyz' >> '/etc/hosts'"; then
break
fi
done
else
while :; do
if sudo tee -a '/etc/hosts' >'/dev/null' <<< $'\n# Only tests\n127.0.0.1\tncd.xyz\n127.0.0.1\twww.ncd.xyz'; then
break
fi
done
fi
} || setup-domains-array
}
read -p '* Generate single certificate? (Y/n) '
[ "${REPLY,,}" != 'n' ] && echo "IS_SINGLE='true'" > "$SOURCE_SINGLE"
. "$SOURCE_SINGLE" 2>&-
echo '>> Setting up config files...'
if "${IS_SINGLE:-false}"; then
cat << EOF >> "$NGINX_FOLDER/post.conf"
server {
listen 443 ssl;
listen [::]:443 ssl;
#!SERVERNAMES!#
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
ssl_certificate /etc/letsencrypt/live/$DOMAINS/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/$DOMAINS/privkey.pem;
# this static or a specific proxy location
location / {
root /var/www/html;
}
}
EOF
else
for domain in "${DOMAINS[@]}"; do
[ "${PREVIOUS_DOMAIN#www.}" = "${domain#www.}" ] && {
DOMAINS_GROUPS+=("$PREVIOUS_DOMAIN $domain")
} || {
[[ -z "$PREVIOUS_DOMAIN" || "$domain" =~ ^www\. ]] && {
PREVIOUS_DOMAIN="$domain"
continue
}
DOMAINS_GROUPS+=("$domain")
}
PREVIOUS_DOMAIN="$domain"
done
unset CONCAT
rm -f "$SOURCE_CERT"
for group in "${DOMAINS_GROUPS[@]}"; do
CONCAT+="'$group' "
SHORTED="$(cut -d ' ' -f '1' <<< "$group" | sed 's/^www.//')"
echo "--cert-name '$SHORTED'" >> "$SOURCE_CERT"
cat << EOF >> "$NGINX_FOLDER/post.conf"
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name $group;
include /etc/nginx/conf.d/includes/global.conf;
include /etc/nginx/conf.d/includes/global.locations;
include /etc/nginx/conf.d/$SHORTED/ssl.conf;
}
EOF
mkdir "$NGINX_FOLDER/conf/$SHORTED/"
cat <<- EOF > "$NGINX_FOLDER/conf/$SHORTED/ssl.conf"
ssl_certificate /etc/letsencrypt/live/$SHORTED/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/$SHORTED/privkey.pem;
EOF
done
echo "DOMAINS_GROUPS=(${CONCAT% })" > "$SOURCE_GROUPS"
fi
STRING_DOMAINS="${DOMAINS[@]}"
find "$NGINX_FOLDER/" -maxdepth 1 -type f -exec sed -i "s|#!SERVERNAMES!#|server_name $STRING_DOMAINS;|" '{}' \+
cp -f "$NGINX_FOLDER/pre.conf" "$NGINX_FOLDER/conf/default.conf"
echo '>> Executing SSL script setup...'
cd './project/'
./ssl.sh