-
Notifications
You must be signed in to change notification settings - Fork 2
/
envexport
executable file
·85 lines (76 loc) · 1.75 KB
/
envexport
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
#!/bin/bash
declare -a vars=(
AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY
AWS_DEFAULT_REGION
EC2_USER_ID S3_BUCKET
EC2_PRIVATE_KEY EC2_CERT
)
declare -a vars_missing=()
declare -a files=("${EC2_CERT}" "${EC2_PRIVATE_KEY}")
declare -a files_missing
declare var
declare file
function echo_err()
{
echo "${*}" 1>&2
}
function print_file_contents_cmd()
{
local file_path="${1}"
echo "cat << EOF > \"${file_path}\""
cat "${file_path}"
echo EOF
}
# Variable validation.
for var in ${vars[*]}
do
if [ -z "${!var}" ]
then
vars_missing+=(${var})
fi
done
if [ ${#vars_missing[*]} -gt 0 ]
then
echo "The following variables were unset:"
for var in ${vars_missing[*]}
do
echo_err " ${var}"
done
exit 1
fi
# Certificate file validation.
for file in ${files[*]}
do
if [ ! -f "${file}" ]
then
files_missing+=(${file})
fi
done
if [ ${#files_missing[*]} -gt 0 ]
then
echo "The following files were missing:"
for var in ${files_missing[*]}
do
echo_err " ${var}"
done
exit 1
fi
# Print commands to import a valid setup for euca2ools.
{
for var in ${vars[*]}
do
echo "export ${var}=\"${!var}\"" | sed "s#${HOME}#\${HOME}#g"
done
echo
if [ "$(dirname ${EC2_CERT})" = "$(dirname ${EC2_PRIVATE_KEY})" ]
then
echo "mkdir -p -m 0700 \"$(dirname ${EC2_CERT})\""
else
echo "mkdir -p -m 0700 \"$(dirname ${EC2_CERT})\" \"$(dirname ${EC2_PRIVATE_KEY})\""
fi
echo "touch \"${EC2_CERT}\" \"${EC2_PRIVATE_KEY}\""
echo "chmod 0600 \"${EC2_CERT}\" \"${EC2_PRIVATE_KEY}\""
print_file_contents_cmd "${EC2_CERT}"
print_file_contents_cmd "${EC2_PRIVATE_KEY}"
echo "history -c 0"
} | sed "s#${HOME}#\${HOME}#g"