@@ -45,22 +45,27 @@ aws::get_org_id() {
4545 rollbar::fatal_trap \
4646 " Dock-Init: Failed to Render Org Script" \
4747 " Consule-Template was unable to realize the given template."
48+ if [ -z ${AWS_ACCESS_KEY+x} ] || [ -z ${AWS_SECRET_KEY+x} ]; then
49+ ORG_SCRIPT=$DOCK_INIT_BASE /util/get-org-id.sh
4850
49- ORG_SCRIPT=$DOCK_INIT_BASE /util/get-org-id.sh
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 "
5054
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 "
55+ consul-template -config=" ${config} " -once -template=" ${template} "
5456
55- consul-template -config= " ${config} " -once -template= " ${template} "
57+ rollbar::clear_trap
5658
57- rollbar::clear_trap
58-
59- # give amazon a chance to get the auth
60- sleep 5
59+ # give amazon a chance to get the auth
60+ sleep 5
6161
62- # Attempt to fetch the org id from the tags via the fetch script
63- backoff aws::fetch_org_id_from_tags
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+ sleep 5
67+ backoff aws::get_org_id_onprem
68+ fi
6469
6570 if [[ " $ORG_ID " == " " ]]; then
6671 # this will print an error, so that's good
@@ -73,3 +78,36 @@ aws::get_org_id() {
7378
7479 log::info " Got Org ID: $ORG_ID "
7580}
81+
82+ aws::get_org_id_onprem () {
83+ local attempt=${1}
84+ log::info ' Attempting to get org id on prem'
85+ data=' {"attempt":' " ${attempt} " ' }'
86+
87+ rollbar::warning_trap \
88+ " Dock-Init: Cannot Fetch Org" \
89+ " Attempting to get the Org Tag from AWS and failing." \
90+ " $data "
91+
92+ EC2_HOME=/usr/local/ec2
93+ export EC2_HOME
94+
95+ JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/jre
96+ export JAVA_HOME
97+
98+ local instance_id=$( ec2-metadata -i | awk ' {print $2}' )
99+
100+ # Note: this only works for us-.{4}-\d
101+ local region=$( ec2-metadata --availability-zone | awk ' { where = match($2, /us\-.+\-[1|2]/); print substr($2, where, 9); }' )
102+
103+ ORG_ID=$( bash /usr/local/ec2/bin/ec2-describe-tags \
104+ --aws-access-key=" ${AWS_ACCESS_KEY} " \
105+ --aws-secret-key=" ${AWS_SECRET_KEY} " \
106+ --filter " resource-type=instance" \
107+ --filter " resource-id=${instance_id} " \
108+ --filter " key=org" \
109+ --region " ${region} " \
110+ | awk ' {print $5}' )
111+
112+ export ORG_ID
113+ }
0 commit comments