This repository has been archived by the owner on Feb 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.sh
executable file
Β·258 lines (224 loc) Β· 6.82 KB
/
setup.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/sh
set -e
## default variable
MSG_STATUS="0"
avm_dir=""
## Check a command
## $1 binary to check
## $2 report but don't fail
is_installed(){
if command -v "${1}" > /dev/null 2>&1; then
fail="0"
else
fail="1"
fi
if ! [ -z "${2}" ]; then
echo "${fail}"
else
if [ "${fail}" = "1" ]; then msg_exit "Opps '${1}' is not installed or not in your path."; fi
fi
}
tput_installed=$(is_installed "tput" 1)
tput_alternative(){
printf "%s" "${*}"
}
if [ "${tput_installed}" = "1" ]; then
alias tput=tput_alternative
fi
## Crazy printing stuff
##
## Print status (print newline if verbose)
print_status() {
printf "[%s] %s ... " "$(date +%H:%M:%S)" "$*"
if ! [ -z "${AVM_VERBOSE}" ]; then printf "\n"; MSG_STATUS=1; fi
}
# Print a check to complete the status message (ignore if verbose for prety printing)
print_done() {
if [ -z "${AVM_VERBOSE}" ]; then printf "β
\n"; MSG_STATUS="0";fi
}
print_failed() {
if [ -z "${AVM_VERBOSE}" ]; then printf "β \n";fi
}
# Print a warning message
print_warning() {
echo "β οΈ $(tput bold)$(tput setaf 1)$*$(tput sgr0) β οΈ "
}
# Print a verbose message
print_verbose() {
if ! [ -z "${AVM_VERBOSE}" ]; then echo "π» $(tput bold)$*$(tput sgr0)"; fi
}
# Print a red error
print_error() {
printf "$(tput bold)$(tput setaf 1)%s$(tput sgr0)\n" "$@" >&2
}
## Print Error msg and exit
## $1 optional msg
msg_exit() {
if [ "${MSG_STATUS}" = "1" ]; then print_failed; fi
printf "\n"
if ! [ -z "${1}" ]; then
# We know why it failed
print_error "Setup failed π’."
print_error "${1}"
else
print_error "Setup failed π’. You can try the folloiwng"
print_error "1. Running the setup again."
print_error "2. Increase verbosity level by populating 'AVM_VERBOSE=v' supports '', 'v', 'vv' or 'vvv'"
print_error "3. Open an issue and paste the out REMOVE any sensitve data"
fi
exit 99
}
setup_canceled() {
printf "\n"
print_warning "Setup aborted by user π±. You can run it again later."
exit 130
}
# Print a happy green message
setup_done() {
printf "\n%s%sπ π π π Happy Ansibleing%s π π π π\n" "$(tput bold)" "$(tput setaf 2)" "$(tput sgr0)"
}
setup_exit() {
ret="$?"
if [ "${ret}" = "0" ]; then
setup_done
elif [ "${ret}" = "99" ]; then
: # We failed lets not do anything :(
elif [ "${ret}" = "130" ]; then
: # User cancled
else
# error
msg_exit
fi
}
## Setup signal traps
trap setup_exit EXIT
trap setup_canceled INT
setup_verboisty(){
## Setup verbosity could be empty or v, vv or vvv'
## Show only status messages
## v show verbose messages, but mute stdout, stderr
## vv show verbose messages, show stdout, stderr
## vvv show verbose messages, show stdout, stderr and set -x
AVM_VERBOSE="${AVM_VERBOSE-}"
AVM_VERBOSE="$(echo "${AVM_VERBOSE}" | tr '[:upper:]' '[:lower:]')"
if [ "${AVM_VERBOSE}" = "" ] || [ "${AVM_VERBOSE}" = "stdout" ]; then
: # Cool Do nothing
elif [ "${AVM_VERBOSE}" = "v" ]; then
print_warning " verbosity level 1"
elif [ "${AVM_VERBOSE}" = "vv" ]; then
print_warning " verbosity level 2"
elif [ "${AVM_VERBOSE}" = "vvv" ]; then
print_warning " verbosity level 3"
set -x
else
msg_exit "Unknown verbosity ${AVM_VERBOSE}"
fi
}
setup_verboisty
## Run command as a different user if you have SETUP_USER env set
##
RUN_COMMAND_AS() {
if [ "${SETUP_USER}" = "${USER}" ]; then
command_2_run=${*}
else
command_2_run=sudo su "${SETUP_USER}" -c "${*}"
fi
case "${AVM_VERBOSE}" in
'' | 'v')
eval "${command_2_run}" > /dev/null 2>&1
;;
"stdout")
eval "${command_2_run}"
;;
*)
(>&2 print_verbose "Executing ${command_2_run}")
eval "${command_2_run}"
;;
esac
}
## Include a file
##
INCLUDE_FILE(){
print_verbose "Sourcing file '${1}'"
if ! [ -f "${1}" ]; then
msg_exit "Failed to include file ${1}"
fi
# shellcheck disable=SC1090
. "${1}"
}
## Manage git
##
## $1 git repo (required)
## $2 package name (required)
## $3 branch (required)
## $4 return variable name (required)
## $5 submodules upgdate
manage_git(){
git_repo="${1}"
package_name="${2}"
branch="${3}"
submodule="${5}"
#
source_git_dir="${AVM_BASEDIR}/.source_git"
app_git_dir="${source_git_dir}/${package_name}"
RUN_COMMAND_AS "mkdir -p ${source_git_dir}"
if ! [ -d "${app_git_dir}/.git" ]; then
cd "${source_git_dir}" || msg_exit "Failed to cd into '${source_git_dir}'"
print_verbose "Cloning '${git_repo}' to ${app_git_dir}"
RUN_COMMAND_AS "git clone ${git_repo} --recursive"
else
cd "${app_git_dir}" || msg_exit "Failed to cd into '${app_git_dir}'"
RUN_COMMAND_AS "git fetch origin --tags"
fi
# will also run this first run :(
print_verbose "checking out '${branch}' from '${app_git_dir}'"
cd "${app_git_dir}"
RUN_COMMAND_AS "git checkout ${branch}"
if git show-ref --verify "refs/heads/${branch}" > /dev/null 2>&1; then
print_verbose "git pulling from '${app_git_dir}'"
RUN_COMMAND_AS "git pull -q --rebase"
fi
if [ -z "${submodule}" ]; then
print_verbose "git updating submodule for '${git_repo}'"
cd "${source_git_dir}/${package_name}"
RUN_COMMAND_AS "git submodule update --quiet --init --recursive"
fi
# Return path
eval "${4}=${source_git_dir}/${package_name}"
}
# AVM version to install. Supports git releases (default to master)
# if set to "local" will use pwd good for debuging and CI
AVM_VERSION="${AVM_VERSION-master}"
## What user is used for the setup and he's home dir
SETUP_USER="${SETUP_USER-$USER}"
SETUP_USER_HOME="${SETUP_USER_HOME:-$(eval echo "~${SETUP_USER}")}"
print_verbose "Setup SETUP_USER=${SETUP_USER} and SETUP_USER_HOME=${SETUP_USER_HOME}"
## Ignore sudo errors
AVM_IGNORE_SUDO="${AVM_IGNORE_SUDO-0}"
## AVM base dir (default to ~/.avm)
AVM_BASEDIR="${AVM_BASEDIR:-$SETUP_USER_HOME/.avm}"
## Supported types is pip and git. If no type is defined pip will be used
DEFAULT_INSTALL_TYPE="${DEFAULT_INSTALL_TYPE:-pip}"
## Should we force python venv installation with each run
AVM_UPDATE_VENV="${AVM_UPDATE_VENV:-'no'}"
## Ansible bin path it should be something in your path
ANSIBLE_BIN_PATH="${ANSIBLE_BIN_PATH:-/usr/local/bin}"
## We have 2 options depanding on verion
## 1- local used for development and in CI for testing
## 2- Cloning the repo from github then checking the version
avm_dir_setup(){
print_status "Setting AVM version '${AVM_VERSION}' directory"
is_installed "git"
if [ "${AVM_VERSION}" = "local" ]; then
MY_PATH="$(dirname "${0}")" # relative
DIR="$( cd "${MY_PATH}" && pwd )" # absolutized and normalized
avm_dir="${DIR}"
else
manage_git https://github.com/ahelal/avm.git avm "${AVM_VERSION}" avm_dir
fi
print_done
}
avm_dir_setup
# Include Main file
INCLUDE_FILE "${avm_dir}/avm/main.sh"
exit 0