@@ -8,64 +8,52 @@ source "${DOCK_INIT_BASE}/lib/util/halter.sh"
8
8
# @author Ryan Sandor Richards
9
9
# @module aws
10
10
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."
14
17
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} "
17
25
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 "
24
26
rollbar::clear_trap
27
+ # give amazon a chance to get the auth
28
+ sleep 5
25
29
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"
38
31
}
39
32
40
33
# 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 () {
42
35
log::info " Setting Github Org ID"
43
36
44
37
# Generate the org-tag fetching script
45
38
rollbar::fatal_trap \
46
39
" Dock-Init: Failed to Render Org Script" \
47
40
" Consule-Template was unable to realize the given template."
48
41
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
56
44
57
- rollbar::clear_trap
45
+ EC2_HOME=/usr/local/ec2
46
+ export EC2_HOME
58
47
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
61
50
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); }' )
68
54
55
+ backoff aws::fetch_org_id
56
+ backoff aws::fetch_poppa_id
69
57
if [[ " $ORG_ID " == " " ]]; then
70
58
# this will print an error, so that's good
71
59
rollbar::report_error \
@@ -76,9 +64,10 @@ aws::get_org_id() {
76
64
fi
77
65
78
66
log::info " Got Org ID: $ORG_ID "
67
+ log::info " Got Poppa ID: $POPPA_ID "
79
68
}
80
69
81
- aws::get_org_id_onprem () {
70
+ aws::fetch_org_id () {
82
71
local attempt=${1}
83
72
log::info ' Attempting to get org id on prem'
84
73
data=' {"attempt":' " ${attempt} " ' }'
@@ -88,25 +77,35 @@ aws::get_org_id_onprem() {
88
77
" Attempting to get the Org Tag from AWS and failing." \
89
78
" $data "
90
79
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}' )
93
88
94
- JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/jre
95
- export JAVA_HOME
89
+ export ORG_ID
90
+ }
96
91
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"
98
95
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."
101
100
102
- ORG_ID =$( bash /usr/local/ec2/bin/ec2-describe-tags \
101
+ POPPA_ID =$( bash /usr/local/ec2/bin/ec2-describe-tags \
103
102
--aws-access-key=" ${AWS_ACCESS_KEY} " \
104
103
--aws-secret-key=" ${AWS_SECRET_KEY} " \
105
104
--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 } " \
109
108
| awk ' {print $5}' )
110
109
111
- export ORG_ID
110
+ export POPPA_ID
112
111
}
0 commit comments