Skip to content

Commit

Permalink
Provide a way to support checking compatible version of mithril and n…
Browse files Browse the repository at this point in the history
…ode binaries.
  • Loading branch information
TrevorBenson committed Aug 7, 2024
1 parent 3e6c998 commit ccf4e33
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 8 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/mithril-latest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ jobs:
- name: Assigns release version
run: |
VERSION=$(cat ./files/docker/node/release-versions/mithril-latest.txt)
- name: Source mithril.library and check upgrade safety
id: safety-check
run: |
. scripts/cnode-helper-scripts/mithril.library offline
check_mithril_upgrade_safe
- name: Check for modified files
id: git-check
run: echo ::set-output name=modified::$([ -z "`git status --porcelain`" ] && echo "false" || echo "true")
- name: Commit latest release version
if: steps.git-check.outputs.modified == 'true'
if: steps.git-check.outputs.modified == 'true' && steps.safety-check.outcome == 'success'
run: |
git config --global user.name ${{ secrets.REPO_SCOPED_USER }}
git config --global user.email ${{ secrets.REPO_SCOPED_EMAIL }}
git commit -am "New mithril release version ${VERSION}"
git push
git push
82 changes: 76 additions & 6 deletions scripts/cnode-helper-scripts/mithril.library
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
#!/usr/bin/env bash
# shellcheck disable=SC2034,SC2086,SC2230,SC2206,SC2140,SC2059,SC2154
#shellcheck source=/dev/null
# shellcheck source=/dev/null

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

. "$(dirname $0)"/env offline
if [[ "${1}" == "offline" ]] ; then
# Sourcing mithril.library in offline mode is for GitHub Actions workflow to safely
# update the files/docker/node/release-versions/mithril-latest.txt file without
# setting it to an incompatible version with the currently suported cardano-node
# version. There is no need to source the environment file in offline mode.
OFFLINE_MODE="Y"
echo "INFO: Sourcing mithril.library in offline mode, skipping sourcing of the environment file."
else
OFFLINE_MODE="N"
. "$(dirname $0)"/env
fi

#############################
# Mithril General functions #
Expand Down Expand Up @@ -116,10 +126,34 @@ read_optional_ips_from_input() {
done
}

semantic_version_compare () {
if [[ "${1}" == "${2}" ]]
then
return 0
fi
local IFS=.
local i semantic_version1=($1) semantic_version2=($2)
# fill empty fields in semantic_version1 with zeros
for ((i=${#semantic_version1[@]}; i<${#semantic_version2[@]}; i++))
do
semantic_version1[i]=0
done
for ((i=0; i<${#semantic_version1[@]}; i++))
do
if ((10#${semantic_version1[i]:=0} > 10#${semantic_version2[i]:=0}))
then
return 1
fi
if ((10#${semantic_version1[i]} < 10#${semantic_version2[i]}))
then
return 2
fi
done
return 0
}

set_defaults() {
MITHRIL_LATEST_VERSION=$(curl -s https://raw.githubusercontent.com/cardano-community/guild-operators/alpha/files/docker/node/release-versions/mithril-latest.txt)
set_node_minimum_version
NODE_CURRENT_VERSION=$(cardano-node --version | awk 'NR==1{print $2}')

[[ -z "${MITHRILBIN}" ]] && MITHRILBIN="${HOME}"/.local/bin/"$(basename "${0::-3}")"
if [[ $(basename "${0::-3}") == "mithril-signer" ]] && { [[ -z "${POOL_NAME}" ]] || [[ "${POOL_NAME}" == "CHANGE_ME" ]]; }; then
Expand Down Expand Up @@ -149,14 +183,50 @@ set_env_file_ownership() {
${sudo} chown $USER:$USER "${MITHRIL_HOME}"/mithril.env
}

check_mithril_upgrade_safe() {
if [[ "${OFFLINE_MODE}" == "Y" ]]; then
# When run in offline mode, the node version is obtained from the repository
NODE_CURRENT_VERSION=$(cat files/docker/node/release-versions/cardano-node-latest.txt)
else
# When run in online mode, the node version is obtained from the cardano-node binary
NODE_CURRENT_VERSION=$(cardano-node --version | awk 'NR==1{print $2}')
fi
set_node_minimum_version
echo -e "Checking semantic_version_comare: "
semantic_version_compare "${NODE_CURRENT_VERSION}" "${MITHRIL_MINIMUM_NODE_VERSION}"
if [[ $? -lt 2 ]]; then
# Node version is greater than or equal to the minimum required version for latest mithril release
# Set MITHRIL_UPGRADE_SAFE to Y to allow mithril upgrade by scripts
MITHRIL_UPGRADE_SAFE="Y"
echo "INFO: A mithril upgrade is safe."
echo "INFO: The latest mithril release version: ${MITHRIL_LATEST_VERSION}."
echo "INFO: The current Node version: ${NODE_CURRENT_VERSION}."
echo "INFO: The Mithril minimum required node version: ${MITHRIL_MINIMUM_NODE_VERSION}."
# Return 0 to indicate that the node version is safe for mithril upgrade for CI workflows
return 0
else
# Node version is less than the minimum required version for latest mithril release
echo "WARNING: A mithril upgrade is not safe."
echo "WARNING: The latest mithril release version: ${MITHRIL_LATEST_VERSION}."
echo "WARNING: The current Node version: ${NODE_CURRENT_VERSION}."
echo "WARNING: The Mithril minimum required node version: ${MITHRIL_MINIMUM_NODE_VERSION}."
echo "WARNING: The latest mithril release does not support the installed node version. Please upgrade the node version first."
MITHRIL_UPGRADE_SAFE="N"
# Return 1 to indicate that the node version is not safe for mithril upgrade for CI workflows
return 1
fi
}

set_node_minimum_version() {
response_file=$(mktemp)
status_code=$(curl -s -o "$response_file" -w "%{http_code}" https://raw.githubusercontent.com/input-output-hk/mithril/${MITHRIL_LATEST_VERSION}/networks.json)

if [[ "$status_code" -gt 200 ]]; then
NODE_MINIMUM_VERSION=""
MITHRIL_MINIMUM_NODE_VERSION=""
else
NODE_MINIMUM_VERSION=$(jq -r ".${NETWORK_NAME,,}.\"cardano-minimum-version\".\"mithril-signer\"" "$response_file")
NETWORK=${NETWORK_NAME,,}
NETWORK=${NETWORK:-mainnet}
MITHRIL_MINIMUM_NODE_VERSION=$(jq -r ".${NETWORK}.\"cardano-minimum-version\".\"mithril-signer\"" "$response_file")
fi
rm -f "$response_file"
}
Expand Down

0 comments on commit ccf4e33

Please sign in to comment.