forked from otakup0pe/awsswitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsswitch.sh
executable file
·119 lines (109 loc) · 3.73 KB
/
awsswitch.sh
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env bash
function problems {
echo "ERROR $1"
exit 1
}
function usage {
>&2 echo "NOPE"
exit 1
}
if [ "$AWSSWITCH_CONFIG" != "awscli" ] && [ "$AWSSWITCH_KEYS" == "" ] ; then
problems "AWSSWITCH_KEYS is not defined"
exit 1
fi
AWSSWITCH_CURRENT="${HOME}/.awsaccount"
if [ "$AWS_AUTO_SCALING_HOME" != "" ] ; then
AWS_AUTOSCALE_CREDENTIAL_FILE=${AWS_AUTO_SCALING_HOME}/creds
fi
function aws_list {
grep -e '^#[^ ]' "$AWSSWITCH_KEYS" | cut -c 2-
}
function aws_use {
NAME="$1"
if [ -z "$TMPDIR" ] ; then
T="/tmp/awsswitch${RANDOM}"
else
T="${TMPDIR}/awsswitch${RANDOM}"
fi
if [ "$AWSSWITCH_CONFIG" == "awscli" ] ; then
REGION=$(grep -A 1 -E "^\[profile ${NAME}\]$" "${HOME}/.aws/config" 2> /dev/null | tail -n 1 | cut -f 2 -d '=')
# default region if not in config file.
if [ -z "$REGION" ]; then
REGION="us-east-1"
fi
KEY=$(grep -A 2 -E "^\[${NAME}\]$" "${HOME}/.aws/credentials" 2> /dev/null | tail -n 2 | head -n 1 | cut -f 2 -d '=')
SECRET=$(grep -A 2 -E "^\[${NAME}\]$" "${HOME}/.aws/credentials" 2> /dev/null | tail -n 1 | cut -f 2 -d '=')
if [ -z "$REGION" ] || \
[ -z "$KEY" ] || \
[ -z "$SECRET" ] ; then
problems "awsaccount not found"
fi
cat <<EOF > "$T"
#${NAME}
- id: "${KEY}"
secret: "${SECRET}"
region: "${REGION}"
EOF
mv "$T" "$AWSSWITCH_CURRENT" ; chmod 0600 "$AWSSWITCH_CURRENT"
else
if grep -A 3 -e "^#${NAME}$" "$AWSSWITCH_KEYS" &> "$T" ; then
mv "$T" "$AWSSWITCH_CURRENT" ; chmod 0600 "$AWSSWITCH_CURRENT"
else
rm -f "$T"
problems "awsaccount not found"
fi
fi
}
function aws_eval {
if [ -e "$AWSSWITCH_CURRENT" ] ; then
REGION="$(tail -n 1 "$AWSSWITCH_CURRENT" | cut -f 2 -d ':' | sed -e 's! !!g; s!\"!!g')"
KEY="$(tail -n 3 "$AWSSWITCH_CURRENT" | head -n 1 | cut -f 2 -d ':' | sed -e 's! !!g; s!\"!!g')"
SECRET="$(tail -n 2 "$AWSSWITCH_CURRENT" | head -n 1 | cut -f 2 -d ':' | sed -e 's! !!g; s!\"!!g')"
if [ "$AWS_SECRET_KEY" != "$SECRET" ] || [ -z "$AWS_DEFAULT_REGION" ] ; then
echo "export AWS_DEFAULT_REGION=$REGION"
fi
echo "export AWS_ACCOUNT=$(head -n 1 "$AWSSWITCH_CURRENT" | cut -f 2 -d '#')"
echo "export AWS_ACCESS_KEY_ID=$KEY"
echo "export AWS_SECRET_ACCESS_KEY=$SECRET"
echo "export AWS_ACCESS_KEY=$KEY"
echo "export AWS_SECRET_KEY=$SECRET"
echo "export EC2_REGION=$AWS_DEFAULT_REGION"
if [ ! -z "$AWS_AUTOSCALE_CREDENTIAL_FILE" ] ; then
echo "AWSAccessKeyId=$KEY" > "$AWS_AUTOSCALE_CREDENTIAL_FILE"
echo "AWSSecretKey=$SECRET" >> "$AWS_AUTOSCALE_CREDENTIAL_FILE"
chmod 600 "$AWS_AUTOSCALE_CREDENTIAL_FILE"
fi
if [ "$AWSSWITCH_S3CFG" == "true" ] ; then
echo "[default]" > "${HOME}/.s3cfg"
echo "access_key = ${KEY}" >> "${HOME}/.s3cfg"
echo "secret_key = ${SECRET}" >> "${HOME}/.s3cfg"
chmod 600 "${HOME}/.s3cfg"
fi
if [ "$AWSSWITCH_FOG" == "true" ] ; then
echo "default:" > "${HOME}/.fog"
echo " aws_access_key_id: ${KEY}" >> "${HOME}/.fog"
echo " aws_secret_access_key: ${SECRET}" >> "${HOME}/.fog"
chmod 600 "${HOME}/.fog"
fi
else
echo "export AWS_ACCOUNT=none"
echo "export AWS_DEFAULT_REGION=\"\""
fi
}
if [ $# == 2 ] ; then
if [ "$1" == "use" ] ; then
aws_use "$2"
else
usage
fi
elif [ $# == 1 ] ; then
if [ "$1" == "eval" ] ; then
aws_eval
elif [ "$1" == "list" ] ; then
aws_list
else
usage
fi
else
usage
fi