-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstartup.sh
More file actions
104 lines (93 loc) · 3.19 KB
/
Copy pathstartup.sh
File metadata and controls
104 lines (93 loc) · 3.19 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
#!/bin/bash
#
# This script figures out a hostname based on the value in the role file.
# E.g, "someapp_webserver" would result in the hostname someapp-webserver-xxx
# where "xxx" is the next available number. The hostname is set locally and
# a CNAME record in Route53 is created.
#
# The FQDN becomes HOSTNAME . ENVIRONMENT . DOMAIN
# (e.g. someapp-webserver-000.prod.example.com)
#
# The environment portion is taken from the puppet configuration file. "Prod"
# is used if the environment is "production" (puppet's default).
#
# Once the hostname is set, the puppet service is restarted so that the certs
# are generated for the correct hostname.
# Finally the hostname is set on the "Name" tag of the ec2 instance
#
# Dependencies:
# This script assumes puppet is configured and running, and that the following
# packages are installed:
# * route53-cli
# * aws cli
# * ec2-metadata
#
# Date: 2013-12-26
# Author: Tyler Stroud <tyler@tylerstroud.com>
# BEGIN CONFIGURATION
# Set these variables accordingly
HOSTED_ZONE_ID=
DOMAIN=
AWS_REGION=us-east-1
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
ROLE_FILE=/etc/role
EC2_METADATA=/opt/aws/bin/ec2-metadata
AWS=/usr/bin/aws
ROUTE53=/usr/bin/route53
HOSTNAME_CMD=/bin/hostname
LOGGER=/usr/bin/logger
CAT=/bin/cat
SED=/bin/sed
AWK=/bin/awk
PRINTF=/usr/bin/printf
CURL=/usr/bin/curl
# END CONFIGURATION
export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY
# Set environment value form the puppet config
ENV=$(awk -F "=" '/^environment/ {print $2}' /etc/puppet/puppet.conf | sed 's/[\t ]*//g')
# Use "prod" if env is "production" or empty
if [ "$ENV" == "production" ] || [ -z "$ENV" ]; then
ENV=prod
fi
# TODO: Can we populate this via facter?
# Exit and do nothing if SYSTEM_ROLE is empty
SYSTEM_ROLE=`[ -f $ROLE_FILE ] && $CAT $ROLE_FILE`
if [ -z $SYSTEM_ROLE ]; then
exit 0
fi
# The system role shold be in the form of application_role (e.g., someapp_webserver, someapp_mongod, etc)
app=${SYSTEM_ROLE%_*}
role=${SYSTEM_ROLE#*_}
hosts=`$ROUTE53 get $HOSTED_ZONE_ID | $AWK -v pattern=^$app-$role.*$ENV.$DOMAIN '$0~pattern { print $1 }'`
IFS=$'\n' sorted=($(sort <<<"${hosts[*]}"))
hosts=($hosts)
ipv4=`$CURL -fs http://169.254.169.254/latest/meta-data/public-ipv4`
i=0
while [ true ]; do
num=`$PRINTF "%03d" $i`
hostname="$app-$role-$num.$ENV"
fqdn="$hostname.$DOMAIN"
if [ "${hosts[$i]}" != "$fqdn." ]; then
$SED -i "s/^HOSTNAME=.*$/HOSTNAME=$fqdn/" /etc/sysconfig/network
$HOSTNAME_CMD $fqdn
# Add fqdn to hosts file
$CAT<<EOF > /etc/hosts
# This file is automatically generated by /usr/sbin/onboot.sh script
127.0.0.1 localhost
$ipv4 $fqdn $hostname
EOF
$LOGGER "Setting hostname: $fqdn"
$LOGGER "Creating CNAME record in Route53"
ec2_public_hostname=`$EC2_METADATA -p | $SED 's/public-hostname: //'`
$ROUTE53 add_record $HOSTED_ZONE_ID $fqdn CNAME $ec2_public_hostname 30 > /tmp/create-route53-cname.out
# Restart puppet after hostname change
/sbin/service puppet restart
break
fi
let i+=1
done
# Finaly, set hostname as "Name" tag
instance_id=`$EC2_METADATA -i | awk '{ print $2 }'`
aws ec2 create-tags --region=$AWS_REGION --resources=$instance_id --tags Key=Name,Value=$hostname