Skip to content

Instantly share code, notes, and snippets.

@AstroTom
Last active July 5, 2018 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AstroTom/8ad2596ac2202290c15f845284de0d2d to your computer and use it in GitHub Desktop.
Save AstroTom/8ad2596ac2202290c15f845284de0d2d to your computer and use it in GitHub Desktop.
Register DNS and variables from TAGS of instance
#! /bin/bash
# Last update 2018-06-21 v0.2
# Author: Tom R.
#
# wishtrip-register-from-tags.sh - Register DNS and variables from TAGS of instance
# 1) get tags into env and then register DNS
# 2) create /etc/rnt.conf with value of RNT_ENV from "Env" tag
# 3) Set the hostname from tag "Hostname"
# 4) Set the DNS in route53 from tags "Host" and "Env"
#
# e.g. assuming the following tags:
# Hostname - bastion
# Env - dev
#
# Creates the DNS:
# bastion.dev.ext.wishtrip.com
# bastion.dev.int.wishtrip.com
#
# Dependancies:
# requires AWS CLI
# requires working outgoing network
# Requires AWS permissions: ec2:read tags, dns create & update
# requires sudo permission (with no passwd) to update hostname
# and create /etc/rnt.conf
#
# Can install as regular script
# # sudo install -D register-from-tags.sh /opt/wishtrip/register-from-tags.sh
#
# Should be installed as startup script if you want the RNT_ENV set early
# # update-rc.d register-from-tags start 01 3 .
#
# debug
exec 1>/tmp/register-from-tags.log
exec 2>>/tmp/register-from-tags.log
set -x
logger -t wishtrip "Starting $0"
#
# Name - get-ec2-tag <Tag_Name>
#
# Returns - value of <Tag_Name>
# - null if tag does not exist or any other error
get-ec2-tag()
{
TAG_NAME=$1
aws ec2 describe-tags --debug --filters "Name=resource-id,Values=$INSTANCE_ID" "Name=key,Values=${TAG_NAME}" --region=$EC2_REGION --output=text | cut -f5
}
# add /usr/local/bin to include AWS tools (when called from init script)
PATH=$PATH:/usr/local/bin
# If AWS not installed, abort
command -v aws >/dev/null 2>&1 || { echo >&2 "$0: aws CLI not installed. Aborting."; logger -t wishtrip "$0: aws CLI not installed. Aborting." exit 1; }
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
EC2_AVAIL_ZONE=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)
EC2_REGION=$(echo $EC2_AVAIL_ZONE | sed 's/[a-z]$//')
#EC2_REGION=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document|grep region|cut -d\" -f4)
[[ -z $EC2_REGION ]] && (echo >&2 "Variable is unset";exit 1)
# make sure network and DNS are up before continuing
# test access to AWS EC2 endpoint
until nc -zw1 ec2.${EC2_REGION}.amazonaws.com 443; do echo "waiting for network"; sleep 1; done
# set RNT_ENV from Env tag
RNT_ENV=$(get-ec2-tag "Env")
if [[ -z $RNT_ENV ]]
then
echo Error: Could not get tag Env
exit 1
fi
# create /etc/rnt.conf
# put the "export" on a seperate line so the file is compatible with systemd EnvironmentFile=
sudo bash -c "echo -e RNT_ENV=$RNT_ENV \\\\nexport RNT_ENV > /etc/rnt.conf"
Host=$(get-ec2-tag "Hostname")
if [[ -z $Host ]]
then
echo Error: Could not get tag Host
exit 1
fi
sudo hostname $Host.$RNT_ENV
sudo bash -c "echo $Host.$RNT_ENV > /etc/hostname"
sed -i 's/127.0.0.1.*/127.0.0.1\t'"localhost $Host.$RNT_ENV"'/g' /etc/hosts
#
# Register DNS
#
# Get the local and public IP Address that is assigned to the instance
#LOCAL_IPV4=$(ec2metadata --local-ipv4)
#PUBLIC_IPV4=$(ec2metadata --public-ipv4)
LOCAL_IPV4=$(curl -s http://instance-data/latest/meta-data/local-ipv4)
PUBLIC_IPV4=$(curl -s http://instance-data/latest/meta-data/public-ipv4)
Domain=wishtrip.com
TTL=60
logger -t wishtrip "$0: About to add DNS: $Host.$RNT_ENV.int.$Domain A $LOCAL_IPV4"
# replace cli53 with aws cli
# Create a new or update the A-Records on Route53 with public and private IP address
#cli53 rrcreate --replace "$Domain" "$Host.$RNT_ENV.int $TTL A $LOCAL_IPV4"
#cli53 rrcreate --replace "$Domain" "$Host.$RNT_ENV.ext $TTL A $PUBLIC_IPV4"
domain-name2id()
{
# Description - converd DNS domain name to AWS Zone ID
# - input 1 - DNS name to convert
# output Zone ID
[[ -z $1 ]] && (echo "$0 missing arg";exit 1)
Domain=$1
aws route53 list-hosted-zones --output text |grep wishtrip.com|cut -f3|cut -d/ -f3
}
Zone=$(domain-name2id $Domain)
# Json template
Json_tpl='
{
"Comment": "Update the A record set",
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "_NAME_._DOMAIN_",
"Type": "A",
"TTL": _TTL_,
"ResourceRecords": [
{
"Value": "_IP_"
}
]
}
}
]
}
'
# Update json template
TTL=60
Json=$Json_tpl
Json="${Json/_DOMAIN_/$Domain}"
Json="${Json/_TTL_/$TTL}"
# Update json template
NAME=$Host.$RNT_ENV.int
IP=$LOCAL_IPV4
Json="${Json/_NAME_/$NAME}"
Json="${Json/_IP_/$IP}"
aws route53 change-resource-record-sets --hosted-zone-id $Zone --change-batch "$Json"
# Register external IP if it exists
if [[ $PUBLIC_IPV4 != *xml* ]]
then
# Update json template
Json=$Json_tpl
Json="${Json/_DOMAIN_/$Domain}"
Json="${Json/_TTL_/$TTL}"
NAME=$Host.$RNT_ENV.ext
IP=$PUBLIC_IPV4
Json="${Json/_NAME_/$NAME}"
Json="${Json/_IP_/$IP}"
logger -t wishtrip "$0: About to add EXT DNS: $NAME$Domain A $LOCAL_IPV4"
aws route53 change-resource-record-sets --hosted-zone-id $Zone --change-batch "$Json"
fi
logger -t wishtrip "Finished $0"
@AstroTom
Copy link
Author

AstroTom commented May 14, 2018

TBD

  • resolve.conf
    Also update /etc/resolve.conf by updated the "search to look at the local subdomain, e.g.
    search dev1.int.wishtrip.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment