Skip to content
Snippets Groups Projects
Commit 8c44652e authored by Robert Martin-Legene's avatar Robert Martin-Legene
Browse files

Shellcheck safe

parent 1dd6ed69
No related branches found
No related tags found
No related merge requests found
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
# Robert Martin-Legene <robert@nic.ar> # Robert Martin-Legene <robert@nic.ar>
if [ -z "${BFAHOME}" ]; then echo "\$BFAHOME not set. Did you source bfa/bin/env ?" >&2; exit 1; fi if [ -z "${BFAHOME}" ]; then echo "\$BFAHOME not set. Did you source bfa/bin/env ?" >&2; exit 1; fi
source ${BFAHOME}/bin/libbfa.sh || exit 1 # shellcheck disable=SC1090
source "${BFAHOME}/bin/libbfa.sh" || exit 1
declare -A commands help declare -A commands help
...@@ -15,13 +16,14 @@ function register_subcommand ...@@ -15,13 +16,14 @@ function register_subcommand
function _usage() function _usage()
{ {
local after='' local after c
for c in $( echo ${!commands[*]} | sort ) after=''
for c in $(echo "${!commands[@]}" | sort)
do do
after="${after}|$c" after="${after}|$c"
done done
echo "Usage: $(basename $0) {${after:1}}" >&2 echo "Usage: $(basename "$0") {${after:1}}" >&2
for c in $( echo ${!commands[*]} | sort ) for c in $(echo "${!commands[@]}" | sort)
do do
printf '%-15s %s\n' "$c" "${help[$c]}" >&2 printf '%-15s %s\n' "$c" "${help[$c]}" >&2
done done
...@@ -37,18 +39,18 @@ function _max ...@@ -37,18 +39,18 @@ function _max
function pidsfromsigfiles function pidsfromsigfiles
{ {
local pids= local pids pid file
local file pids=
for file in \ for file in \
${BFANETWORKDIR}/bootnode.pid \ "${BFANETWORKDIR}/bootnode.pid" \
${BFANETWORKDIR}/start-bootnode-loop.pid \ "${BFANETWORKDIR}/start-bootnode-loop.pid"\
${BFANODEDIR}/monitor.pid \ "${BFANODEDIR}/monitor.pid" \
${BFANODEDIR}/start-monitor-loop.pid \ "${BFANODEDIR}/start-monitor-loop.pid" \
${BFANODEDIR}/geth.pid \ "${BFANODEDIR}/geth.pid" \
${BFANODEDIR}/start-geth-loop.pid "${BFANODEDIR}/start-geth-loop.pid"
do do
test -r "$file" || continue test -r "$file" || continue
local pid=$(< "$file") pid=$(< "$file")
if ! [[ "$pid" =~ ^[0-9]+$ ]] if ! [[ "$pid" =~ ^[0-9]+$ ]]
then then
rm -f "$file" rm -f "$file"
...@@ -66,71 +68,84 @@ function pidsfromsigfiles ...@@ -66,71 +68,84 @@ function pidsfromsigfiles
function sendsig function sendsig
{ {
local signal=$1 local signal pids
signal=$1
shift shift
local pids="$*" pids="$*"
test -n "$pids" || return if [ -z "$pids" ]
then
return
fi
# shellcheck disable=SC2086
ps -p ${pids// /,} ps -p ${pids// /,}
echo "Sending ${signal} signal to pid $pids." echo "Sending ${signal} signal to pid $pids."
kill "$signal" $pids || true kill "$signal" "$pids" || true
} }
register_subcommand 'kill' 'killbfastuff' 'Kill BFA background processes (no questions asked).' register_subcommand 'kill' 'killbfastuff' 'Kill BFA background processes (no questions asked).'
function killbfastuff function killbfastuff
{ {
local pids=$(pidsfromsigfiles) local pids
pids=$(pidsfromsigfiles)
if [ -z "$pids" ] if [ -z "$pids" ]
then then
echo "Nothing to send signals to." >&2 echo "Nothing to send signals to." >&2
exit 2 exit 2
fi fi
sendsig -KILL $pids sendsig -KILL "$pids"
} }
register_subcommand 'stop' 'graceful' 'Ask the BFA background processes to end gracefully.' register_subcommand 'stop' 'graceful' 'Ask the BFA background processes to end gracefully.'
function graceful function graceful
{ {
local max=10 local max pids
local pids=$(pidsfromsigfiles) max=30
pids=$(pidsfromsigfiles)
if [ -z "$pids" ]
then
echo "Nothing to send signals to." >&2
exit 1
fi
sendsig -TERM "$pids"
sleep 1
while : while :
do do
max=$((max - 1))
test "$max" -eq 0 && break
if [ -z "$pids" ]
then
echo "Nothing to send signals to." >&2
break
fi
sendsig -TERM $pids
sleep 0.4
pids=$(pidsfromsigfiles) pids=$(pidsfromsigfiles)
max=$((max - 1))
test "$max" -gt 0 || break
test -n "$pids" || break
printf '\rThese are still alive: %s\x1b[J' "$pids"
sleep 0.5
done done
test -z "$pids" || echo "This/these pids is/are still running: $f" printf '\r\x1b[J'
if [ -n "$pids" ]
then
printf 'This/these pids is/are still running: %s\n' "$pids"
fi
} }
register_subcommand 'initdb' 'initdb' 'Stop geth and reset the node to block zero (genesis).' register_subcommand 'initdb' 'initdb' 'Stop geth and reset the node to block zero (genesis).'
function initdb function initdb
{ {
killbfastuff killbfastuff
yes | geth --cache 0 --datadir ${BFANODEDIR} removedb yes | geth --cache 0 --datadir "${BFANODEDIR}" removedb
geth --networkid ${BFANETWORKID} --cache 0 --datadir ${BFANODEDIR} init ${BFANETWORKDIR}/genesis.json geth --networkid "${BFANETWORKID}" --cache 0 --datadir "${BFANODEDIR}" init "${BFANETWORKDIR}/genesis.json"
} }
register_subcommand 'exportdb' 'exportdb' 'Export blockchain in chunks of 1 million blocks per file.' register_subcommand 'exportdb' 'exportdb' 'Export blockchain in chunks of 1 million blocks per file.'
function exportdb function exportdb
{ {
local delta=1000000 local delta maxblocks blockstart toblock filename
delta=1000000
graceful graceful
local maxblocks=$(bfageth --exec 'eth.blockNumber' console 2> /dev/null) maxblocks=$(bfageth --exec 'eth.blockNumber' console 2> /dev/null)
# 0 is genesis.. shouldn't dump that # 0 is genesis.. shouldn't dump that
local toblock blockstart=1
local blockstart=1
while [ "$blockstart" -lt "$maxblocks" ] while [ "$blockstart" -lt "$maxblocks" ]
do do
toblock=$(( blockstart + delta - 1 )) toblock=$(( blockstart + delta - 1 ))
test "$toblock" -gt "$maxblocks" && test "$toblock" -gt "$maxblocks" &&
toblock=$maxblocks toblock=$maxblocks
local filename
printf -v filename 'bfa2018.blocks.%09d-%09d.export.gz' "$blockstart" "$toblock" printf -v filename 'bfa2018.blocks.%09d-%09d.export.gz' "$blockstart" "$toblock"
if [ ! -e "$filename" ] if [ ! -e "$filename" ]
then then
...@@ -144,11 +159,11 @@ function exportdb ...@@ -144,11 +159,11 @@ function exportdb
register_subcommand 'importdb' 'importdb' 'Import blocks safely from previous block exports.' register_subcommand 'importdb' 'importdb' 'Import blocks safely from previous block exports.'
function importdb function importdb
{ {
local dumpurl="https://s3.wasabisys.com/bfa/blockdumps" local dumpurl delta blockstart toblock
local delta=1000000 dumpurl="https://s3.wasabisys.com/bfa/blockdumps"
delta=1000000
graceful graceful
local toblock blockstart=1
local blockstart=1
while : while :
do do
toblock=$(( blockstart + delta - 1 )) toblock=$(( blockstart + delta - 1 ))
...@@ -156,7 +171,7 @@ function importdb ...@@ -156,7 +171,7 @@ function importdb
curl --fail "${dumpurl}/${filename}" || break curl --fail "${dumpurl}/${filename}" || break
blockstart=$(( toblock + 1 )) blockstart=$(( toblock + 1 ))
done done
geth --networkid ${BFANETWORKID} --datadir "${BFANODEDIR}" --syncmode "full" --gcmode "archive" import <( geth --networkid "${BFANETWORKID}" --datadir "${BFANODEDIR}" --syncmode "full" --gcmode "archive" import <(
n=1 n=1
while gzip -dc "bfa2018-1Mblocksstartingat${n}.block.export.gz" 2>/dev/null while gzip -dc "bfa2018-1Mblocksstartingat${n}.block.export.gz" 2>/dev/null
do do
...@@ -184,7 +199,7 @@ function admin_syncmode ...@@ -184,7 +199,7 @@ function admin_syncmode
echo "between speed and paranoia. You can change the setting, according to" echo "between speed and paranoia. You can change the setting, according to"
echo "your needs." echo "your needs."
mode=$( cat ${BFANODEDIR}/syncmode 2>/dev/null || true ) mode=$(cat "${BFANODEDIR}/syncmode" 2>/dev/null || true)
mode=${mode:-fast} mode=${mode:-fast}
echo "Your current mode is set to ${mode}" echo "Your current mode is set to ${mode}"
killed=0 killed=0
...@@ -194,7 +209,7 @@ function admin_syncmode ...@@ -194,7 +209,7 @@ function admin_syncmode
echo echo
while [ -z "${mode}" ] while [ -z "${mode}" ]
do do
read -p "Which mode do you wish? : " mode read -rp "Which mode do you wish? : " mode
modefilter "$mode" modefilter "$mode"
if [[ "$mode" =~ ^full$|^fast$|^light$ ]] if [[ "$mode" =~ ^full$|^fast$|^light$ ]]
then then
...@@ -205,7 +220,7 @@ function admin_syncmode ...@@ -205,7 +220,7 @@ function admin_syncmode
fi fi
done done
echo "Remembering your choice." echo "Remembering your choice."
echo $mode > ${BFANODEDIR}/syncmode echo "$mode" > "${BFANODEDIR}/syncmode"
if [ "$orgmode" = "fast" ] && [ "$mode" = "full" ] if [ "$orgmode" = "fast" ] && [ "$mode" = "full" ]
then then
echo "You increased your paranoia level. The proper thing to do now," echo "You increased your paranoia level. The proper thing to do now,"
...@@ -218,17 +233,17 @@ function admin_syncmode ...@@ -218,17 +233,17 @@ function admin_syncmode
then then
if [ -r "${BFANODEDIR}/geth.pid" ] if [ -r "${BFANODEDIR}/geth.pid" ]
then then
pid=$( cat ${BFANODEDIR}/geth.pid ) pid=$(< "${BFANODEDIR}/geth.pid")
kill -0 $pid 2>/dev/null && kill -0 "$pid" 2>/dev/null &&
echo "Killing running geth." && echo "Killing running geth." &&
killed=1 killed=1
while ! kill $pid 2>/dev/null while ! kill "$pid" 2>/dev/null
do do
sleep 1 sleep 1
done done
fi fi
initdb initdb
test $killed -eq 1 && test "$killed" -eq 1 &&
echo && echo &&
echo "The startup.sh should restart your geth shortly." echo "The startup.sh should restart your geth shortly."
fi fi
...@@ -240,34 +255,35 @@ function admin_syncmode ...@@ -240,34 +255,35 @@ function admin_syncmode
register_subcommand 'bootnode' 'admin_bootnode' 'Enable/disable the local bootnode.' register_subcommand 'bootnode' 'admin_bootnode' 'Enable/disable the local bootnode.'
function admin_bootnode function admin_bootnode
{ {
keyfile=${BFANETWORKDIR}/bootnode/key keyfile="${BFANETWORKDIR}/bootnode/key"
echo "Only very few wants to actually run a boot node." echo "Only very few wants to actually run a boot node."
echo "If you have a keyfile for a bootnode, then you will" echo "If you have a keyfile for a bootnode, then you will"
echo "automatically start one, when restarting your system." echo "automatically start one, when restarting your system."
if [ -f $keyfile ] if [ -f "$keyfile" ]
then then
echo "You are set up to run a boot node." echo "You are set up to run a boot node."
echo "Deleting your bootnode keyfile disables your bootnode." echo "Deleting your bootnode keyfile disables your bootnode."
yesno n "Do you want to delete your bootnode keyfile?" yesno n "Do you want to delete your bootnode keyfile?"
if [ "$REPLY" = "y" ] if [ "$REPLY" = "y" ]
then then
rm $keyfile rm "$keyfile"
fi fi
pidfile=${BFANETWORKDIR}/bootnode/pid pidfile="${BFANETWORKDIR}/bootnode/pid"
if [ -r $pidfile ] if [ -r "$pidfile" ]
then then
pid=`cat $pidfile` pid=$(< "$pidfile")
kill -0 $pid && if kill -0 "$pid"
echo "Terminating your bootnode." && then
kill `cat $pidfile` || echo "Terminating your bootnode."
true kill "$(< "$pidfile")" || true
fi
fi fi
else else
echo "You are not set up to run a boot node." echo "You are not set up to run a boot node."
yesno n "Do you want to create a keyfile for a bootnode?" yesno n "Do you want to create a keyfile for a bootnode?"
if [ "$REPLY" = "y" ] if [ "$REPLY" = "y" ]
then then
bootnode -genkey $keyfile bootnode -genkey "$keyfile"
fi fi
echo "You can now start your bootnode by running start.sh" echo "You can now start your bootnode by running start.sh"
fi fi
...@@ -290,16 +306,16 @@ function bfaaccount ...@@ -290,16 +306,16 @@ function bfaaccount
function create_account function create_account
{ {
local num=0 local num filename plural
local filename num=0
for filename in ${BFANODEDIR}/keystore/* for filename in "${BFANODEDIR}"/keystore/*
do do
test -f "$filename" && test -f "$filename" &&
num=$(( num + 1 )) num=$(( num + 1 ))
done done
if [ "$num" -gt 0 ] if [ "$num" -gt 0 ]
then then
local plural="" plural=""
if [ "$num" -ne 1 ] if [ "$num" -ne 1 ]
then then
plural="s" plural="s"
...@@ -310,20 +326,20 @@ function create_account ...@@ -310,20 +326,20 @@ function create_account
return return
fi fi
fi fi
geth --cache 0 --datadir ${BFANODEDIR} --password /dev/null account new geth --cache 0 --datadir "${BFANODEDIR}" --password /dev/null account new
} }
register_subcommand 'truncatelog' 'truncatelog' \ register_subcommand 'truncatelog' 'truncatelog' \
'Truncate the log file. You may want to stop the background processes first.' 'Truncate the log file. You may want to stop the background processes first.'
function truncatelog function truncatelog
{ {
true > ${BFANODEDIR}/log true > "${BFANODEDIR}/log"
} }
register_subcommand 'bfageth' 'bfageth' 'Start geth for BFA.' register_subcommand 'bfageth' 'bfageth' 'Start geth for BFA.'
function bfageth function bfageth
{ {
exec geth --config ${BFANETWORKDIR}/conf.bfa2018.local+full+archive "$@" exec geth --config "${BFANETWORKDIR}/conf.bfa2018.local+full+archive" "$@"
} }
function bfaadmin function bfaadmin
...@@ -339,7 +355,7 @@ function bfaadmin ...@@ -339,7 +355,7 @@ function bfaadmin
create_account create_account
;; ;;
*) *)
echo Usage: `basename $0` "{bootnode|syncmode|account}" echo "Usage: $(basename "$0") {bootnode|syncmode|account}"
trap '' ERR trap '' ERR
exit 1 exit 1
esac esac
...@@ -348,25 +364,27 @@ function bfaadmin ...@@ -348,25 +364,27 @@ function bfaadmin
register_subcommand 'tail' 'bfatail' 'tail -f on the logfile.' register_subcommand 'tail' 'bfatail' 'tail -f on the logfile.'
function bfatail function bfatail
{ {
exec tail -n 100 -F ${BFANODEDIR}/log exec tail -n 100 -F "${BFANODEDIR}/log"
exit 1 exit 1
} }
register_subcommand 'log' 'bfalog' 'Open the logfile with less(1).' register_subcommand 'log' 'bfalog' 'Open the logfile with less(1).'
function bfalog function bfalog
{ {
exec less ${BFANODEDIR}/log exec less "${BFANODEDIR}/log"
exit 1 exit 1
} }
function main function main
{ {
case "$(basename $0)" in case "$(basename "$0")" in
'bfa') 'bfa')
local cmd local cmd
cmd=$1 cmd=$1
shift || _usage shift || _usage
test -n "$cmd" && test -n "${commands[$cmd]}" || _usage test -n "$cmd" || _usage
test -n "${commands[$cmd]}" || _usage
# shellcheck disable=SC2086
eval ${commands[$cmd]} "$*" eval ${commands[$cmd]} "$*"
;; ;;
'admin.sh') 'admin.sh')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment