-
Notifications
You must be signed in to change notification settings - Fork 34
/
vultr_app.sh
274 lines (222 loc) · 6.36 KB
/
vultr_app.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
#!/bin/bash
###################################################################
## Vultr Marketplace Helper Functions
function error_detect_on() {
set -euo pipefail
}
function error_detect_off() {
set +euo pipefail
}
function enable_verbose_commands() {
set -x pipefail
}
function disable_verbose_commands() {
set +x pipefail
}
function get_hostname() {
HOSTNAME=$(curl --fail -s "http://169.254.169.254/latest/meta-data/hostname")
echo "${HOSTNAME}"
}
function get_userdata() {
USERDATA=$(curl --fail -s "http://169.254.169.254/latest/user-data")
echo "${USERDATA}"
}
function get_sshkeys() {
KEYS=$(curl --fail -s "http://169.254.169.254/current/ssh-keys")
echo "${KEYS}"
}
function get_var() {
local val="$(curl --fail -s -H "Metadata-Token: vultr" http://169.254.169.254/v1/internal/app-${1} 2>/dev/null)"
local __result=$1
eval $__result="'${val}'"
}
function get_ip
{
local val="$(curl --fail -s -H "Metadata-Token: vultr" http://169.254.169.254/v1/internal/meta-data/meta-data/public-ipv4 2>/dev/null)"
local __result=$1
eval $__result="'${val}'"
}
function wait_on_apt_lock() {
DPKG_ID=$(lsof -t /var/lib/dpkg/lock) || true
APT_ID=$(lsof -t /var/lib/apt/lists/lock) || true
CACHE_ID=$(lsof -t /var/cache/apt/archives/lock) || true
until [[ "${DPKG_ID}${APT_ID}${CACHE_ID}" == "" ]]
do
echo "Waiting for apt lock held by: [${DPKG_ID}-${APT_ID}-${CACHE_ID}]"
sleep 3
DPKG_ID=$(lsof -t /var/lib/dpkg/lock) || true
APT_ID=$(lsof -t /var/lib/apt/lists/lock) || true
CACHE_ID=$(lsof -t /var/cache/apt/archives/lock) || true
done
echo "/var/lib/dpkg/lock is unlocked."
echo "/var/lib/apt/lists/lock is unlocked."
echo "/var/cache/apt/archives/lock is unlocked."
}
function apt_safe() {
wait_on_apt_lock
apt install -y $@
}
function apt_update_safe() {
wait_on_apt_lock
apt update -y
}
function apt_upgrade_safe() {
wait_on_apt_lock
DEBIAN_FRONTEND=noninteractive apt upgrade -y
}
function apt_remove_safe() {
wait_on_apt_lock
apt remove -y $@ --auto-remove
}
function apt_clean_safe() {
wait_on_apt_lock
apt autoremove -y
wait_on_apt_lock
apt autoclean -y
}
function update_and_clean_packages() {
# RHEL/CentOS
if [[ -f /etc/redhat-release ]]; then
yum update -y
yum clean all
# Ubuntu / Debian
elif [[ "$(grep -c "debian" /etc/os-release)" != "0" ]]; then
apt_update_safe
apt_upgrade_safe
apt_clean_safe
fi
}
function set_vultr_kernel_option() {
# RHEL/CentOS
if [[ -f /etc/redhat-release ]]; then
/sbin/grubby --update-kernel=ALL --args vultr
# Ubuntu / Debian
elif [[ "$(grep -c "ID=debian" /etc/os-release)" != "0" ]]; then
sed -i "/^GRUB_CMDLINE_LINUX_DEFAULT=/ s/\"$/ vultr\"/" /etc/default/grub
update-grub
fi
}
function install_cloud_init() {
if [ -x "$(command -v cloud-init)" ]; then
echo "cloud-init is already installed."
return
fi
update_and_clean_packages
if [[ -f /etc/redhat-release ]]; then
BUILD="rhel"
DIST="rpm"
fi
if [[ "$(grep -c "ID=ubuntu" /etc/os-release)" != "0" ]]; then
BUILD="universal"
DIST="deb";
fi
if [[ "$(grep -c "ID=debian" /etc/os-release)" != "0" ]]; then
BUILD="debian"
DIST="deb";
fi
if [[ "${DIST}" == "" ]]; then
echo "Undetected OS, please install from source!"
exit 255
fi
RELEASE=$1
if [[ "${RELEASE}" == "" ]]; then
RELEASE="latest"
fi
if [[ "${RELEASE}" != "latest" ]] && [[ "${RELEASE}" != "nightly" ]]; then
echo "${RELEASE} is an invalid release option. Allowed: latest, nightly"
exit 255
fi
# Lets remove all traces of previously installed cloud-init
# Ubuntu installs have proven problematic with their left over
# configs for the installer in recent versions
cleanup_cloudinit || true
wget https://ewr1.vultrobjects.com/cloud_init_beta/cloud-init_${BUILD}_${RELEASE}.${DIST} -O /tmp/cloud-init_${BUILD}_${RELEASE}.${DIST}
if [[ "${DIST}" == "rpm" ]]; then
yum install -y /tmp/cloud-init_${BUILD}_${RELEASE}.${DIST}
elif [[ "${DIST}" == "deb" ]]; then
apt_safe /tmp/cloud-init_${BUILD}_${RELEASE}.${DIST}
fi
rm -f /tmp/cloud-init_${BUILD}_${RELEASE}.${DIST}
}
function cleanup_cloudinit() {
rm -rf /etc/cloud
rm -rf /etc/systemd/system/cloud-init.target.wants/*
rm -rf /usr/src/cloud*
rm -rf /usr/local/bin/cloud*
rm -rf /usr/bin/cloud*
rm -rf /usr/lib/cloud*
rm -rf /usr/local/bin/cloud*
rm -rf /lib/systemd/system/cloud*
rm -rf /var/lib/cloud
rm -rf /var/log/cloud*
rm -rf /run/cloud-init
}
function clean_tmp() {
mkdir -p /tmp
chmod 1777 /tmp
rm -rf /tmp/*
rm -rf /var/tmp/*
}
function clean_keys() {
rm -f /root/.ssh/authorized_keys /etc/ssh/*key*
touch /etc/ssh/revoked_keys
chmod 600 /etc/ssh/revoked_keys
}
function clean_logs() {
find /var/log -mtime -1 -type f -exec truncate -s 0 {} \;
rm -rf /var/log/*.gz
rm -rf /var/log/*.[0-9]
rm -rf /var/log/*.log
rm -rf /var/log/lastlog
rm -rf /var/log/wtmp
echo "" >/var/log/auth.log
}
function clean_history() {
history -c
echo "" > /root/.bash_history
unset HISTFILE
}
function clean_mloc() {
/usr/bin/updatedb
}
function clean_random() {
rm -f /var/lib/systemd/random-seed
}
function clean_machine_id() {
rm -f /etc/machine-id
touch /etc/machine-id
}
function clean_free_space() {
dd if=/dev/zero of=/zerofile
sync
rm -f /zerofile
sync
}
function trim_ssd() {
fstrim /
}
function cleanup_marketplace_scripts() {
rm -f /root/*.sh
}
function disable_network_manager() {
## Disable NetworkManager, replace with network-scripts
systemctl stop NetworkManager
systemctl disable NetworkManager
sed -i 's/^ONBOOT.*/ONBOOT=yes/g' /etc/sysconfig/network-scripts/ifcfg-*
sed -i 's/^NM_CONTROLLED.*/NM_CONTROLLED=no/g' /etc/sysconfig/network-scripts/ifcfg-*
yum install -y network-scripts
}
function clean_system() {
update_and_clean_packages
set_vultr_kernel_option
clean_tmp
clean_keys
clean_logs
clean_history
clean_random
clean_machine_id
clean_mloc || true
clean_free_space || true
trim_ssd || true
cleanup_marketplace_scripts
}