Skip to content

Instantly share code, notes, and snippets.

@nolash
Last active December 3, 2017 23:27
Show Gist options
  • Save nolash/d8088d4d84128a6abf8ee98735a107dc to your computer and use it in GitHub Desktop.
Save nolash/d8088d4d84128a6abf8ee98735a107dc to your computer and use it in GitHub Desktop.
Script for polling bzzd.ipc when starting swarm
#!/bin/bash
function killswarm {
kill -TERM $1
exit 1
}
# take the newest key if none is given
function latest_key {
echo -n `find $1/keystore -regex ".*--[a-fA-F0-9].*" | sort -r | head -n1 | sed -e 's/.*Z--\([a-fA-F0-9]*\)$/\1/g'`
}
while test $# != 0
do
case "$1" in
-d) args_d=$2; shift;; #datadir
-a) args_a=$2; shift;; #account
-e) args_e=$2; shift;; #ens-api path
-p) args_p=$2; shift;; #p2p port
-x) args_x=$2; shift;; #command to run after ipc is up
*) break;;
esac
shift
done
DATADIR=${args_d:-~/.ethereum}
if [ ! -d $DATADIR ]; then
>&2 echo "Datadir $DATADIR does not exist"
exit 1
fi
ACCOUNT=${args_a:-$(latest_key $DATADIR)}
SWARM=$GOPATH/bin/swarm
ETHAPI=${args_e}
PORT=${args_p:+" --port $args_p"}
args=(
--bzzaccount "${ACCOUNT}"
--datadir "${DATADIR}"
--ens-api "${args_e}"
$PORT
)
function ipcexec {
if [ ! -z ${args_x} ]; then
${args_x} ${DATADIR} ${ACCOUNT}
exit $?
fi
}
($SWARM "${args[@]}" < pass) &
for i in {1..5}; do
if [ -e $DATADIR/bzzd.ipc ]; then
ipcowner=`lsof -t $DATADIR/bzzd.ipc`
if [ "$!" == "$ipcowner" ]; then
ipcexec && exit 0
else
if [ ! -z ${ipcowner} ]; then
>&2 echo "Process ${ipcowner} already owns the this socket, exiting..." && killswarm $!
fi
fi
fi
>&2 echo "polling bzzd.ipc"
sleep 1
done
>&2 echo "gave up waiting for ipc, killing swarm"
killswarm $!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment