-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_server.sh
More file actions
296 lines (250 loc) · 7.13 KB
/
Copy pathinit_server.sh
File metadata and controls
296 lines (250 loc) · 7.13 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/bin/bash
GATEWAY_DIR='/docker/docker-gateway'
USER='user'
USER_DIR=/home/$USER
set -e
function printMessage {
echo ">>" $1
}
function printHeader {
echo "##############################"
echo ">>" $1
echo "##############################"
}
function detectOS()
{
if grep -Eqii "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
DISTRO='centos'
PM='yum'
elif grep -Eqi "Red Hat Enterprise Linux Server" /etc/issue || grep -Eq "Red Hat Enterprise Linux Server" /etc/*-release; then
DISTRO='redhat'
PM='yum'
elif grep -Eqi "Fedora" /etc/issue || grep -Eq "Fedora" /etc/*-release; then
DISTRO='fedora'
PM='yum'
elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
DISTRO='debian'
PM='apt'
elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
DISTRO='ubuntu'
PM='apt'
else
DISTRO='unknown'
fi
}
function isPackageExists() {
return dpkg -l "$1" &> /dev/null
}
function isPackageInstalled() {
dpkg-query -Wf'${db:Status-abbrev}' "$1" 2>/dev/null | grep -q '^i'
}
function installDocker {
if [ -x "$(command -v docker)" ]; then
echo '[✔] Docker is already installed.'
else
apt update
apt upgrade -y
#apt remove docker docker-engine docker.io
if isPackageExists docker ; then
apt remove docker;
fi
if isPackageExists docker-engine ; then
apt remove docker;
fi
if isPackageExists docker.io ; then
apt remove docker;
fi
apt install \
apt-transport-https \
ca-certificates \
curl \
gnupg2 \
software-properties-common -y
printHeader "Installing Docker"
curl -fsSL https://download.docker.com/linux/$DISTRO/gpg | apt-key add -
add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/$DISTRO \
$(lsb_release -cs) \
stable"
apt update
apt install docker-ce -y
fi
}
function installDockerCompose {
if [ -x "$(command -v docker-compose)" ]; then
echo '[✔] Docker Compose is already installed.'
else
printHeader "Installing Docker Compose"
curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
fi
}
function setupDockerGateway {
if [ -d "$GATEWAY_DIR" ]; then
echo '[✔] Docker gateway is already installed., for update try `git pull` in' $GATEWAY_DIR
else
printHeader "Configuring Docker Gateway"
# install git
apt install git -y
# clone configuration for gateway
mkdir -p $GATEWAY_DIR
git clone https://github.com/goodservers/docker-gateway.git $GATEWAY_DIR
fi
}
function runDockerGateway {
# create network which connects the containers
if ! [ `docker network ls | grep 'nginx-proxy' | wc -l` -eq 1 ]; then
docker network create nginx-proxy
fi
# run docker gateway
cd $GATEWAY_DIR; docker-compose up -d
}
function setupDockerUser {
# causes issues with Micro Name Service Caching Daemon, need to remove
if isPackageInstalled unscd ; then
apt remove --purge unscd -y
fi
if [ -d $USER_DIR ]; then
echo '[✔] User is already installed.'
else
printHeader "Configuring user to be used by Docker Gateway"
# prepare user which runs in containers (Docker security)
useradd $USER
echo "$USER:x:999:999:,,,:$USER_DIR:/bin/bash" >>/etc/passwd
echo "$USER:!:15392:0:99999:7:::" >>/etc/shadow
mkdir -p $USER_DIR/.ssh/
echo "#Here place your public ssh key" > $USER_DIR/.ssh/authorized_keys
# set users permission to his homedir
chown -Rc $USER:$USER $USER_DIR
# add rights to run docker
usermod -aG docker $USER
fi
}
function setupUserSSHKey {
# setup ssh key
echo 'Do you want to upload your public key or generate new private key?'
select yn in 'Upload public' 'Generate new private key'; do
case $yn in
'Upload public' ) nano +10 $USER_DIR/.ssh/authorized_keys; break;;
'Generate new private key' )
# https://medium.com/risan/upgrade-your-ssh-key-to-ed25519-c6e8d60d3c54
ssh-keygen -f $USER_DIR/.ssh/deploy.guide.key -o -a 100 -t ed25519 -C 'deploy@deploy.guide' -P ''
cat $USER_DIR/.ssh/deploy.guide.key.pub >> $USER_DIR/.ssh/authorized_keys
echo "Copy your private key (it will be visible just for now):"
cat $USER_DIR/.ssh/deploy.guide.key
rm $USER_DIR/.ssh/deploy.guide.key
break;;
esac
done
}
function disableSSHDPassword {
# Disable password login
sed -i '/PasswordAuthentication/s/yes/no/g' /etc/ssh/sshd_config
# Restart sshd
sudo service ssh restart
printMessage "PasswordAuthentication was disabled"
}
function guide {
printHeader "Do you want to install Docker?"
select yn in "Yes" "No"; do
case $yn in
'Yes' ) installDocker; break;;
'No' )
break;;
esac
done
printHeader "Do you want to install Docker Compose?"
select yn in "Yes" "No"; do
case $yn in
'Yes' ) installDockerCompose; break;;
'No' )
break;;
esac
done
printHeader "Do you want to setup Docker Gateway?"
select yn in "Yes" "No"; do
case $yn in
'Yes' ) setupDockerGateway; break;;
'No' )
break;;
esac
done
printHeader "Do you want to run Docker Gateway?"
select yn in "Yes" "No"; do
case $yn in
'Yes' ) runDockerGateway; break;;
'No' )
break;;
esac
done
printHeader "Do you want to setup user for Docker gateway?"
select yn in "Yes" "No"; do
case $yn in
'Yes' ) setupDockerUser; break;;
'No' )
break;;
esac
done
printHeader "Do you want to setup SSH key for user: $USER?"
select yn in "Yes" "No"; do
case $yn in
'Yes' ) setupUserSSHKey; break;;
'No' )
break;;
esac
done
}
options=$(getopt -o hgt --long color: -- "$@")
[ $? -eq 0 ] || {
echo "Incorrect options provided"
exit 1
}
eval set -- "$options"
while true; do
case "$1" in
-h)
HELP=true;
;;
-g)
GUIDE=true;
;;
-t)
TEST=true;
;;
# --color)
# shift; # The arg is next in position args
# COLOR=$1
# [[ ! $COLOR =~ BLUE|RED|GREEN ]] && {
# echo "Incorrect options provided"
# exit 1
# }
# ;;
--)
shift
break
;;
esac
shift
done
detectOS
if [ $DISTRO = "ubuntu" ] || [ $DISTRO = 'debian' ]; then
if [ $TEST ]; then
installDocker
installDockerCompose
setupDockerGateway
setupDockerUser
elif [ $GUIDE ]; then
guide
else
installDocker
installDockerCompose
setupDockerGateway
runDockerGateway
setupDockerUser
setupUserSSHKey
fi
else
printMessage "Unsupported distribution"
exit 1
fi
set +e