is RUN_AS_USER is set, some commands are executed with sudo

This commit is contained in:
jash
2018-10-29 23:06:09 +01:00
committed by kexkey
parent 71c7aa7170
commit 86eee3ab33

85
dist/setup.sh vendored
View File

@@ -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
}