From 4a60c535757a690cdaba0f6452f0287ea5588d64 Mon Sep 17 00:00:00 2001
From: Robert Martin-Legene <robert@nic.ar>
Date: Fri, 24 May 2019 20:30:50 -0300
Subject: [PATCH] Solving permission problems

---
 bin/installbfa.sh | 71 ++++++++++++++++++++++++-----------------------
 1 file changed, 37 insertions(+), 34 deletions(-)

diff --git a/bin/installbfa.sh b/bin/installbfa.sh
index ebc8cb2..49ccab0 100755
--- a/bin/installbfa.sh
+++ b/bin/installbfa.sh
@@ -25,6 +25,23 @@ function    info
     echo '***'
 }
 
+# Runs as the owner of the given directory, and in the given directory
+function runasownerof
+{
+    path=$1
+    precmd=
+    shift 1
+    pushd $path
+    if [ $( stat --format=%u . ) -ne $UID ]
+    then
+        precmd="sudo --set-home -u $( stat --format=%U . )"
+    fi
+    unset path
+    ${precmd} "$@"
+    popd
+    unset precmd
+}
+
 # For getting a recent nodejs
 function nodejsinstall
 {
@@ -49,16 +66,15 @@ function nodejsinstall
 
 function web3install
 {
-    cd ~bfa
-    test -r package.json ||
+    test -r ~bfa/package.json ||
         info "Initialising nodejs." &&
-        sudo --set-home -u bfa npm init -y
+        runasownerof ~bfa npm init -y
     # nodejs package(s) that we need.
-    echo 'require("web3")' | sudo --set-home -u bfa nodejs 2>/dev/null && return
+    echo 'require("web3")' | runasownerof ~bfa nodejs 2>/dev/null && return
     info "Installing nodejs module: web3 (will show many warnings)"
-    sudo --set-home -u bfa npm install web3
+    runasownerof ~bfa npm install web3
     info "Installing nodejs module: require"
-    sudo --set-home -u bfa npm install require
+    runasownerof ~bfa npm install require
 }
 
 function golanginstall
@@ -114,22 +130,16 @@ function golanginstall
 function gethinstall
 {
     install --verbose --owner=bfa --group=bfa --directory ${NEW}
-    cd       ${NEW}
-    if [ -d go-ethereum ]
+    if [ -d ${NEW}/go-ethereum ]
     then
     	info "Running git pull to ensure that the local go-ethereum repo is up-to-date."
-    	cd      ${NEW}/go-ethereum
-    	sudo -u bfa git pull
+        runasownerof ${NEW}/go-ethereum git pull
     else
     	info "Download geth source code."
-        sudo -u bfa git clone https://github.com/ethereum/go-ethereum
-    	cd      ${NEW}/go-ethereum
+        runasownerof ${NEW} git clone https://github.com/ethereum/go-ethereum
     fi
-    #
     info "Compiling geth"
-    echo PATH=$PATH
-    make all
-    chown -R bfa:bfa .
+    runasownerof . make all
     HISBINDIR=$( echo ~bfa/bin )
     install --verbose --owner=bfa --group=bfa --directory ${HISBINDIR}
     install --verbose --owner=bfa --group=bfa --target-directory=${HISBINDIR} ${NEW}/go-ethereum/build/bin/{geth,bootnode,abigen,ethkey,puppeth,rlpdump,wnode,swarm,swarm-smoke}
@@ -155,29 +165,26 @@ function usersetup
         info "Adding required user \"bfa\""
         adduser --disabled-password --gecos 'Blockchain Federal Argentina' bfa
     fi
-    cd ~bfa
     # If we're running inside a docker, this may already exist but
     # probably owned by root. Let's make sure things are proper.
-    chown -R bfa:bfa .
+    chown -R bfa:bfa ~bfa
     #
 }
 
 function userconfig
 {
-    if [ $( expand < .bashrc | grep -E "source ${BFAHOME}/bin/env" | wc -l ) -eq 0 ]
+    if [ $( expand < ~bfa/.bashrc | grep -E "source ${BFAHOME}/bin/env" | wc -l ) -eq 0 ]
     then
         info "Adding to automatically source ${BFAHOME}/bin/env via .bashrc"
         echo "test -r ${BFAHOME}/bin/env && source ${BFAHOME}/bin/env" >> .bashrc
     fi
     # cloning if not done already, or just update (pull)
-    if [ ! -d "$BFAHOME" ]
+    if [ ! -d "${BFAHOME}" ]
     then
         # initial cloning
-        git clone https://gitlab.bfa.ar/blockchain/nucleo.git $BFAHOME
-        cd "$BFAHOME"
+        runasownerof ${BFAHOME%/*} git clone https://gitlab.bfa.ar/blockchain/nucleo.git $BFAHOME
     else
-        cd "$BFAHOME"
-        git pull
+        runasownerof "${BFAHOME}" git pull
     fi
     if [ ! -e "${BFAHOME}/bin/env" ]
     then
@@ -210,7 +217,8 @@ function setupquestions
     then
         BFAHOME=$( echo ~bfa/bfa )
     fi
-    unset envfile
+    # Default to production
+    envfile=network/env
     if [ ! -e "${BFAHOME}/bin/env" ]
     then
         REPLY=
@@ -225,15 +233,10 @@ function setupquestions
                 echo
             done
         fi
-        case "$REPLY" in
-            2)
-                envfile=test2network/env
-                ;;
-            *)
-                # Default to production
-                envfile=network/env
-                ;;
-        esac
+        if [ "$REPLY" = "2" ]
+        then
+            envfile=test2network/env
+        fi
     fi
 }
 
-- 
GitLab