/
manage-tools
executable file
·286 lines (246 loc) · 6.63 KB
/
manage-tools
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
#!/usr/bin/env bash
set -eu -o pipefail
# set -x
CTF_TOOLS_ROOT="$(dirname "${BASH_SOURCE[0]}")/.."
function usage()
{
cat <<END
Usage: $(basename $0) [-sv] (setup|install) tool
Where:
-s allow running things with sudo (i.e., to install debs)
-n "nice" the installer to reduce background load
-v verbose mode. print log while installing
-f force certain actions (such as installing over an installed tool)
tool the name of the tool. if "all", does the action on all (installed) tools
Actions:
setup set up the environment (install some basic packages)
install installs a tool
END
}
function tool_log()
{
echo "$(tput setaf 4 2>/dev/null)TOOLS | $TOOL |$(tput sgr0 2>/dev/null) $@"
}
function detect_distribution()
{
if which pacman >/dev/null 2>&1; then
echo "archlinux"
elif which apt-get >/dev/null 2>&1; then
if lsb_release -a 2>/dev/null | grep -i ubuntu >/dev/null 2>&1; then
echo "ubuntu"
else
echo "debian"
fi
elif which dnf >/dev/null 2>&1; then
echo "fedora"
else
echo ""
fi
}
function base_build_setup_debian()
{
PACKAGE_REQS="build-essential libtool g++ gcc texinfo curl wget automake autoconf python python-dev git subversion unzip virtualenvwrapper lsb-release"
PACKAGE_COUNT=$(echo $PACKAGE_REQS | tr ' ' '\n' | wc -l)
sudo apt-get update
if [ $(dpkg -l $PACKAGE_REQS | grep "^ii" | wc -l) -ne $PACKAGE_COUNT ]
then
if [ "$ALLOW_SUDO" -eq 1 ]; then
sudo apt-get -y install $PACKAGE_REQS
else
TOOL=SETUP tool_log "Please install the following packages: $PACKAGE_REQS"
fi
fi
if ! dpkg --print-foreign-architectures | grep -q i386
then
if [ "$ALLOW_SUDO" -eq 1 ]
then
sudo dpkg --add-architecture i386
sudo apt-get update
else
TOOL=SETUP tool_log "Certain tools need i386 libraries (enable with 'dpkg --add-architecture i386; apt-get update')."
fi
fi
}
function base_build_setup_arch()
{
PACKAGE_REQS="curl wget python2 python3 git subversion unzip python-virtualenvwrapper"
if [ "$ALLOW_SUDO" -eq 1 ]; then
sudo pacman -Syu --noconfirm --needed $PACKAGE_REQS
sudo pacman -Syu --noconfirm --needed base-devel || true
else
TOOL=SETUP tool_log "Please install the following packages: $PACKAGE_REQS"
fi
if ! grep "^\[multilib\]$" /etc/pacman.conf >/dev/null; then
if [ "$ALLOW_SUDO" -eq 1 ]; then
sudo sh -c 'cat >> /etc/pacman.conf' <<EOF
[multilib]
Include = /etc/pacman.d/mirrorlist
EOF
else
TOOL=SETUP tool_log "Certain tools need i386 libraries (enable multilib in /etc/pacman.conf')."
return
fi
fi
if [ "$ALLOW_SUDO" -eq 1 ] \
&& grep "^\[multilib\]$" /etc/pacman.conf >/dev/null \
&& ! sudo pacman -Qk gcc-multilib >/dev/null
then
sudo pacman -Syy --noconfirm
#sudo pacman -Syu --noconfirm multilib-devel
# unfortunately we cannot do --noconfirm if we might choose to replace
# a package such as gcc with gcc-multilib, therefore this workaround
printf "\ny\ny\ny\n" | sudo pacman -Syu multilib-devel
fi
}
function base_build_setup_fedora()
{
PACKAGE_REQS="libtool gcc gcc-c++ clang cmake texinfo curl wget automake autoconf python python-devel git subversion unzip python-virtualenvwrapper redhat-rpm-config"
if [ "$ALLOW_SUDO" -eq 1 ]; then
sudo dnf -y install $PACKAGE_REQS
else
TOOL=SETUP tool_log "Please install the following packages: $PACKAGE_REQS"
fi
# TODO: check whether we have to explicitly enable i386 package support
}
function base_build_setup()
{
case "$1" in
"ubuntu")
;& # fallthrough
"debian")
base_build_setup_debian
;;
"archlinux")
base_build_setup_arch
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
;;
"fedora")
base_build_setup_fedora
;;
*)
TOOL=SETUP tool_log "Cannot detect or unsupported distribution"
esac
}
if [[ $# -eq 0 ]]; then
usage
exit 1
fi
DISTRI=$(detect_distribution)
while [[ $1 == -* ]]
do
case $1 in
-s)
export ALLOW_SUDO=1
;;
-n)
export NICE_LEVEL=10
;;
-f)
export FORCE=1
;;
-v)
export VERBOSE_OUTPUT=1
;;
*)
usage
exit
;;
esac
shift
done
[[ -z ${ALLOW_SUDO+x} ]] && export ALLOW_SUDO=0
[[ -z ${FORCE+x} ]] && export FORCE=0
[[ -z ${VERBOSE_OUTPUT+x} ]] && export VERBOSE_OUTPUT=0
[[ -z ${NICE_LEVEL+x} ]] && export NICE_LEVEL=0
export EXPECTFAIL=${EXPECTFAIL:-0}
if [[ $# -ge 1 ]]; then
ACTION="$1"
fi
if [[ $# -eq 2 ]]; then
TOOL="$2"
else
TOOL=""
fi
# handle the special all tool
if [[ "$TOOL" == "all" ]]
then
case $ACTION in
install)
for t in $($0 list)
do
$0 $ACTION $t
done
exit 0
;;
*)
TOOL="" tool_log "action $ACTION cannot handle the special \"all\" tool"
usage
exit 1
;;
esac
fi
case $ACTION in
setup)
base_build_setup "$DISTRI"
;;
install)
cd $TOOL
# the first line in all install and uninstall scripts should have the -e flag, otherwise fail
if [ $(for i in install* ; do if [ -e "$i" ]; then head -1 "$i"; fi; done | sort | uniq | grep -v '^#!/bin/bash -ex$' | wc -l) -ne 0 ];
then
tool_log "not all install/uninstall/test scripts start with '#!/bin/bash -ex', which is a must for accurate testing."
exit 1
fi
tool_log "starting install, logging to $PWD/install.log"
rm -f install.log
# first get distri specific dependencies
if [[ $(find . -name 'install-root*' | wc -l) -ge 1 ]]; then
INSTALL_ROOT_SCRIPT="./install-root-$DISTRI"
# use debian install script if we are on ubuntu and no ubuntu
# specific install script exists
if [[ "$DISTRI" == "ubuntu" \
&& ! -x "$INSTALL_ROOT_SCRIPT" \
&& -x "./install-root-debian" ]]
then
INSTALL_ROOT_SCRIPT="./install-root-debian"
fi
if [[ -x "$INSTALL_ROOT_SCRIPT" && "$ALLOW_SUDO" -eq 1 ]]; then
set +e
if [[ "$VERBOSE_OUTPUT" -eq 1 ]]; then
sudo env DISTRI=$DISTRI "$INSTALL_ROOT_SCRIPT" 2>&1 | tee -a install.log
else
sudo env DISTRI=$DISTRI "$INSTALL_ROOT_SCRIPT" >> install.log 2>&1
fi
INSTALL_FAILED=$?
set -e
if [[ "$INSTALL_FAILED" -eq 0 ]]; then
tool_log "system dependencies installed"
else
tool_log "INSTALL FAILED: $INSTALL_ROOT_SCRIPT failed to install dependencies"
cat install.log >&2
exit 1
fi
else
tool_log "Warning: make sure build dependencies are installed!"
fi
fi
# execute install script
set +e
if [ "$VERBOSE_OUTPUT" -eq 1 ]; then
DISTRI=$DISTRI PATH=$CTF_TOOLS_ROOT/bin/:$PATH nice -n$NICE_LEVEL ./install 2>&1 | tee -a install.log
else
DISTRI=$DISTRI PATH=$CTF_TOOLS_ROOT/bin/:$PATH nice -n$NICE_LEVEL ./install >>install.log 2>&1
fi
INSTALL_FAILED=$?
set -e
if [ "$INSTALL_FAILED" -eq 0 ]; then
tool_log "install finished"
else
tool_log "INSTALL FAILED"
cat install.log >&2
exit 1
fi
cd ..
$0 bin $TOOL
;;
esac