44uk
11/20/2017 - 4:16 AM

nis_mainnet_setup.sh

nis_mainnet_setup.sh

#!/bin/bash
# curl -kL ${RAW_SOURCE_ADDRESS} | bash
BASE_URL=https://nem.ninja
TMP_DIR=.cache-mainnet
BLOCK_SIZE=1453k
#VERSION=0.6.95

java -version 2>&1 | grep 1.8
[ $? -eq 1 ] && apt-get update && apt-get install openjdk-8-jre java-common -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-74 \
  | 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.h2-$BLOCK_SIZE.db.zip ] \
  && curl -o $TMP_DIR/nis5_mainnet.h2-$BLOCK_SIZE.db.zip $BASE_URL/nis5_mainnet.h2-$BLOCK_SIZE.db.zip

[ ! -e $HOME/nem/nis/data ] \
  && mkdir -p $HOME/nem/nis/data
[ ! -e $HOME/nem/nis/data/nis5_mainnet.h2.db ] \
  && jar xf $TMP_DIR/nis5_mainnet.h2-$BLOCK_SIZE.db.zip && mv nis5_mainnet.h2.db $HOME/nem/nis/data/

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

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

[Install]
WantedBy=multi-user.target
__EOD__

mv $TMP_DIR/nis.service /etc/systemd/system/
systemctl enable nis
systemctl start nis

# preparing for https
add-apt-repository ppa:gslin/dehydrated
apt-get update && apt-get install stunnel4 dehydrated -y --allow-unauthenticated