@@ -8,64 +8,52 @@ source "${DOCK_INIT_BASE}/lib/util/halter.sh"
88# @author Ryan Sandor Richards
99# @module aws
1010
11- # Backoff routine that attempts to fetch the dock's org id from EC2 tags
12- aws::fetch_org_id_from_tags () {
13- local attempt=${1}
11+ # get aws creds for these scripts...
12+ aws::get_aws_creds () {
13+ # Generate the org-tag fetching script
14+ rollbar::fatal_trap \
15+ " Dock-Init: Failed to Render Org Script" \
16+ " Consule-Template was unable to realize the given template."
1417
15- log::info ' Attempting to get org id...'
16- data=' {"attempt":' " ${attempt} " ' }'
18+ ORG_SCRIPT=$DOCK_INIT_BASE /util/get-aws-creds.sh
19+
20+ local config=" $DOCK_INIT_BASE /consul-resources/template-config.hcl"
21+ local template=" $DOCK_INIT_BASE "
22+ template+=" /consul-resources/templates/get-aws-creds.sh.ctmpl:$ORG_SCRIPT "
23+
24+ consul-template -config=" ${config} " -once -template=" ${template} "
1725
18- rollbar::warning_trap \
19- " Dock-Init: Cannot Fetch Org" \
20- " Attempting to get the Org Tag from AWS and failing." \
21- " $data "
22- ORG_ID=$( bash " $ORG_SCRIPT " )
23- log::trace " Script Output: $ORG_ID "
2426 rollbar::clear_trap
27+ # give amazon a chance to get the auth
28+ sleep 5
2529
26- if [[ " $ORG_ID " != " " ]]; then
27- # Assume first value in host_tags comma separated list is org ID...
28- ORG_ID=$( echo " $ORG_ID " | cut -d, -f 1)
29- export ORG_ID
30- return 0
31- else
32- # report the attempt to rollbar, since we don't want this to always fail
33- rollbar::report_warning \
34- " Dock-Init: Failed to Fetch Org" \
35- " Org Script returned an empty string. Retrying."
36- return 1
37- fi
30+ source " ${DOCK_INIT_BASE} /util/get-aws-creds.sh"
3831}
3932
4033# Fetches the org tags from EC2 and sets it to the `ORG_ID` environment variable
41- aws::get_org_id () {
34+ aws::get_org_ids () {
4235 log::info " Setting Github Org ID"
4336
4437 # Generate the org-tag fetching script
4538 rollbar::fatal_trap \
4639 " Dock-Init: Failed to Render Org Script" \
4740 " Consule-Template was unable to realize the given template."
4841 if [ -z ${AWS_ACCESS_KEY+x} ] || [ -z ${AWS_SECRET_KEY+x} ]; then
49- ORG_SCRIPT=$DOCK_INIT_BASE /util/get-org-id.sh
50-
51- local config=" $DOCK_INIT_BASE /consul-resources/template-config.hcl"
52- local template=" $DOCK_INIT_BASE "
53- template+=" /consul-resources/templates/get-org-tag.sh.ctmpl:$ORG_SCRIPT "
54-
55- consul-template -config=" ${config} " -once -template=" ${template} "
42+ backoff aws::get_aws_creds
43+ fi
5644
57- rollbar::clear_trap
45+ EC2_HOME=/usr/local/ec2
46+ export EC2_HOME
5847
59- # give amazon a chance to get the auth
60- sleep 5
48+ JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/jre
49+ export JAVA_HOME
6150
62- # Attempt to fetch the org id from the tags via the fetch script
63- backoff aws::fetch_org_id_from_tags
64- else
65- log::info " Taking aws creds from system"
66- backoff aws::get_org_id_onprem
67- fi
51+ export INSTANCE_ID=$( ec2-metadata -i | awk ' {print $2}' )
52+ # Note: this only works for us-.{4}-\d
53+ export REGION=$( ec2-metadata --availability-zone | awk ' { where = match($2, /us\-.+\-[1|2]/); print substr($2, where, 9); }' )
6854
55+ backoff aws::fetch_org_id
56+ backoff aws::fetch_poppa_id
6957 if [[ " $ORG_ID " == " " ]]; then
7058 # this will print an error, so that's good
7159 rollbar::report_error \
@@ -76,9 +64,10 @@ aws::get_org_id() {
7664 fi
7765
7866 log::info " Got Org ID: $ORG_ID "
67+ log::info " Got Poppa ID: $POPPA_ID "
7968}
8069
81- aws::get_org_id_onprem () {
70+ aws::fetch_org_id () {
8271 local attempt=${1}
8372 log::info ' Attempting to get org id on prem'
8473 data=' {"attempt":' " ${attempt} " ' }'
@@ -88,25 +77,35 @@ aws::get_org_id_onprem() {
8877 " Attempting to get the Org Tag from AWS and failing." \
8978 " $data "
9079
91- EC2_HOME=/usr/local/ec2
92- export EC2_HOME
80+ ORG_ID=$( bash /usr/local/ec2/bin/ec2-describe-tags \
81+ --aws-access-key=" ${AWS_ACCESS_KEY} " \
82+ --aws-secret-key=" ${AWS_SECRET_KEY} " \
83+ --filter " resource-type=instance" \
84+ --filter " resource-id=${INSTANCE_ID} " \
85+ --filter " key=org" \
86+ --region " ${REGION} " \
87+ | awk ' {print $5}' )
9388
94- JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/jre
95- export JAVA_HOME
89+ export ORG_ID
90+ }
9691
97- local instance_id=$( ec2-metadata -i | awk ' {print $2}' )
92+ # Fetches the poppa tags from EC2 and sets it to the `POPPA_ID` environment variable
93+ aws::fetch_poppa_id () {
94+ log::info " Setting Poppa ID"
9895
99- # Note: this only works for us-.{4}-\d
100- local region=$( ec2-metadata --availability-zone | awk ' { where = match($2, /us\-.+\-[1|2]/); print substr($2, where, 9); }' )
96+ # Generate the org-tag fetching script
97+ rollbar::fatal_trap \
98+ " Dock-Init: Failed to Render Org Script" \
99+ " Consule-Template was unable to realize the given template."
101100
102- ORG_ID =$( bash /usr/local/ec2/bin/ec2-describe-tags \
101+ POPPA_ID =$( bash /usr/local/ec2/bin/ec2-describe-tags \
103102 --aws-access-key=" ${AWS_ACCESS_KEY} " \
104103 --aws-secret-key=" ${AWS_SECRET_KEY} " \
105104 --filter " resource-type=instance" \
106- --filter " resource-id=${instance_id } " \
107- --filter " key=org" \
108- --region " ${region } " \
105+ --filter " resource-id=${INSTANCE_ID } " \
106+ --filter " key=runnable- org-id " \
107+ --region " ${REGION } " \
109108 | awk ' {print $5}' )
110109
111- export ORG_ID
110+ export POPPA_ID
112111}
0 commit comments