Created
August 2, 2017 00:46
-
-
Save brodygov/620cc0405be5073d7aff7f628e4336c1 to your computer and use it in GitHub Desktop.
auto-set-ec2-hostname chef recipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Cookbook Name::ubuntu_hardened_ami | |
# Recipe::hostname | |
# Script to set hostname and /etc/hosts | |
template '/usr/local/bin/auto-set-ec2-hostname' do | |
source 'auto-set-ec2-hostname.erb' | |
owner 'root' | |
group 'root' | |
mode '0755' | |
end | |
case node[:platform_version] | |
when '16.04', '18.04' | |
# use systemd in newer ubuntu releases | |
# TODO this is not really tested | |
template '/etc/systemd/system/auto-ec2-hostname.service' do | |
source 'auto-ec2-hostname.service.erb' | |
owner 'root' | |
group 'root' | |
mode '0644' | |
end | |
when '14.04' | |
# use sysv init in older ubuntu releases | |
service 'auto-ec2-hostname' do | |
supports :start => true | |
action :nothing | |
end | |
template '/etc/init.d/auto-ec2-hostname' do | |
source 'auto-ec2-hostname.init.d.erb' | |
owner 'root' | |
group 'root' | |
mode '0755' | |
notifies :enable, 'service[auto-ec2-hostname]' | |
notifies :start, 'service[auto-ec2-hostname]' | |
end | |
else | |
raise "Unexpected ubuntu platform_version: #{node[:platform_version].inspect}" | |
end | |
directory '/etc/auto-hostname' do | |
owner 'root' | |
group 'root' | |
mode '0755' | |
end | |
file '/etc/auto-hostname/domain' do | |
content "aws.uscis.dhs.gov\n" | |
owner 'root' | |
group 'root' | |
mode '0644' | |
end | |
file '/etc/auto-hostname/prefix' do | |
content "hardened\n" | |
owner 'root' | |
group 'root' | |
mode '0644' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
### BEGIN INIT INFO | |
# Provides: auto-ec2-hostname | |
# Required-Start: dbus | |
# Required-Stop: | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: | |
# Short-Description: Automatically set EC2 instance hostname | |
# Description: Set the EC2 instance hostname with auto-set-ec2-hostname | |
### END INIT INFO | |
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin | |
# shellcheck disable=SC1091 | |
. /lib/lsb/init-functions | |
do_start() { | |
log_begin_msg "Running auto-set-ec2-hostname" | |
auto-set-ec2-hostname run | |
log_end_msg $? | |
} | |
case "$1" in | |
start) | |
do_start | |
;; | |
stop) | |
# noop | |
;; | |
*) | |
echo "Usage: $0 start" >&2 | |
exit 3 | |
;; | |
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description= *** Automatically set EC2 instance hostname *** | |
After=dbus.service | |
[Service] | |
Type=oneshot | |
RemainAfterExit=yes | |
ExecStart=/usr/local/bin/auto-set-ec2-hostname run | |
[Install] | |
WantedBy=multi-user.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eu | |
# Version 1.0 | |
basename="$(basename "$0")" | |
CONFIG_DIR=/etc/auto-hostname | |
prefix_file="$CONFIG_DIR/prefix" | |
domain_file="$CONFIG_DIR/domain" | |
usage() { | |
cat >&2 <<EOM | |
usage: $basename ACTION [options] | |
ACTION: | |
run set hostname and hosts | |
dryrun show what would be changed without making changes | |
Automatically set instance hostname to include EC2 instance ID. If | |
$prefix_file is present, use it as a prefix for the hostname. If | |
$domain_file is present, use it as the domain for the hostname. | |
When changing the hostname, also overwrite /etc/hosts with a new copy that | |
contains the new hostname (so sudo doesn't complain). | |
The resulting hostname will look like: | |
PREFIX-i-abcd1234.DOMAIN | |
Options: | |
--skip-hosts Don't overwrite /etc/hosts | |
EOM | |
} | |
if [ $# -lt 1 ]; then | |
usage | |
exit 1 | |
fi | |
case "$1" in | |
run) realrun=1 ;; | |
dryrun) realrun= ;; | |
*) | |
usage | |
exit 1 | |
;; | |
esac | |
shift | |
skip_hosts= | |
while [[ $# -gt 0 ]]; do | |
case "$1" in | |
--skip-hosts) | |
skip_hosts=1 | |
;; | |
*) | |
usage | |
exit 1 | |
esac | |
shift | |
done | |
log() { | |
echo >&2 "$*" | |
logger -t "$basename" "$*" | |
} | |
run() { | |
log "+ $*" | |
"$@" | |
} | |
saferun() { | |
if [ -n "$realrun" ]; then | |
log "+ $*" | |
"$@" | |
else | |
log "(DRY RUN) + $*" | |
fi | |
} | |
safelog() { | |
if [ -n "$realrun" ]; then | |
log "$*" | |
else | |
log "(DRY RUN) $*" | |
fi | |
} | |
safelog "Starting up" | |
prefix= | |
domain= | |
if [ -e "$prefix_file" ]; then | |
prefix="$(cat "$prefix_file")" | |
fi | |
if [ -e "$domain_file" ]; then | |
domain="$(cat "$domain_file")" | |
fi | |
if [ -r /var/lib/cloud/data/instance-id ]; then | |
instance_id="$(cat /var/lib/cloud/data/instance-id)" | |
log "Found instance ID $instance_id from /var/lib/cloud/data/instance-id" | |
else | |
instance_id="$(run curl -sSf http://169.254.169.254/latest/meta-data/instance-id)" | |
fi | |
# $prefix-$instance_id.$domain | |
name="$instance_id" | |
if [ -n "$prefix" ]; then | |
name="$prefix-$name" | |
fi | |
if [ -n "$domain" ]; then | |
name="$name.$domain" | |
fi | |
cur_hostname="$(hostname)" | |
if [ "$cur_hostname" = "$name" ]; then | |
log "Hostname is already set to $name" | |
exit | |
fi | |
safelog "Setting hostname to '$name'" | |
saferun hostnamectl set-hostname "$name" | |
if [ -z "$skip_hosts" ] && ! grep "$name" /etc/hosts >/dev/null; then | |
safelog "Replacing /etc/hosts" | |
if [ -n "$realrun" ]; then | |
run cat > /etc/hosts <<EOM | |
# This file was first created by $basename on $(date "+%F %T") | |
127.0.1.1 $name | |
127.0.0.1 localhost | |
# The following lines are desirable for IPv6 capable hosts | |
::1 ip6-localhost ip6-loopback | |
fe00::0 ip6-localnet | |
ff00::0 ip6-mcastprefix | |
ff02::1 ip6-allnodes | |
ff02::2 ip6-allrouters | |
ff02::3 ip6-allhosts | |
EOM | |
fi | |
fi | |
log 'All done' | |
# vim: set ft=sh : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment