-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathopenstack_creds
executable file
·155 lines (142 loc) · 4.53 KB
/
openstack_creds
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
#!/bin/bash
# Script to make managing OpenStack credentials with password store easy.
# To be used in combination with the functions in bash-functions file
#
# This script provides the following commands:
# * chchreds to change which credentials to load
# * creds to load the existing credentials in a new environment
# * rmcreds to clear the current credentials from your current environment
#
# First you'll need to source the bash-functions file. This is best done from
# within your ~/.bashrc file:
#
# . ~/projects/openstack-bash-creds-helper/bash-functions
#
# Then add any OpenStack credentials files to pass, with a .openrc extension
# for each of your openrc environment files.
#
# The format of the credential files should look something like this:
#
# export OS_AUTH_URL=http://keystone.domain.name:5000/v3/
# export OS_NO_CACHE=true
# export OS_PROJECT_NAME=tenant
# export OS_USERNAME=username
# export OS_PASSWORD=password
#
# And optionally, you could add the os_creds function to your bash prompt to
# give you the currently loaded credentials in your PS1
#
# PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\] \w\[\033[01;33m\]$(os_creds)\[\033[00m\] \$ '
#
# Andy Botting <[email protected]>
CHCREDS="${CHCRED_FILE:-$HOME/.chcred}"
PSDIR="${PASSWORD_STORE_DIR:-$HOME/.password-store/}"
PSDIR="${PSDIR%/}/"
MATCH=".openrc"
SUFFIX=".gpg"
# Colours
BRIGHT=$(tput bold)
RED="$(tput bold)$(tput setaf 1)"
GREEN="$(tput bold)$(tput setaf 2)"
YELLOW="$(tput bold)$(tput setaf 3)"
BLUE="$(tput bold)$(tput setaf 4)"
MAGENTA="$(tput bold)$(tput setaf 5)"
NORMAL=$(tput sgr0)
# colorise the environment in labels
color_env() {
echo "$1" | sed -e "s/\/\(production\|prod\)\//\/${RED}\1${NORMAL}\//g" \
-e "s/\/\(rctest\)\//\/${YELLOW}\1${NORMAL}\//g" \
-e "s/\/\(development\|dev\)\//\/${MAGENTA}\1${NORMAL}\//g"
}
choose() {
VAR=$1
eval DEF=\$"$VAR"
shift 1
CHOICE=""
# Match any given options
case $DEF in
[[:digit:]]*)
opt=1
for x in "$@"; do
if [ "$DEF" = "$opt" ]; then
CHOICE=$x
break
fi
opt=$((opt+1))
done
;;
[[:alpha:]]*)
for x in "$@"; do
if [ "$DEF$MATCH" = "$x" ]; then
CHOICE=$x
break
fi
done
;;
esac
# Show list of choices
if [ -z "$CHOICE" ]; then
[ -f "$CHCREDS" ] && DEF=$(cat "$CHCREDS")
DEF_MATCHED=0
for X in "$@"; do
if [ "$DEF" = "$X" ]; then
DEF_MATCHED=1
break
fi
done
if [ $DEF_MATCHED -eq 0 ]; then
DEF=$1
fi
while [ ! "$CHOICE" ]; do
echo "${GREEN}==> ${NORMAL}${BRIGHT}Choose credential:${NORMAL}"
opt=1
for x in "$@"; do
label=$(color_env "$x")
[ "$x" == "$DEF" ] && label="${label%"$MATCH"} ${GREEN}*${NORMAL}"
printf " ${BRIGHT}%2.d${NORMAL} ${label%"$MATCH"}\n" $opt
eval CHOICE_N$opt="$x"
opt=$((opt+1))
done
echo -n "[${BLUE}${DEF%"$MATCH"}${NORMAL}]: "
read -r ANS
if [ "$ANS" = "" ]; then
CHOICE=$DEF
else
case $ANS in
[[:digit:]]*)
if [ "$ANS" -gt 0 ] && [ "$ANS" -le $# ]; then
eval CHOICE=\$CHOICE_N"$ANS"
fi
;;
esac
fi
done
fi
eval "$VAR"="$CHOICE"
}
choose_fzf() {
printf '%s\n' "$1" | \
sed -e "s/\/\(production\|prod\)\//\/${RED}\1${NORMAL}\//g" \
-e "s/\/\(rctest\)\//\/${YELLOW}\1${NORMAL}\//g" \
-e "s/\/\(development\|dev\)\//\/${MAGENTA}\1${NORMAL}\//g" | \
awk '{printf "%02d\t%s\n", NR, $0;}' | \
fzf --exact --tabstop=2 --tac --ansi
}
creds=$(find -L "$PSDIR" -type f -name "*$MATCH$SUFFIX" | sort | \
sed -e "s,$PSDIR,,g" -e "s,$SUFFIX,,g" -e "s,$MATCH,,g")
# If the required choice was provided via argv
if [ -n "$1" ]; then
cred=$1
else
# If fzf is installed, prefer it over the older method
if builtin type -P fzf &> /dev/null; then
cred=$(choose_fzf "$creds" | awk '{print $NF}') # strip index
else
# shellcheck disable=SC2086
choose cred $creds
fi
fi
[ -z "$cred" ] && exit 1
# Add the .openrc suffix back again
echo "$cred$MATCH" > "$CHCREDS"
exit 0