forked from weka/terraform-azure-weka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user-data.sh
executable file
·121 lines (100 loc) · 3.71 KB
/
user-data.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
#!/bin/bash
set -ex
curl -i ${report_url}?code="${function_app_default_key}" -H "Content-Type:application/json" -d "{\"hostname\": \"$HOSTNAME\", \"type\": \"progress\", \"message\": \"Running init script\"}"
handle_error () {
if [ "$1" -ne 0 ]; then
curl -i ${report_url}?code="${function_app_default_key}" -H "Content-Type:application/json" -d "{\"hostname\": \"$HOSTNAME\", \"type\": \"error\", \"message\": \"${2}\"}"
exit 1
fi
}
while fuser /var/{lib/{dpkg,apt/lists},cache/apt/archives}/lock >/dev/null 2>&1; do
sleep 2
done
apt update -y
# set apt private repo
if [[ "${apt_repo_url}" ]]; then
mv /etc/apt/sources.list /etc/apt/sources.list.bak
echo "deb ${apt_repo_url} focal main restricted universe" > /etc/apt/sources.list
echo "deb ${apt_repo_url} focal-updates main restricted" >> /etc/apt/sources.list
fi
INSTALLATION_PATH="/tmp/weka"
mkdir -p $INSTALLATION_PATH
for(( i=0; i<${nics_num}; i++ )); do
cat <<-EOF | sed -i "/ eth$i/r /dev/stdin" /etc/netplan/50-cloud-init.yaml
mtu: 3900
EOF
done
# config network with multi nics
echo "200 eth0-rt" >> /etc/iproute2/rt_tables
echo "network:"> /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
echo " config: disabled" >> /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
gateway=$(ip r | grep default | awk '{print $3}')
eth=$(ifconfig | grep eth0 -C2 | grep 'inet ' | awk '{print $2}')
cat <<-EOF | sed -i "/ set-name: eth0/r /dev/stdin" /etc/netplan/50-cloud-init.yaml
routes:
- to: ${subnet_range}
via: $gateway
metric: 200
table: 200
- to: 0.0.0.0/0
via: $gateway
table: 200
routing-policy:
- from: $eth/32
table: 200
- to: $eth/32
table: 200
EOF
netplan apply
cat >>/usr/sbin/remove-routes.sh <<EOF
#!/bin/bash
set -ex
EOF
for(( i=1; i<${nics_num}; i++ )); do
cat >>/usr/sbin/remove-routes.sh <<EOF
while ! ip route | grep eth$i; do
ip route
sleep 5
done
/usr/sbin/ip route del ${subnet_range} dev eth$i
EOF
done
chmod +x /usr/sbin/remove-routes.sh
cat >/etc/systemd/system/remove-routes.service <<EOF
[Unit]
Description=Remove specific routes
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
ExecStart=/bin/bash /usr/sbin/remove-routes.sh
[Install]
WantedBy=multi-user.target
EOF
ip route # show routes before removing
systemctl daemon-reload
systemctl enable remove-routes.service
systemctl start remove-routes.service
systemctl status remove-routes.service || true # show status of remove-routes.service
ip route # show routes after removing
# attach disk
while ! [ "$(lsblk | grep ${disk_size}G | awk '{print $1}')" ] ; do
echo "waiting for disk to be ready"
sleep 5
done
wekaiosw_device=/dev/"$(lsblk | grep ${disk_size}G | awk '{print $1}')"
status=0
mkfs.ext4 -L wekaiosw $wekaiosw_device 2>&1 | tee /tmp/output || status=$?
handle_error $status "$(cat /tmp/output)"
mkdir -p /opt/weka 2>&1 | tee /tmp/output || status=$?
handle_error $status "$(cat /tmp/output)"
mount $wekaiosw_device /opt/weka 2>&1 | tee /tmp/output || status=$?
handle_error $status "$(cat /tmp/output)"
rm /tmp/output
echo "LABEL=wekaiosw /opt/weka ext4 defaults 0 2" >>/etc/fstab
rm -rf $INSTALLATION_PATH
compute_name=$(curl -s -H Metadata:true --noproxy "*" "http://169.254.169.254/metadata/instance?api-version=2021-02-01" | jq '.compute.name')
compute_name=$(echo "$compute_name" | cut -c2- | rev | cut -c2- | rev)
curl ${deploy_url}?code="${function_app_default_key}" --fail -H "Content-Type:application/json" -d "{\"vm\": \"$compute_name:$HOSTNAME\"}" > /tmp/deploy.sh
chmod +x /tmp/deploy.sh
/tmp/deploy.sh 2>&1 | tee /tmp/weka_deploy.log