Skip to content

Instantly share code, notes, and snippets.

@AlexeyALeonov
Last active January 27, 2018 20:03
Show Gist options
  • Save AlexeyALeonov/ad4504404dfd6259aab2581e94162fec to your computer and use it in GitHub Desktop.
Save AlexeyALeonov/ad4504404dfd6259aab2581e94162fec to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# taken from the https://www.reddit.com/r/ethereum/comments/6bkg4r/5_eth_for_anyone_puts_storj_farmer_on_qnap/
#
# Run this using a command-line similar to the following:
# apt-get update && apt-get install curl -y && bash <(curl -s -L https://goo.gl/xqS97L)
#
# Detect Debian users running the script with "sh" instead of bash
if readlink /proc/$$/exe | grep -qs "dash"; then
echo "This script needs to be run with bash, not sh"
exit 1
fi
if [[ "$EUID" -ne 0 ]]; then
echo "Sorry, you need to run this as root"
exit 2
fi
# Check that curl is installed
if [ -z $(which curl) ]; then
echo ""
echo "Missing curl binary, installing first ..."
echo ""
apt-get update && apt-get install curl -y
fi
# Use colors, but only if connected to a terminal, and that terminal
# supports them.
NO_COLORS=false
if which tput >/dev/null 2>&1; then
ncolors=$(tput colors)
fi
if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BLUE="$(tput setaf 4)"
BOLD="$(tput bold)"
NORMAL="$(tput sgr0)"
else
NO_COLORS=true
RED=""
GREEN=""
YELLOW=""
BLUE=""
BOLD=""
NORMAL=""
fi
# Only enable exit-on-error after the non-critical colorization stuff,
# which may fail on systems lacking tput or terminfo
set -e
main() {
# File to check if installed
INSTALLATION_CHECK_FILE="/etc/.storj-installed"
# Check if already installed
if [[ -f "$INSTALLATION_CHECK_FILE" ]]; then
print_already_installed
exit
fi
###
# Configuration starts here.
###
clear
printf "${BOLD}Welcome to the Storj farmer installation.${NORMAL}\n"
echo ""
echo "I need to ask a few questions before the installation."
echo "In a case where you don't know or want to use the default, just press ENTER."
echo ""
# Detect public IP address
EXTERNAL_IP=$(curl -s "http://ipecho.net/plain")
# NAT detection
BEHIND_NAT=false
INTERNAL_IP=$(ip addr | grep 'inet' | grep -v inet6 | grep -vE '127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -1)
if [[ "$INTERNAL_IP" != "$EXTERNAL_IP" ]]; then
BEHIND_NAT=true
fi
PORT=4000
DISK_SIZE="2GB"
STORAGE_PATH="/root/storjfarmer"
CONFIG_FILENAME="config.json"
WALLET_ADDRESS=""
NUM_TUNNEL_CONNS=2
TUNNEL_START_PORT=4001
TUNNEL_END_PORT=4003
printf "${YELLOW}First, what is your external (public) IPv4 address or hostname?${NORMAL}\n"
read -p "IP address/Hostname: " -e -i $EXTERNAL_IP EXTERNAL_IP
echo ""
printf "${YELLOW}What is your ERC20 wallet address? (Required)${NORMAL}\n"
read -p "Wallet address: " -e WALLET_ADDRESS
echo ""
printf "${YELLOW}What port should Storjfarmer run on?${NORMAL}\n"
read -p "Port number: " -e -i $PORT PORT
echo ""
printf "${YELLOW}Where should I store the data?${NORMAL}\n"
read -p "Directory: " -e -i $STORAGE_PATH STORAGE_PATH
echo ""
# Create folder if it doesn't exist
mkdir -p $STORAGE_PATH
printf "${YELLOW}How much space can you share? Accepted formats are GB/TB. For example, 2TB.${NORMAL}\n"
read -p "Disk space: " -e -i $DISK_SIZE DISK_SIZE
echo ""
printf "${YELLOW}How many tunnel connections to open for other farmers?${NORMAL}\n"
read -p "Number of tunnel connections: " -e -i $NUM_TUNNEL_CONNS NUM_TUNNEL_CONNS
echo ""
printf "${YELLOW}Enter the starting port number for the tunnel server.${NORMAL}\n"
read -p "Starting port number: " -e -i $TUNNEL_START_PORT TUNNEL_START_PORT
echo ""
printf "${YELLOW}Enter the ending port number for the tunnel server.${NORMAL}\n"
read -p "Ending port number: " -e -i $TUNNEL_END_PORT TUNNEL_END_PORT
echo ""
# End of configuration
echo ""
read -p "${BOLD}Thanks! The installation will start now. Press ENTER to continue.${NORMAL}"
###
# Installation starts here.
###
touch $INSTALLATION_CHECK_FILE
printf "${YELLOW}Installing required packages ...${NORMAL}\n"
apt-get install supervisor wget git npm python -y
printf "${YELLOW}Installing n for Node.js version management ... ${NORMAL}\n"
npm install -g n
printf "${YELLOW}Installing latest version of Node.js using n ...${NORMAL}\n"
n lts
print_installed_node_ver
# Clear the Bash exec cache so it will load the new versions
hash -r
printf "${YELLOW}Installing yarn for package management ...${NORMAL}\n"
npm install -g yarn &> /dev/null # fix npm#15914
printf "${YELLOW}Installing storjshare-daemon (this might take a while) ...${NORMAL}\n"
yarn global add storjshare-daemon --ignore-engines --silent --no-progress &> /dev/null
# Configure Storj
CONFIG_PATH="$STORAGE_PATH/$CONFIG_FILENAME"
storjshare create \
--storj "$WALLET_ADDRESS" \
--storage "$STORAGE_PATH" \
--size "$DISK_SIZE" \
--rpcaddress "$EXTERNAL_IP" \
--rpcport "$PORT" \
--manualforwarding \
--maxtunnels "$NUM_TUNNEL_CONNS" \
--tunnelportmin "$TUNNEL_START_PORT" \
--tunnelportmax "$TUNNEL_END_PORT" \
-o "$CONFIG_PATH" \
--noedit
echo ""
# FIX: printf -- invalid option
printf "${BOLD}"
echo "-- Storjfarmer is configured! --"
printf "${NORMAL}"
echo ""
echo "You need to do some manual tasks before continuing."
# NAT detection
if [[ $BEHIND_NAT ]]; then
echo ""
echo "I have detected that you are behind a NAT."
echo "In this case, you will need to make sure that you have set up the following port forwards:"
printf "${GREEN}"
printf "%d/tcp -> %s:%d\n" "$PORT" "$INTERNAL_IP" "$PORT"
# Loop for min->max ports
for i in `seq "$TUNNEL_START_PORT" "$TUNNEL_END_PORT"`;
do
printf "%d/tcp -> %s:%d\n" "$i" "$INTERNAL_IP" "$i"
done
printf "${NORMAL}"
echo ""
fi
echo "If you have a firewall, ensuring it is allowing the following ports:"
printf "${GREEN}"
printf "%d/tcp\n" "$PORT"
# Loop for min->max ports
for i in `seq "$TUNNEL_START_PORT" "$TUNNEL_END_PORT"`;
do
printf "%d/tcp\n" "$i"
done
printf "${NORMAL}"
echo ""
read -p "${BOLD}Once you have confirmed the above, press ENTER to start the Storj farmer.${NORMAL}"
printf "${YELLOW}Installing supervisor config ...${NORMAL}\n"
install_supervisor_conf $CONFIG_PATH
printf "${YELLOW}Running cleanup task ...${NORMAL}\n"
cleanup
printf "${YELLOW}Giving a few seconds for Storj service to start ...\n${NORMAL}"
sleep 5
printf "${GREEN}Storj farmer status:${NORMAL}\n"
# Fix for coloring issues in some terminals
if [[ $NO_COLORS ]]; then
storjshare status | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
else
storjshare status
fi
}
print_already_installed() {
clear
printf "${RED}"
echo "--- ERROR ---"
printf "${NORMAL}"
echo ""
echo 'Hey, it seems you have already run this installation!'
echo 'In that case, you should delete this container and restart the installation,'
echo 'because it may have left some files behind in the previous installation attempt.'
echo ""
echo ""
}
print_installed_node_ver() {
NODE_VERSION=$(node -v)
printf "${BLUE}Node.js ${NODE_VERSION} installed!${NORMAL}"
echo ""
}
cleanup() {
apt-get remove npm -y
apt-get autoremove -y
}
install_supervisor_conf() {
STORJSHARE_BIN=`which storjshare`
# Daemon supervisor
printf "[program:storj-daemon]
command=%s daemon
autostart=true
autorestart=true" $STORJSHARE_BIN >> /etc/supervisor/conf.d/storj-daemon.conf
# Farmer supervisor
printf "[program:storj-farmer]
command=%s start --config %s
autostart=true
autorestart=true" $STORJSHARE_BIN $1 >> /etc/supervisor/conf.d/storj-farmer.conf
# Update supervisor and enable
service supervisor restart
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment