44uk
5/26/2017 - 3:05 PM

setup_mainnet_nis.sh

setup_mainnet_nis.sh

#!/bin/bash
NIS_URL=http://localhost:7890
BASE_URL=https://bob.nem.ninja
TMP_DIR=.cache
BLOCK_SIZE=1323k
API_CURL_OPTS='-s -H "Accept: application/json" -H "Content-Type: application/json"'

apt-get update && apt-get install openjdk-8-jre -y

[ ! -e $TMP_DIR ] && mkdir $TMP_DIR

# nem!
if [ -e $TMP_DIR/README.txt ]; then
  cat $TMP_DIR/README.txt
else
  curl -s $BASE_URL/README.txt | tee $TMP_DIR/README.txt
fi

# fetch latest NIS version
VERSION=`curl -s $BASE_URL/version.txt`

# download files
[ ! -e $TMP_DIR/nis-$VERSION.tgz ] ¥
  && curl -o $TMP_DIR/nis-$VERSION.tgz $BASE_URL/nis-$VERSION.tgz
[ ! -e $TMP_DIR/nis-$VERSION.tgz.sig ] ¥
  && curl -o $TMP_DIR/nis-$VERSION.tgz.sig $BASE_URL/nis-$VERSION.tgz.sig

# fetch hash
curl http://bigalice3.nem.ninja:7890/transaction/get?hash=`cat $TMP_DIR/nis-$VERSION.tgz.sig | grep txId | cut -d' ' -f2` ¥
  | grep -ioE '"payload":"[0-9a-z]+"' ¥
  | cut -d':' -f2 ¥
  | tr  -d '"' ¥
  | cut -c 11- ¥
  | echo "`cat -`  $TMP_DIR/nis-$VERSION.tgz" ¥
  > $TMP_DIR/nis-$VERSION.sha256sum

# checksum
#sha256sum --check $TMP_DIR/nis-$VERSION.sha256sum
shasum -a 256 -c $TMP_DIR/nis-$VERSION.sha256sum # for mac

if [ $? != 0 ]; then
  echo "Checksum failure! nis-$VERSION.tgz maybe broken or hacked?!"
  exit 1
fi

# extract files
tar zxf $TMP_DIR/nis-$VERSION.tgz

# download db
[ ! -e $TMP_DIR/nis5_mainnet-$BLOCK_SIZE.h2.db.zip ] ¥
  && curl -o $TMP_DIR/nis5_mainnet-$BLOCK_SIZE.h2.db.zip $BASE_URL/nis5_mainnet-$BLOCK_SIZE.h2.db.zip
[ ! -e $TMP_DIR/nis5_mainnet-$BLOCK_SIZE.h2.db.zip.sig ] ¥
  && curl -o $TMP_DIR/nis5_mainnet-$BLOCK_SIZE.h2.db.zip.sig $BASE_URL/nis5_mainnet-$BLOCK_SIZE.h2.db.zip.sig

curl -s http://bigalice3.nem.ninja:7890/transaction/get?hash=`cat $TMP_DIR/nis5_mainnet-$BLOCK_SIZE.h2.db.zip.sig | grep txId | cut -d' ' -f2` ¥
  | grep -ioE '"payload":"[0-9a-z]+"' ¥
  | cut -d':' -f2 ¥
  | tr  -d '"' ¥
  | cut -c 11- ¥
  | echo "`cat -`  $TMP_DIR/nis5_mainnet-$BLOCK_SIZE.h2.db.zip" ¥
  > $TMP_DIR/nis5_mainnet-$BLOCK_SIZE.h2.db.zip.sha256sum
sha256sum --check $TMP_DIR/nis5_mainnet-$BLOCK_SIZE.h2.db.zip.sha256sum
shasum -a 256 -c $TMP_DIR/nis5_mainnet-$BLOCK_SIZE.h2.db.zip.sha256sum

if [ $? != 0 ]; then
  echo "Checksum failure! nis5_mainnet-$BLOCK_SIZE.h2.db.zip maybe broken or hacked?!"
fi

[ ! -e $HOME/nem/nis/data ] ¥
  && mkdir -p $HOME/nem/nis/data
[ ! -e $HOME/nem/nis/data/nis5_mainnet.h2.db ] ¥
  && unzip .cache/nis5_mainnet-$BLOCK_SIZE.h2.db.zip -d $HOME/nem/nis/data/

## custom harvest.sh
cat << __EOD__ > package/harvest.with_wait.sh
#!/bin/bash

if [ ¥$# -ne 1 ]; then
	echo "you need to provide private key";
	exit 1
fi

key=¥`echo ¥$1 | sed 's/[0-9a-fA-F]*//'¥`
if [ -n "¥$key" ];
then
	echo "provided key must be in hexadecimal format"
	exit 2
fi

# additional script { ----
echo -n "waiting for NIS to recieve request."
until (curl -m 1 $API_CURL_OPTS $NIS_URL/status | grep -ioE '"code":6' > /dev/null); do
  echo -n "."
  sleep 1s
done
# ---- }

curl $API_CURL_OPTS -d "{'value':'¥$1'}" $NIS_URL/account/unlock
__EOD__
chmod +x package/harvest.with_wait.sh


## custom nix.runNis.sh
cat << __EOD__ > package/nix.runNis.with_start_harvesting.sh
#!/bin/bash

cd nis
java -Xms512M -Xmx1G -cp ".:./*:../libs/*" org.nem.deploy.CommonStarter
cd -

# additional script { ----
./harvest.with_wait.sh 'private key' > /dev/null 2>&1 &
# ---- }
__EOD__
chmod +x package/nix.runNis.with_start_harvesting.sh


# create nis.service for systemd
cat << __EOD__ > $TMP_DIR/nis.service
[Unit]
Description = NEM Infrastructure Server

[Service]
ExecStart=$HOME/package/nix.runNis.with_start_harvesting.sh
Restart=always
Type=simple

[Install]
WantedBy=multi-user.target
__EOD__

# enable nis.service
# sudo mv $TMP_DIR/nis.service /etc/systemd/system/