#!/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 || grep VERSION_CODENAME /etc/os-release) 2>/dev/null | cut -d'=' -f2 ) #APT Vars APT_ENV="DEBIAN_FRONTEND=noninteractive NEEDRESTART_MODE=a" 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_ENV apt-get install git curl apt-transport-https bc wget gnupg -yq 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 (echo -e "Package: nodejs\nPin: origin deb.nodesource.com\nPin-Priority: 999" | sudo tee /etc/apt/preferences.d/nodesource) curl -sL https://deb.nodesource.com/gpgkey/nodesource.gpg.key | gpg --dearmor | sudo tee /usr/share/keyrings/nodesource.gpg >/dev/null (echo "deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_16.x ${DEB_ID} main" | sudo tee /etc/apt/sources.list.d/nodesource.list) sudo apt-get update sudo $APT_ENV apt-get install nodejs -yq elif [[ ! -z $RPM ]]; then sudo yum install gcc-c++ make -y curl -sL https://rpm.nodesource.com/setup_16.x | sudo -E bash - > /dev/null 2>&1 fi success "Installed node.js & npm!" heading "Installing Yarn..." if [[ ! -z $DEB ]]; then curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null (echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list) sudo apt-get update sudo $APT_ENV apt-get install yarn -yq 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 PostgreSQL..." if [[ ! -z $DEB ]]; then curl -sL https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /usr/share/keyrings/pgdg.gpg >/dev/null (echo "deb [signed-by=/usr/share/keyrings/pgdg.gpg] http://apt.postgresql.org/pub/repos/apt ${DEB_ID}-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list ) sudo apt-get update sudo $APT_ENV apt-get install postgresql -yq 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 program dependencies..." if [[ ! -z $DEB ]]; then sudo $APT_ENV apt-get install build-essential pkg-config libtool autoconf automake libpq-dev jq libjemalloc-dev -yq 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 NTP..." sudo timedatectl set-ntp off > /dev/null 2>&1 || true # disable the default systemd timesyncd service if [[ ! -z $DEB ]]; then sudo $APT_ENV apt-get install ntp -yq 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_ENV apt-get upgrade -yq sudo $APT_ENV apt-get dist-upgrade -yq sudo apt-get autoremove -yq 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 ; 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 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"