Skip to content
Snippets Groups Projects
Commit 8b27249f authored by Wieslaw Kedzierski's avatar Wieslaw Kedzierski
Browse files

Add dedicated script activate_beekeeper.sh.

This script will set beekeeper to be used with CLIVE_BEEKEEPER_SESSION_TOKEN. This script will also intercept signals, so that beekeeper instance will be closed gently, without leaving locks on wallet.
parent f8ad2a8e
No related branches found
No related tags found
3 merge requests!503v1.27.5.18 Release,!497Wkedzierski/251 cli dedicated launch script,!496Draft: Msobczyk/encrypt profile
......@@ -38,6 +38,8 @@ USER clive
SHELL ["/bin/bash", "-c"]
ADD ./docker/entrypoint.sh .
RUN mkdir /clive/scripts
ADD ./scripts/activate_beekeeper.sh /clive/scripts/
ADD --chown=clive ./pyproject.toml /clive/
ADD --chown=clive ./poetry.lock /clive/
......
......@@ -6,126 +6,6 @@ set -euo pipefail
SCRIPTPATH="$(cd -- "$(dirname "$0")" >/dev/null 2>&1 && pwd -P)"
TESTNET_NODE_LOG_FILE="testnet_node.log"
INTERACTIVE_CLI_MODE=0
BEEKEEPER_ALREADY_CLOSED=0
trap "clean_up" SIGTERM SIGINT SIGQUIT SIGHUP EXIT
# Clean after termination of shell
clean_up() {
close_beekeeper
}
# Start Beekeeper with prepared session token
start_beekeeper_with_prepared_session_token() {
echo "Starting beekeeper with prepared session token"
output=$(clive beekeeper spawn)
BEEKEEPER_HTTP_ENDPOINT=$(echo "$output" | grep -oE 'http://[0-9.]+:[0-9]+')
CLIVE_BEEKEEPER__SESSION_TOKEN=$(curl -s --data '{
"jsonrpc": "2.0",
"method": "beekeeper_api.create_session",
"params": {
"salt": "clive-cli-session",
"notifications_endpoint": "'"${BEEKEEPER_HTTP_ENDPOINT}"'"
},
"id": 1
}' "${BEEKEEPER_HTTP_ENDPOINT}" | jq .result.token | tr -d '"')
if [[ "${CLIVE_BEEKEEPER__SESSION_TOKEN}" == "null" ]]; then
echo "Error: There is no valid token."
exit 1
fi
export CLIVE_BEEKEEPER__SESSION_TOKEN=${CLIVE_BEEKEEPER__SESSION_TOKEN}
export BEEKEEPER_HTTP_ENDPOINT=${BEEKEEPER_HTTP_ENDPOINT}
echo "Beekeeper session token: ${CLIVE_BEEKEEPER__SESSION_TOKEN}"
echo "Beekeeper address : ${BEEKEEPER_HTTP_ENDPOINT}"
}
# Close beekeeper
close_beekeeper() {
if [[ ${BEEKEEPER_ALREADY_CLOSED} -eq 0 ]]; then
echo "Closing beekeeper : ${BEEKEEPER_HTTP_ENDPOINT}"
clive beekeeper close
BEEKEEPER_ALREADY_CLOSED=1
fi
}
# Unlock wallet for selected profile
unlock_wallet() {
read -rsp "Enter password for profile ${SELECTED_PROFILE}: " password
echo
password="${password//$'\n'/}"
response=$(curl -s --data '{
"jsonrpc": "2.0",
"method": "beekeeper_api.unlock",
"params": {
"token": "'"${CLIVE_BEEKEEPER__SESSION_TOKEN}"'",
"wallet_name": "'"${SELECTED_PROFILE}"'",
"password": "'"${password}"'"
},
"id": 1
}' "${BEEKEEPER_HTTP_ENDPOINT}")
error=$(echo "${response}" | jq .error)
if [[ "${error}" != "null" ]]; then
error_message=$(echo "${error}" | jq .message)
echo "Error: ${error_message}."
exit 1
fi
}
# Print info about how to create profile
how_to_create_profile() {
echo ""
echo "If you want to create profile, please do the following."
echo "clive configure profile add --profile-name PROFILE_NAME --password PROFILE_PASSWORD"
echo ""
}
# Select one of the existing profiles
select_profile(){
output=$(clive show profiles)
profiles=$(echo "$output" | grep -oP "\[\K[^\]]+")
IFS=',' read -ra profile_array <<< "$profiles"
if [[ -n "${SELECTED_PROFILE:-}" ]]; then
echo "You have pass profile name ${SELECTED_PROFILE} using '--profile' flag."
unlock_wallet
return
fi
if [ ${#profile_array[@]} -eq 0 ]; then
echo "There are no profiles."
how_to_create_profile
else
echo "Select profile:"
for i in "${!profile_array[@]}"; do
profile=$(echo "${profile_array[i]}" | tr -d "' ")
echo "$((i + 1)). $profile"
done
echo "$(( ${#profile_array[@]} + 1 )). create new profile"
read -rp "Enter the number: " choice
if [[ $choice -ge 1 && $choice -le ${#profile_array[@]} ]]; then
selected_profile=$(echo "${profile_array[$((choice - 1))]}" | tr -d "' ")
echo "You selected: $selected_profile"
SELECTED_PROFILE=${selected_profile}
echo "Selected profile is ${SELECTED_PROFILE}"
unlock_wallet
elif [[ $choice -eq $(( ${#profile_array[@]} + 1 )) ]]; then
echo "You selected: create new profile"
how_to_create_profile
else
echo "Error: Invalid selection!"
exit 1
fi
fi
}
# Print usage/help
print_help() {
......@@ -171,36 +51,10 @@ wait_for_testnet() {
echo "Testnet node is ready to use."
}
# Execute a script passed as an argument
execute_passed_script() {
if [[ -n "${FILE_TO_EXECUTE:-}" ]]; then
if [[ -f "${FILE_TO_EXECUTE}" ]]; then
echo "Executing file: ${FILE_TO_EXECUTE}"
# shellcheck disable=SC1090
source "${FILE_TO_EXECUTE}"
else
echo "Error: ${FILE_TO_EXECUTE} does not exist or is not a file."
exit 1
fi
fi
}
# Run shell with prepared Clive
run_clive() {
clive --install-completion >/dev/null 2>&1
if [ ${#CLIVE_ARGS[@]} -ne 0 ]; then
clive "${CLIVE_ARGS[@]}"
fi
bash
}
# Launch Clive in CLI mode
launch_cli() {
start_beekeeper_with_prepared_session_token
select_profile
execute_passed_script
run_clive
close_beekeeper
clive --install-completion >/dev/null 2>&1
exec bash --init-file /clive/scripts/activate_beekeeper.sh
}
# Parse command-line arguments
......
#!/bin/bash
BEEKEEPER_ALREADY_CLOSED=0
trap "clean_up" SIGTERM SIGQUIT SIGHUP EXIT
# Retry passed function
retry() {
local func=$1
local retries=0
local max_retries=3
if ! declare -f "$func" > /dev/null; then
echo "Error: '$func' is not a function."
exit 1
fi
while (( retries < max_retries )); do
"$func"
# shellcheck disable=SC2181
if [[ $? -eq 0 ]]; then
return 0
fi
((retries++))
echo "Attempt $retries of $max_retries failed. Retrying..."
done
echo "Maximum attempts reached. Exiting."
exit 1
}
# Start Beekeeper with prepared session token
start_beekeeper_with_prepared_session_token() {
output=$(clive beekeeper spawn)
# shellcheck disable=SC2181
if [[ $? -ne 0 ]]; then
echo "Error: Fail to spawn Beekeeper. Aborting..."
exit 1
fi
BEEKEEPER_HTTP_ENDPOINT=$(echo "$output" | grep -oE 'http://[0-9.]+:[0-9]+')
CLIVE_BEEKEEPER__SESSION_TOKEN=$(curl -s --data '{
"jsonrpc": "2.0",
"method": "beekeeper_api.create_session",
"params": {
"salt": "clive-cli-session",
"notifications_endpoint": "'"${BEEKEEPER_HTTP_ENDPOINT}"'"
},
"id": 1
}' "${BEEKEEPER_HTTP_ENDPOINT}" | jq .result.token | tr -d '"')
if [[ "${CLIVE_BEEKEEPER__SESSION_TOKEN}" == "null" ]]; then
echo "Error: There is no valid token."
exit 1
fi
export CLIVE_BEEKEEPER__SESSION_TOKEN=${CLIVE_BEEKEEPER__SESSION_TOKEN}
export BEEKEEPER_HTTP_ENDPOINT=${BEEKEEPER_HTTP_ENDPOINT}
}
# Unlock wallet for selected profile
unlock_wallet() {
read -rsp "Enter password for profile ${SELECTED_PROFILE}: " password
echo
password="${password//$'\n'/}"
response=$(curl -s --data '{
"jsonrpc": "2.0",
"method": "beekeeper_api.unlock",
"params": {
"token": "'"${CLIVE_BEEKEEPER__SESSION_TOKEN}"'",
"wallet_name": "'"${SELECTED_PROFILE}"'",
"password": "'"${password}"'"
},
"id": 1
}' "${BEEKEEPER_HTTP_ENDPOINT}")
error=$(echo "${response}" | jq .error)
if [[ "${error}" != "null" ]]; then
return 1
fi
return 0
}
# Print info about how to create profile
how_to_create_profile() {
echo ""
echo "If you want to create profile, please do the following."
echo "clive configure profile add --profile-name PROFILE_NAME --password PROFILE_PASSWORD"
echo ""
}
# Select one of the existing profiles
select_profile(){
clive_profiles=$(clive show profiles)
clive_profiles_formated=$(echo "$clive_profiles" | grep -oP "\[\K[^\]]+")
IFS=',' read -ra profile_array <<< "$clive_profiles_formated"
if [[ -n "${SELECTED_PROFILE:-}" ]]; then
for profile in "${profile_array[@]}"; do
profile=$(echo "${profile}" | tr -d "' ")
if [[ "${profile}" == "${SELECTED_PROFILE}" ]]; then
retry unlock_wallet
return
fi
done
echo "Error: Given profile ${SELECTED_PROFILE} does not exists."
echo "${clive_profiles}"
exit 1
fi
if [ ${#profile_array[@]} -eq 0 ]; then
echo "There are no profiles."
how_to_create_profile
else
echo "Select profile:"
for i in "${!profile_array[@]}"; do
profile=$(echo "${profile_array[i]}" | tr -d "' ")
echo "$((i + 1)). $profile"
done
echo "$(( ${#profile_array[@]} + 1 )). create new profile"
read -rp "Enter the number: " choice
if [[ $choice -ge 1 && $choice -le ${#profile_array[@]} ]]; then
selected_profile=$(echo "${profile_array[$((choice - 1))]}" | tr -d "' ")
echo "You selected: $selected_profile"
SELECTED_PROFILE=${selected_profile}
echo "Selected profile is ${SELECTED_PROFILE}"
retry unlock_wallet
elif [[ $choice -eq $(( ${#profile_array[@]} + 1 )) ]]; then
echo "You selected: create new profile"
how_to_create_profile
else
echo "Error: Invalid selection!"
return 1
fi
fi
}
# Execute a script passed as an argument
execute_passed_script() {
if [[ -n "${FILE_TO_EXECUTE:-}" ]]; then
if [[ -f "${FILE_TO_EXECUTE}" ]]; then
echo "Executing file: ${FILE_TO_EXECUTE}"
# shellcheck disable=SC1090
source "${FILE_TO_EXECUTE}"
exit 0
else
echo "Error: ${FILE_TO_EXECUTE} does not exist or is not a file."
exit 1
fi
fi
}
close_beekeeper() {
if [[ ${BEEKEEPER_ALREADY_CLOSED} -eq 0 ]]; then
clive beekeeper close >/dev/null 2>&1
BEEKEEPER_ALREADY_CLOSED=1
fi
}
# Execute before entering interactive mode
setup() {
start_beekeeper_with_prepared_session_token
retry select_profile
execute_passed_script
# shellcheck disable=SC1090
source ~/.bashrc
}
# Clean after termination of shell
clean_up() {
trap '' SIGINT
echo "Please wait. Cleaning up..."
close_beekeeper
trap - SIGINT
}
setup
......@@ -112,7 +112,9 @@ configure_docker_volumes() {
# Function to clean up (stop container) on exit signals
cleanup() {
echo "Stopping container ${CONTAINER_NAME}...."
docker stop "$CONTAINER_NAME"
echo "Cleanup actions done."
}
trap cleanup HUP INT QUIT TERM
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment