/
cephlab_rc_local
174 lines (161 loc) · 5.33 KB
/
cephlab_rc_local
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
## {{ ansible_managed }}
#set lockfile = '/.cephlab_rc_local'
# Set proper location for firstboot ansible post-install trigger
#set distro = $getVar('distro','').split("-")[0]
#set distro_ver = $getVar('distro','').split("-")[1]
#if ($distro == 'RHEL') or ($distro == 'CentOS')
#set distro_ver = $distro_ver.split(".")[0]
#end if
#if ($distro == 'Fedora' and int($distro_ver) >= 22) or ($distro == 'RHEL' and int($distro_ver) >= 8)
#set script = '/etc/rc.d/rc.local'
#else if $distro == 'CentOS' and int($distro_ver) >= 9
#set script = '/etc/rc.d/rc.local'
systemctl enable rc-local.service
#else if $distro == 'openSUSE'
#set script = '/etc/init.d/boot.local'
#else
#set script = '/etc/rc.local'
#end if
cat > $script <<\EOF
#!/bin/bash
# Redirect rc.local output to our console so it's in teuthology console logs
exec 2> /dev/ttyS1
exec 1>&2
set -ex
# This function will print the date to console in a clean way.
# In other words, it'll just print the date without it looking like this:
# + date -u +%FT%T.%N
# + cut -c1-23
# 2020-05-15T14:15:33.087
TheTimeIs ()
{
{ set +x; } 2>/dev/null
date -u +%FT%T.%N | cut -c1-23
{ set -x; } 2>/dev/null
}
{% if rclocal_nameserver is defined %}
if [ ! -f /.cephlab_net_configured ]; then
#if $distro == 'openSUSE'
udevadm trigger
sleep 5
#end if
#raw
nics=$(ls -1 /sys/class/net | grep -v lo)
for nic in $nics; do
TheTimeIs
# Bring the NIC up so we can detect if a link is present
ifconfig $nic up || ip link set $nic up
# Sleep for a bit to let the NIC come up
sleep 5
if ethtool $nic | grep -q "Link detected: yes"; then
if command -v zypper &>/dev/null; then
echo -e "DEVICE=$nic\nBOOTPROTO=dhcp\nSTARTMODE=auto" > /etc/sysconfig/network/ifcfg-$nic
elif command -v apt-get &>/dev/null; then
echo -e "auto lo\niface lo inet loopback\n\nauto $nic\niface $nic inet dhcp" > /etc/network/interfaces
else
echo -e "DEVICE=$nic\nBOOTPROTO=dhcp\nONBOOT=yes" > /etc/sysconfig/network-scripts/ifcfg-$nic
fi
# Don't bail if NIC fails to go down or come up
{ set +e; } 2>/dev/null
TheTimeIs
# Bounce the NIC so it gets a DHCP address
ifdown $nic
ifup $nic
attempts=0
# Try for 5 seconds to ping our Cobbler host
#end raw
while ! ping -I $nic -nq -c1 $http_server && [ $attempts -lt 5 ]; do
#raw
sleep 1
attempts=$[$attempts+1]
done
if [ $attempts == 5 ]; then
# If we can't ping our Cobbler host, remove the DHCP config for this NIC.
# It must either be on a non-routable network or has no reachable DHCP server.
ifdown $nic
rm -f /etc/sysconfig/network-scripts/ifcfg-$nic
sed -i "/$nic/d" /etc/network/interfaces
# Go back to bailing if anything fails bringing the next NIC up
set -e
else
# We found our routable NIC!
# Write our lockfile so this only gets run on firstboot
TheTimeIs
touch /.cephlab_net_configured
# Break out of the loop once we've found our routable NIC
break
fi
else
# Take the NIC back down if it's not connected
ifconfig $nic down || ip link set $nic down
fi
done
fi
# Don't error out if the `ip` command returns rc 1
set +e
attempts=0
myips=""
until [ "$myips" != "" ] || [ $attempts -ge 10 ]; do
myips=$(ip -4 addr | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v '127.0.0.1\|127.0.1.1')
attempts=$[$attempts+1]
sleep 1
done
set -e
if [ -n "$myips" ]; then
for ip in $myips; do
if timeout 1s ping -I $ip -nq -c1 {{ rclocal_nameserver }} 2>&1 >/dev/null; then
newhostname=$(dig +short -x $ip @{{ rclocal_nameserver }} | sed 's/\.com.*/\.com/g')
if [ -n "$newhostname" ]; then
hostname $newhostname
newdomain=$(hostname -d)
shorthostname=$(hostname -s)
echo $shorthostname > /etc/hostname
if grep -q $newdomain /etc/hosts; then
# Replace
sed -i "s/.*$newdomain.*/$ip $newhostname $shorthostname/g" /etc/hosts
else
# Or add to top of file
sed -i '1i'$ip' '$newhostname' '$shorthostname'\' /etc/hosts
fi
fi
# Quit after first IP that can ping our nameserver
# in the extremely unlikely event the testnode has two IPs
break
fi
done
fi
#end raw
{% endif %}
# Regenerate SSH host keys on boot if needed
if command -v zypper &> /dev/null; then
if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa
systemctl restart sshd
fi
elif command -v apt-get &>/dev/null; then
if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
dpkg-reconfigure openssh-server
fi
fi
# Only run once.
if [ -e $lockfile ]; then
exit 0
fi
# Wait until we get 10 ping responses from Cobbler host
# before calling post-install trigger
until ping -nq -c10 $http_server
do
echo "Waiting for network"
sleep 3
done
# Output message to console indicating Ansible is being run
set +x
echo -e "==================================\nInstructing Cobbler to run Ansible\n Waiting for completion\n==================================" > /dev/console
TheTimeIs
set -x
# Run the post-install trigger a second time
curl --max-time 1800 --silent "http://$http_server:$http_port/cblr/svc/op/trig/mode/post/system/$system_name" -o /dev/null || true
TheTimeIs
touch $lockfile
EOF
chmod +x $script