Skip to content

Commit

Permalink
Add missing user variables in scripts on update check (#1129)
Browse files Browse the repository at this point in the history
updates to update code for env, CNTools, gLiveView, topologyUpdater & cncli wrapper script.

gLV bumped to 1.23.0
CNTools bumped to 8.7.0
 - CNTools configuration moved from cntools.config to cntools.sh
  • Loading branch information
Scitz0 committed Oct 12, 2021
1 parent 395c20c commit 05bcef0
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 168 deletions.
4 changes: 4 additions & 0 deletions docs/Scripts/cntools-changelog.md
Expand Up @@ -6,6 +6,10 @@ All notable changes to this tool will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [8.7.0] - 2021-10-05
#### Changed
- CNTools configuration moved from cntools.config to cntools.sh

## [8.6.6] - 2021-09-26
#### Fixed
- Pool rotation date calculation fix, 8.6.4 didn't properly fix it
Expand Down
26 changes: 23 additions & 3 deletions scripts/cnode-helper-scripts/cncli.sh
Expand Up @@ -35,6 +35,8 @@ usage() {
Usage: $(basename "$0") [operation <sub arg>]
Script to run CNCLI, best launched through systemd deployed by 'deploy-as-systemd.sh'
-u Skip script update check overriding UPDATE_CHECK value in env (must be first argument to script)
sync Start CNCLI chainsync process that connects to cardano-node to sync blocks stored in SQLite DB (deployed as service)
leaderlog One-time leader schedule calculation for current epoch, then continously monitors and calculates schedule for coming epochs, 1.5 days before epoch boundary on MainNet (deployed as service)
force Manually force leaderlog calculation and overwrite even if already done, exits after leaderlog is calculated
Expand All @@ -52,6 +54,9 @@ usage() {
exit 1
}

SKIP_UPDATE=N
[[ $1 = "-u" ]] && SKIP_UPDATE=Y && shift

if [[ $# -eq 1 ]]; then
subcommand=$1
subarg=""
Expand Down Expand Up @@ -138,17 +143,32 @@ cncliInit() {

. "${PARENT}"/env offline &>/dev/null # ignore any errors, re-sourced later

if [[ "${UPDATE_CHECK}" == "Y" ]]; then
if [[ ${UPDATE_CHECK} = Y && ${SKIP_UPDATE} != Y ]]; then

echo "Checking for script updates..."

# Check availability of checkUpdate function
if [[ ! $(command -v checkUpdate) ]]; then
echo -e "\nCould not find checkUpdate function in env, make sure you're using official guild docos for installation!"
exit 1
fi

# check for env update
! checkUpdate env ${BATCH_AUTO_UPDATE} && exit 1
! checkUpdate cncli.sh ${BATCH_AUTO_UPDATE} && exit 1
ENV_UPDATED=${BATCH_AUTO_UPDATE}
checkUpdate env N N N
case $? in
1) ENV_UPDATED=Y ;;
2) exit 1 ;;
esac

# check for cncli.sh update
checkUpdate cncli.sh ${ENV_UPDATED}
case $? in
1) $0 "-u" "$@"; exit 0 ;; # re-launch script with same args skipping update check
2) exit 1 ;;
esac
fi

# source common env variables in case it was updated
until . "${PARENT}"/env; do
echo "sleeping for 10s and testing again..."
Expand Down
27 changes: 0 additions & 27 deletions scripts/cnode-helper-scripts/cntools.config

This file was deleted.

18 changes: 8 additions & 10 deletions scripts/cnode-helper-scripts/cntools.library
Expand Up @@ -9,9 +9,9 @@
# Major: Any considerable change in the code base, big feature, workflow or breaking change from previous version
CNTOOLS_MAJOR_VERSION=8
# Minor: Changes and features of minor character that can be applied without breaking existing functionality or workflow
CNTOOLS_MINOR_VERSION=6
CNTOOLS_MINOR_VERSION=7
# Patch: Backwards compatible bug fixes. No additional functionality or major changes
CNTOOLS_PATCH_VERSION=6
CNTOOLS_PATCH_VERSION=0

CNTOOLS_VERSION="${CNTOOLS_MAJOR_VERSION}.${CNTOOLS_MINOR_VERSION}.${CNTOOLS_PATCH_VERSION}"

Expand All @@ -25,7 +25,6 @@ if ! mkdir -p "${WALLET_FOLDER}" 2>/dev/null; then myExit 1 "${FG_RED}ERROR${NC}
if ! mkdir -p "${POOL_FOLDER}" 2>/dev/null; then myExit 1 "${FG_RED}ERROR${NC}: Failed to create pool directory: ${POOL_FOLDER}"; fi
if ! mkdir -p "${ASSET_FOLDER}" 2>/dev/null; then myExit 1 "${FG_RED}ERROR${NC}: Failed to create asset directory: ${POOL_ASSET}"; fi
[[ -z ${TIMEOUT_NO_OF_SLOTS} ]] && TIMEOUT_NO_OF_SLOTS=600
[[ ${SHELLEY_TRANS_FILENAME} =~ ^${CNODE_HOME}/db/ ]] && mkdir -p "${CNODE_HOME}/guild-db" && SHELLEY_TRANS_FILENAME="${CNODE_HOME}/guild-db/shelley_trans_epoch"
[[ -z ${WALLET_SELECTION_FILTER_LIMIT} ]] && WALLET_SELECTION_FILTER_LIMIT=10
[[ -z ${KES_ALERT_PERIOD} ]] && KES_ALERT_PERIOD=172800 # default 2 days
[[ -z ${KES_WARNING_PERIOD} ]] && KES_WARNING_PERIOD=604800 # default 7 days
Expand All @@ -34,6 +33,7 @@ if ! mkdir -p "${ASSET_FOLDER}" 2>/dev/null; then myExit 1 "${FG_RED}ERROR${NC}:
[[ -z ${ENABLE_DIALOG} ]] && ENABLE_DIALOG=true
[[ ${ENABLE_ADVANCED} = "true" ]] && ADVANCED_MODE="true"
[[ -z ${CHECK_KES} ]] && CHECK_KES=true
[[ -z ${CNTOOLS_LOG} ]] && CNTOOLS_LOG="${LOG_DIR}/cntools-history.log"

############################################################
# library sourced by cntools with common taskes to perform #
Expand Down Expand Up @@ -130,17 +130,15 @@ protectionPreRequisites() {
! cmdAvailable "gpg" && return 1

if ! cmdAvailable "chattr" &>/dev/null; then
[[ ${ENABLE_CHATTR} = true ]] && println ERROR "chattr command not available but enabled in config, please install or disable in cntools.config and re-run CNTools" && return 1
[[ ${ENABLE_CHATTR} = true ]] && echo -e "chattr command not available but enabled in config, please install or disable in cntools.config and re-run CNTools" && return 1
elif [[ ${ENABLE_CHATTR} = true ]]; then # chattr available and enabled, make sure sudo access to chattr is enabled
touch "${TMP_DIR}"/test
println DEBUG "Testing chattr access permission, enter user password if requested..."
echo -e "Testing chattr access permission, enter user password if requested..."
if ! sudo chattr -i "${TMP_DIR}"/test; then
rm -f "${TMP_DIR}"/test
echo
println ERROR "${FG_YELLOW}WARN${NC}: Elevated privileges needed for chattr command used to write protect wallet and pool keys"
println ERROR "Add required sudo permissions or run the following command to add passwordless sudo access to chattr command for '$(whoami)' user"
echo
println ERROR "echo \"$(whoami) ALL=NOPASSWD: $(command -v chattr)\" | sudo tee /etc/sudoers.d/cntools"
echo -e "\n${FG_YELLOW}WARN${NC}: Elevated privileges needed for chattr command used to write protect wallet and pool keys"
echo -e "Add required sudo permissions or run the following command to add passwordless sudo access to chattr command for '$(whoami)' user"
echo -e "echo \"$(whoami) ALL=NOPASSWD: $(command -v chattr)\" | sudo tee /etc/sudoers.d/cntools"
return 1
fi
rm -f "${TMP_DIR}"/test
Expand Down
195 changes: 110 additions & 85 deletions scripts/cnode-helper-scripts/cntools.sh
@@ -1,6 +1,53 @@
#!/usr/bin/env bash
# shellcheck disable=SC1090,SC2086,SC2154,SC2034,SC2012,SC2140,SC2028

. "$(dirname $0)"/env offline

# legacy config (deprecated and removed in future major version update)
if [[ -f "$(dirname $0)"/cntools.config ]]; then
! . "$(dirname $0)"/cntools.config && exit 1
clear && waitToProceed "${FG_RED}cntools.config deprecated and will be removed in future major version!${NC}\n"\
"Uncomment and set any customization in User Variables section of cntools.sh instead."\
"Once done, delete cntools.config file to get rid of this message.\n"\
"press any key to proceed .."
fi

######################################
# User Variables - Change as desired #
# Common variables set in env file #
######################################

#TIMEOUT_NO_OF_SLOTS=600 # used when waiting for a new block to be created

# log cntools activities (comment or set empty to disable)
# LOG_DIR set in env file
#CNTOOLS_LOG="${LOG_DIR}/cntools-history.log"

# kes rotation warning (in seconds)
# if disabled KES check will be skipped on startup
#CHECK_KES=false
#KES_ALERT_PERIOD=172800 # default 2 days
#KES_WARNING_PERIOD=604800 # default 7 days

# limit for extended wallet selection menu filtering (balance check and delegation status)
# if more wallets exist than limit set these checks will be disabled to improve performance
#WALLET_SELECTION_FILTER_LIMIT=10

# enable or disable chattr used to protect keys from being overwritten [true|false] (not supported on all systems)
# if disabled standard read-only permission is set instead
#ENABLE_CHATTR=true

# enable or disable dialog used to help in file/dir selection by providing a gui to see available files and folders. [true|false] (not supported on all systems)
# if disabled standard tty input is used
#ENABLE_DIALOG=true

# enable advanced/developer features like metadata transactions, multi-asset management etc. [true|false] (not needed for SPO usage)
#ENABLE_ADVANCED=false

######################################
# Do NOT modify code below #
######################################

########## Global tasks ###########################################

# General exit handler
Expand Down Expand Up @@ -34,20 +81,23 @@ usage() {
-o Activate offline mode - run CNTools in offline mode without node access, a limited set of functions available
-a Enable advanced/developer features like metadata transactions, multi-asset management etc (not needed for SPO usage)
-u Skip script update check overriding UPDATE_CHECK value in env
-b Run CNTools and look for updates on alternate branch instead of master of guild repository (only for testing/development purposes)
EOF
}

CNTOOLS_MODE="CONNECTED"
ADVANCED_MODE="false"
SKIP_UPDATE=N
PARENT="$(dirname $0)"
[[ -f "${PARENT}"/.env_branch ]] && BRANCH="$(cat "${PARENT}"/.env_branch)" || BRANCH="master"

while getopts :oab: opt; do
while getopts :oaub: opt; do
case ${opt} in
o ) CNTOOLS_MODE="OFFLINE" ;;
a ) ADVANCED_MODE="true" ;;
u ) SKIP_UPDATE=Y ;;
b ) echo "${OPTARG}" > "${PARENT}"/.env_branch ;;
\? ) myExit 1 "$(usage)" ;;
esac
Expand All @@ -72,45 +122,11 @@ else
. "${PARENT}"/env &>/dev/null
fi

if [[ ${CNTOOLS_MODE} = "CONNECTED" ]]; then
if [[ "${UPDATE_CHECK}" == "Y" ]]; then
echo "Checking for script updates..."
# Check availability of checkUpdate function
if [[ ! $(command -v checkUpdate) ]]; then
echo -e "\nCould not find checkUpdate function in env, make sure you're using official guild docos for installation!"
myExit 1
fi
# check for env update
! checkUpdate env && myExit 1
fi
. "${PARENT}"/env
rc=$?
else
. "${PARENT}"/env offline
rc=$?
fi
case $rc in # ignore exit code 0 and 2, any other exits script
0) : ;; # ok
2) clear ;; # ignore
*) myExit 1 "ERROR: CNTools failed to load common env file\nPlease verify set values in 'User Variables' section in env file or log an issue on GitHub" ;;
esac

# get cntools config parameters
! . "${PARENT}"/cntools.config && myExit 1

# get helper functions from library file
! . "${PARENT}"/cntools.library && myExit 1

archiveLog # archive current log and cleanup log archive folder

exec 6>&1 # Link file descriptor #6 with normal stdout.
exec 7>&2 # Link file descriptor #7 with normal stderr.
[[ -n ${CNTOOLS_LOG} ]] && exec > >( tee >( while read -r line; do logln "INFO" "${line}"; done ) )
[[ -n ${CNTOOLS_LOG} ]] && exec 2> >( tee >( while read -r line; do logln "ERROR" "${line}"; done ) >&2 )
[[ -n ${CNTOOLS_LOG} ]] && exec 3> >( tee >( while read -r line; do logln "DEBUG" "${line}"; done ) >&6 )
exec 8>&1 # Link file descriptor #8 with custom stdout.
exec 9>&2 # Link file descriptor #9 with custom stderr.

# check for required command line tools
if ! cmdAvailable "curl" || \
! cmdAvailable "jq" || \
Expand All @@ -125,60 +141,61 @@ fi
if [[ ${CNTOOLS_MODE} = "CONNECTED" ]]; then
# check to see if there are any updates available
clear
if [[ "${UPDATE_CHECK}" == "Y" ]]; then
println DEBUG "CNTools version check...\n"
if curl -s -f -m ${CURL_TIMEOUT} -o "${PARENT}"/cntools.library.tmp "${URL}/cntools.library" && [[ -f "${PARENT}"/cntools.library.tmp ]]; then
GIT_MAJOR_VERSION=$(grep -r ^CNTOOLS_MAJOR_VERSION= "${PARENT}"/cntools.library.tmp |sed -e "s#.*=##")
GIT_MINOR_VERSION=$(grep -r ^CNTOOLS_MINOR_VERSION= "${PARENT}"/cntools.library.tmp |sed -e "s#.*=##")
GIT_PATCH_VERSION=$(grep -r ^CNTOOLS_PATCH_VERSION= "${PARENT}"/cntools.library.tmp |sed -e "s#.*=##")
GIT_VERSION="${GIT_MAJOR_VERSION}.${GIT_MINOR_VERSION}.${GIT_PATCH_VERSION}"
if ! versionCheck "${GIT_VERSION}" "${CNTOOLS_VERSION}"; then
println DEBUG "A new version of CNTools is available"
echo
println DEBUG "Installed Version : ${FG_LGRAY}${CNTOOLS_VERSION}${NC}"
println DEBUG "Available Version : ${FG_GREEN}${GIT_VERSION}${NC}"
if getAnswer "\nDo you want to upgrade to the latest version of CNTools?"; then
if curl -s -f -m ${CURL_TIMEOUT} -o "${PARENT}"/cntools.sh.tmp "${URL}/cntools.sh" && \
mv -f "${PARENT}"/cntools.sh "${PARENT}/cntools.sh_bkp$(printf '%(%s)T\n' -1)" && \
mv -f "${PARENT}"/cntools.library "${PARENT}/cntools.library_bkp$(printf '%(%s)T\n' -1)" && \
mv -f "${PARENT}"/cntools.sh.tmp "${PARENT}"/cntools.sh && \
mv -f "${PARENT}"/cntools.library.tmp "${PARENT}"/cntools.library && \
chmod 750 "${PARENT}"/cntools.sh; then
myExit 0 "Update applied successfully!\n\nPlease start CNTools again!"
else
myExit 1 "${FG_RED}Update failed!${NC}\n\nPlease use prereqs.sh or manually update CNTools"
fi
waitForInput
fi
else
# check if CNTools was recently updated, if so show whats new
if curl -s -f -m ${CURL_TIMEOUT} -o "${TMP_DIR}"/cntools-changelog.md "${URL_DOCS}/cntools-changelog.md"; then
if ! cmp -s "${TMP_DIR}"/cntools-changelog.md "${PARENT}/cntools-changelog.md"; then
# Latest changes not shown, show whats new and copy changelog
clear
exec >&6 # normal stdout
sleep 0.1
if [[ ! -f "${PARENT}/cntools-changelog.md" ]]; then
# special case for first installation or 5.0.0 upgrade, print release notes until previous major version
println OFF "~ CNTools - What's New ~\n\n" "$(sed -n "/\[${CNTOOLS_MAJOR_VERSION}\.${CNTOOLS_MINOR_VERSION}\.${CNTOOLS_PATCH_VERSION}\]/,/\[$((CNTOOLS_MAJOR_VERSION-1))\.[0-9]\.[0-9]\]/p" "${TMP_DIR}"/cntools-changelog.md | head -n -2)" | less -X
else
# print release notes from current until previously installed version
[[ $(cat "${PARENT}/cntools-changelog.md") =~ \[([[:digit:]])\.([[:digit:]])\.([[:digit:]])\] ]]
cat <(println OFF "~ CNTools - What's New ~\n") <(awk "1;/\[${BASH_REMATCH[1]}\.${BASH_REMATCH[2]}\.${BASH_REMATCH[3]}\]/{exit}" "${TMP_DIR}"/cntools-changelog.md | head -n -2 | tail -n +7) <(echo -e "\n [Press 'q' to quit and proceed to CNTools main menu]\n") | less -X
fi
exec >&8 # custom stdout
cp "${TMP_DIR}"/cntools-changelog.md "${PARENT}/cntools-changelog.md"
fi
if [[ ${UPDATE_CHECK} = Y && ${SKIP_UPDATE} != Y ]]; then

