From edae7b1e005a87b8c1124abb87fd27e50eb45b87 Mon Sep 17 00:00:00 2001 From: Tiago Tarifa Munhoz Date: Thu, 9 Sep 2021 09:09:31 -0300 Subject: [PATCH] Fix jq errors with sha256sum API KEY with end '-' --- docker-scripts/configure.sh | 50 ++++++++++++------------- docker-scripts/run.sh | 73 +++++++++++++++++++------------------ 2 files changed, 62 insertions(+), 61 deletions(-) diff --git a/docker-scripts/configure.sh b/docker-scripts/configure.sh index 8db9875..4791365 100644 --- a/docker-scripts/configure.sh +++ b/docker-scripts/configure.sh @@ -1,62 +1,62 @@ #!/bin/bash -e -config_json=/app/appsettings.json -runtime_config=/app/LiGet.runtimeconfig.json +declare -r config_json=/app/appsettings.json +declare -r runtime_config=/app/LiGet.runtimeconfig.json if [ "${LIGET_SKIP_APPCONFIG_GEN}" == "true" ]; then echo "LIGET_SKIP_APPCONFIG_GEN is set. Will not generate $config_json" else - cat $config_json | jq --arg cfg ${LIGET_API_KEY_HASH} '. | .ApiKeyHash = $cfg' | sponge $config_json + jq --arg cfg "${LIGET_API_KEY_HASH}" '. | .ApiKeyHash = $cfg' "$config_json" | sponge "$config_json" # .Database - cat $config_json | jq --arg cfg ${LIGET_EF_RUN_MIGRATIONS} '. | .Database.RunMigrations = ($cfg == "true")' | sponge $config_json - cat $config_json | jq --arg cfg ${LIGET_DB_TYPE} '. | .Database.Type = $cfg' | sponge $config_json - cat $config_json | jq --arg cfg "${LIGET_DB_CONNECTION_STRING}" '. | .Database.ConnectionString = $cfg' | sponge $config_json + jq --arg cfg "${LIGET_EF_RUN_MIGRATIONS}" '. | .Database.RunMigrations = ($cfg == "true")' "$config_json" | sponge "$config_json" + jq --arg cfg "${LIGET_DB_TYPE}" '. | .Database.Type = $cfg' "$config_json" | sponge "$config_json" + jq --arg cfg "${LIGET_DB_CONNECTION_STRING}" '. | .Database.ConnectionString = $cfg' "$config_json" | sponge "$config_json" if [ "${LIGET_STORAGE_BACKEND}" == "simple2" ]; then - cat $config_json | jq '.| .Storage .Type = "FileSystem"' | sponge $config_json - cat $config_json | jq --arg cfg ${LIGET_SIMPLE2_ROOT_PATH} '. | .Storage.Path = $cfg' | sponge $config_json + jq '.| .Storage .Type = "FileSystem"' "$config_json" | sponge "$config_json" + jq --arg cfg "${LIGET_SIMPLE2_ROOT_PATH}" '. | .Storage.Path = $cfg' "$config_json" | sponge "$config_json" fi # .Search - cat $config_json | jq --arg cfg ${LIGET_SEARCH_PROVIDER} '. | .Search.Type = $cfg' | sponge $config_json + jq --arg cfg "${LIGET_SEARCH_PROVIDER}" '. | .Search.Type = $cfg' "$config_json" | sponge "$config_json" # .Cache - cat $config_json | jq --arg cfg ${LIGET_CACHE_ENABLED} '. | .Cache.Enabled = ($cfg == "true")' | sponge $config_json - cat $config_json | jq --arg cfg ${LIGET_CACHE_PROXY_SOURCE_INDEX} '. | .Cache.UpstreamIndex = $cfg' | sponge $config_json + jq --arg cfg "${LIGET_CACHE_ENABLED}" '. | .Cache.Enabled = ($cfg == "true")' "$config_json" | sponge "$config_json" + jq --arg cfg "${LIGET_CACHE_PROXY_SOURCE_INDEX}" '. | .Cache.UpstreamIndex = $cfg' "$config_json" | sponge "$config_json" if [ "${LIGET_NUPKG_CACHE_BACKEND}" == "simple2" ]; then - cat $config_json | jq --arg cfg ${LIGET_NUPKG_CACHE_SIMPLE2_ROOT_PATH} '. | .Cache.PackagesPath = $cfg' | sponge $config_json + jq --arg cfg "${LIGET_NUPKG_CACHE_SIMPLE2_ROOT_PATH}" '. | .Cache.PackagesPath = $cfg' "$config_json" | sponge "$config_json" fi - cat $config_json | jq --arg cfg ${LIGET_BAGET_COMPAT_ENABLED} '. | .BaGetCompat.Enabled = ($cfg == "true")' | sponge $config_json + jq --arg cfg "${LIGET_BAGET_COMPAT_ENABLED}" '. | .BaGetCompat.Enabled = ($cfg == "true")' "$config_json" | sponge "$config_json" if [ "${LIGET_LOG_BACKEND}" == "console" ]; then - cat $config_json | jq --arg cfg ${LIGET_LOG_LEVEL} '. | .Logging.Console.LogLevel.Default = $cfg' | sponge $config_json + jq --arg cfg "${LIGET_LOG_LEVEL}" '. | .Logging.Console.LogLevel.Default = $cfg' "$config_json" | sponge "$config_json" elif [ "${LIGET_LOG_BACKEND}" == "gelf" ]; then if [ -z "${LIGET_LOG_GELF_HOST}" ]; then echo "LIGET_LOG_GELF_HOST must be specified when logging backend is gelf" exit 1 fi - cat $config_json | jq --arg cfg ${LIGET_LOG_LEVEL} '. | .Logging.GELF.LogLevel.Default = $cfg' | sponge $config_json - cat $config_json | jq --arg cfg ${LIGET_LOG_GELF_HOST} '. | .Graylog.Host = $cfg' | sponge $config_json - cat $config_json | jq --arg cfg ${LIGET_LOG_GELF_SOURCE} '. | .Graylog.LogSource = $cfg' | sponge $config_json - cat $config_json | jq --arg cfg ${LIGET_LOG_GELF_PORT} '. | .Graylog.Port = ($cfg | tonumber)' | sponge $config_json + jq --arg cfg "${LIGET_LOG_LEVEL}" '. | .Logging.GELF.LogLevel.Default = $cfg' "$config_json" | sponge "$config_json" + jq --arg cfg "${LIGET_LOG_GELF_HOST}" '. | .Graylog.Host = $cfg' "$config_json" | sponge "$config_json" + jq --arg cfg "${LIGET_LOG_GELF_SOURCE}" '. | .Graylog.LogSource = $cfg' "$config_json" | sponge "$config_json" + jq --arg cfg "${LIGET_LOG_GELF_PORT}" '. | .Graylog.Port = ($cfg | tonumber)' "$config_json" | sponge "$config_json" if [ -n "${LIGET_LOG_GELF_ENVIRONMENT}" ]; then - cat $config_json | jq --arg cfg ${LIGET_LOG_GELF_ENVIRONMENT} '. | .Graylog.AdditionalFields.environment = $cfg' | sponge $config_json + jq --arg cfg "${LIGET_LOG_GELF_ENVIRONMENT}" '. | .Graylog.AdditionalFields.environment = $cfg' "$config_json" | sponge "$config_json" fi else echo "Unknown logging backend: ${LIGET_LOG_BACKEND}" fi echo "LiGet configuration generated:" - cat $config_json + cat "$config_json" fi if [ "${LIGET_SKIP_RUNTIMECONFIG_GEN}" == "true" ]; then echo "LIGET_SKIP_RUNTIMECONFIG_GEN is set. Will not generate $runtime_config" else - cat $runtime_config | jq --arg cfg ${LIGET_GC_CONCURRENT} '. | .configProperties."System.GC.Concurrent" = ($cfg == "true")' | sponge $runtime_config - cat $runtime_config | jq --arg cfg ${LIGET_GC_SERVER} '. | .configProperties."System.GC.Server" = ($cfg == "true")' | sponge $runtime_config - cat $runtime_config | jq --arg cfg ${LIGET_THREAD_POOL_MIN} '. | .configProperties."System.Threading.ThreadPool.MinThreads" = ($cfg | tonumber)' | sponge $runtime_config - cat $runtime_config | jq --arg cfg ${LIGET_THREAD_POOL_MAX} '. | .configProperties."System.Threading.ThreadPool.MaxThreads" = ($cfg | tonumber)' | sponge $runtime_config + jq --arg cfg "${LIGET_GC_CONCURRENT}" '. | .configProperties."System.GC.Concurrent" = ($cfg == "true")' "$runtime_config" | sponge "$runtime_config" + jq --arg cfg "${LIGET_GC_SERVER}" '. | .configProperties."System.GC.Server" = ($cfg == "true")' "$runtime_config" | sponge "$runtime_config" + jq --arg cfg "${LIGET_THREAD_POOL_MIN}" '. | .configProperties."System.Threading.ThreadPool.MinThreads" = ($cfg | tonumber)' "$runtime_config" | sponge "$runtime_config" + jq --arg cfg "${LIGET_THREAD_POOL_MAX}" '. | .configProperties."System.Threading.ThreadPool.MaxThreads" = ($cfg | tonumber)' "$runtime_config" | sponge "$runtime_config" echo "LiGet runtime configuration generated:" - cat $runtime_config + cat "$runtime_config" fi diff --git a/docker-scripts/run.sh b/docker-scripts/run.sh index 52586e4..b83173b 100644 --- a/docker-scripts/run.sh +++ b/docker-scripts/run.sh @@ -1,14 +1,21 @@ #!/bin/bash set -e -DIRECTORY="/data/simple2" -OWNER_USERNAME="liget" -OWNER_GROUPNAME="liget" +#-> Comments with '->' can be deleted, if you want it. + +#-> It's bad pratice upper case variables. It can accidentaly replace a system variable. +#-> CamelCase is the best choice in my opinion +#-> declare -x = exportable variables (subshells can recognise it) +#-> declare -r = readonly variable (I call it constant) +declare -xr Directory="/data/simple2" +declare -xr OwnerUserName="liget" +declare -xr OwnerGroupName="liget" +declare -x ErrorMsg="" # First deployment bootstrap, we might want to change permissions of mounted volumes -if [ ! -f /data/ef.sqlite/sqlite.db ]; then +if [[ ! -f /data/ef.sqlite/sqlite.db ]]; then echo "Database does not exist yet. Setting up directory access" - mkdir -p /data/simple2 /data/ef.sqlite /cache/simple2 + mkdir -p "$Directory" /data/ef.sqlite /cache/simple2 chown -R liget:liget /data/ /cache/ fi @@ -20,37 +27,30 @@ fi # This is the directory we expect to be mounted as docker volume. # From that directory we know uid and gid. -if [ ! -d "$DIRECTORY" ]; then - echo "$DIRECTORY does not exist, expected to be mounted as docker volume" - exit 1; -fi +#-> A fancy way to report an error +[[ ! -d "$Directory" ]] \ + && ErrorMsg="* $Directory does not exist, expected to be mounted as docker volume\n" -ret=false -getent passwd $OWNER_USERNAME >/dev/null 2>&1 && ret=true +getent passwd $OwnerUserName >/dev/null 2>&1 \ + || ErrorMsg="${ErrorMsg}* User $OwnerUserName does not exist\n" -if ! $ret; then - echo "User $OWNER_USERNAME does not exist" - exit 1; -fi -ret=false -getent passwd $OWNER_GROUPNAME >/dev/null 2>&1 && ret=true -if ! $ret; then - echo "Group $OWNER_GROUPNAME does not exist" - exit 1; +getent passwd $OwnerGroupName >/dev/null 2>&1 \ + || ErrorMsg="${ErrorMsg}* Group $OwnerGroupName does not exist\n" + +if [[ -n "$ErrorMsg" ]]; then + echo -e "$ErrorMsg" + exit 1 fi -NEWUID=$(ls --numeric-uid-gid -d $DIRECTORY | awk '{ print $3 }') -NEWGID=$(ls --numeric-uid-gid -d $DIRECTORY | awk '{ print $4 }') -OLDUID=$(id -u liget) -OLDGID=$(id -g liget) +#-> < <(command) = process substitution: +# https://www.gnu.org/software/bash/manual/html_node/Process-Substitution.html +read s s NewUID NewGID s < <(ls --numeric-uid-gid -d $Directory) +declare -r OldUID=$(id -u liget) +declare -r OldGID=$(id -g liget) -if [[ $NEWUID != $OLDUID && $NEWUID != 0 ]]; then - usermod -u $NEWUID $OWNER_USERNAME -fi -if [[ $NEWGID != $OLDGID && $NEWGID != 0 ]]; then - groupmod -g $NEWGID $OWNER_GROUPNAME -fi -chown $NEWUID:$NEWGID -R /home/liget +[[ "$NewUID" != "$OldUID" && "$NewUID" != 0 ]] && usermod -u "$NewUID" "$OwnerUserName" +[[ "$NewGID" != "$OldGID" && "$NewGID" != 0 ]] && groupmod -g "$NewGID" "$OwnerGroupName" +chown "$NewUID":"$NewGID" -R /home/liget ########################################################################### # Start server @@ -59,16 +59,17 @@ chown $NEWUID:$NEWGID -R /home/liget /usr/bin/configure-liget cd /app -if [ ! -z ${LIGET_IMPORT_ON_BOOT+x} ]; then - if [[ $NEWGID != 0 ]]; then - sudo -u liget -E -H dotnet /app/LiGet.dll import --path ${LIGET_IMPORT_ON_BOOT} +#->I have no ideia why this '+x' are here. Because of it, this 'if' statement will be aways true +if [[ -n "${LIGET_IMPORT_ON_BOOT+x}" ]]; then + if [[ "$NewGID" != 0 ]]; then + sudo -u liget -E -H dotnet /app/LiGet.dll import --path "${LIGET_IMPORT_ON_BOOT}" else echo "WARNING: running liget as root" - dotnet /app/LiGet.dll import --path ${LIGET_IMPORT_ON_BOOT} + dotnet /app/LiGet.dll import --path "${LIGET_IMPORT_ON_BOOT}" fi fi -if [[ $NEWGID != 0 ]]; then +if [[ "$NewGID" != 0 ]]; then exec sudo -u liget -E -H dotnet /app/LiGet.dll else echo "WARNING: running liget as root"