-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpm.linux
executable file
·377 lines (322 loc) · 7.99 KB
/
hpm.linux
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/bin/bash
platform='unknown'
PROTO="https"
HCORP_RELEASE="releases.hashicorp.com"
HCORP_DIR="${HOME}/.hpm/hashicorp"
HCORP_PRODUCTS=("terraform" "vault" "consul" "packer" "nomad" "serf" "sentinel" "vagrant")
PACKAGED_PRODUCTS=("vagrant")
DEBUG=0
SHOW_VERSION=1
TIMEOUT=10
bold=$(tput bold)
normal=$(tput sgr0)
function get_os() {
case "${OSTYPE}" in
solaris*) echo "solaris" ;;
darwin*) echo "osx" ;;
linux*) echo "linux" ;;
bsd*) echo "freebsd" ;;
msys*) echo "windows" ;;
*) echo "unknown: ${OSTYPE}" ;;
esac
}
function get_arch() {
arch=$(uname -m)
case "${arch}" in
x86_64*) echo "amd64" ;;
x86_32*) echo "x86" ;;
*) echo "unknown: ${arch}" ;;
esac
}
function test_depends() {
local current_os=${1}
case "${current_os}" in
solaris*) echo "solaris todo" ;;
darwin*) echo "install chtf formula with hombrew : brew instaff chtf"; exit 0 ;;
linux*)
if [[ ! -x /usr/bin/unzip ]]; then
echo "install unzip"
exit 65
fi
if [[ ! -x /usr/bin/wget ]]; then
echo "install wget"
exit 65
fi
;;
bsd*) echo "freebsd todo" ;;
msys*) echo "windows is not supported." && exit 666 ;;
*) echo "unknown: ${current_os}" ;;
esac
}
function list_installed_releases() {
local p=${1}
local c=${2}
if [[ -d ${HCORP_DIR}/${p} ]]
then
for rel in `find ${HCORP_DIR}/${p} -mindepth 1 -maxdepth 1 -type d|sort --version-sort`
do
release=$(echo ${rel} | awk -F\/ '{print $NF}')
if [[ "${release}" == "${c}" ]]
then
echo " * ${release}"
else
echo " ${release}"
fi
done
fi
}
function get_activ_release() {
local p=${1}
local activ=""
if [[ -L "${HOME}/bin/${p}" ]]
then
activ=$(ls -l ${HOME}/bin/${p} |awk '{print $NF}'|awk -F\/ '{print $5}')
fi
echo ${activ}
}
function check_release_is_installed() {
local p=${1}
local r=${2}
if [[ " ${PACKAGED_PRODUCTS[@]} " =~ " ${p} " ]]
then
check_release_is_installed_from_package ${p} ${r}
else
check_release_is_installed_from_archive ${p} ${r}
fi
}
function check_release_is_installed_from_archive() {
local p=${1}
local r=${2}
if [[ ! -x ${HCORP_DIR}/${p}/${r}/${p} ]]
then
echo "0"
else
echo "1"
fi
}
function check_release_is_installed_from_package() {
local d=$(get_distribution)
case "${d}" in
debian|ubuntu)
#dpkg -l vagrant|grep vagrant|awk '{print $3}'
res=$(dpkg -l ${p}|grep -c ${p})
;;
redhat|centos|fedora)
res=$(rpm -qa|grep -c ${p})
;;
esac
echo ${res}
}
function get_releases_url() {
local p=${1}
local releases_url=$(printf "%s://%s/%s/%s" ${PROTO} ${HCORP_RELEASE} ${p})
echo ${releases_url}
}
function get_archive_url() {
local p=${1}
local r=${2}
local z=${3}
local archive_url=$(printf "%s://%s/%s/%s/%s" ${PROTO} ${HCORP_RELEASE} ${p} ${r} ${z})
echo ${archive_url}
}
# https://gist.github.com/hrwgc/7455343
#if [[ `wget -S --spider ${url} 2>&1 | grep 'HTTP/1.1 200 OK'` ]]; then echo "true"; else echo "false"; fi
function validate_url() {
local url=${1}
if [[ ${DEBUG} -eq "1" ]]; then
echo "wget --no-hsts --timeout=${TIMEOUT} --server-response --spider ${url} 2>&1|grep -c 'HTTP/1.1 200 OK'"
fi
local valid=$(wget --no-hsts --timeout=${TIMEOUT} --server-response --spider ${url} 2>&1|grep -c 'HTTP/1.1 200 OK')
if [[ ${valid} -eq "0" ]]; then
echo "This release is not available."
exit 0
fi
}
function get_distribution() {
distrib=$(lsb_release -i -s|tr '[:upper:]' '[:lower:]')
echo ${distrib}
}
function get_archive_extension() {
local p=${1}
local d=$(get_distribution)
if [[ ! " ${PACKAGED_PRODUCTS[@]} " =~ " ${p} " ]]
then
echo "zip"
else
case "${d}" in
debian*) echo "deb" ;;
ubuntu*) echo "deb" ;;
redhat*) echo "rpm" ;;
centos*) echo "rpm" ;;
fedora*) echo "rpm" ;;
*) echo "tar.xz" ;;
esac
fi
}
function get_archive_name() {
local p=${1}
local r=${2}
local o=${3}
local a=${4}
local e=${5}
if [[ " ${PACKAGED_PRODUCTS[@]} " =~ " ${p} " ]]
then
if [[ ${a} -eq "amd64" ]]
then
na="x86_64"
else
na="i686"
fi
local archive_name=$(printf "%s_%s_%s.%s" ${p} ${r} ${na} ${e})
else
local archive_name=$(printf "%s_%s_%s_%s.%s" ${p} ${r} ${o} ${a} ${e})
fi
echo ${archive_name}
}
function wget_archive() {
local url=${1}
local output=${2}
if [[ ${DEBUG} -eq "1" ]]; then
#echo "wget --no-hsts --quiet ${url} --output-document=/tmp/${output}"
echo "wget --show-progress --progress=bar --no-hsts ${url} --output-document=/tmp/${output}"
fi
#wget --no-hsts --quiet ${url} --output-document=/tmp/${output}
wget --show-progress --progress=bar --no-hsts ${url} --output-document=/tmp/${output}
}
function get_release_folder() {
local p=${1}
local r=${2}
local folder=$(printf "%s/%s/%s" ${HCORP_DIR} ${p} ${r})
echo ${folder}
}
function create_release_folder() {
local f=${1}
mkdir -p ${f}
}
function unzip_archive() {
local z=${1}
local d=${2}
if [[ ${DEBUG} -eq "1" ]]; then
echo "unzip -qq /tmp/${z} -d ${d}/"
echo "rm -f /tmp/${z}"
fi
unzip -qq /tmp/${z} -d ${d}/
rm -f /tmp/${z}
}
function install_package() {
local d=${1}
local p=${2}
case "${d}" in
debian|ubuntu)
dpkg -i /tmp/${p}
apt-get -f install
;;
redhat|centos|fedora)
rpm -ivh /tmp/${p}
;;
esac
}
function create_link() {
local p=${1}
local d=${2}
mkdir -p /${HOME}/bin
rm -f ${HOME}/bin/${p}
ln -s ${d}/${p} ${HOME}/bin/${p}
}
function run_version() {
local p=${1}
${p} --version
}
function show_help() {
echo ""
echo "${bold}Hashicorp Product Manager${normal} aka hpm"
echo ""
echo "Usage :"
echo ""
echo "hpm <product> [release]"
echo ""
echo "Where ${bold}product${normal} can be :"
for p in "${HCORP_PRODUCTS[@]}"
do
echo " - ${p}"
done
echo ""
echo "And ${bold}release${normal} the version tou want to be activated"
echo ""
}
if [ $# -eq 0 ]
then
show_help
exit 0
fi
if [ $# -eq 1 ]
then
product=${1}
distribution=$(lsb_release -i -s|tr '[:upper:]' '[:lower:]')
if [[ ! " ${HCORP_PRODUCTS[@]} " =~ " ${product} " ]]
then
echo "Product ${product} not found or not supporeted. Sorry."
exit 100
fi
active_release=$(get_activ_release ${product})
list_installed_releases ${product} ${active_release}
url_releases=$(get_releases_url ${product} ${release})
echo ""
echo "To get avaliable releases check this link :"
echo " - ${url_releases}"
echo ""
exit 0
fi
product=${1}
release=${2}
current_os=$(get_os)
current_arch=$(get_arch)
if [[ ${DEBUG} -eq "1" ]]; then
echo ${current_os}
echo ${current_arch}
fi
test_depends ${current_os}
distrib=$(get_distribution)
archive_ext=$(get_archive_extension ${product})
if [[ ${DEBUG} -eq "1" ]]; then
echo ${distrib}
echo ${archive_ext}
fi
if [[ ! " ${PACKAGED_PRODUCTS[@]} " =~ " ${product} " ]]
then
release_folder=$(get_release_folder ${product} ${release})
else
release_folder=""
fi
release_is_installed=$(check_release_is_installed ${product} ${release})
if [[ ${DEBUG} -eq "1" ]]; then
echo ${release_folder}
echo ${release_is_installed}
fi
if [[ "${release_is_installed}" -ne "1" ]]
then
archive_name=$(get_archive_name ${product} ${release} ${current_os} ${current_arch} ${archive_ext})
archive=$(get_archive_url ${product} ${release} ${archive_name})
url_exists=$(validate_url ${archive})
if [[ ${DEBUG} -eq "1" ]]; then
echo ${archive_name}
echo ${archive}
echo ${url_exists}
fi
wget_archive ${archive} ${archive_name}
if [[ " ${PACKAGED_PRODUCTS[@]} " =~ " ${product} " ]]
then
install_package ${distrib} ${archive_name}
else
create_release_folder ${release_folder}
unzip_archive ${archive_name} ${release_folder}
fi
fi
if [[ ! " ${PACKAGED_PRODUCTS[@]} " =~ " ${product} " ]]
then
create_link ${product} ${release_folder}
fi
if [[ ${SHOW_VERSION} -eq "1" ]]; then
run_version ${product}
fi
exit 0