-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawsenv.sh
160 lines (125 loc) · 3.38 KB
/
awsenv.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/bash
#
# Changing between amazon accounts
#
# This script must be sourced by your ~/.bashrc or something
#
# Marcus Vinicius Fereira ferreira.mv[ at ].gmail.com
# 2012-11
###
### My different profiles
###
[ -z "$AWSENV_PROFILES_DIR" ] && {
echo
echo "AWSENV_PROFILES_DIR is not set. See readme."
echo
return
}
profiles_dir="$AWSENV_PROFILES_DIR"
template_dir="$AWSENV_TEMPLATE_DIR"
###
### Enforcing...
###
[ ! -e ~/.aws ] && {
mkdir ~/.aws && chmod 700 ~/.aws
}
###
### to be used by PS1
###
function __awsenv_ps1() {
if [ "$AWSENV_PROFILE" != "" ]
then
echo "[aws:$AWSENV_PROFILE]"
fi
}
current_profile=$( readlink ~/.aws )
export AWSENV_PROFILE=${current_profile##*/}
###
### Routines
###
function awsenv-ls() {
echo
echo "AWSEnv: Profiles"
echo "----------------"
builtin cd "${profiles_dir}" && find * -type d -prune
echo
}
function awsenv-set() {
profile="$1"
if [ ! -d "${profiles_dir}/${profile}" ]
then
echo
echo "Profile: DOES NOT exist: [${profile}]"
echo
return
fi
###
### credentials
###
ln -nsf "${profiles_dir}/${profile}" ~/.aws && \
export AWSENV_PROFILE="${profile}"
# From my credential-file, all other vars are evaluated when read
export AWS_CREDENTIAL_FILE="$HOME/.aws/aws-credential-file.cfg"
export AWS_ACCESS_KEY_ID=$( awk -F= '/AccessKey/ {print $2}' $AWS_CREDENTIAL_FILE 2>/dev/null || echo 'NOT-FOUND' )
export AWS_SECRET_ACCESS_KEY=$( awk -F= '/SecretKey/ {print $2}' $AWS_CREDENTIAL_FILE 2>/dev/null || echo 'NOT-FOUND' )
# EC2
export EC2_CERT="$(/bin/ls $HOME/.aws/certificate.pem 2>/dev/null || echo 'NOT-FOUND' )"
export EC2_PRIVATE_KEY="$(/bin/ls $HOME/.aws/private-key-in-PCKS8-format.pem 2>/dev/null || echo 'NOT-FOUND' )"
}
function awsenv-generate() {
profile="$1"
if [ -z "${profile}" ]
then
echo
echo "Usage: awsenv-generate profile"
echo
return
fi
if [ ! -d "${profiles_dir}/${profile}" ]
then
echo
echo "Profile: DOES NOT exist: [${profile}]"
echo
return
fi
if [ ! -d "${template_dir}" ]
then
echo
echo "Templates dir: DOES NOT exist: [${profile}]"
echo "See readme."
echo
return
fi
credentials="${profiles_dir}/${profile}/aws-credential-file.cfg"
if [ ! -f "${credentials}" ]
then
echo
echo "Credentials file: DOES NOT exist: [${credentials}]"
echo "You must create it!"
echo
echo "File contents:"
echo " AWSAccessKeyId=<<your-key-here!!>>"
echo " AWSSecretKey=<<your-secret-here!!>>"
echo
return
fi
aws_access_key=$( awk -F= '/AccessKey/ {print $2}' $credentials )
aws_secret_key=$( awk -F= '/SecretKey/ {print $2}' $credentials )
# do it.
echo
echo "Using credentials: [$credentials]"
echo "Using Access_Key: [$aws_access_key]"
echo
builtin cd "${template_dir}"
for f in `find . -type f `
do
file=${f#./*}
echo "Generating: $file"
sed -e "s|__my_access_key__|${aws_access_key}|" \
-e "s|__my_secret_key__|${aws_secret_key}|" \
$file > "${profiles_dir}/${profile}/generated-${file}"
ln -nsf "$HOME/.aws/generated-${file}" $HOME/${file}
done
echo
}
# vim:ft=sh: