@@ -45,22 +45,26 @@ 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+ backoff aws::get_org_id_onprem
67+ fi
6468
6569 if [[ " $ORG_ID " == " " ]]; then
6670 # this will print an error, so that's good
@@ -73,3 +77,36 @@ aws::get_org_id() {
7377
7478 log::info " Got Org ID: $ORG_ID "
7579}
80+
81+ aws::get_org_id_onprem () {
82+ local attempt=${1}
83+ log::info ' Attempting to get org id on prem'
84+ data=' {"attempt":' " ${attempt} " ' }'
85+
86+ rollbar::warning_trap \
87+ " Dock-Init: Cannot Fetch Org" \
88+ " Attempting to get the Org Tag from AWS and failing." \
89+ " $data "
90+
91+ EC2_HOME=/usr/local/ec2
92+ export EC2_HOME
93+
94+ JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/jre
95+ export JAVA_HOME
96+
97+ local instance_id=$( ec2-metadata -i | awk ' {print $2}' )
98+
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); }' )
101+
102+ ORG_ID=$( bash /usr/local/ec2/bin/ec2-describe-tags \
103+ --aws-access-key=" ${AWS_ACCESS_KEY} " \
104+ --aws-secret-key=" ${AWS_SECRET_KEY} " \
105+ --filter " resource-type=instance" \
106+ --filter " resource-id=${instance_id} " \
107+ --filter " key=org" \
108+ --region " ${region} " \
109+ | awk ' {print $5}' )
110+
111+ export ORG_ID
112+ }
0 commit comments