-
Notifications
You must be signed in to change notification settings - Fork 23
/
EasyGitian
executable file
·393 lines (365 loc) · 13.3 KB
/
EasyGitian
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/bin/bash
# Copyright (c) 2017 MAZA Network Developers, Robert Nelson (guruvan)
test -f EasyGitian.env && source EasyGitian.env
if [ "$EASYGITIAN_DEBUG}" = "true" ] ; then
DEBUG=true
set -xeo pipefail
fi
HOSTOS=$(uname | tr '[:upper:]' '[:lower:]')
if [ "${EASYGITIAN_WSL}" = true ] ;then
export WSL=true
fi
#if [ "${EASYGITIAN_OS}" = "Darwin" ] ; then
if [ "${HOSTOS}" = "darwin" ] ; then
echo "Checking for VT-x Virtualization Capabilities in your CPU..."
sysctl -a | grep machdep.cpu.features|grep -i vmx > /dev/null 2>&1 || { echo "VirtualBox requires VT-x Virtualization capabilities. Exiting..." ; exit 1; }
echo "Virtualization capabilities found"
num_procs=$(sysctl -n hw.physicalcpu)
echo "Checking number of Physical CPU cores... $num_procs"
HOST_MEM=$(sysctl -n hw.memsize); HOST_MEM=$((HOST_MEM/1048576))
elif [ "${HOSTOS}" = "linux" ] ; then
HOST_MEM=$(grep MemTotal /proc/meminfo |awk '{print $2 / 1024 }'|awk -F\. '{print $1}')
echo "Checking for 64bit CPU..."
grep lm /proc/cpuinfo > /dev/null 2>&1 || { echo "64 bit CPU required. Exiting..." ; exit 1; }
echo "64bit CPU found"
echo "Checking for VT-x Virtualization Capabilities in your CPU..."
grep vmx /proc/cpuinfo > /dev/null 2>&1 || { echo "VirtualBox requires VT-x Virtualization capabilities. Exiting..." ; exit 1; }
echo "Virtualization capabilities found"
num_procs=$(grep -c processor /proc/cpuinfo)
echo "Checking number of Physical CPU cores... $num_procs"
else
echo "Host OS: ${HOSTOS} not recognized. Exiting..."
exit 1
fi
## make sure we have enough ram
need_mem=$((num_procs*1250))
echo "Required RAM for the ${num_procs} processors you have is $need_mem MB"
echo "Checking total RAM on host... ${HOST_MEM} MB"
if [ $HOST_MEM -le $need_mem ] ; then
echo "You have ${HOST_MEM} MB of RAM, Absolute mininum is 1250 MB per Processor"
# TODO allow user to set this
echo "Exiting..."
exit
fi
#EG_VERSION=0.1.0
trap cleanup INT TERM
die () {
case $1 in
1) echo "EasyGitian already running"
exit 1
;;
2) printf "\\nInsufficient disk space in user directory ' 50GB required, %s available\\n" "$vboxspace"
exit 2
;;
3) printf "\\nInsufficient disk space in EasyGitian directory 20GB required, %s available\\n" "$egbspace"
exit 3
;;
5) printf "\\nInstallation of Vagrant and VirtualBox failed\\nPlease retry\\nYou may want to edit EasyGitian.env and add a line\\nexport EASYGITIAN_DEBUG=true\\n"
exit 5
;;
*) echo "unknown error"
exit 999
;;
esac
}
cleanup () {
test -f ./build_list && rm ./build_list
echo "Exiting EasyGitian"
}
check_diskspace () {
if [ "${EASYGITIAN_WSL}" = "true" ]; then
egbspace=$(df -k ./|grep -v Filesystem |awk '{print $4}')
vboxspace=$(df -k "${EASYGITIAN_WINUSERDIR}" |grep -v Filesystem |awk '{print $4}')
else
egbspace=$(df -k ./|grep dev|awk '{print $4}')
vboxspace=$(df -k "$HOME" |grep dev|awk '{print $4}')
fi
case "$1" in
install) printf "\\nChecking disk space for initial VM launch...\\n\\n"
if [[ $vboxspace -le 50000000 ]]; then
die 2 #insufficient diskspace
fi
if [ $egbspace -le 20000000 ]; then
die 3
fi
;;
build) printf "\\nChecking disk space for your build...\\n\\n"
if [[ $vboxspace -le 20000000 ]]; then
die 2 #insufficient diskspace
fi
if [ $egbspace -le 20000000 ]; then
die 3
fi
;;
esac
}
check_self () {
# Crude check to see if a build is running
# shuold be corresponding check in the VM
if [ "${HOSTOS}" = "darwin" ] ; then
pgrep EasyGitian || die 1
elif [ "${HOSTOS}" = "linux" ] ; then
pgrep EasyGitian || die 1
fi
}
install_prereqs () {
if [ "${WSL}" = "true" ] ; then
VBOXMANAGE="/mnt/c/Program Files/Oracle/VirtualBox/VBoxManage.exe"
test -f /usr/bin/vagrant \
&& test -f "/mnt/c/HashiCorp/Vagrant/bin/vagrant.exe" \
&& vagrant version \
&& which "${VBOXMANAGE}" \
&& echo "Virtualbox appears to be installed" \
&& echo "Virtualbox Version: $("${VBOXMANAGE}" -v)" \
&& echo "" \
&& if [ "$("${VBOXMANAGE}" list extpacks |grep "Usable" | awk '{print $2}')" = "true" ] ; then
echo "Virtualbox Extension Pack installed:"
"${VBOXMANAGE}" list extpacks|grep -E '(Version|Revision)'
echo ""
touch ./.prereq_install_complete
fi
else
VBOXMANAGE=$(which VBoxManage)
which vagrant > /dev/null 2>&1 \
&& echo "Vagrant appears to be installed" \
&& vagrant version \
&& echo "" \
&& which VBoxManage > /dev/null 2>&1 \
&& echo "Virtualbox appears to be installed" \
&& echo "Virtualbox Version: $(VBoxManage -v)" \
&& echo "" \
&& if [ "$(VBoxManage list extpacks |grep "Usable" | awk '{print $2}')" = "true" ] ; then
echo "Virtualbox Extension Pack installed:"
VBoxManage list extpacks|grep -E '(Version|Revision)'
echo ""
touch ./.prereq_install_complete
fi
fi
if [ -f ./.prereq_install_complete ] ; then
echo "Install already completed"
else
echo "Attempting to help you install Vagrant & Virtualbox"
./"${HOSTOS}"-Base-System.sh || die 5
fi
}
first_run () {
if [ -f .vbox-vm-initialized ] ; then
echo "VM Initialized"
else
echo "Checking Disk Space"
check_diskspace install
echo "Initializing VM"
./VBox_VM_Initialize.sh && echo "VM Initialized"
test -f .vbox-vm-initialized || echo "VM initialization failed"
fi
}
launch_vm () {
test -f .vbox-vm-initialized || first_run
vagrant status | grep -i running || vagrant up
}
make_env () {
test -f .skip_config || ./USER_CONFIG.sh
}
get_repos () {
test -f ./USER_CONFIG.env || { echo "USER_CONFIG.env not found, run EasyGitian make_env" ;exit 10; }
. ./USER_CONFIG.env
test -d ./repos || mkdir -p ./repos
pushd ./repos
# Set up build
echo "Checking for initial ${COIN} git directories - codebase, gitian.sigs, detached-sigs"
test -d ./"${VGITIAN_COIN}" || { echo "${VGITIAN_COIN} git directory not found, cloning..."; git clone "$VGITIAN_URL"; }
test -d ./"${VGITIAN_COIN}-gitian.sigs" || { echo "${VGITIAN_COIN} gitian.sigs directory not found, cloning..."; git clone "$VGITIAN_SIGREPO" "${VGITIAN_COIN}"-gitian.sigs; }
test -d ./"${VGITIAN_COIN}-detached-sigs" || { echo "${VGITIAN_COIN} detached-sigs directory not found, cloning..."; git clone "$VGITIAN_DETACHEDSIGREPO" "${VGITIAN_COIN}"-detached-sigs; }
pushd "${VGITIAN_COIN}"
# check current descriptors
if [ "$VGITIAN_COMMIT" = "false" ] ;then
git checkout v"${VGITIAN_VERSION}"
else
git checkout "${VGITIAN_VERSION}"
fi
popd
popd
}
# ma
make_tarball () {
test -f .skip_osx_builds || ./Make_SDK_tarball.sh
}
run_build () {
test -f ./build_list && rm ./build_list
test -f .prereq_install_complete || install_prereqs
echo "Vagrant and Virtualbox installed"
test -f ./USER_CONFIG.env || make_env
grep VGITIAN_MEM ./USER_CONFIG.env || make_env
echo "Gitian settings configuration checked"
get_repos
SDKNAME=$(grep "sdk.tar.gz" repos/"${VGITIAN_COIN}"/contrib/gitian-descriptors/gitian-osx.yml|grep -v BASEPREFIX)
SDKVER=$(echo "$SDKNAME"| awk -F [X.] '{print $2"."$3}')
test -f ./inputs/MacOSX"${SDKVER}".sdk.tar.gz || make_tarball
echo "OSX SDK tarball checked"
test -f .vbox-vm-initialized || first_run
echo "Virtualbox VM should be initialized"
check_diskspace build \
&& printf "\\nDisk Space OK\\n\\n" \
&& vagrant status | grep running || vagrant up \
&& printf "VM Started - running build\\n" \
&& if [ "${HOSTOS}" = "darwin" ] ; then
# we can be pretty sure we have openssl on osx
EASYGITIAN_BUILD_ID=$(echo "$(openssl rand -hex 4)"|tr '[:lower:]' '[:upper:]')
else
# osx throws an Illegal byte sequece error for this
EASYGITIAN_BUILD_ID=$(tr -dc 'A-F0-9' < /dev/urandom |head -c8)
fi \
&& sed -i.bkp '/EASYGITIAN_BUILD_ID/d' USER_CONFIG.env \
&& echo "export EASYGITIAN_BUILD_ID=$(echo ${EASYGITIAN_BUILD_ID})" >> USER_CONFIG.env \
&& echo "Build ID for this build: ${EASYGITIAN_BUILD_ID}" \
&& echo "########################################################" >> Build_History.EGB \
&& echo "########################################################" >> Build_History.EGB \
&& echo "Start Build: ${EASYGITIAN_BUILD_ID} at $(date) " >> Build_History.EGB \
&& cat USER_CONFIG.env EasyGitian.env >> Build_History.EGB \
&& printf "\\n########## End Environment Build: %s ##########\\n\\n" ${EASYGITIAN_BUILD_ID} >> Build_History.EGB \
&& printf "\\n\\nIf you'd like to watch additional build output\\nOpen a new terminal and run\\n\\n source ~/EasyGitian.env; vagrant ssh\\n\\nthen run \\n\\ntail -f gitian-builder/var/install.log\\n\\ntail -f gitian-builder/var/build.log\\n\\n" \
&& chmod +x ./gitian-build.sh \
&& vagrant ssh -c './run-gitian-build'
# TODO opening a new terminal was hacky at best
#read -p "Watch Build in a new terminal?" -n 1 -r
#echo
#if [[ $REPLY =~ ^[Yy]$ ]]
# then
# ./EasyGitian watch_fromrun &
}
clean_vm () {
if [ ! -f .vbox-vm-initialized ]; then
echo "VM Not initialized"
exit 4
fi
vagrant status | grep "not created" && vm_created=no
if [ "${vm_created}" = "no" ]; then
echo "VM Not created"
exit 5
fi
vagrant status | grep running && vagrant halt
vagrant snapshot list Gitian-builder_jessie |grep Gitian-Clean \
&& echo "Found snapshot for Gitian-builder_jessie" \
&& vagrant snapshot restore --no-provision Gitian-builder_jessie Gitian-Clean \
&& echo "Snapshot restored - You should have a clean VM now" \
&& touch .vbox-vm-initialized \
&& echo "Gitian-builder_jessie VM cleaned - snapshot restored" \
&& echo "Your vm should be started. You can run:" \
&& echo "" \
&& echo "./EasyGitian " \
&& echo "" \
&& echo "To begin a build" \
&& exit 0
# or you don't have a snapshot
echo "Looks like you may not have a good snapshot"
echo "You may want to run:"
echo "./EasyGitian rebuild_vm"
}
destroy_vm () {
vagrant destroy && \
test -f .vbox-vm-initialized && rm .vbox-vm-initialized \
&& test -f .vbox-vm-snapshot-clean && rm .vbox-vm-snapshot-clean \
&& test -f .prereq_install_complete && rm .prereq_install_completea \
&& test -f .vagrant && rm -rf .vagrant
}
rebuild_vm () {
destroy_vm
first_run
}
reboot_vm () {
echo "Rebooting Gitian-builder_jessie VM"
vagrant reload
}
start_vm () {
echo "Starting Gitian-builder_jessie VM"
vagrant status | grep running || vagrant up
}
halt_vm () {
vagrant halt
echo "Your GitianBuilder VM should be shutdown now"
echo "You can save it to run more builds, or clean up"
cat USAGE.md
exit 0
}
watch_fromrun () {
# if [ "$1" = "fromrun" ]; then
started=$SECONDS
while [ ! -f ./build_list ]; do
sleep 10
if [ "$SECONDS" -ge "$(( started + 180))" ];then
printf "\\nBuild is taking too long to start\\n Can't open new window"
exit 88
fi
done
if [ "$HOSTOS" = "darwin" ]; then
osascript <<END
tell application "Terminal"
do script "cd \"$(pwd)\";./EasyGitian watch_build"
end tell
END
elif [ "$HOSTOS" = "linux" ]; then
echo "Sorry - not implemented. Open a new terminal and run"
echo "./EasyGitian watch_build"
#xterm -display $DISPLAY -e
fi
# fi
}
watch_build () {
echo " "
echo "Watching build logs"
vagrant ssh -c '/host_vagrantdir/watch_build.sh'
exit
}
toggle_gui () {
gui=$(grep "vb.gui" Vagrantfile|awk '{print $3}')
if [ "$gui" = "true" ] ; then
sed -i.guitrue 's/vb\.gui\ \=\ true/vb\.gui\ \=\ false/g' Vagrantfile
rm Vagrantfile.guitrue
echo "VM will run in headless mode"
else
sed -i.guifalse 's/vb\.gui\ \=\ false/vb\.gui\ \=\ true/g' Vagrantfile
rm Vagrantfile.guifalse
echo "VM will run with GUI - login on console as"
echo "username: vagrant"
echo "password: vagrant"
fi
}
case "$1" in
install_prereqs) install_prereqs # Get Vagrant and Vbox
;;
first_run) first_run # First run after install or rebuild_vm
;;
make_env) make_env # Make USER_CONFIG.evn
;;
make_tarball) make_tarball # make OSX SDK tarball
;;
run_build) run_build # default, check system & run build
;;
get_repos) get_repos # make sure we have coin repo and sigs repos
;;
clean_vm) clean_vm # restore vm from snapshot
;;
rebuild_vm) rebuild_vm # rebuild from base box
;;
destroy_vm) destroy_vm # wipe out vm and vdi files
;;
reboot_vm) reboot_vm # reboot & reread vagrantfile
;;
halt_vm) halt_vm # shut down
;;
watch_build) watch_build # watch build progress in this terminal
;;
toggle_gui) toggle_gui # toggle Vbos headless or GUI mode
;;
watch_fromrun) watch_fromrun # open new terminal window to watch build
;;
show_bounties) cat ./BOUNTY.md # display info about paying or receiving bounties for work on Easy Gitian
;;
show_addresses) cat ./BOUNTY_ADDRESSES.md # payment addresses
;;
"") echo "No option specified, running build"
run_build
;;
*) cat USAGE.md
exit 1
esac