From 6567d4362d9674b95c5f1638cb2ca76a1428d003 Mon Sep 17 00:00:00 2001 From: Russel Waters Date: Wed, 7 Aug 2019 11:23:21 -0400 Subject: [PATCH] Update entry script (#2160) Use ENTRYPOINT CMD instead of CMD to allow for greater flexibility ex. `docker logs ` after running with `nano_node --wallet_list` ``` EXECUTING: nano_node --wallet_list Wallet ID: 4633783D98EFE13AC7884FB8299E39032358575C4DA696B9800C6EB0AD1634A4 nano_1womoper6rkhq1atc9y5rsqr6jzatia9ch4mezmtxdixhip68aibxtuefadz ``` If vacuum limit is set and db size is greater than limit vacuum before launching daemon container stops if daemon dies allowing docker to restart if configured This is a change from the how things currently run as the size is not checked and the daemon restarted if over size. ``` Usage: /entry.sh nano_node [daemon] [cli_options] [-l] [-v size] daemon start as daemon cli_options nano_node cli options -l log to console -v vacuum database if over size GB on startup /entry.sh bash [other] other bash pass through /entry.sh [*] * usage default: /entry.sh nano_node daemon -l ``` --- docker/node/Dockerfile | 4 +- docker/node/entry.sh | 160 +++++++++++++++++++++++++---------------- 2 files changed, 103 insertions(+), 61 deletions(-) diff --git a/docker/node/Dockerfile b/docker/node/Dockerfile index 5a39c745..61d28e1a 100644 --- a/docker/node/Dockerfile +++ b/docker/node/Dockerfile @@ -19,4 +19,6 @@ COPY docker/node/entry.sh /entry.sh COPY docker/node/config /usr/share/nano/config RUN chmod +x /entry.sh RUN ln -s /usr/bin/nano_node /usr/bin/rai_node -CMD ["/bin/bash", "/entry.sh"] +ENV PATH="${PATH}:/usr/bin" +ENTRYPOINT ["/bin/bash", "/entry.sh"] +CMD ["nano_node daemon -l"] diff --git a/docker/node/entry.sh b/docker/node/entry.sh index bcd47386..653a9fb4 100644 --- a/docker/node/entry.sh +++ b/docker/node/entry.sh @@ -1,23 +1,89 @@ #!/bin/bash -PATH="${PATH:-/bin}:/usr/bin" -export PATH -set -euo pipefail -IFS=$'\n\t' +set -Eeuo pipefail + +usage() { + echo -e \ + "Usage:\n" \ + " $0 nano_node [daemon] [cli_options] [-l] [-v size]\n" \ + " daemon\n" \ + " start as daemon\n\n" \ + " cli_options\n" \ + " nano_node cli options \n\n" \ + " -l\n" \ + " log to console \n\n" \ + " -v\n" \ + " vacuum database if over size GB on startup\n\n" \ + " $0 bash [other]\n" \ + " other\n" \ + " bash pass through\n" \ + " $0 [*]\n" \ + " *\n" \ + " usage\n\n" \ + "default:\n" \ + " $0 nano_node daemon -l" +} + +OPTIND=1 +command=() +IFS=' ' read -r -a TEMP_OPTS <<<"$@" +passthrough=() +db_size=0 +log_to_cerr=0 + +if [ ${#TEMP_OPTS[@]} -lt 2 ]; then + usage + exit 1 +fi + +if [[ "${TEMP_OPTS[0]}" = 'nano_node' ]]; then + unset 'TEMP_OPTS[0]' + command+=("nano_node") + shift; + for i in "${TEMP_OPTS[@]}"; do + case $i in + "daemon" ) + command+=("--daemon") + ;; + * ) + passthrough+=("$i") + ;; + esac + done + for i in "${passthrough[@]}"; do + if [[ "$i" =~ "-v" ]]; then + db_size=${i//-v/} + echo "Vacuum DB if over $db_size GB on startup" + elif [[ "$i" = '-l' ]]; then + echo "\"log_to_cerr\":\"true\"" + log_to_cerr=1 + else + command+=("$i") + fi + done +elif [[ "${TEMP_OPTS[0]}" = 'bash' ]]; then + unset 'TEMP_OPTS[0]' + echo -e "EXECUTING ${TEMP_OPTS[*]}\n" + exec "${TEMP_OPTS[@]}" + exit 0; +else + usage + exit 1; +fi network="$(cat /etc/nano-network)" case "${network}" in - live|'') - network='live' - dirSuffix='' - ;; - beta) - dirSuffix='Beta' - ;; - test) - dirSuffix='Test' - ;; + live|'') + network='live' + dirSuffix='' + ;; + beta) + dirSuffix='Beta' + ;; + test) + dirSuffix='Test' + ;; esac raidir="${HOME}/RaiBlocks${dirSuffix}" @@ -26,59 +92,33 @@ dbFile="${nanodir}/data.ldb" if [ -d "${raidir}" ]; then echo "Moving ${raidir} to ${nanodir}" - mv $raidir $nanodir + mv "$raidir" "$nanodir" else mkdir -p "${nanodir}" fi if [ ! -f "${nanodir}/config.json" ]; then - echo "Config File not found, adding default." - cp "/usr/share/nano/config/${network}.json" "${nanodir}/config.json" - cp "/usr/share/nano/config/${network}_rpc.json" "${nanodir}/rpc_config.json" + echo "Config File not found, adding default." + cp "/usr/share/nano/config/${network}.json" "${nanodir}/config.json" + cp "/usr/share/nano/config/${network}_rpc.json" "${nanodir}/rpc_config.json" fi -# Start watching the log file we are going to log output to -logfile="${nanodir}/nano-docker-output.log" -tail -F "${logfile}" & +if [[ $log_to_cerr -eq 1 ]]; then + sed -i 's/"log_to_cerr": "false",/"log_to_cerr": "true",/g' "${nanodir}/config.json" +else + sed -i 's/"log_to_cerr": "true",/"log_to_cerr": "false",/g' "${nanodir}/config.json" +fi -pid='' -firstTimeComplete='' -while true; do - if [ -n "${firstTimeComplete}" ]; then - sleep 10 - fi - firstTimeComplete='true' - - if [ -f "${dbFile}" ]; then - dbFileSize="$(stat -c %s "${dbFile}" 2>/dev/null)" - if [ "${dbFileSize}" -gt $[1024 * 1024 * 1024 * 20] ]; then - echo "ERROR: Database size grew above 20GB (size = ${dbFileSize})" >&2 - - while [ -n "${pid}" ]; do - kill "${pid}" >/dev/null 2>/dev/null || : - if ! kill -0 "${pid}" >/dev/null 2>/dev/null; then - pid='' - fi - done - - nano_node --vacuum +if [[ "${command[1]}" = "--daemon" ]]; then + if [[ $db_size -ne 0 ]]; then + if [ -f "${dbFile}" ]; then + dbFileSize="$(stat -c %s "${dbFile}" 2>/dev/null)" + if [ "${dbFileSize}" -gt $((1024 * 1024 * 1024 * db_size)) ]; then + echo "ERROR: Database size grew above ${db_size}GB (size = ${dbFileSize})" >&2 + nano_node --vacuum + fi fi fi - - if [ -n "${pid}" ]; then - if ! kill -0 "${pid}" >/dev/null 2>/dev/null; then - pid='' - fi - fi - - if [ -z "${pid}" ]; then - nano_node --daemon & - pid="$!" - fi - - if [ "$(stat -c '%s' "${logfile}")" -gt 4194304 ]; then - cp "${logfile}" "${logfile}.old" - : > "${logfile}" - echo "$(date) Rotated log file" - fi -done >> "${logfile}" 2>&1 +fi +echo -e "EXECUTING: ${command[*]}\n" +exec "${command[@]}"