From 0b172029d1f16d1a4a0f00ba984fef110309ef44 Mon Sep 17 00:00:00 2001 From: Brian Faust Date: Wed, 11 Nov 2020 08:25:05 +0200 Subject: [PATCH] chore: add `install-next.sh` script for devnet installations (#4161) --- install-next.sh | 291 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 291 insertions(+) create mode 100644 install-next.sh diff --git a/install-next.sh b/install-next.sh new file mode 100644 index 0000000000..18be552a76 --- /dev/null +++ b/install-next.sh @@ -0,0 +1,291 @@ +#!/usr/bin/env bash + +set -e + +# Typography +red=$(tput setaf 1) +green=$(tput setaf 2) +yellow=$(tput setaf 3) +lila=$(tput setaf 4) +pink=$(tput setaf 5) +blue=$(tput setaf 6) +white=$(tput setaf 7) +black=$(tput setaf 8) + +bold=$(tput bold) +reset=$(tput sgr0) + +heading () +{ + echo " ${lila}==>${reset}${bold} $1${reset}" +} + +success () +{ + echo " ${green}==>${reset}${bold} $1${reset}" +} + +info () +{ + echo " ${blue}==>${reset}${bold} $1${reset}" +} + +warning () +{ + echo " ${yellow}==>${reset}${bold} $1${reset}" +} + +error () +{ + echo " ${red}==>${reset}${bold} $1${reset}" +} + +# Detect pkg type +DEB=$(which apt-get || :) +RPM=$(which yum || :) + +# Detect SystemV / SystemD +SYS=$([[ -L "/sbin/init" ]] && echo 'SystemD' || echo 'SystemV') + +# Detect Debian/Ubuntu derivative +DEB_ID=$( (grep DISTRIB_CODENAME /etc/upstream-release/lsb-release || grep DISTRIB_CODENAME /etc/lsb-release) 2>/dev/null | cut -d'=' -f2 ) + +if [[ ! -z $DEB ]]; then + success "Running install for Debian derivative" +elif [[ ! -z $RPM ]]; then + success "Running install for RedHat derivative" +else + heading "Unsupported system" + exit 1; +fi + +if [[ $(locale -a | grep ^en_US.UTF-8) ]] || [[ $(locale -a | grep ^en_US.utf8) ]]; then + if ! $(grep -E "(en_US.UTF-8)" "$HOME/.bashrc"); then + # Setting the bashrc locale + echo "export LC_ALL=en_US.UTF-8" >> "$HOME/.bashrc" + echo "export LANG=en_US.UTF-8" >> "$HOME/.bashrc" + echo "export LANGUAGE=en_US.UTF-8" >> "$HOME/.bashrc" + + # Setting the current shell locale + export LC_ALL="en_US.UTF-8" + export LANG="en_US.UTF-8" + export LANGUAGE="en_US.UTF-8" + fi +else + # Install en_US.UTF-8 Locale + if [[ ! -z $DEB ]]; then + sudo locale-gen en_US.UTF-8 + sudo update-locale LANG=en_US.UTF-8 + elif [[ ! -z $RPM ]]; then + sudo localedef -c -i en_US -f UTF-8 en_US.UTF-8 + fi + + # Setting the current shell locale + export LC_ALL="en_US.UTF-8" + export LANG="en_US.UTF-8" + export LANGUAGE="en_US.UTF-8" + + # Setting the bashrc locale + echo "export LC_ALL=en_US.UTF-8" >> "$HOME/.bashrc" + echo "export LANG=en_US.UTF-8" >> "$HOME/.bashrc" + echo "export LANGUAGE=en_US.UTF-8" >> "$HOME/.bashrc" +fi + +heading "Installing system dependencies..." + +if [[ ! -z $DEB ]]; then + sudo apt-get update + sudo apt-get install -y git curl apt-transport-https update-notifier +elif [[ ! -z $RPM ]]; then + sudo yum update -y + sudo yum install git curl epel-release --skip-broken -y +fi + +success "Installed system dependencies!" + +heading "Installing node.js & npm..." + +sudo rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/{npm*,node*,man1/node*} +sudo rm -rf ~/{.npm,.forever,.node*,.cache,.nvm} + +if [[ ! -z $DEB ]]; then + sudo wget --quiet -O - https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key add - + (echo "deb https://deb.nodesource.com/node_12.x ${DEB_ID} main" | sudo tee /etc/apt/sources.list.d/nodesource.list) + sudo apt-get update + sudo apt-get install nodejs -y + +elif [[ ! -z $RPM ]]; then + sudo yum install gcc-c++ make -y + curl -sL https://rpm.nodesource.com/setup_12.x | sudo -E bash - > /dev/null 2>&1 +fi + +success "Installed node.js & npm!" + +heading "Installing Yarn..." + +if [[ ! -z $DEB ]]; then + curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - + (echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list) + + sudo apt-get update + sudo apt-get install -y yarn +elif [[ ! -z $RPM ]]; then + curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo + sudo yum install yarn -y +fi + +success "Installed Yarn!" + +heading "Installing PM2..." + +sudo yarn global add pm2 +pm2 install pm2-logrotate +pm2 set pm2-logrotate:max_size 500M +pm2 set pm2-logrotate:compress true +pm2 set pm2-logrotate:retain 7 + +success "Installed PM2!" + +heading "Installing program dependencies..." + +if [[ ! -z $DEB ]]; then + sudo apt-get install build-essential libcairo2-dev pkg-config libtool autoconf automake python libpq-dev jq libjemalloc-dev -y +elif [[ ! -z $RPM ]]; then + sudo yum groupinstall "Development Tools" -y -q + sudo yum install postgresql-devel jq jemalloc-devel -y -q +fi + +success "Installed program dependencies!" + +heading "Installing PostgreSQL..." + +if [[ ! -z $DEB ]]; then + sudo apt-get update + sudo apt-get install postgresql postgresql-contrib -y +elif [[ ! -z $RPM ]]; then + sudo yum install postgresql-server postgresql-contrib -y + + if [[ "$SYS" == "SystemV" ]]; then + sudo service postgresql initdb + sudo service postgresql start + else + sudo postgresql-setup initdb || true + sudo sed -i '/^host all all 127.0.0.1\/32 ident/ s/ident/md5/' /var/lib/pgsql/data/pg_hba.conf > /dev/null 2>&1 || true + sudo systemctl start postgresql + fi +fi + +success "Installed PostgreSQL!" + +heading "Installing NTP..." + +sudo timedatectl set-ntp off > /dev/null 2>&1 || true # disable the default systemd timesyncd service + +if [[ ! -z $DEB ]]; then + sudo apt-get install ntp -yyq + if [ -z "$(sudo service ntp status |grep running)" ] ; then + sudo ntpd -gq + fi + +elif [[ ! -z $RPM ]]; then + sudo yum install chrony -y -q + + if [ -z "$(sudo service chronyd status |grep running)" ] ; then + sudo chronyd -q + fi +fi + +success "Installed NTP!" + +heading "Installing system updates..." + +if [[ ! -z $DEB ]]; then + sudo apt-get update + sudo apt-get upgrade -yqq + sudo apt-get dist-upgrade -yq + sudo apt-get autoremove -yyq + sudo apt-get autoclean -yq +elif [[ ! -z $RPM ]]; then + sudo yum update + sudo yum clean all +fi + +success "Installed system updates!" + +heading "Installing ARK Core..." + +while ! yarn global add @arkecosystem/core@next ; do + read -p "Installing ARK Core failed, do you want to retry? [y/N]: " choice + if [[ ! "$choice" =~ ^(yes|y|Y) ]] ; then + exit 1 + fi +done + +echo 'export PATH=$(yarn global bin):$PATH' >> ~/.bashrc +export PATH=$(yarn global bin):$PATH +ark config:publish --network=devnet +ark config:cli --channel=next + +success "Installed ARK Core!" + +readNonempty() { + prompt=${1} + answer="" + while [ -z "${answer}" ] ; do + read -p "${prompt}" answer + done + echo "${answer}" +} + +# setup postgres username, password and database +read -p "Would you like to configure the database? [y/N]: " choice + +if [[ "$choice" =~ ^(yes|y|Y) ]]; then + choice="" + while [[ ! "$choice" =~ ^(yes|y|Y) ]] ; do + databaseUsername=$(readNonempty "Enter the database username: ") + databasePassword=$(readNonempty "Enter the database password: ") + databaseName=$(readNonempty "Enter the database name: ") + + echo "database username: ${databaseUsername}" + echo "database password: ${databasePassword}" + echo "database name: ${databaseName}" + read -p "Proceed? [y/N]: " choice + done + + ark env:set --key=CORE_DB_USERNAME --value="${databaseUsername}" + ark env:set --key=CORE_DB_PASSWORD --value="${databasePassword}" + ark env:set --key=CORE_DB_DATABASE --value="${databaseName}" + + userExists=$(sudo -i -u postgres psql -tAc "SELECT 1 FROM pg_user WHERE usename = '${databaseUsername}'") + databaseExists=$(sudo -i -u postgres psql -tAc "SELECT 1 FROM pg_database WHERE datname = '${databaseName}'") + + if [[ $userExists == 1 ]]; then + read -p "The database user ${databaseUsername} already exists, do you want to recreate it? [y/N]: " choice + + if [[ "$choice" =~ ^(yes|y|Y) ]]; then + if [[ $databaseExists == 1 ]]; then + sudo -i -u postgres psql -c "ALTER DATABASE ${databaseName} OWNER TO postgres;" + fi + sudo -i -u postgres psql -c "DROP USER ${databaseUsername}" + sudo -i -u postgres psql -c "CREATE USER ${databaseUsername} WITH PASSWORD '${databasePassword}' CREATEDB;" + fi + else + sudo -i -u postgres psql -c "CREATE USER ${databaseUsername} WITH PASSWORD '${databasePassword}' CREATEDB;" + fi + + if [[ $databaseExists == 1 ]]; then + read -p "The database ${databaseName} already exists, do you want to overwrite it? [y/N]: " choice + + if [[ "$choice" =~ ^(yes|y|Y) ]]; then + sudo -i -u postgres psql -c "DROP DATABASE ${databaseName};" + sudo -i -u postgres psql -c "CREATE DATABASE ${databaseName} WITH OWNER ${databaseUsername};" + elif [[ "$choice" =~ ^(no|n|N) ]]; then + sudo -i -u postgres psql -c "ALTER DATABASE ${databaseName} OWNER TO ${databaseUsername};" + fi + else + sudo -i -u postgres psql -c "CREATE DATABASE ${databaseName} WITH OWNER ${databaseUsername};" + fi +fi + +exec "$BASH"