diff --git a/installer/sashimono-install.sh b/installer/sashimono-install.sh index 883af44..748e6ed 100755 --- a/installer/sashimono-install.sh +++ b/installer/sashimono-install.sh @@ -34,6 +34,7 @@ nodejs_util_bin="/usr/bin/node" jshelper_bin="$setup_helper_dir/jshelper/index.js" mb_cli_exit_err="MB_CLI_EXITED" +mb_cli_out_prefix="CLI_OUT" multi_choice_result="" noroot_user=${SUDO_USER:-$(whoami)} @@ -75,7 +76,7 @@ function rollback() { echo "Rolling back the installation..." if [ "$UPGRADE" == "0" ]; then burn_leases - deregister + check_and_deregister "$script_dir"/sashimono-uninstall.sh -f ROLLBACK fi @@ -127,8 +128,7 @@ function multi_choice_output() { } function wait_for_funds() { - local res=$(exec_mb wait-for-funds "$@") - if [[ "$res" == *"$mb_cli_exit_err"* ]]; then + if ! res=$(exec_mb wait-for-funds $1 $2); then multi_choice "Do you want to re-check the balance" "Retry/Abort/Rollback" && local input=$(multi_choice_output) if [ "$input" == "Retry" ]; then wait_for_funds "$@" && return 0 @@ -261,12 +261,17 @@ function check_dependencies() { function exec_mb() { local res=$(sudo -u $MB_XRPL_USER MB_DATA_DIR="$MB_XRPL_DATA" node "$MB_XRPL_BIN" "$@" | tee /dev/fd/2) - echo $res + + local return_code=0 + [[ "$res" == *"$mb_cli_exit_err"* ]] && return_code=1 + + res=$(echo "$res" | sed -n -e "/^$mb_cli_out_prefix: /p") + echo "${res#"$mb_cli_out_prefix: "}" + return $return_code } function burn_leases() { - local res=$(exec_mb burn-leases) - if [[ "$res" == *"$mb_cli_exit_err"* ]]; then + if ! res=$(exec_mb burn-leases); then multi_choice "An error occurred while burning! What do you want to do" "Retry/Abort/Rollback" && local input=$(multi_choice_output) if [ "$input" == "Retry" ]; then burn_leases "$@" && return 0 @@ -298,10 +303,8 @@ function exec_jshelper() { } function mint_leases() { - local res=$(exec_mb mint-leases $total_instance_count) - if [[ "$res" == *"$mb_cli_exit_err"* ]]; then - res=$(echo "$res" | tail -n 2 | head -n 1) - if [[ "$res" == "LEASE_ERR"* ]]; then + if ! res=$(exec_mb mint-leases $total_instance_count); then + if [[ "$res" == "LEASE_ERR" ]]; then if confirm "Do you want to burn minted tokens. (N will abort the installation)" "n"; then burn_leases && mint_leases "$@" && return 0 else @@ -323,9 +326,7 @@ function mint_leases() { } function deregister() { - local res=$(exec_mb deregister $1) - if [[ "$res" == *"$mb_cli_exit_err"* ]]; then - res=$(echo "$res" | tail -n 2 | head -n 1) + if ! res=$(exec_mb deregister $1); then multi_choice "An error occurred while registering! What do you want to do" "Retry/Abort/Rollback" && local input=$(multi_choice_output) if [ "$input" == "Retry" ]; then deregister "$@" && return 0 @@ -340,11 +341,7 @@ function deregister() { } function register() { - echo "Registration calling" - - local res=$(exec_mb register $country_code $cpu_micro_sec $ramKB $swapKB $diskKB $total_instance_count $cpu_model_name $cpu_count $cpu_mhz $email_address $description) - if [[ "$res" == *"$mb_cli_exit_err"* ]]; then - res=$(echo "$res" | tail -n 2 | head -n 1) + if ! res=$(exec_mb register $country_code $cpu_micro_sec $ram_kb $swap_kb $disk_kb $total_instance_count $cpu_model $cpu_count $cpu_speed $email_address $description); then multi_choice "An error occurred while registering! What do you want to do" "Retry/Abort/Rollback" && local input=$(multi_choice_output) if [ "$input" == "Retry" ]; then register "$@" && return 0 @@ -358,19 +355,30 @@ function register() { return 0 } +function accept_reg_token() { + if ! res=$(exec_mb accept-reg-token); then + multi_choice "An error occurred while accepting the reg token! What do you want to do" "Retry/Abort/Rollback" && local input=$(multi_choice_output) + if [ "$input" == "Retry" ]; then + accept_reg_token "$@" && return 0 + elif [ "$input" == "Rollback" ]; then + rollback + else + abort + fi + return 1 + fi + return 0 +} + function check_balance() { - local res=$(exec_mb check-balance) - if [[ "$res" == *"$mb_cli_exit_err"* ]]; then - res=$(echo "$res" | tail -n 2 | head -n 1) - if [[ "$res" == "ERROR"* ]]; then - multi_choice "Balance check failed! What do you want to do" "retry/abort/rollback" && local input=$(choice_output) - if [ "$input" == "retry" ]; then - check_balance && return 0 - elif [ "$input" == "rollback" ]; then - rollback - else - abort - fi + if ! res=$(exec_mb check-balance); then + multi_choice "Do you want to re-check the balance" "Retry/Abort/Rollback" && local input=$(multi_choice_output) + if [ "$input" == "Retry" ]; then + check_balance "$@" && return 0 + elif [ "$input" == "Rollback" ]; then + rollback + else + abort fi return 1 fi @@ -379,28 +387,53 @@ function check_balance() { } function check_and_register() { - local res=$(exec_mb check-reg) - if [[ "$res" == *"$mb_cli_exit_err"* ]]; then - res=$(echo "$res" | tail -n 2 | head -n 1) - if [[ "$res" == "ACC_NOT_FOUND"* ]]; then + if ! res=$(exec_mb check-reg); then + if [[ "$res" == "ACC_NOT_FOUND" ]]; then echo "Account not found, Please check your account and try again." && abort - elif [[ "$res" == "INVALID_REG"* ]]; then + return 1 + elif [[ "$res" == "INVALID_REG" ]]; then echo "Invalid registration please transfer and try again" && abort - elif [[ "$res" == "PENDING_SELL_OFFER"* ]]; then + return 1 + elif [[ "$res" == "PENDING_SELL_OFFER" ]]; then register && return 0 - elif [[ "$res" == "PENDING_TRANSFER"* ]] || [[ "$res" == "NOT_REGISTERED"* ]]; then + return 1 + elif [[ "$res" == "PENDING_TRANSFER" ]] || [[ "$res" == "NOT_REGISTERED" ]]; then check_balance && register && return 0 + return 1 fi - return 1 - fi - - res=$(echo "$res" | tail -n 2 | head -n 1) - if [[ "$res" == "REGISTERED" ]]; then + elif [[ "$res" == "REGISTERED" ]]; then echo "This host is registered" return 0 - else - echo "Invalid registration please transfer and try again" && abort fi + + echo "Invalid registration please transfer and try again" && abort + return 1 +} + +function check_and_deregister() { + if ! res=$(exec_mb check-reg); then + if [[ "$res" == "NOT_REGISTERED" ]]; then + echo "This host is de-registered" + return 0 + elif [[ "$res" == "ACC_NOT_FOUND" ]]; then + echo "Account not found, Please check your account and try again." && abort + return 1 + elif [[ "$res" == "INVALID_REG" ]]; then + echo "Invalid registration please transfer and try again" && abort + return 1 + elif [[ "$res" == "PENDING_SELL_OFFER" ]]; then + accept_reg_token && deregister && return 0 + return 1 + elif [[ "$res" == "PENDING_TRANSFER" ]]; then + echo "There a pending transfer, Please re-install and try again." && abort + return 1 + fi + elif [[ "$res" == "REGISTERED" ]]; then + deregister + return 0 + fi + + echo "Invalid registration please transfer and try again" && abort return 1 } @@ -454,12 +487,10 @@ function evr_balance_check_and_wait { } function prepare() { - local res=$(exec_mb prepare $inetaddr) - if [[ "$res" == *"$mb_cli_exit_err"* ]]; then - res=$(echo "$res" | tail -n 2 | head -n 1) + if ! res=$(exec_mb prepare $inetaddr); then multi_choice "An error occurred while preparing the account! What do you want to do" "Retry/Abort/Rollback" && local input=$(multi_choice_output) if [ "$input" == "Retry" ]; then - prepare "$@" && return 0 + register "$@" && return 0 elif [ "$input" == "Rollback" ]; then rollback else diff --git a/installer/setup-test.sh b/installer/setup-test.sh index 3d25231..e47581c 100755 --- a/installer/setup-test.sh +++ b/installer/setup-test.sh @@ -18,6 +18,7 @@ export MIN_OPERATIONAL_DURATON=3 mb_cli_exit_err="MB_CLI_EXITED" + mb_cli_out_prefix="CLI_OUT" multi_choice_result="" public_config_url="https://raw.githubusercontent.com/EvernodeXRPL/evernode-resources/main/definitions/definitions.json" @@ -49,7 +50,7 @@ function rollback() { echo "Rollbacking the instalation.." burn_leases - deregister + check_and_deregister exit 1 } @@ -288,13 +289,18 @@ function exec_mb() { local res=$(MB_DATA_DIR="$MB_XRPL_DATA" node "/home/chalith/Workspace/HotpocketDev/sashimono/mb-xrpl/app.js" "$@" | tee >(grep -v "$mb_cli_exit_err" >/dev/fd/2)) - echo $res + + local return_code=0 + [[ "$res" == *"$mb_cli_exit_err"* ]] && return_code=1 + + res=$(echo "$res" | sed -n -e "/^$mb_cli_out_prefix: /p") + echo "${res#"$mb_cli_out_prefix: "}" + return $return_code } function burn_leases() { - local res=$(exec_mb burn-leases) - if [[ "$res" == *"$mb_cli_exit_err"* ]]; then - multi_choice "An error occured while burning! What do you want to do" "Retry/Abort/Rollback" && local input=$(multi_choice_output) + if ! res=$(exec_mb burn-leases); then + multi_choice "An error occurred while burning! What do you want to do" "Retry/Abort/Rollback" && local input=$(multi_choice_output) if [ "$input" == "Retry" ]; then burn_leases "$@" && return 0 elif [ "$input" == "Rollback" ]; then @@ -308,17 +314,15 @@ } function mint_leases() { - local res=$(exec_mb mint-leases $total_instance_count) - if [[ "$res" == *"$mb_cli_exit_err"* ]]; then - res=$(echo "$res" | tail -n 2 | head -n 1) - if [[ "$res" == "LEASE_ERR"* ]]; then + if ! res=$(exec_mb mint-leases $total_instance_count); then + if [[ "$res" == "LEASE_ERR" ]]; then if confirm "Do you want to burn minted tokens. (N will abort the installation)" "n"; then burn_leases && mint_leases "$@" && return 0 else abort fi else - multi_choice "An error occured while minting! What do you want to do" "Retry/Abort/Rollback" && local input=$(multi_choice_output) + multi_choice "An error occurred while minting! What do you want to do" "Retry/Abort/Rollback" && local input=$(multi_choice_output) if [ "$input" == "Retry" ]; then mint_leases "$@" && return 0 elif [ "$input" == "Rollback" ]; then @@ -333,10 +337,8 @@ } function deregister() { - local res=$(exec_mb deregister $1) - if [[ "$res" == *"$mb_cli_exit_err"* ]]; then - res=$(echo "$res" | tail -n 2 | head -n 1) - multi_choice "An error occured while registering! What do you want to do" "Retry/Abort/Rollback" && local input=$(multi_choice_output) + if ! res=$(exec_mb deregister $1); then + multi_choice "An error occurred while registering! What do you want to do" "Retry/Abort/Rollback" && local input=$(multi_choice_output) if [ "$input" == "Retry" ]; then deregister "$@" && return 0 elif [ "$input" == "Rollback" ]; then @@ -350,10 +352,8 @@ } function register() { - local res=$(exec_mb register $country_code $cpu_micro_sec $ram_kb $swap_kb $disk_kb $total_instance_count $cpu_model $cpu_count $cpu_speed $email_address $description) - if [[ "$res" == *"$mb_cli_exit_err"* ]]; then - res=$(echo "$res" | tail -n 2 | head -n 1) - multi_choice "An error occured while registering! What do you want to do" "Retry/Abort/Rollback" && local input=$(multi_choice_output) + if ! res=$(exec_mb register $country_code $cpu_micro_sec $ram_kb $swap_kb $disk_kb $total_instance_count $cpu_model $cpu_count $cpu_speed $email_address $description); then + multi_choice "An error occurred while registering! What do you want to do" "Retry/Abort/Rollback" && local input=$(multi_choice_output) if [ "$input" == "Retry" ]; then register "$@" && return 0 elif [ "$input" == "Rollback" ]; then @@ -366,11 +366,24 @@ return 0 } + function accept_reg_token() { + if ! res=$(exec_mb accept-reg-token); then + multi_choice "An error occurred while accepting the reg token! What do you want to do" "Retry/Abort/Rollback" && local input=$(multi_choice_output) + if [ "$input" == "Retry" ]; then + accept_reg_token "$@" && return 0 + elif [ "$input" == "Rollback" ]; then + rollback + else + abort + fi + return 1 + fi + return 0 + } + function prepare() { - local res=$(exec_mb prepare $inetaddr) - if [[ "$res" == *"$mb_cli_exit_err"* ]]; then - res=$(echo "$res" | tail -n 2 | head -n 1) - multi_choice "An error occured while preparing the account! What do you want to do" "Retry/Abort/Rollback" && local input=$(multi_choice_output) + if ! res=$(exec_mb prepare $inetaddr); then + multi_choice "An error occurred while preparing the account! What do you want to do" "Retry/Abort/Rollback" && local input=$(multi_choice_output) if [ "$input" == "Retry" ]; then register "$@" && return 0 elif [ "$input" == "Rollback" ]; then @@ -384,18 +397,14 @@ } function check_balance() { - local res=$(exec_mb check-balance) - if [[ "$res" == *"$mb_cli_exit_err"* ]]; then - res=$(echo "$res" | tail -n 2 | head -n 1) - if [[ "$res" == "ERROR"* ]]; then - multi_choice "Do you want to re-check the balance" "Retry/Abort/Rollback" && local input=$(multi_choice_output) - if [ "$input" == "Retry" ]; then - check_balance "$@" && return 0 - elif [ "$input" == "Rollback" ]; then - rollback - else - abort - fi + if ! res=$(exec_mb check-balance); then + multi_choice "Do you want to re-check the balance" "Retry/Abort/Rollback" && local input=$(multi_choice_output) + if [ "$input" == "Retry" ]; then + check_balance "$@" && return 0 + elif [ "$input" == "Rollback" ]; then + rollback + else + abort fi return 1 fi @@ -404,8 +413,7 @@ } function wait_for_funds() { - local res=$(exec_mb wait-for-funds "$@") - if [[ "$res" == *"$mb_cli_exit_err"* ]]; then + if ! res=$(exec_mb wait-for-funds $1 $2); then multi_choice "Do you want to re-check the balance" "Retry/Abort/Rollback" && local input=$(multi_choice_output) if [ "$input" == "Retry" ]; then wait_for_funds "$@" && return 0 @@ -421,28 +429,53 @@ } function check_and_register() { - local res=$(exec_mb check-reg) - if [[ "$res" == *"$mb_cli_exit_err"* ]]; then - res=$(echo "$res" | tail -n 2 | head -n 1) - if [[ "$res" == "ACC_NOT_FOUND"* ]]; then + if ! res=$(exec_mb check-reg); then + if [[ "$res" == "ACC_NOT_FOUND" ]]; then echo "Account not found, Please check your account and try again." && abort - elif [[ "$res" == "INVALID_REG"* ]]; then + return 1 + elif [[ "$res" == "INVALID_REG" ]]; then echo "Invalid registration please transfer and try again" && abort - elif [[ "$res" == "PENDING_SELL_OFFER"* ]]; then + return 1 + elif [[ "$res" == "PENDING_SELL_OFFER" ]]; then register && return 0 - elif [[ "$res" == "PENDING_TRANSFER"* ]] || [[ "$res" == "NOT_REGISTERED"* ]]; then + return 1 + elif [[ "$res" == "PENDING_TRANSFER" ]] || [[ "$res" == "NOT_REGISTERED" ]]; then check_balance && register && return 0 + return 1 fi - return 1 - fi - - res=$(echo "$res" | tail -n 2 | head -n 1) - if [[ "$res" == "REGISTERED" ]]; then + elif [[ "$res" == "REGISTERED" ]]; then echo "This host is registered" return 0 - else - echo "Invalid registration please transfer and try again" && abort fi + + echo "Invalid registration please transfer and try again" && abort + return 1 + } + + function check_and_deregister() { + if ! res=$(exec_mb check-reg); then + if [[ "$res" == "NOT_REGISTERED" ]]; then + echo "This host is de-registered" + return 0 + elif [[ "$res" == "ACC_NOT_FOUND" ]]; then + echo "Account not found, Please check your account and try again." && abort + return 1 + elif [[ "$res" == "INVALID_REG" ]]; then + echo "Invalid registration please transfer and try again" && abort + return 1 + elif [[ "$res" == "PENDING_SELL_OFFER" ]]; then + accept_reg_token && deregister && return 0 + return 1 + elif [[ "$res" == "PENDING_TRANSFER" ]]; then + echo "There a pending transfer, Please re-install and try again." && abort + return 1 + fi + elif [[ "$res" == "REGISTERED" ]]; then + deregister + return 0 + fi + + echo "Invalid registration please transfer and try again" && abort return 1 } diff --git a/mb-xrpl/app.js b/mb-xrpl/app.js index ae3a4c3..5e588c2 100644 --- a/mb-xrpl/app.js +++ b/mb-xrpl/app.js @@ -43,6 +43,9 @@ async function main() { else if (process.argv.length >= 3 && process.argv[2] === 'prepare') { await new Setup().prepareHostAccount(process.argv[3]); } + else if (process.argv.length >= 2 && process.argv[2] === 'accept-reg-token') { + await new Setup().acceptRegToken(); + } else if (process.argv.length >= 13 && process.argv[2] === 'register') { await new Setup().register(process.argv[3], parseInt(process.argv[4]), parseInt(process.argv[5]), parseInt(process.argv[6]), parseInt(process.argv[7]), parseInt(process.argv[8]), process.argv[9], parseInt(process.argv[10]), parseInt(process.argv[11]), process.argv[12], process.argv[13]); diff --git a/mb-xrpl/lib/setup.js b/mb-xrpl/lib/setup.js index f6441bc..fb130f3 100644 --- a/mb-xrpl/lib/setup.js +++ b/mb-xrpl/lib/setup.js @@ -147,7 +147,7 @@ class Setup { await hostClient.xrplApi.connect(); if (!await hostClient.xrplAcc.exists()) { - throw "ACC_NOT_FOUND"; + throw "CLI_OUT: ACC_NOT_FOUND"; } await hostClient.connect(); @@ -158,12 +158,12 @@ class Setup { if (regUriToken) { const registered = await hostClient.isRegistered(); if (registered) { - console.log("REGISTERED"); + console.log("CLI_OUT: REGISTERED"); await hostClient.disconnect(); return true; } else { - throw "INVALID_REG"; + throw "CLI_OUT: INVALID_REG"; } } @@ -172,15 +172,15 @@ class Setup { const registryAcc = new evernode.XrplAccount(hostClient.config.registryAddress); const sellOffer = (await registryAcc.getURITokens()).find(o => o.Issuer == registryAcc.address && o.index == regInfo.uriTokenId && o.Amount); if (sellOffer) - throw "PENDING_SELL_OFFER"; + throw "CLI_OUT: PENDING_SELL_OFFER"; } // Check whether pending transfer exists. const transferPending = await hostClient.isTransferee(); if (transferPending) - throw "PENDING_TRANSFER"; + throw "CLI_OUT: PENDING_TRANSFER"; - throw "NOT_REGISTERED" + throw "CLI_OUT: NOT_REGISTERED" } catch (e) { await hostClient.disconnect(); @@ -205,9 +205,9 @@ class Setup { const isAReReg = await hostClient.isTransferee(); const evrBalance = await hostClient.getEVRBalance(); if (!isAReReg && hostClient.config.hostRegFee > evrBalance) - throw `ERROR: EVR balance in the account is less than the registration fee (${hostClient.config.hostRegFee}EVRs).`; + throw `EVR balance in the account is less than the registration fee (${hostClient.config.hostRegFee}EVRs).`; else if (isAReReg && evrBalance < parseFloat(evernode.EvernodeConstants.NOW_IN_EVRS)) - throw `ERROR: EVR balance in the account is insufficient for re-registration.`; + throw `EVR balance in the account is insufficient for re-registration.`; await hostClient.disconnect(); return true; } @@ -217,6 +217,31 @@ class Setup { } } + async acceptRegToken() { + console.log("Accepting registration token..."); + const config = this.#getConfig(); + const acc = config.xrpl; + await setEvernodeDefaults(acc.network, acc.governorAddress, acc.rippledServer); + + const hostClient = new evernode.HostClient(acc.address, acc.secret); + await hostClient.connect(); + + // Update the Defaults with "xrplApi" of the client. + evernode.Defaults.set({ + xrplApi: hostClient.xrplApi + }); + + try { + var res = await hostClient.acceptRegToken({ retryOptions: { maxRetryAttempts: MAX_TX_RETRY_ATTEMPTS, feeUplift: Math.floor(acc.affordableExtraFee / MAX_TX_RETRY_ATTEMPTS) } }); + await hostClient.disconnect(); + return res; + } + catch (e) { + await hostClient.disconnect(); + throw e; + } + } + async register(countryCode, cpuMicroSec, ramKb, swapKb, diskKb, totalInstanceCount, cpuModel, cpuCount, cpuSpeed, emailAddress, description) { console.log("Registering host..."); let cpuModelFormatted = cpuModel.replaceAll('_', ' '); @@ -265,21 +290,25 @@ class Setup { if (leases.length) { for (const l of leases) { if (l.Amount && l.Amount.value !== acc.leaseAmount) { - throw 'LEASE_ERR: Lease amount is inconsistent with existing.'; + console.error('Lease amount is inconsistent with existing.'); + throw 'CLI_OUT: LEASE_ERR'; } const tokenInfo = evernode.UtilHelpers.decodeLeaseTokenUri(l.URI); if (tokenInfo.leaseAmount !== acc.leaseAmount) { - throw 'LEASE_ERR: Lease amount is inconsistent with existing.'; + console.error('Lease amount is inconsistent with existing.'); + throw 'CLI_OUT: LEASE_ERR'; } const leaseIndex = tokenInfo.leaseIndex; const outboundIP = tokenInfo.outboundIP; if ((outboundIP && !config?.networking?.ipv6?.subnet) || (!outboundIP && config?.networking?.ipv6?.subnet)) { - throw 'LEASE_ERR: Outbound IP is inconsistent with existing.'; + console.error('Outbound IP is inconsistent with existing.'); + throw 'CLI_OUT: LEASE_ERR'; } else if (outboundIP && config?.networking?.ipv6?.subnet) { if (!UtilHelper.isSameIPV6Subnet(outboundIP, config?.networking?.ipv6?.subnet)) { - throw 'LEASE_ERR: Outbound IP is inconsistent with existing.'; + console.error('Outbound IP is inconsistent with existing.'); + throw 'CLI_OUT: LEASE_ERR'; } } @@ -340,13 +369,15 @@ class Setup { console.log(`Created lease offer ${i + 1} of ${unoffered.length}.`); } else { - throw 'LEASE_ERR: Lease amounts are inconsistent.'; + console.error('Lease amounts are inconsistent.'); + throw 'CLI_OUT: LEASE_ERR'; } i++; } } else { - throw 'LEASE_ERR: No unoffered leases.'; + console.error('No unoffered leases.'); + throw 'CLI_OUT: LEASE_ERR'; } await hostClient.disconnect();