From 86eee3ab33bd58c1ebadd41bff86d8c93b787c55 Mon Sep 17 00:00:00 2001 From: jash Date: Mon, 29 Oct 2018 23:06:09 +0100 Subject: [PATCH] is RUN_AS_USER is set, some commands are executed with sudo --- dist/setup.sh | 85 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 73 insertions(+), 12 deletions(-) diff --git a/dist/setup.sh b/dist/setup.sh index ef7a6be..35a9fba 100755 --- a/dist/setup.sh +++ b/dist/setup.sh @@ -120,11 +120,32 @@ modify_permissions() { for d in "${directories[@]}" do if [[ -e $d ]]; then - chmod -R og-rwx $d + step " modify permissions: $d" + try chmod -R og-rwx $d + next fi done } +modify_owner() { + if [[ ! ''$RUN_AS_USER == '' ]]; then + local directories=("$BITCOIN_DATAPATH" "$LIGHTNING_DATAPATH" "$PROXY_DATAPATH" "$GATEKEEPER_DATAPATH") + local user=$(id -u $RUN_AS_USER):$(id -g $RUN_AS_USER) + for d in "${directories[@]}" + do + if [[ -e $d ]]; then + step " modify owner \"$RUN_AS_USER\": $d " + if [[ $(id -u) == 0 ]]; then + try chown -R $user $d + else + try sudo chown -R $user $d + fi + next + fi + done + fi +} + configure() { local current_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" ## build setup docker image @@ -168,8 +189,13 @@ copy_file() { local doCopy=0 local sourceFile=$1 local targetFile=$2 + local sudo='' local createBackup=1 + if [[ $4 == 1 ]]; then + sudo='sudo ' + fi + if [[ ! ''$3 == '' ]]; then createBackup=$3 fi @@ -179,12 +205,12 @@ copy_file() { fi if [[ -f $targetFile ]]; then - cmp --silent $sourceFile $targetFile + ${sudo}cmp --silent $sourceFile $targetFile if [[ $? == 1 ]]; then # different content if [[ $createBackup == 1 ]]; then - step " create backup of $targetFile" - try cp $targetFile $targetFile-$(date +"%y-%m-%d-%T") + step " create backup of $targetFile " + try ${sudo}cp $targetFile $targetFile-$(date +"%y-%m-%d-%T") next fi doCopy=1 @@ -197,14 +223,47 @@ copy_file() { if [[ $doCopy == 1 ]]; then local basename=$(basename "$sourceFile") - step " copy $basename" - try cp $sourceFile $targetFile + step " copy $basename " + try ${sudo}cp $sourceFile $targetFile next fi } +create_user() { + #check if user exists + if [[ ! ''$RUN_AS_USER == '' ]]; then + local OS=$(uname -s) + + if [[ $OS == 'Darwin' ]]; then + echo "Automatic user creation not supported on OSX." + echo "Please create the user \"$RUN_AS_USER\" by hand." + else + if [[ ! $RUN_AS_USER ]]; then + echo "No runtime user. Aborting" + exit 1 + fi + + id -u $RUN_AS_USER > /dev/null 2>&1 + if [[ $? == 1 ]]; then + step " create user $RUN_AS_USER " + if [[ $(id -u) == 0 ]]; then + try useradd $RUN_AS_USER + else + try sudo useradd $RUN_AS_USER + fi + next + fi + fi + fi +} + install_docker() { + local sudo=0 + + if [[ ! ''$RUN_AS_USER == '' ]]; then + sudo=1 + fi local archpath=$(uname -m) # compat mode for SatoshiPortal repo @@ -222,9 +281,9 @@ install_docker() { fi if [ -d $GATEKEEPER_DATAPATH ]; then - copy_file $sourceDataPath/gatekeeper/api.properties $GATEKEEPER_DATAPATH/api.properties - copy_file $sourceDataPath/gatekeeper/keys.properties $GATEKEEPER_DATAPATH/keys.properties - copy_file $sourceDataPath/gatekeeper/ip-whitelist.conf $GATEKEEPER_DATAPATH/ip-whitelist.conf + copy_file $sourceDataPath/gatekeeper/api.properties $GATEKEEPER_DATAPATH/api.properties 1 ${sudo} + copy_file $sourceDataPath/gatekeeper/keys.properties $GATEKEEPER_DATAPATH/keys.properties 1 ${sudo} + copy_file $sourceDataPath/gatekeeper/ip-whitelist.conf $GATEKEEPER_DATAPATH/ip-whitelist.conf 1 ${sudo} fi if [ ! -d $PROXY_DATAPATH ]; then @@ -240,7 +299,7 @@ install_docker() { next fi if [ -d $BITCOIN_DATAPATH ]; then - copy_file $sourceDataPath/bitcoin/bitcoin.conf $BITCOIN_DATAPATH/bitcoin.conf + copy_file $sourceDataPath/bitcoin/bitcoin.conf $BITCOIN_DATAPATH/bitcoin.conf 1 ${sudo} fi fi @@ -256,8 +315,8 @@ install_docker() { next fi if [ -d $LIGHTNING_DATAPATH ]; then - copy_file $sourceDataPath/lightning/c-lightning/config $LIGHTNING_DATAPATH/config - copy_file $sourceDataPath/lightning/c-lightning/bitcoin.conf $LIGHTNING_DATAPATH/bitcoin.conf + copy_file $sourceDataPath/lightning/c-lightning/config $LIGHTNING_DATAPATH/config 1 ${sudo} + copy_file $sourceDataPath/lightning/c-lightning/bitcoin.conf $LIGHTNING_DATAPATH/bitcoin.conf 1 ${sudo} fi fi fi @@ -284,6 +343,8 @@ install_docker() { next fi + create_user + modify_owner cowsay }