-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathansible-aliases.sh
executable file
·143 lines (132 loc) · 2.79 KB
/
ansible-aliases.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
#!/bin/bash
set -eu
set -o pipefail
DOCKER_IMAGE='gdiener/ansible'
DOCKER_TAG='latest'
SSH_KEY_ENV=''
WORKSPACE_ENV=" -v \$(pwd):/workspace/"
EXTRA_ENV=''
REMOVE=false
FILE_NAME=~/.ansible-aliases
FILE_INCLUDE="source ${FILE_NAME}"
COMMANDS=(
'ansible'
'ansible-playbook'
'ansible-vault'
'ansible-galaxy'
'ansible-console'
'ansible-config'
'ansible-doc'
'ansible-inventory'
'ansible-pull'
)
SHELL_CONFIG_FILES=(
~/.kshrc
~/.bashrc
~/.zshrc
~/.bash_profile
)
function error_message {
echo "[!] ${1}, use '--help' flag for more information"
exit 1
}
function message {
echo ">> ${1}"
}
function help_message {
echo
echo "Usage: ansible-aliases [OPTIONS]"
echo
message "Install dockerized Ansible tools aliases"
echo
echo 'Options:'
echo ' -k, --key string The path of the SSH key to use'
echo ' -w, --workspace string The path of the workspace to use (default $(pwd))'
echo ' -t, --tag string The Docker tag to use'
echo ' -e, --env string Set environment variables'
echo ' --remove Uninstall Ansible aliases'
exit 2
}
while [[ $# -gt 0 ]];do
key="${1}"
case ${key} in
-h|--help)
help_message
;;
-k|--key)
if [ -z ${2+x} ]; then
error_message "Missing ${key} value"
fi
SSH_KEY_ENV="-e \"SSH_KEY=\$(cat ${2})\""
shift
shift
;;
-2|--workspace)
if [ -z ${2+x} ]; then
error_message "Missing ${key} value"
fi
WORKSPACE_ENV="-v ${2}:/playbook/"
shift
shift
;;
-t|--tag)
if [ -z ${2+x} ]; then
error_message "Missing ${key} value"
fi
DOCKER_TAG=${2}
shift
shift
;;
-e|--env)
if [ -z ${2+x} ]; then
error_message "Missing ${key} value"
fi
EXTRA_ENV+=" -e \"${2}\""
shift
shift
;;
--remove)
REMOVE=true
shift
;;
*)
error_message "Unrecognized option ${key}"
;;
esac
done
for _FILE in ${SHELL_CONFIG_FILES[@]}; do
if [ -w "${_FILE}" ]; then
if [ "${REMOVE}" = true ]; then
if grep -q "${FILE_INCLUDE}" ${_FILE}; then
sed -i.old "s#${FILE_INCLUDE}##" ${_FILE}
message "Aliases file uninstalled from ${_FILE}"
fi
else
if grep -q "${FILE_INCLUDE}" ${_FILE}; then
message "Aliases file is already installed in ${_FILE}"
else
cp ${_FILE} ${_FILE}.old
echo '' >> ${_FILE}
echo ${FILE_INCLUDE} >> ${_FILE}
message "Aliases file installed in ${_FILE}"
fi
break
fi
fi
done
if [ "${REMOVE}" = true ]; then
if [ -w "${FILE_NAME}" ]; then
rm ${FILE_NAME}
message "Aliases file removed"
fi
exit 0
fi
if [ ! -w "${FILE_NAME}" ]; then
touch ${FILE_NAME}
else
> ${FILE_NAME}
fi
for _COMMAND in ${COMMANDS[@]}; do
echo "alias ${_COMMAND}='docker run ${SSH_KEY_ENV} ${WORKSPACE_ENV} ${EXTRA_ENV} -it ${DOCKER_IMAGE}:${DOCKER_TAG} ${_COMMAND}'" >> ${FILE_NAME}
message "Alias ${_COMMAND} installed"
done