aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Crute <mike@crute.us>2020-05-19 15:50:11 +0000
committerMike Crute <mike@crute.us>2020-05-19 15:50:11 +0000
commit1f4f4fccb897c8a0bc8bb27c51452fef17debbfd (patch)
tree1ae51b9cc83588e1b698ccc382063089a0fd0675
parent834cac92da29bc852ef9325e8a2cc6e38d7adee9 (diff)
downloadtiny-ec2-bootstrap-1f4f4fccb897c8a0bc8bb27c51452fef17debbfd.tar.bz2
tiny-ec2-bootstrap-1f4f4fccb897c8a0bc8bb27c51452fef17debbfd.tar.xz
tiny-ec2-bootstrap-1f4f4fccb897c8a0bc8bb27c51452fef17debbfd.zip
Change whitespace from spaces to tabs
-rw-r--r--tiny-ec2-bootstrap94
1 files changed, 47 insertions, 47 deletions
diff --git a/tiny-ec2-bootstrap b/tiny-ec2-bootstrap
index 8f39f42..e8c44ab 100644
--- a/tiny-ec2-bootstrap
+++ b/tiny-ec2-bootstrap
@@ -1,5 +1,5 @@
1#!/sbin/openrc-run 1#!/sbin/openrc-run
2# vim:set ft=sh et ts=4 sts=4 sw=4: 2# vim:set ft=sh noet ts=4:
3 3
4description="Provides EC2 cloud bootstrap" 4description="Provides EC2 cloud bootstrap"
5 5
@@ -8,81 +8,81 @@ EC2_USER=${EC2_USER:-alpine}
8IMDS2_TOKEN_TTL=${IMDS2_TOKEN_TTL:-5} 8IMDS2_TOKEN_TTL=${IMDS2_TOKEN_TTL:-5}
9 9
10depend() { 10depend() {
11 need net 11 need net
12 provide cloud-final 12 provide cloud-final
13} 13}
14 14
15_get_metadata_token() { 15_get_metadata_token() {
16 echo -ne "PUT /latest/api/token HTTP/1.0\r\nX-aws-ec2-metadata-token-ttl-seconds: $IMDS2_TOKEN_TTL\r\n\r\n" | 16 echo -ne "PUT /latest/api/token HTTP/1.0\r\nX-aws-ec2-metadata-token-ttl-seconds: $IMDS2_TOKEN_TTL\r\n\r\n" |
17 nc 169.254.169.254 80 | tail -n 1 17 nc 169.254.169.254 80 | tail -n 1
18} 18}
19 19
20_get_metadata() { 20_get_metadata() {
21 local uri="$1" 21 local uri="$1"
22 wget -qO - --header "X-aws-ec2-metadata-token: $(_get_metadata_token)" \ 22 wget -qO - --header "X-aws-ec2-metadata-token: $(_get_metadata_token)" \
23 "http://169.254.169.254/latest/$uri" 2>/dev/null 23 "http://169.254.169.254/latest/$uri" 2>/dev/null
24} 24}
25 25
26_update_hostname() { 26_update_hostname() {
27 local ec2_fqdn="$(_get_metadata meta-data/hostname)" 27 local ec2_fqdn="$(_get_metadata meta-data/hostname)"
28 local short_hostname="${ec2_fqdn%%\.*}" 28 local short_hostname="${ec2_fqdn%%\.*}"
29 echo "$short_hostname" > /etc/hostname 29 echo "$short_hostname" > /etc/hostname
30 hostname -F /etc/hostname 30 hostname -F /etc/hostname
31 echo -e "127.0.1.1\t$ec2_fqdn $short_hostname" >> /etc/hosts 31 echo -e "127.0.1.1\t$ec2_fqdn $short_hostname" >> /etc/hosts
32} 32}
33 33
34_set_ssh_keys() { 34_set_ssh_keys() {
35 local user="$1" 35 local user="$1"
36 local group="$(getent passwd "$user" | cut -d: -f4)" 36 local group="$(getent passwd "$user" | cut -d: -f4)"
37 local ssh_dir="$(getent passwd "$user" | cut -d: -f6)/.ssh" 37 local ssh_dir="$(getent passwd "$user" | cut -d: -f6)/.ssh"
38 local keys_file="$ssh_dir/authorized_keys" 38 local keys_file="$ssh_dir/authorized_keys"
39 39
40 if [ ! -d "$ssh_dir" ]; then 40 if [ ! -d "$ssh_dir" ]; then
41 mkdir -p "$ssh_dir" 41 mkdir -p "$ssh_dir"
42 chmod 755 "$ssh_dir" 42 chmod 755 "$ssh_dir"
43 fi 43 fi
44 44
45 [ -f "$keys_file" ] && rm "$keys_file" 45 [ -f "$keys_file" ] && rm "$keys_file"
46 46
47 touch "$keys_file" 47 touch "$keys_file"
48 chmod 600 "$keys_file" 48 chmod 600 "$keys_file"
49 chown -R "$user:$group" "$ssh_dir" 49 chown -R "$user:$group" "$ssh_dir"
50 50
51 for key in $(_get_metadata meta-data/public-keys/); do 51 for key in $(_get_metadata meta-data/public-keys/); do
52 _get_metadata "meta-data/public-keys/${key%=*}/openssh-key/" >> "$keys_file" 52 _get_metadata "meta-data/public-keys/${key%=*}/openssh-key/" >> "$keys_file"
53 done 53 done
54} 54}
55 55
56_run_userdata() { 56_run_userdata() {
57 user_data=$(_get_metadata user-data) 57 user_data=$(_get_metadata user-data)
58 if echo "$user_data" | grep -q '^#!/'; then 58 if echo "$user_data" | grep -q '^#!/'; then
59 echo "$user_data" > /var/lib/cloud/user-data.sh 59 echo "$user_data" > /var/lib/cloud/user-data.sh
60 chmod +x /var/lib/cloud/user-data.sh 60 chmod +x /var/lib/cloud/user-data.sh
61 /var/lib/cloud/user-data.sh 2>&1 | tee /var/log/cloud-bootstrap.log 61 /var/lib/cloud/user-data.sh 2>&1 | tee /var/log/cloud-bootstrap.log
62 echo "User Data Script Exit Status: $?" 62 echo "User Data Script Exit Status: $?"
63 fi 63 fi
64} 64}
65 65
66_resize_root_partition() { 66_resize_root_partition() {
67 resize2fs "$(mountpoint -n / | cut -d' ' -f1)" 67 resize2fs "$(mountpoint -n / | cut -d' ' -f1)"
68} 68}
69 69
70_disable_password() { 70_disable_password() {
71 echo "$1:*" | chpasswd -e 71 echo "$1:*" | chpasswd -e
72} 72}
73 73
74start() { 74start() {
75 # Don't bootstrap if the host has already been bootstrapped 75 # Don't bootstrap if the host has already been bootstrapped
76 [ -f "/var/lib/cloud/.bootstrap-complete" ] && return 0 76 [ -f "/var/lib/cloud/.bootstrap-complete" ] && return 0
77 77
78 [ -d "/var/lib/cloud" ] || mkdir -p /var/lib/cloud 78 [ -d "/var/lib/cloud" ] || mkdir -p /var/lib/cloud
79 79
80 ebegin "Disabling root password"; _disable_password root; eend $? 80 ebegin "Disabling root password"; _disable_password root; eend $?
81 ebegin "Disabling $EC2_USER password"; _disable_password "$EC2_USER"; eend $? 81 ebegin "Disabling $EC2_USER password"; _disable_password "$EC2_USER"; eend $?
82 ebegin "Resizing root partition"; _resize_root_partition; eend $? 82 ebegin "Resizing root partition"; _resize_root_partition; eend $?
83 ebegin "Setting ec2 hostname"; _update_hostname; eend $? 83 ebegin "Setting ec2 hostname"; _update_hostname; eend $?
84 ebegin "Setting ec2 user ssh keys"; _set_ssh_keys "$EC2_USER"; eend $? 84 ebegin "Setting ec2 user ssh keys"; _set_ssh_keys "$EC2_USER"; eend $?
85 ebegin "Running ec2 user data script"; _run_userdata; eend $? 85 ebegin "Running ec2 user data script"; _run_userdata; eend $?
86 86
87 touch "/var/lib/cloud/.bootstrap-complete" 87 touch "/var/lib/cloud/.bootstrap-complete"
88} 88}