-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeanuts.sh
More file actions
executable file
·380 lines (322 loc) · 9.56 KB
/
peanuts.sh
File metadata and controls
executable file
·380 lines (322 loc) · 9.56 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env bash
#
# Peanuts Bash Library
# Author: Everaldo Canuto <everaldo.canuto@gmail.com>
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or distribute
# this software, either in source code form or as a compiled binary, for any
# purpose, commercial or non-commercial, and by any means.
#
# In jurisdictions that recognize copyright laws, the author or authors of this
# software dedicate any and all copyright interest in the software to the
# public domain. We make this dedication for the benefit of the public at large
# and to the detriment of our heirs and successors. We intend this dedication
# to be an overt act of relinquishment in perpetuity of all present and future
# rights to this software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
### Global settings ############################################################
set -e
export DEBIAN_FRONTEND=noninteractive
### System constants ###########################################################
ESSENTIAL_PACKAGES=(sudo openssh-server ufw nano lsb-release)
SSHD_CONFIG="/etc/ssh/sshd_config"
### Utilities ##################################################################
function info() {
bold=$(tput bold)
norm=$(tput sgr0)
printf "${bold}--> ${1}${norm}\n"
}
function fset() {
file=$1
argc=$2
sepc=$(echo -e $argc | sed -n -e "s/.*\([[:space:]:=]\).*/\1/p")
name=$(echo -e $argc | sed "s/[:=[:space:]].*//")
if [ ! -f "$file.orig" ]; then
cp "$file" "$file.orig"
fi
if grep --quiet "^[# ;]*$name[[:space:]:=]" $file; then
sed -i -e "s|^[# ;]*$name[ ]*$sepc.\+|$argc|" $file
else
echo "$argc" >> $file
fi
}
function fget() {
file=$1
argc=$2
sed -n -e "s|^ *$argc[ =]\+\(.*\) *|\1|p" $file
}
function check_for_root_privileges() {
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
}
function check_for_system() {
system=$(fget /etc/os-release ID)
if [[ "$system" != "$1" ]]; then
echo "This script must be run on $1 systems" 1>&2
exit 1
fi
}
function common_system_checks() {
check_for_root_privileges
check_for_system "$1"
}
function urlretrieve() {
rurl=$1; file=$2; attr=$3
wget -O $file $rurl
chmod $attr $file
}
### System #####################################################################
function system_set_timezone() {
timedatectl set-timezone $1
}
function system_set_locale() {
echo "LANG=$1" > /etc/default/locale
sed -i "s/# *$1/$1/" /etc/locale.gen
sed -i "s/# *$2/$2/" /etc/locale.gen
locale-gen
}
function system_set_hostname() {
HOSTNAME="$1"
echo "$HOSTNAME" > /etc/hostname
hostname -F /etc/hostname
}
function system_set_swapfile() {
SWAPSIZE=$1
SWAPFILE=$2
if [ ! -f "$SWAPFILE" ]; then
fallocate -l ${SWAPSIZE} ${SWAPFILE}
chmod 0600 $SWAPFILE
mkswap $SWAPFILE
swapon $SWAPFILE
cat >> /etc/fstab <<-EOF
$SWAPFILE none swap sw 0 0
EOF
fi
}
function system_backup_file() {
if [ ! -f "$1.orig" ]; then
cp "$1" "$1.orig"
fi
}
function system_sources() {
mirr="${1:-us}"
comp="${2:-main contrib non-free}"
name=$(dpkg --status tzdata | grep Provides | cut -f2 -d '-')
file="/etc/apt/sources.list"
system_backup_file $file
if [ ! -s /dev/stdin ]; then
cat > $file <<-EOF
deb http://ftp.$mirr.debian.org/debian $name main contrib non-free
deb http://ftp.$mirr.debian.org/debian/ $name-updates main contrib non-free
deb http://ftp.$mirr.debian.org/debian $name-backports main contrib non-free
deb http://security.debian.org/ $name/updates main contrib non-free
EOF
else
cat /dev/stdin > $file
fi
}
function system_update() {
apt-get update
}
function system_upgrade() {
apt-get update
apt-get upgrade --yes
apt-get autoremove --yes --purge
apt-get clean
}
function system_install() {
apt-get install --yes $@
}
function system_install_docker() {
info "Install Docker"
system_install docker.io docker-compose
if [ $# -gt 0 ]; then
usermod -aG docker $@
fi
}
function system_install_nodejs() {
info "Installing NodeJs"
system_install nodejs npm yarnpkg
}
function system_set_nicemotd() {
info "Setting message of the day"
motdname="nice-motd"
if [ -n "$1" ]; then
motdname="nice-motd-$1"
fi
motdfurl="https://raw.githubusercontent.com/ecanuto/peanuts/master/${motdname}.sh"
motdpath="/etc/update-motd.d"
motdfile="${motdpath}/01-${motdname}"
curl -so $motdfile $motdfurl
system_backup_file /etc/motd
rm -f /etc/motd
chmod -x $motdpath/*
chmod +x $motdfile
}
function system_add_user() {
USERNAME="$1"
USERHOME="/home/$USERNAME"
USGROUPS="$2"
if [ ! -d "$USERHOME" ]; then
adduser --disabled-password --gecos "" $USERNAME
if [ ! -z "$USGROUPS" ]; then
usermod -a -G $USGROUPS $USERNAME
fi
usermod -p "" $USERNAME
chage -d 0 $USERNAME
fi
}
function system_add_user_group() {
usermod -G $2 $1
}
function system_add_user_key() {
USERNAME="$1"
USERKEYS="$2"
if [ $1 = "root" ]; then
USERHOME="/$USERNAME"
else
USERHOME="/home/$USERNAME"
fi
mkdir -p $USERHOME/.ssh
cat > $USERHOME/.ssh/authorized_keys <<-EOF
$USERKEYS
EOF
chmod 700 $USERHOME/.ssh
chmod 600 $USERHOME/.ssh/authorized_keys
chown -R $USERNAME:$USERNAME $USERHOME/.ssh
}
function common_ssh_settings() {
info "SSH settings"
cat > /etc/ssh/sshd_config.d/01-local-stuff.conf <<-EOF
X11Forwarding no
PasswordAuthentication no
UseDNS no
EOF
systemctl restart ssh
}
function common_system_settings() {
while [ $# -gt 0 ]; do
if [[ $1 == "-"* ]]; then
v="${1/-/}"
declare $v="$2"
fi
shift
done
check_for_root_privileges
if [ ! -z "$d" ]; then
check_for_system "$d"
fi
info "System upgrade"
system_sources
system_upgrade
info "Installing common packages"
system_install \
openssh-server lsb-release bash-completion command-not-found \
sudo nano rsync htop wget curl git locales
info "Locale settings"
system_set_locale "en_US.UTF-8" "$l"
if [ ! -z "$t" ]; then
system_set_timezone "$t"
fi
info "SSH settings"
if [ ! -z "$s" ]; then
fset $SSHD_CONFIG "Port $s"
fi
fset $SSHD_CONFIG "X11Forwarding no"
fset $SSHD_CONFIG "PasswordAuthentication no"
fset $SSHD_CONFIG "UseDNS no"
systemctl restart ssh
if [ ! -z ${g+x} ]; then
info "Setting some nice stuff like toilet motd"
system_goodstuff
fi
}
### MySQL ######################################################################
function mysql_secure_settings() {
# remove anonymous users
mysql -u root -e "DELETE FROM mysql.user WHERE user=''"
# disallow root login remotely
mysql -u root -e "DELETE FROM mysql.user WHERE user='root' AND host NOT IN ('127.0.0.1', 'localhost', '::1');"
# remove 'test' database
mysql -u root -e "DROP DATABASE IF EXISTS test"
# reload privilege tables
mysql -u root -e "FLUSH PRIVILEGES"
}
function mysql_common_settings() {
mysql_secure_settings
mysql -u root -e "UPDATE mysql.user SET plugin='mysql_native_password' WHERE User='root'"
mysql -u root -e "FLUSH PRIVILEGES"
}
function mysql_create_database() {
mysql -u root -e "CREATE DATABASE IF NOT EXISTS $1 CHARACTER SET utf8"
if [ ! -z "$2" ]; then
mysql -u root -e "GRANT ALL PRIVILEGES ON $1.* TO '$2'@'localhost' IDENTIFIED BY '$3'"
mysql -u root -e "GRANT ALL PRIVILEGES ON $1.* TO '$2'@'127.0.0.1' IDENTIFIED BY '$3'"
mysql -u root -e "FLUSH PRIVILEGES"
fi
}
### PostgreSQL #################################################################
function pgsql_create_user() {
while [ $# -gt 0 ]; do
param=""
if [[ $1 == "-s"* ]]; then
param=$1
shift
fi
if ! sudo -i -u postgres psql -t -c "\du $1" | grep -qw .; then
sudo -i -u postgres createuser -U postgres $param $1
fi
shift
done
}
### Nginx ######################################################################
function nginx_common_settings() {
fset /etc/nginx/nginx.conf "\tserver_names_hash_bucket_size 128;"
fset /etc/nginx/nginx.conf "\tworker_connections 2048;"
fset /etc/nginx/nginx.conf "\tserver_tokens off;"
echo "<center>$HOSTNAME</center>" > /var/www/html/index.html
systemctl restart nginx
}
### PHP-FPM ####################################################################
function phpfpm_common_settings() {
phpconf=`ls /etc/php/*/fpm/php.ini`
fset $phpconf "upload_max_filesize = 32M"
fset $phpconf "post_max_size = 32M"
fset $phpconf "short_open_tag = On"
fset $phpconf "date.timezone = $(cat /etc/timezone)"
wwwconf=`ls /etc/php/*/fpm/pool.d/www.conf`
fset $wwwconf "pm.status_path = /status"
fset $wwwconf "pm.max_children = 12"
fset $wwwconf "pm.max_spare_servers = 6"
servicename=$(basename `ls /lib/systemd/system/php*fpm.*`)
servicename="${servicename%.*}"
systemctl restart $servicename
}
### BOOTSTRAP ##################################################################
function common_bootstrap() {
common_system_settings "$@"
}
function lemp_bootstrap() {
common_system_settings "$@"
info "Installing MySQL Server"
system_install mysql-server
mysql_common_settings
info "Installing Nginx"
system_install nginx-extras
nginx_common_settings
info "PHP-FPM setup"
system_install \
php7.0-cli php7.0-curl php7.0-fpm php7.0-gd php7.0-intl php7.0-mcrypt \
php7.0-mysql php7.0-mbstring php7.0-xml php7.0-zip php7.0-soap
phpfpm_common_settings
}