-
Notifications
You must be signed in to change notification settings - Fork 31
/
install-converge.sh
executable file
·240 lines (194 loc) · 4.64 KB
/
install-converge.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
#!/bin/sh
# Copyright © 2016 Asteris, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This script downloads converge to a system.
# If possible, the Operating System and processor
# type will be detected.
# install-converge.sh [-v <version>]
# -d Directory for converge binary (default /usr/local/bin/)
# -v Converge version to install
set -e
__run() {
# Default to latest stable
version="0.6.0"
install_dir="/usr/local/bin"
base_url="https://github.com/asteris-llc/converge/releases/download"
machine=$(uname -m)
os=$(uname -s)
# Get a temporary directory
if [ -z "$TMPDIR" ]; then
tmp="/tmp"
else
tmp="$TMPDIR"
fi
tmp_dir="${tmp}/install-converge.sh.$$"
(umask 077 && mkdir "${tmp_dir}") || exit 1
check_dir () {
if [ ! -d "$1" ]; then
echo "Directory $1 does not exist, exiting"
exit 1
fi
}
usage () {
echo "Usage: install-converge.sh [-v <version>]"
echo " -d Directory for converge binary (default ${install_dir})"
echo " -v Converge version to install (default ${version})"
if test "${#}" -gt 0; then
echo
echo "${@}"
fi
exit 1
}
not_supported () {
echo "Unable to download converge binary for $os-$machine"
echo "Most likely means that there is not an official release for that os+machine pair"
exit 1
}
do_wget () {
echo "Trying wget..."
wget -O "$2" "$1" 2>"$tmp_dir/stderr"
rc="$?"
grep "ERROR404" "$tmp_dir/stderr" >/dev/null 2>&1 && not_supported
if test "$rc" -ne 0 || test ! -s "$2"; then
return 1
fi
return 0
}
do_curl () {
echo "Trying curl..."
echo "$1"
curl --retry 5 -sL -D "$tmp_dir/stderr" "$1" > "$2"
rc="$?"
grep "404 Not Found" "$tmp_dir/stderr" >/dev/null 2>&1 && not_supported
if test "$rc" -ne 0 || test ! -s "$2"; then
return 1
fi
return 0
}
do_fetch() {
echo "Trying fetch..."
fetch -o "$2" "$1" 2>"$tmp_dir/stderr"
test "$?" -ne 0 && return 1
return 0
}
do_perl () {
echo "Trying perl..."
perl -r 'use LWP::Simple; getprint($ARGV[0]);' "$1" > "$2" 2>"$tmp_dir/stderr"
rc="$?"
grep "404 Not Found" "$tmp_dir/stderr" >/dev/null 2>&1 && not_supported
if test "$rc" -ne 0 || test ! -s "$2"; then
return 1
fi
return 0
}
do_python () {
echo "Trying python..."
python -c "import sys,urllib2 ; sys.stdout.write(urllib2.urlopen(sys.argv[1]).read())" "$1" > "$2" 2>"$tmp_dir/stderr"
rc="$?"
grep "HTTP Error 404" "$tmp_dir/stderr" >/dev/null 2>&1 && not_supported
if test "$rc" -ne 0 || test ! -s "$2"; then
return 1
fi
return 0
}
exists() {
command -v "$1" >/dev/null 2>&1
}
do_download () {
url="$1"
dest="$2"
if exists wget; then
do_wget "$url" "$dest" && return 0
fi
if exists curl; then
do_curl "$url" "$dest" && return 0
fi
if exists fetch; then
do_fetch "$url" "$dest" && return 0
fi
if exists perl; then
do_perl "$url" "$dest" && return 0
fi
if exists python; then
do_python "$url" "$dest" && return 0
fi
echo "Unable to retrieve package"
exit 1
}
extract() {
file="$1"
dest="$2"
cd "$dest" && tar zxvf "$file" converge
}
while getopts :d:r:v: opt; do
case "$opt" in
d)
install_dir="$OPTARG"
;;
v)
version="$OPTARG"
;;
\?)
usage "Invalid flag: $OPTARG"
;;
esac
done
case "${machine}" in
"x86_64" | "amd64" | "x64")
machine="amd64"
;;
"i386" | "i86pc" | "x86" | "i686")
machine="386"
;;
"armv6l" | "armv7l")
machine="arm"
;;
"arm" | "arm64" | "ppc64" | "ppc64le")
# Nothing required
;;
*)
echo "Unsupported machine type: ${machine}"
exit 1
;;
esac
case "${os}" in
"Darwin")
os="darwin"
;;
"FreeBSD")
os="freebsd"
;;
"OpenBSD")
os="openbsd"
;;
"Linux")
os="linux"
;;
"solaris")
# Nothing to do
;;
*)
echo "Unsupported OS type: $os"
exit 1
;;
esac
check_dir "${install_dir}"
do_download "${base_url}/${version}/converge_${version}_${os}_${machine}.tar.gz" "${tmp_dir}/converge_${version}_${os}_${machine}.tar.gz"
extract "${tmp_dir}/converge_${version}_${os}_${machine}.tar.gz" "${install_dir}"
chmod 0755 "${install_dir}/converge"
if [ -n "$tmp_dir" ]; then
rm -r "${tmp_dir}"
fi
}
__run