2023-07-04 11:46:49 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2024-02-21 20:50:57 +00:00
|
|
|
# Environment variables from files
|
2024-10-01 13:05:22 +00:00
|
|
|
# If set to file path, read the file and export the variables
|
|
|
|
# If set to directory path, read all files in the directory and export the variables
|
2024-02-21 20:50:57 +00:00
|
|
|
if [[ -n "${ENV_PATH}" ]]; then
|
2024-03-01 12:23:05 +00:00
|
|
|
set -a
|
|
|
|
[[ -f "${ENV_PATH}" ]] && source "${ENV_PATH}" || for f in "${ENV_PATH}"/*; do source "$f"; done
|
|
|
|
set +a
|
2024-02-21 20:50:57 +00:00
|
|
|
fi
|
|
|
|
|
2023-07-10 13:15:06 +00:00
|
|
|
# Parameters
|
2024-02-15 13:57:50 +00:00
|
|
|
if [[ -z "${CODEX_NAT}" ]]; then
|
|
|
|
if [[ "${NAT_IP_AUTO}" == "true" && -z "${NAT_PUBLIC_IP_AUTO}" ]]; then
|
|
|
|
export CODEX_NAT=$(hostname --ip-address)
|
|
|
|
echo "Private: CODEX_NAT=${CODEX_NAT}"
|
|
|
|
elif [[ -n "${NAT_PUBLIC_IP_AUTO}" ]]; then
|
|
|
|
# Run for 60 seconds if fail
|
|
|
|
WAIT=120
|
|
|
|
SECONDS=0
|
|
|
|
SLEEP=5
|
|
|
|
while (( SECONDS < WAIT )); do
|
|
|
|
export CODEX_NAT=$(curl -s -f -m 5 "${NAT_PUBLIC_IP_AUTO}")
|
|
|
|
# Check if exit code is 0 and returned value is not empty
|
|
|
|
if [[ $? -eq 0 && -n "${CODEX_NAT}" ]]; then
|
|
|
|
echo "Public: CODEX_NAT=${CODEX_NAT}"
|
|
|
|
break
|
|
|
|
else
|
|
|
|
# Sleep and check again
|
|
|
|
echo "Can't get Public IP - Retry in $SLEEP seconds / $((WAIT - SECONDS))"
|
|
|
|
sleep $SLEEP
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
2023-07-10 13:15:06 +00:00
|
|
|
fi
|
|
|
|
|
2024-02-15 13:57:50 +00:00
|
|
|
# Stop Codex run if can't get NAT IP when requested
|
|
|
|
if [[ "${NAT_IP_AUTO}" == "true" && -z "${CODEX_NAT}" ]]; then
|
|
|
|
echo "Can't get Private IP - Stop Codex run"
|
|
|
|
exit 1
|
|
|
|
elif [[ -n "${NAT_PUBLIC_IP_AUTO}" && -z "${CODEX_NAT}" ]]; then
|
|
|
|
echo "Can't get Public IP in $WAIT seconds - Stop Codex run"
|
|
|
|
exit 1
|
2023-11-17 13:48:25 +00:00
|
|
|
fi
|
|
|
|
|
2023-09-19 07:57:39 +00:00
|
|
|
# If marketplace is enabled from the testing environment,
|
|
|
|
# The file has to be written before Codex starts.
|
2024-11-03 06:51:57 +00:00
|
|
|
for key in PRIV_KEY ETH_PRIVATE_KEY; do
|
|
|
|
keyfile="private.key"
|
|
|
|
if [[ -n "${!key}" ]]; then
|
|
|
|
[[ "${key}" == "PRIV_KEY" ]] && echo "PRIV_KEY variable is deprecated and will be removed in the next releases, please use ETH_PRIVATE_KEY instead!"
|
|
|
|
echo "${!key}" > "${keyfile}"
|
|
|
|
chmod 600 "${keyfile}"
|
|
|
|
export CODEX_ETH_PRIVATE_KEY="${keyfile}"
|
|
|
|
echo "Private key set"
|
|
|
|
fi
|
|
|
|
done
|
2023-09-19 07:57:39 +00:00
|
|
|
|
2024-09-23 14:37:17 +00:00
|
|
|
# Circuit downloader
|
|
|
|
# cirdl [circuitPath] [rpcEndpoint] [marketplaceAddress]
|
|
|
|
if [[ "$@" == *"prover"* ]]; then
|
2024-10-01 13:05:22 +00:00
|
|
|
echo "Prover is enabled - Run Circuit downloader"
|
|
|
|
|
|
|
|
# Set variables required by cirdl from command line arguments when passed
|
|
|
|
for arg in data-dir circuit-dir eth-provider marketplace-address; do
|
|
|
|
arg_value=$(grep -o "${arg}=[^ ,]\+" <<< $@ | awk -F '=' '{print $2}')
|
|
|
|
if [[ -n "${arg_value}" ]]; then
|
|
|
|
var_name=$(tr '[:lower:]' '[:upper:]' <<< "CODEX_${arg//-/_}")
|
|
|
|
export "${var_name}"="${arg_value}"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
# Set circuit dir from CODEX_CIRCUIT_DIR variables if set
|
2024-09-23 14:37:17 +00:00
|
|
|
if [[ -z "${CODEX_CIRCUIT_DIR}" ]]; then
|
|
|
|
export CODEX_CIRCUIT_DIR="${CODEX_DATA_DIR}/circuits"
|
|
|
|
fi
|
2024-10-01 13:05:22 +00:00
|
|
|
|
|
|
|
# Download circuit
|
2024-09-23 14:37:17 +00:00
|
|
|
mkdir -p "${CODEX_CIRCUIT_DIR}"
|
|
|
|
chmod 700 "${CODEX_CIRCUIT_DIR}"
|
2024-10-01 13:05:22 +00:00
|
|
|
download="cirdl ${CODEX_CIRCUIT_DIR} ${CODEX_ETH_PROVIDER} ${CODEX_MARKETPLACE_ADDRESS}"
|
|
|
|
echo "${download}"
|
|
|
|
eval "${download}"
|
2024-09-23 14:37:17 +00:00
|
|
|
[[ $? -ne 0 ]] && { echo "Failed to download circuit files"; exit 1; }
|
|
|
|
fi
|
|
|
|
|
2023-07-04 11:46:49 +00:00
|
|
|
# Run
|
|
|
|
echo "Run Codex node"
|
|
|
|
exec "$@"
|