diff --git a/README.md b/README.md index 2f5023738240afe205561931a63ae48a9972a782..32cbaa6fd50dc72ec83e338ed0980a3ca331bf8d 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ - `npm install web3` 3. Install the BFA software: - `git clone https://gitlab.bfa.ar/blockchain/nucleo.git` -4. `source ${HOME}/bfa/bin` +4. `source ${HOME}/bfa/bin/env` - You can include this line in your .bash_profile if you want. It is perfectly safe to source it multiple times. - `echo 'source ${HOME}/bfa/bin' > ~/.bashrc` 5. Add crontab to start on system reboot (if you are running a sealer you MUST do this): diff --git a/bin/installbfa.sh b/bin/installbfa.sh new file mode 100755 index 0000000000000000000000000000000000000000..9ab1b85a8da14ddac30ee5d8c75650cc643d6e54 --- /dev/null +++ b/bin/installbfa.sh @@ -0,0 +1,228 @@ +#!/bin/bash + +NODEJSPGP=0x68576280 + +trap 'exit 1' ERR +set -o errtrace +# Be verbose +set -e + +if [ `id -u` -ne 0 ] +then + exec sudo $0 +fi + +function info +{ + echo + echo '***' + echo "*** $@" + echo '***' +} + +function yesno +{ + local defval=$1 + shift + local yn="yN" + test "${defval,,}" = "y" && + yn="Yn" + REPLY= + while [ "${REPLY}" != "y" -a "${REPLY}" != "n" ] + do + read -s -n 1 -p "${*}? [${yn}]: " + test -z "${REPLY}" && + REPLY=${defval:0:1} + REPLY=${REPLY,,} + test "$REPLY" != "y" -a "$REPLY" != "n" && + REPLY= + done + echo $REPLY + if [ "$REPLY" = "n" ] + then + return 1 + fi + return 0 +} + +# For getting a recent nodejs +function nodejs_pre +{ + info nodejs + pkgs="$pkgs nodejs" + # Nodejs software repository PGP key + if [ `apt-key export ${NODEJSPGP} 2>&1 | wc -l` -le 50 ] + then + yesno n Add nodejs software repository PGP key && + apt-key adv --keyserver keyserver.ubuntu.com --recv ${NODEJSPGP} + fi + local file=/etc/apt/sources.list.d/nodejs.list + if [ ! -r "$file" ] + then + info Adding nodejs repository to apt sources. + echo "deb https://deb.nodesource.com/node_7.x $(lsb_release -sc) main" > $file + info And now updating the software package list. + apt update + fi +} + +function nodejs_post +{ + info Initialising nodejs. + test -r "package.json" || + npm init -y + # nodejs package(s) that we need. + nodejs -e 'require("web3")' 2>/dev/null && return + yesno n Installing nodejs module: web3 + npm install web3 +} + +function golang_pre +{ + unset installedusrlocalgo + if [ ! -d /usr/local/go ] + then + installedusrlocalgo=1 + info Downloading package of go binaries. + mkdir -p ~/new + cd ~/new + local arch= + case "$(uname -m)" in + x86_64) + arch=amd64 + ;; + *) + echo Do not know how to help you. >&2 + exit 1 + ;; + esac + + #Download go*.linux-${ARCH}.tar.gz from https://golang.org/dl/ + golangurl=$( + curl -f https://golang.org/dl/ 2>/dev/null | + grep linux-${arch}.tar.gz | + grep downloadBox | + sed 's/.*href="//;s/".*//' | + head -1 + ) + name=$( basename $golangurl ) + if [ -r "$name" ] + then + # If we have the download, check if it is corrupt + tar -ztf "$name" >/dev/null 2>&1 || + rm -f "$name" + fi + if [ ! -r "$name" ] + then + curl -f -O $golangurl || + rm -f "$name" + fi + # Integrity checking the archive + tar -xzf "$name" + info Unpacking $name into /usr/local + tar -C /usr/local -xzf go*.tar.gz + fi + PATH=${PATH}:/usr/local/go/bin +} + +function geth_pre +{ + mkdir -p ~/new + cd ~/new + info Download geth source code. + test -d go-ethereum || + git clone https://github.com/ethereum/go-ethereum + cd ~/new/go-ethereum + info Running git pull to ensure that the local go-ethereum repo is up-to-date. + git pull +} + +function geth_compile +{ + cd ~/new/go-ethereum + info Compiling geth + make geth +} + +function geth_post +{ + mkdir -p ~bfa/bin + cp -vp ~/new/go-ethereum/build/bin/geth ~bfa/bin/ + chown -R bfa:bfa ~bfa +} + +function aptinstall +{ + local inst= + for pkg in $pkgs + do + dpkg --verify $pkg 2>/dev/null || + inst="$inst $pkg" + done + test -n "$inst" && + apt --install-suggests -y install $inst || true +} + +function usersetup +{ + if echo ~bfa | grep -qvE '^/' && yesno n Add required user \"bfa\" + then + info Adding BFA user \"bfa\". + adduser --disabled-password --gecos 'Blockchain Federal Argentina' bfa + fi + cd ~bfa + # cloning if not done already + if [ ! -d ~bfa/bfa ] + then + git clone https://gitlab.bfa.ar/blockchain/nucleo.git bfa + fi + # updating + cd bfa + git pull + # + cd ~bfa + if expand < .bashrc | grep -Evq 'source .*bfa/bin/env' + then + info Adding to automatically source \~bfa/bfa/bin/env via .bashrc + echo 'source ~/bfa/bin/env' >> .bashrc + fi +} + +function golang_post +{ + if [ -n "${installedusrlocalgo}" ] + then + yesno n Remove unneeded go installation /usr/local/go && + rm -rf /usr/local/go + fi +} + +function cronit +{ + if crontab -u bfa -l | expand | grep -qvE '^@reboot +bfa/bin/cron.sh$' + then + yesno n Install crontab to start automatically upon reboot || return + ( crontab -u bfa -l ; echo '@reboot bfa/bin/cron.sh' ) | crontab -u bfa - + fi +} + +function welcome +{ + echo + echo "(re)log in as user bfa" +} + + +# development tools +pkgs="$pkgs curl build-essential git" +nodejs_pre +aptinstall +usersetup +nodejs_post +golang_pre +geth_pre +geth_compile +geth_post +golang_post +cronit +welcome