echo "Checking for script updates..."

# Check availability of checkUpdate function
if [[ ! $(command -v checkUpdate) ]]; then
myExit 1 "\nCould not find checkUpdate function in env, make sure you're using official guild docos for installation!"
fi

# check for env update
ENV_UPDATED=N
checkUpdate env N N N
case $? in
1) ENV_UPDATED=Y ;;
2) myExit 1 ;;
esac

# source common env variables in case it was updated
. "${PARENT}"/env
case $? in
1) myExit 1 "ERROR: CNTools failed to load common env file\nPlease verify set values in 'User Variables' section in env file or log an issue on GitHub" ;;
2) clear ;;
esac

# check for cntools update
checkUpdate cntools.library "${ENV_UPDATED}" Y N
case $? in
1) checkUpdate cntools.sh Y
case $? in
1) $0 "$@" "-u"; myExit 0 ;; # re-launch script with same args skipping update check
2) exit 1 ;;
esac
;;
2) exit 1 ;;
esac

# check if CNTools was recently updated, if so show whats new
if curl -s -f -m ${CURL_TIMEOUT} -o "${TMP_DIR}"/cntools-changelog.md "${URL_DOCS}/cntools-changelog.md"; then
if ! cmp -s "${TMP_DIR}"/cntools-changelog.md "${PARENT}/cntools-changelog.md"; then
# Latest changes not shown, show whats new and copy changelog
clear
sleep 0.1
if [[ ! -f "${PARENT}/cntools-changelog.md" ]]; then
# special case for first installation or 5.0.0 upgrade, print release notes until previous major version
echo -e "~ CNTools - What's New ~\n\n" "$(sed -n "/\[${CNTOOLS_MAJOR_VERSION}\.${CNTOOLS_MINOR_VERSION}\.${CNTOOLS_PATCH_VERSION}\]/,/\[$((CNTOOLS_MAJOR_VERSION-1))\.[0-9]\.[0-9]\]/p" "${TMP_DIR}"/cntools-changelog.md | head -n -2)" | less -X
else
println ERROR "\n${FG_RED}ERROR${NC}: failed to download changelog from GitHub!"
waitForInput
# print release notes from current until previously installed version
[[ $(cat "${PARENT}/cntools-changelog.md") =~ \[([[:digit:]])\.([[:digit:]])\.([[:digit:]])\] ]]
cat <(echo -e "~ CNTools - What's New ~\n") <(awk "1;/\[${BASH_REMATCH[1]}\.${BASH_REMATCH[2]}\.${BASH_REMATCH[3]}\]/{exit}" "${TMP_DIR}"/cntools-changelog.md | head -n -2 | tail -n +7) <(echo -e "\n [Press 'q' to quit and proceed to CNTools main menu]\n") | less -X
fi
cp "${TMP_DIR}"/cntools-changelog.md "${PARENT}/cntools-changelog.md"
fi
rm -f "${PARENT}"/cntools.sh.tmp
rm -f "${PARENT}"/cntools.library.tmp
else
println ERROR "\n${FG_RED}ERROR${NC}: failed to download cntools.library from GitHub, unable to perform version check!"
waitForInput
echo -e "\n${FG_RED}ERROR${NC}: failed to download changelog from GitHub!"
waitToProceed
fi
fi

