-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathec2-init.script
More file actions
executable file
·147 lines (112 loc) · 4.34 KB
/
ec2-init.script
File metadata and controls
executable file
·147 lines (112 loc) · 4.34 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
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
#!/bin/sh
# Author: Artem Butusov <[email protected]>
# License: MIT
# Version: 1.0.4
# https://github.com/sormy/ec2-init
# shellcheck shell=sh
# shellcheck disable=SC2129
EC2_META_DATA_URL="http://169.254.169.254/latest/meta-data"
EC2_USER_DATA_URL="http://169.254.169.254/latest/user-data"
EC2_INIT_MODULES="${EC2_INIT_MODULES:-hostname ssh exec}"
EC2_INIT_LOCK="${EC2_INIT_LOCK:-/var/lib/ec2-init.lock}"
EC2_INIT_LOG="${EC2_INIT_LOG:-/var/log/ec2-init.log}"
EC2_INIT_USER_DATA="${EC2_INIT_USER_DATA:-/var/lib/ec2-init.user-data}"
CURL="${CURL:-curl --silent --max-time 1 --retry-delay 1 --retry 10 --retry-all-errors --fail}"
start() {
echo "Running EC2 initialization ..."
echo "Running EC2 initialization" > "$EC2_INIT_LOG"
timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
echo "Start time: $timestamp" >> "$EC2_INIT_LOG"
instance_id=$($CURL "$EC2_META_DATA_URL/instance-id")
if [ "$instance_id" = "" ]; then
echo "Unable to obtain EC2 metadata"
echo "Unable to obtain EC2 metadata" >> "$EC2_INIT_LOG"
exit 1
fi
echo "Discovered EC2 instance ID: $instance_id" >> "$EC2_INIT_LOG"
if [ -f "$EC2_INIT_LOCK" ] && [ "$(cat "$EC2_INIT_LOCK")" = "$instance_id" ]; then
echo "EC2 initialization is already completed"
echo "EC2 initialization is already completed" > "$EC2_INIT_LOG"
exit 0
fi
init_systemd
echo "$EC2_INIT_MODULES" | grep -q '\bhostname\b' && init_hostname
echo "$EC2_INIT_MODULES" | grep -q '\bssh\b' && init_ssh
echo "$EC2_INIT_MODULES" | grep -q '\bexec\b' && init_exec "$instance_id"
echo "$instance_id" > "$EC2_INIT_LOCK"
chmod o-rwx "$EC2_INIT_LOCK"
case "$EC2_INIT_LOG" in
/dev/*) ;;
*) chmod o-rwx "$EC2_INIT_LOG";;
esac
timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
echo "End time: $timestamp" >> "$EC2_INIT_LOG"
echo "EC2 initialization is completed"
echo "EC2 initialization is completed" >> "$EC2_INIT_LOG"
}
init_hostname() {
echo "Setting up hostname ..."
echo "Setting up hostname" >> "$EC2_INIT_LOG"
hostname=$($CURL "$EC2_META_DATA_URL/local-hostname")
echo "Discovered hostname: $hostname" >> "$EC2_INIT_LOG"
if prog_exists hostnamectl; then
# systemd
hostnamectl set-hostname "$hostname"
elif [ -f /etc/hostname ]; then
# Void Linux
echo "$hostname" > /etc/hostname
elif [ -f /etc/conf.d/hostname ]; then
# Gentoo / OpenRC
echo "hostname=$hostname" > /etc/conf.d/hostname
fi
}
init_ssh() {
echo "Setting up SSH ..."
echo "Setting up SSH" >> "$EC2_INIT_LOG"
ssh_user_name="${SSH_USER_NAME:-$(whoami)}"
ssh_user_home="${SSH_USER_HOME:-$(user_home "$ssh_user_name")}"
ssh_conf_home="$ssh_user_home/.ssh"
echo "Loading SSH authorized keys to $ssh_conf_home" >> "$EC2_INIT_LOG"
[ -e "$ssh_conf_home" ] && rm -rf "$ssh_conf_home"
mkdir -p "$ssh_conf_home"
chown "$ssh_user_name:$ssh_user_name" "$ssh_conf_home"
chmod 750 "$ssh_conf_home"
$CURL "$EC2_META_DATA_URL/public-keys" \
| cut -d = -f 1 \
| xargs printf "$EC2_META_DATA_URL/public-keys/%s/openssh-key\n" \
| xargs $CURL \
> "$ssh_conf_home/authorized_keys"
chown "$ssh_user_name:$ssh_user_name" "$ssh_conf_home/authorized_keys"
chmod 640 "$ssh_conf_home/authorized_keys"
}
init_exec() {
instance_id="$1"
echo "Executing user script ..."
echo "Executing user script" >> "$EC2_INIT_LOG"
$CURL "$EC2_USER_DATA_URL" > "$EC2_INIT_USER_DATA"
chmod o-rwx "$EC2_INIT_USER_DATA"
if [ "$(stat --printf="%s" "$EC2_INIT_USER_DATA")" = 0 ]; then
echo "User script is empty, nothing to execute" >> "$EC2_INIT_LOG"
elif [ "$(sed -n '/^#!/p;q' "$EC2_INIT_USER_DATA")" != "" ]; then
echo "User script output:" >> "$EC2_INIT_LOG"
echo "---" >> "$EC2_INIT_LOG"
sh "$EC2_INIT_USER_DATA" >> "$EC2_INIT_LOG" 2>&1
echo "---" >> "$EC2_INIT_LOG"
else
echo "User script has missing shebang" >> "$EC2_INIT_LOG"
fi
}
init_systemd() {
if prog_exists systemd-machine-id-setup; then
echo "Executing systemd-machine-id-setup ..."
echo "Executing systemd-machine-id-setup" >> "$EC2_INIT_LOG"
systemd-machine-id-setup
fi
}
user_home() {
grep "^$1:" /etc/passwd | cut -d : -f 6
}
prog_exists() {
which "$1" > /dev/null 2>&1
}
start