-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch
executable file
·148 lines (131 loc) · 4.09 KB
/
fetch
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
#!/usr/bin/env bash
#################################################
### Fetch Hashi Product By Version from internet
#################################################
#Set Script Name variable
SCRIPT=`basename ${BASH_SOURCE[0]}`
DEFAULT_PLATFORM=$(uname -s -m | tr '[:upper:] ' '[:lower:]_' | sed -e 's/_aarch64$/_arm64/g' -e 's/_x86_64$/_amd64/g')
### # functions
function usage () {
local ec=0
local l_MSG=$1
if [ "${l_MSG}" != "" ]; then
echo "Usage Error: $l_MSG"
echo ''
ec=1
fi
echo "Usage: ${SCRIPT} -p <string>"
echo ' where <string> specifies the product name'
echo ''
echo 'Recognized optional command line arguments'
echo '-v <string> -- specifies the target version. Defaults to latest stable version.'
echo "-a <string> -- specifies the target architecture. Defaults to \`${DEFAULT_PLATFORM}\`"
echo '-e -- download enterprise versions'
echo '-n -- do not create a symlink in /usr/local/bin'
exit 1
}
## TODO: implement latest using `https://releases.hashicorp.com/«product»/index.json`
function getLatest () {
local FILTER="-v"
if [ "${ENT}" == "true" ]; then
FILTER=""
fi
local LATEST_VERSION=$(curl -q -s https://releases.hashicorp.com/$1/ |\
tr '<>' ' ' |\
awk "/href=\\\"\\/$1/{ print \$3}" |\
awk -F"_" '{print $2}' |\
grep ${FILTER} "+ent" |\
head -n 1
)
echo "${LATEST_VERSION}"
}
function maybeCreateSymlink () {
if [ -z "${NOCREATESYMLINK}" ]
then
if [ -L "/usr/local/bin/${PRODUCT}" ]
then
if ! rm -rf "/usr/local/bin/${PRODUCT}"; then
echo "Unable to remove existing symlink. Trying with sudo..."
sudo rm -rf "/usr/local/bin/${PRODUCT}"
fi
fi
if ! ln -s "/opt/${PRODUCT}/bin/${PRODUCT}-v${VERSION}" "/usr/local/bin/${PRODUCT}"; then
echo "Unable to create symlink in /usr/local/bin. Trying with sudo..."
sudo ln -s "/opt/${PRODUCT}/bin/${PRODUCT}-v${VERSION}" "/usr/local/bin/${PRODUCT}"
fi
fi
}
### # starting main
### check number of command line arguments
NUMARGS=$#
if [ $NUMARGS -eq 0 ]; then
usage 'No command line arguments specified'
fi
### Start getopts code ###
# Parse command line flags
# If an option should be followed by an argument, it should be followed by a ":".
# The leading ":" suppresses error messages from
# getopts. This is required to get my unrecognized option code to work.
while getopts :ndep:v:a: FLAG; do
case $FLAG in
p) # set option "p" specifying the product
PRODUCT=$OPTARG
;;
v) # set option "v"
VERSION=$OPTARG
;;
a) # set option "a"
ARCHITECTURE=$OPTARG
;;
n) # set option "n"
NOCREATESYMLINK=0
;;
e) # set option "e"
ENT=true
;;
d) # set option "d"
DRYRUN=true
;;
*) # invalid command line arguments
usage "Invalid command line argument $OPTARG"
;;
esac
done
#shift $((OPTIND-1)) # This tells getopts to move on to the next argument.
### # check that Product is not empty
if [ -z "${PRODUCT}" ]
then
usage 'Product name must be specified using option -p <string>'
fi
if [ -z "${ARCHITECTURE}" ]
then
ARCHITECTURE="${DEFAULT_PLATFORM}"
fi
if [ -z "${VERSION}" ]
then
## this is conditional because there is some effort in getting the latest version
VERSION=$(getLatest ${PRODUCT})
fi
echo "Fetching ${PRODUCT} v${VERSION} for ${ARCHITECTURE}..."
URL="https://releases.hashicorp.com/${PRODUCT}/${VERSION}/${PRODUCT}_${VERSION}_${ARCHITECTURE}.zip"
if [ "${DRYRUN}" == "true" ]; then
echo " URL: ${URL}"
echo ""
echo "Skipping download in dry run mode."
exit 0
fi
TMPDIR=`mktemp -d /tmp/fetch.XXXXXXXXXX` || exit 1
pushd ${TMPDIR} > /dev/null
if ! mkdir -p /opt/${PRODUCT}/bin; then
echo "Unable to create /opt/${PRODUCT}/bin. Trying with sudo..."
sudo mkdir -p /opt/${PRODUCT}/bin
fi
wget -q ${URL} -O ${PRODUCT}.zip
unzip -q ${PRODUCT}.zip
if ! mv ${PRODUCT} /opt/${PRODUCT}/bin/${PRODUCT}-v${VERSION}; then
echo "Unable to move downloaded file to /opt/${PRODUCT}/bin/${PRODUCT}-v${VERSION}. Trying with sudo..."
sudo mv ${PRODUCT} /opt/${PRODUCT}/bin/${PRODUCT}-v${VERSION}
fi
popd > /dev/null
rm -rf ${TMPDIR}
maybeCreateSymlink