Expand All @@ -197,6 +214,14 @@ if ! versionCheck "4.4.0" "${BASH_REMATCH[1]}"; then
myExit 1 "BASH does not meet the minimum required version of ${FG_LBLUE}4.4.0${NC}, found ${FG_LBLUE}${BASH_REMATCH[1]}${NC}\n\nPlease upgrade to a newer Linux distribution or compile latest BASH following official docs.\n\nINSTALL: https://www.gnu.org/software/bash/manual/html_node/Installing-Bash.html\nDOWNLOAD: http://git.savannah.gnu.org/cgit/bash.git/ (latest stable TAG)"
fi

exec 6>&1 # Link file descriptor #6 with normal stdout.
exec 7>&2 # Link file descriptor #7 with normal stderr.
[[ -n ${CNTOOLS_LOG} ]] && exec > >( tee >( while read -r line; do logln "INFO" "${line}"; done ) )
[[ -n ${CNTOOLS_LOG} ]] && exec 2> >( tee >( while read -r line; do logln "ERROR" "${line}"; done ) >&2 )
[[ -n ${CNTOOLS_LOG} ]] && exec 3> >( tee >( while read -r line; do logln "DEBUG" "${line}"; done ) >&6 )
exec 8>&1 # Link file descriptor #8 with custom stdout.
exec 9>&2 # Link file descriptor #9 with custom stderr.

# check if there are pools in need of KES key rotation
clear
kes_rotation_needed="no"
Expand Down

0 comments on commit 05bcef0

Please sign in to comment.