|
4 | 4 | # |
5 | 5 | # $ sh -c "$(curl -L https://github.com/intercloud/gobinsec/releases/latest/download/install)" |
6 | 6 | # |
| 7 | +# or (if you don't have curl installed): |
| 8 | +# |
| 9 | +# $ sh -c "$(wget -O - https://github.com/intercloud/gobinsec/releases/latest/download/install)" |
7 | 10 |
|
8 | 11 | set -e |
9 | 12 |
|
10 | | -log() { |
11 | | - echo "$@" |
12 | | -} |
13 | | - |
14 | | -logF() { |
15 | | - echo >&2 "$@" |
16 | | - exit 1 |
17 | | -} |
18 | | - |
19 | | -repo="gobinsec" |
20 | | -owner="intercloud" |
21 | | -binary="${repo}" |
22 | | - |
23 | | -get_os() { |
24 | | - echo $(uname -s | awk '{print tolower($0)}') |
25 | | -} |
26 | | - |
27 | | -get_arch() { |
28 | | - a=$(uname -m) |
29 | | - case ${a} in |
30 | | - "x86_64" | "amd64") |
31 | | - echo "x86_64" |
32 | | - ;; |
33 | | - "i386" | "i486" | "i586") |
34 | | - echo "i386" |
35 | | - ;; |
36 | | - *) |
37 | | - echo ${NIL} |
38 | | - ;; |
39 | | - esac |
40 | | -} |
41 | | - |
42 | | -if [ -z "$githubUrl" ]; then |
43 | | - githubUrl="https://github.com" |
44 | | -fi |
45 | | - |
46 | | -# parse flag |
47 | | -for i in "$@"; do |
48 | | - case $i in |
49 | | - -v=* | --version=*) |
50 | | - version="${i#*=}" |
51 | | - shift |
52 | | - ;; |
53 | | - *) |
54 | | - # unknown option |
55 | | - ;; |
56 | | - esac |
57 | | -done |
58 | | - |
59 | | -if [ -z "$version" ]; then |
60 | | - version="latest" |
61 | | -fi |
| 13 | +NAME="gobinsec" |
62 | 14 |
|
63 | | -# get OS and ARCH and build binary name |
64 | | -os=$(get_os) |
65 | | -arch=$(get_arch) |
| 15 | +# get OS and ARCH and build archive name |
| 16 | +os=`uname | tr '[:upper:]' '[:lower:]'` |
| 17 | +arch=`uname -m` |
66 | 18 |
|
67 | | -log "os: ${os}" |
68 | | -log "arch: ${arch}" |
69 | | -log "version: ${version:-latest}" |
| 19 | +echo "os: ${os}" |
| 20 | +echo "arch: ${arch}" |
70 | 21 |
|
71 | | -fileName="${binary}_${os}_${arch}.tar.gz" |
| 22 | +archive="${NAME}_${os}_${arch}.tar.gz" |
72 | 23 |
|
73 | 24 | # set default installation directory |
74 | 25 | if [ -d "/opt/local/bin" ]; then |
75 | | - executableFolder="/opt/local/bin" |
| 26 | + DEFAULT_DIR="/opt/local/bin" |
76 | 27 | elif [ -d "/opt/bin" ]; then |
77 | | - executableFolder="/opt/bin" |
| 28 | + DEFAULT_DIR="/opt/bin" |
78 | 29 | elif [ -d "/usr/local/bin" ]; then |
79 | | - executableFolder="/usr/local/bin" |
| 30 | + DEFAULT_DIR="/usr/local/bin" |
80 | 31 | elif [ -d "/usr/bin" ]; then |
81 | | - executableFolder="/usr/bin" |
| 32 | + DEFAULT_DIR="/usr/bin" |
82 | 33 | else |
83 | | - executableFolder="/bin" |
| 34 | + DEFAULT_DIR="/bin" |
84 | 35 | fi |
85 | 36 |
|
86 | | -# check command availability |
87 | | -command -v curl >/dev/null 2>&1 || { logF "curl is required"; } |
| 37 | +# select command to download archive |
| 38 | +if hash curl 2>/dev/null |
| 39 | +then |
| 40 | + command="curl -L -o" |
| 41 | +elif hash wget 2>/dev/null |
| 42 | +then |
| 43 | + command="wget -O" |
| 44 | +else |
| 45 | + echo "You must install curl or wget to run this installation script" |
| 46 | + exit 1 |
| 47 | +fi |
88 | 48 |
|
89 | | -# Set URI to download |
90 | | -assetUri="${githubUrl}/${owner}/${repo}/releases/download/${version}/${fileName}" |
91 | | -if [[ "${version}" == "latest" ]]; then |
| 49 | +# download archive in /tmp/${NAME}, unzip it and clean files |
| 50 | +${command} /tmp/${archive} https://github.com/intercloud/${NAME}/releases/latest/download/${archive} |
92 | 51 |
|
93 | | - asset=$( |
94 | | - curl -sSf ${githubUrl}/${owner}/${repo}/releases | |
95 | | - grep -o "${owner}/${repo}/releases/download/.*/${fileName}" | |
96 | | - head -n 1 |
97 | | - ) |
| 52 | +cd /tmp/ |
| 53 | +tar -zxf $archive |
| 54 | +rm $archive LICENSE.txt README.md |
98 | 55 |
|
99 | | - assetUri="${githubUrl}/${asset}" |
| 56 | +# prompt for installation directory |
| 57 | +read -p "Installation directory [${DEFAULT_DIR}]? " directory |
| 58 | +if [ -z "$directory" ] |
| 59 | +then |
| 60 | + directory=${DEFAULT_DIR} |
100 | 61 | fi |
101 | 62 |
|
102 | | -downloadFolder="/tmp/${binary}" |
103 | | -mkdir -p ${downloadFolder} |
104 | | -downloadedFile="${downloadFolder}/${fileName}" |
105 | | - |
106 | | -log "[1/2] Download ${assetUri}" |
107 | | -curl -sS --fail --location --output "${downloadedFile}" "${assetUri}" |
108 | | - |
109 | | -log "[2/2] Install ${binary} to '${executableFolder}'" |
110 | | -if [ -w "${directory}" ]; then |
111 | | - tar -xz -f ${downloadedFile} -C ${executableFolder} |
112 | | - chmod +x ${executableFolder}/${binary} |
| 63 | +# copy binary to installation directory |
| 64 | +if [ -w "${directory}" ] |
| 65 | +then |
| 66 | + mv /tmp/${NAME} ${directory} |
113 | 67 | else |
114 | | - sudo tar -xz -f ${downloadedFile} -C ${executableFolder} |
115 | | - sudo chmod +x ${executableFolder}/${binary} |
| 68 | + sudo mv /tmp/${NAME} ${directory} |
| 69 | + sudo chown root: ${directory}/${NAME} |
116 | 70 | fi |
117 | 71 |
|
118 | | -echo "${binary} installed in '${executableFolder}' directory" |
| 72 | +echo "${NAME} installed in '${directory}' directory" |
0 commit comments