-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrc.local
More file actions
59 lines (51 loc) · 1.87 KB
/
rc.local
File metadata and controls
59 lines (51 loc) · 1.87 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
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# Create the host keys for the SSH server
for key_type in rsa dsa; do
if [ ! -f /etc/ssh/ssh_host_${key_type}_key ]; then
ssh-keygen -t ${key_type} -N '' -f /etc/ssh/ssh_host_${key_type}_key
fi
done
if [ -e /etc/rc.d/sshd ]; then
/etc/rc.d/sshd restart
else
/etc/rc.d/sshd restart
fi
# simple attempt to get the user ssh key using the meta-data service
mkdir -p /root/.ssh
echo >> /root/.ssh/authorized_keys
/usr/local/bin/curl --retry 3 --retry-delay 10 -m 45 -s http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key | grep 'ssh-rsa' >> /root/.ssh/authorized_keys
echo "AUTHORIZED_KEYS:"
echo "************************"
cat /root/.ssh/authorized_keys
echo "************************"
# set the hostname to something sensible
META_HOSTNAME="`/usr/local/bin/curl -s http://169.254.169.254/latest/meta-data/local-hostname`"
META_IP="`/usr/local/bin/curl -s http://169.254.169.254/latest/meta-data/local-ipv4`"
if [ ${META_HOSTNAME} = ${META_IP} ]; then
META_HOSTNAME="`echo $META_HOSTNAME | sed -e 's/\./-/g' | xargs -I {} echo "ip-{}"`"
fi
hostname $META_HOSTNAME
echo >> /etc/hosts
echo "${META_IP} ${META_HOSTNAME}" >> /etc/hosts
# check if the user-data is a script, and if so execute it
TMP_FILE="/tmp/user-data-$$"
/usr/local/bin/curl --retry 3 --retry-delay 10 -m 60 -o $TMP_FILE http://169.254.169.254/latest/user-data
if [ -s $TMP_FILE ]; then
echo "Downloaded user data in $TMP_FILE"
if [ "`head -n 2 $TMP_FILE`" = "#!" ]; then
chmod 700 $TMP_FILE
echo "User data is a script: executing it"
$TMP_FILE > /root/user-data.out 2>&1
fi
fi
exit 0