-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare-vps.sh
executable file
·146 lines (133 loc) · 3.71 KB
/
prepare-vps.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
#!/bin/bash
# Make sure only root can run our script
if [[ "$(id -u)" -ne 0 ]]
then
echo "This script must be run as root" 1>&2
exit 1
fi
upgrade()
{
echo -n "Upgrade the system? (y/N) "
read -r touche </dev/tty
if [[ "$touche" = "y" || "$touche" = "o" ]]
then
apt-get update && apt-get -y upgrade
fi
}
timezone()
{
echo -n "Update timezone? (y/N) "
read -r touche </dev/tty
if [[ "$touche" = "y" || "$touche" = "o" ]]
then
apt-get install ntp ntpdate </dev/tty
if dpkg -l tzdata; then
apt-get install -y tzdata
else
dpkg-reconfigure tzdata
fi
fi
}
password()
{
echo -n "Define / change the root password (usefull for recovery mode)? (y/N) "
read -r touche </dev/tty
if [[ "$touche" = "y" || "$touche" = "o" ]]
then
passwd </dev/tty
fi
}
force_rsa()
{
echo -n "Disable root login using password? (only rsa key) (y/N) "
read -r touche </dev/tty
if [[ "$touche" = "y" || "$touche" = "o" ]]
then
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config
/etc/init.d/ssh restart
fi
}
add_swap()
{
free -h
echo -n "Add swap? (highly recommended) (y/N) "
read -r touche </dev/tty
if [[ "$touche" = "y" || "$touche" = "o" ]]
then
read -rp "What's the size (in GB)? Please input a number between 1 and 4: " swap_value </dev/tty
echo "creating ${swap_value}G swap file"
fallocate -l "${swap_value}G" /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap sw 0 0' | tee -a /etc/fstab
echo 'vm.swappiness = 10' >> /etc/sysctl.conf
echo 'vm.vfs_cache_pressure=50' >> /etc/sysctl.conf
fi
}
mount_vol()
{
echo -n "Mount a volume (like /dev/vdx)? (y/N) "
read -r touche </dev/tty
if [[ "$touche" = "y" || "$touche" = "o" ]]
then
fdisk -l
read -rp "Specify volume name (eg. vdb): " volume_name </dev/tty
read -rp "Specify destination path (eg. /apps): " path_name < /dev/tty
read -rp "Do you really want to format and mount the volume /dev/${volume_name} on ${path_name} ? (y/n) : " touche </dev/tty
if [[ "$touche" = "y" || "$touche" = "o" ]]
then
mkfs -t ext4 "/dev/${volume_name}"
mkdir -p ${path_name}
mount "/dev/${volume_name}" ${path_name}
uuid_value=$(blkid -o value -s UUID /dev/${volume_name})
echo "UUID=\"${uuid_value}\" ${path_name} ext4 defaults,nofail 0 2" >> /etc/fstab
fi
fi
}
install_docker()
{
echo -n "Install Docker from docker-ce repository? (y/N) "
read -r touche </dev/tty
if [[ "$touche" = "y" || "$touche" = "o" ]]
then
apt-get install --yes apt-transport-https ca-certificates curl gnupg lsb-release
local os=$(lsb_release -is | tr "[:upper:]" "[:lower:]")
curl -fsSL "https://download.docker.com/linux/$os/gpg" | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/$os \
$(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
apt update
apt-cache policy docker-ce
apt-get install --yes docker-ce docker-ce-cli containerd.io
systemctl status docker
fi
}
install_docker_compose()
{
echo -n "Install Docker Compose from docker-ce repository? (y/N) "
read -r touche </dev/tty
if [[ "$touche" = "y" || "$touche" = "o" ]]
then
apt-get install --yes docker-compose-plugin bash-completion
docker compose version
fi
}
function trap_ctrlc()
{
echo "Ctrl^C, exiting..."
exit 2
}
vps_prepare()
{
trap "trap_ctrlc" 2 # SIGINT
upgrade
timezone
password
force_rsa
add_swap
mount_vol
install_docker
install_docker_compose
}
vps_prepare "$@"