-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinstall.sh
executable file
·310 lines (262 loc) · 7.39 KB
/
install.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/bin/bash
# ------------------------------------------------------------------------------
# install.sh
# Setup remote ubuntu/debian host from scratch
# This script is a part of DCAPE project
# See https://github.com/dopos/dcape
# ------------------------------------------------------------------------------
# default user login
admin=op
# ------------------------------------------------------------------------------
help() {
cat <<EOF
install.sh - Remote Server Setup Script
Usage:
./install.sh HOST [OPTIONS]
Where HOST is remote server hostname or ip
and OPTIONS are:
-a USER, --admin USER - create & setup user USER for remote login (default: $admin, set '-' to disable)
-c ARGS, --cape ARGS - install docker cape with init args ARGS
-d, --docker - install docker
-e, --extended - install extended package set (mc wget make sudo ntpdate)
-h, --help - show this help
-k FILE, --key: FILE - ssh-copy-id FILE to remote user
-l, --locale - setup local server locale at remote server
-n, --ntpdate - setup ntpdate cron job
-p NN, --port NN - change ssh service port to NN
-s SIZE, --swap SIZE - create swap volume size SIZE
-t, --tune - tune sysctl for virtual server
-u, --update - update system packages
EXAMPLE:
bash install.sh 127.0.0.1 -a op -p 32 -s 1Gb -delntu
SEE ALSO:
* https://github.com/dopos/dcape
EOF
}
host=$1
shift
# ------------------------------------------------------------------------------
# arg parsing based on https://stackoverflow.com/a/14787208
args=$(getopt -l "admin:,cape:,docker,extended,key:,locale,ntpdate,port:,swap:,tune,update" -o "a:c:dek:lnp:s:tu" -- "$@")
eval set -- "$args"
while [ $# -ge 1 ]; do
case "$1" in
--)
# No more options left.
shift
break
;;
-a|--admin)
admin="$2" ; shift ;;
-c|--cape)
cape="$2" ; shift ;;
-d|--docker)
docker="yes" ;;
-e|--extended)
extended="yes" ;;
-k|--key)
key="$2" ; shift ;;
-l|--locale)
locale="yes" ;;
-n|--ntpdate)
ntpdate="yes" ;;
-p|--port)
port="$2" ; shift ;;
-s|--swap)
swap="$2" ; shift ;;
-t|--tune)
tune="yes" ;;
-u|--update)
update="yes" ;;
*)
help ;;
esac
shift
done
if [[ ! "$host" ]] ; then
echo "Error: HOST is required. Aborting"
help
exit 1
elif [[ "$host" == "-h" || "$host" == "--help" ]] ; then
help
exit 1
fi
[[ "$admin" == "-" ]] && admin=
# ------------------------------------------------------------------------------
cat <<EOF
** setup-remote-host.sh options:
host: $host
admin: ${admin:--}
cape: ${cape:--}
docker: ${docker:--}
extended: $extended
key: ${key:--}
locale: $locale
ntpdate: $ntpdate
port: ${port:--}
swap: ${swap:--}
tune: $tune
update: $update
EOF
read -p "[Hit Enter to continue]" X
# ------------------------------------------------------------------------------
echo -n "* ssh keyfile: "
if [[ "$key" ]] ; then
echo "copying..."
ssh-copy-id -i $key root@$host
echo "Done"
else
echo "skip"
fi
echo "** Run remote setup at host $host..."
ssh root@$host 'bash -s' << EOF
# stop on error
set -e
# ------------------------------------------------------------------------------
echo -n "* swapfile: "
if [[ "$swap" ]] ; then
# https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04
swap_file=/swapfile
if [ -f \$swap_file ] ; then
echo "Already exists"
else
echo -n "Enabling $swap..."
fallocate -l $swap \$swap_file
chmod 600 \$swap_file
mkswap \$swap_file
swapon \$swap_file
echo "\$swap_file none swap sw 0 0" >> /etc/fstab
echo "Ok"
fi
else
echo "skip"
fi
# ------------------------------------------------------------------------------
echo -n "* server tune: "
if [[ "$tune" ]] ; then
echo -n "setup..."
grep vm.swappiness /etc/sysctl.conf || {
echo "vm.swappiness=10" >> /etc/sysctl.conf
sysctl vm.swappiness=10
}
grep vfs_cache_pressure /etc/sysctl.conf || {
echo "vfs_cache_pressure=50" >> /etc/sysctl.conf
sysctl vm.vfs_cache_pressure=50
}
echo "Ok"
else
echo "skip"
fi
# ------------------------------------------------------------------------------
echo -n "* locale: "
if [[ "$locale" ]] ; then
echo -n "setup $LC_NAME..."
locale-gen $LC_NAME
echo "Ok"
else
echo "skip"
fi
export DEBIAN_FRONTEND=noninteractive
# ------------------------------------------------------------------------------
echo -n "* update: "
if [[ "$update" ]] ; then
echo -n "run..."
apt update
apt -y upgrade
echo "Ok"
else
echo "skip"
fi
# ------------------------------------------------------------------------------
echo -n "* extended packages: "
if [[ "$extended" ]] ; then
echo -n "install..."
apt-get -y remove apache2 python-samba samba-common
apt-get -y install mc wget make sudo ntpdate git
echo "Ok"
else
echo "skip"
fi
# ------------------------------------------------------------------------------
echo -n "* ntpdate: "
if [[ "$ntpdate" ]] ; then
echo -n "setup..."
which ntpdate || apt-get -y install ntpdate
echo "#!/bin/sh" > /etc/cron.daily/ntpdate
echo "ntpdate -u ntp.ubuntu.com pool.ntp.org" >> /etc/cron.daily/ntpdate
chmod a+x /etc/cron.daily/ntpdate
echo "Ok"
else
echo "skip"
fi
# ------------------------------------------------------------------------------
echo -n "* docker: "
if [[ "$docker" ]] ; then
echo -n "install..."
which docker > /dev/null || wget -qO- https://get.docker.com/ | sh
echo "Ok"
else
echo "skip"
fi
# ------------------------------------------------------------------------------
echo -n "* admin: "
if [[ "$admin" ]] ; then
echo -n "setup user $admin..."
NEWUSER=$admin
HOMEROOT=/home
HOMEDIR=\$HOMEROOT/\$NEWUSER
[ -d \$HOMEROOT ] || mkdir \$HOMEROOT
# Check if user exists already
grep -qe "^\$NEWUSER:" /etc/passwd || useradd -d \$HOMEDIR -m -r -s /bin/bash -Gwww-data -gusers -gdocker \$NEWUSER
[ -d \$HOMEDIR/.ssh ] || sudo -u \$NEWUSER mkdir -m 700 \$HOMEDIR/.ssh
KEYFILE=\$HOMEDIR/.ssh/authorized_keys
if [ ! -f \$KEYFILE ] ; then
cp /root/.ssh/authorized_keys \$KEYFILE
chown \$NEWUSER \$KEYFILE
chmod 600 \$KEYFILE
fi
# allow sudo without pass
[ -f /etc/sudoers.d/\$NEWUSER ] || {
echo "\$NEWUSER ALL=NOPASSWD:ALL" > /etc/sudoers.d/\$NEWUSER
chmod 440 /etc/sudoers.d/\$NEWUSER
}
echo "Ok"
else
echo "skip"
fi
# ------------------------------------------------------------------------------
echo -n "* sshd: "
if [[ "$port" ]] ; then
echo -n "setup ssh with port $port..."
# TODO
sed -i "/^Port 22/c Port $port" /etc/ssh/sshd_config
if [[ "$admin" ]] ; then
echo -n "switch user..."
# Deny root login via ssh
sed -i "/^PermitRootLogin.*/c PermitRootLogin no" /etc/ssh/sshd_config
# Deny password login
sed -i "/#PasswordAuthentication *yes/c PasswordAuthentication no" /etc/ssh/sshd_config
fi
#service ssh reload
echo "Ok"
else
echo "skip"
fi
# ------------------------------------------------------------------------------
# TODO: test it
echo -n "* cape: "
if [[ "$cape" ]] ; then
echo -n "setup dcape with args $cape..."
cd /opt
git clone https://github.com/dopos/dcape.git
cd dcape
make init $cape
make apply
make up
echo "Ok"
else
echo "skip"
fi
EOF
# ------------------------------------------------------------------------------
echo "** Server setup complete"