Skip to content

Commit

Permalink
Merge pull request #42 from functionland/correction-rpi
Browse files Browse the repository at this point in the history
Correction rpi
  • Loading branch information
ehsan6sha authored Feb 19, 2024
2 parents 7e02638 + 38a26df commit 6256068
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 50 deletions.
19 changes: 13 additions & 6 deletions docker/fxsupport/linux/fula.sh
Original file line number Diff line number Diff line change
Expand Up @@ -554,13 +554,20 @@ function dockerPrune() {
}

function restart() {
if [ -f "$RESIZE_SC" ]; then
# Wait for specific flags to indicate completion
while [ ! -f $partition_flag ] || [ ! -f $resize_flag ]; do
sleep 1 # Adjust sleep as needed
done
if sudo crontab -l | grep -q "$FULA_PATH/resize.sh"; then
echo "Resize cron job found, proceeding..."
# Proceed only if the cron job exists
if [ -f "$RESIZE_SC" ]; then
# Wait for specific flags to indicate completion
while [ ! -f $partition_flag ] || [ ! -f $resize_flag ]; do
sleep 1 # Adjust sleep as needed
done
else
echo "Resize script not found" | sudo tee -a $FULA_LOG_PATH
fi
else
echo "Resize script not found" | sudo tee -a $FULA_LOG_PATH
echo "Resize cron job not found in crontab" | sudo tee -a $FULA_LOG_PATH
# Optionally, handle the case when the cron job does not exist
fi
mkdir -p ${HOME_DIR}/.internal

Expand Down
39 changes: 38 additions & 1 deletion docker/fxsupport/linux/readiness-check.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,40 @@ def check_wifi_connection():
return "other"
return None

def attempt_wifi_connection():
config_yaml_path = os.path.join(HOME_PATH, ".internal", "config.yaml")
if os.path.exists(config_yaml_path):
logging.info("config.yaml exists, checking for non-FxBlox WiFi connections")
connections_output = subprocess.getoutput("nmcli con show | grep wifi")
wifi_connections = [line.split()[0] for line in connections_output.split('\n') if "wifi" in line and "FxBlox" not in line]

wifi_connected = False
for wifi_con in wifi_connections:
logging.info(f"Attempting to connect to {wifi_con}")
result = subprocess.run(["sudo", "nmcli", "con", "up", wifi_con], capture_output=True)
if result.returncode == 0:
logging.info(f"Successfully connected to {wifi_con}")
wifi_connected = True
break
else:
logging.error(f"Failed to connect to {wifi_con}")
if result.stderr:
logging.error(f"nmcli error: {result.stderr}")

if not wifi_connected:
logging.info("No successful WiFi connections, defaulting to FxBlox hotspot")
subprocess.run(["sudo", "nmcli", "con", "up", "FxBlox"], capture_output=True)

else:
logging.info("config.yaml does not exist, attempting to start FxBlox hotspot")
subprocess.run(["sudo", "nmcli", "con", "up", "FxBlox"], capture_output=True)

return None

def main():
logging.info("readiness check started")
fula_restart_attempts = 0
cycles_with_no_wifi = 0
while True:
if check_conditions():
logging.info("check_conditions passed")
Expand All @@ -53,7 +84,13 @@ def main():
break # Exit the loop as no further check is needed
else:
logging.info("wifi_status not connected")
subprocess.run(["python", os.path.join(FULA_PATH, "control_led.py"), "red", "5"])
if cycles_with_no_wifi == 6:
logging.info("wifi not connected, attempting to start FxBlox hotspot")
attempt_wifi_connection()
cycles_with_no_wifi = 0

subprocess.run(["python", os.path.join(FULA_PATH, "control_led.py"), "red", "10"])
cycles_with_no_wifi += 1
else:
logging.info("check_conditions failed")
# Check if 'fula_go' exists in `docker ps -a`
Expand Down
15 changes: 14 additions & 1 deletion docker/go-fula/go-fula.sh
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,29 @@ while true; do
if check_internet && check_files_exist; then
log "Internet connected and necessary files exist. Running /app."
node_key_file="/internal/.secrets/node_key.txt"
secret_phrase_file="/internal/.secrets/secret_phrase.txt"
mkdir -p /internal/.secrets

# Generate the node key
new_key=$(/app --generateNodeKey | grep -E '^[a-f0-9]{64}$')
# Check if the node_key file exists and has different content
if [ ! -f "$node_key_file" ] || [ "$new_key" != "$(cat $node_key_file)" ]; then
echo "$new_key" > "$node_key_file"
printf "%s" "$new_key" > "$node_key_file"
log "Node key saved to $node_key_file"
else
log "Node key file already exists and is up to date."
fi

# Generate the 12-word secret phrase
new_secret_phrase=$(/app --generateSecretPhrase)
# Check if the secret_phrase file exists and has different content
if [ ! -f "$secret_phrase_file" ] || [ "$new_secret_phrase" != "$(cat $secret_phrase_file)" ]; then
printf "%s" "$new_secret_phrase" > "$secret_phrase_file"
log "Secret Phrase saved to $secret_phrase_file"
else
log "Secret Phrase file already exists and is up to date."
fi

nmcli con down FxBlox
/app --config /internal/config.yaml
break
Expand Down
70 changes: 28 additions & 42 deletions docker/sugarfunge-node/run_node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ while ! check_writable; do
done

# Wait indefinitely until the password file and /uniondrive folder are available from go-fule docker
while [ ! -f "/internal/box_props.json" ] || [ ! -d "/uniondrive" ] || [ ! -f "/internal/.secrets/node_key.txt" ]; do
sleep 3
while [ ! -f "/internal/box_props.json" ] || [ ! -d "/uniondrive" ] || [ ! -f "/internal/.secrets/node_key.txt" ] || [ ! -f "/internal/.secrets/secret_phrase.txt" ]; do
[ ! -f "/internal/box_props.json" ] && echo "Waiting for /internal/box_props.json to become available..."
[ ! -d "/uniondrive" ] && echo "Waiting for /uniondrive to become available..."
[ ! -f "/internal/.secrets/node_key.txt" ] && echo "Waiting for /internal/.secrets/node_key.txt to become available..."
[ ! -f "/internal/.secrets/secret_phrase.txt" ] && echo "Waiting for /internal/.secrets/secret_phrase.txt to become available..."
sleep 3
done

# Read blox_seed from JSON file
Expand All @@ -56,70 +57,54 @@ secret_phrase_changed=0

# Save blox_seed into password.txt
# Check if /internal/.secrets/password.txt exists and has the same content as $blox_seed
if [ ! -f "/internal/.secrets/password.txt" ] || [ "$blox_seed" != "$(cat /internal/.secrets/password.txt)" ]; then
echo "$blox_seed" > /internal/.secrets/password.txt
if [ ! -f "/internal/.secrets/password.txt" ] || [ "$blox_seed" != "$(xargs < '/internal/.secrets/password.txt')" ]; then
printf "%s" "$blox_seed" > /internal/.secrets/password.txt
blox_seed_changed=1
fi

#save the node key
# Generate the node key only under specific conditions
if [ ! -f "/internal/.secrets/node_key.txt" ]; then
output=$(/sugarfunge-node key generate-node-key 2>&1)
echo "$output"
node_key=$(echo "$output" | tr ' ' '\n' | tail -n 1)
echo "$node_key" > /internal/.secrets/node_key.txt

# Extract the first line from node_peerid.txt
node_peerid=$(echo "$output" | head -n 1)
echo "$node_peerid" > /internal/.secrets/node_peerid.txt
fi

# create Aura and Grandpa keys
# Generate the secret phrase only under specific conditions
if [ ! -f "/internal/.secrets/secret_phrase.txt" ] || [ ! -f "/internal/.secrets/secret_seed.txt" ] || [ "$blox_seed_changed" -ne 0 ]; then
output=$(/sugarfunge-node key generate --scheme Sr25519 --password="$(cat '/internal/.secrets/password.txt')" 2>&1)
if { [ -f "/internal/.secrets/secret_phrase.txt" ] && [ ! -f "/internal/.secrets/secret_seed.txt" ]; } || [ "$blox_seed_changed" -ne 0 ]; then
#output=$(/sugarfunge-node key generate --scheme Sr25519 --password="$(cat '/internal/.secrets/password.txt')" 2>&1)
output=$(/sugarfunge-node key inspect "$(xargs < '/internal/.secrets/secret_phrase.txt')" --scheme Sr25519 --password="$(xargs < '/internal/.secrets/password.txt')" 2>&1)
echo "$output"
secret_phrase=$(echo "$output" | grep "Secret phrase:" | awk '{$1=$2=""; print $0}' | sed 's/^[ \t]*//;s/[ \t]*$//')
if [ ! -f "/internal/.secrets/secret_phrase.txt" ] || [ "$secret_phrase" != "$(cat /internal/.secrets/secret_phrase.txt)" ]; then
echo "$secret_phrase" > /internal/.secrets/secret_phrase.txt
secret_phrase_changed=1
fi
secret_phrase=$(xargs < '/internal/.secrets/secret_phrase.txt')

# Extract the Secret seed using awk and trim any extra spaces
secret_seed=$(echo "$output" | grep "Secret seed:" | awk '{$1=$2=""; print $0}' | sed 's/^[ \t]*//;s/[ \t]*$//')
if [ ! -f "/internal/.secrets/secret_seed.txt" ] || [ "$secret_seed" != "$(cat /internal/.secrets/secret_seed.txt)" ]; then
echo "$secret_seed" > /internal/.secrets/secret_seed.txt
secret_seed=$(printf "%s" "$output" | grep "Secret seed:" | awk '{$1=$2=""; print $0}' | sed 's/^[ \t]*//;s/[ \t]*$//')
if [ ! -f "/internal/.secrets/secret_seed.txt" ] || [ "$secret_seed" != "$(xargs < '/internal/.secrets/secret_seed.txt')" ]; then
printf "%s" "$secret_seed" > /internal/.secrets/secret_seed.txt
fi

# Extract the SS58 Address using awk and trim any extra spaces
account=$(echo "$output" | grep "SS58 Address:" | awk '{$1=$2=""; print $0}' | sed 's/^[ \t]*//;s/[ \t]*$//')
if [ ! -f "/internal/.secrets/account.txt" ] || [ "$account" != "$(cat /internal/.secrets/account.txt)" ]; then
echo "$account" > /internal/.secrets/account.txt
if [ ! -f "/internal/.secrets/account.txt" ] || [ "$account" != "$(xargs < '/internal/.secrets/account.txt')" ]; then
printf "%s" "$account" > /internal/.secrets/account.txt
fi
fi

# create grandpa account
secret_phrase=$(cat /internal/.secrets/secret_phrase.txt)
output_grandpa=$(/sugarfunge-node key inspect --password="$(cat '/internal/.secrets/password.txt')" --scheme Ed25519 "$secret_phrase" 2>&1)
output_grandpa=$(/sugarfunge-node key inspect --password="$(xargs < '/internal/.secrets/password.txt')" --scheme Ed25519 "$(xargs < '/internal/.secrets/secret_phrase.txt')" 2>&1)

echo "$output_grandpa"
# Extract the SS58 Address using awk and trim any extra spaces
account_grandpa=$(echo "$output_grandpa" | grep "SS58 Address:" | awk '{$1=$2=""; print $0}' | sed 's/^[ \t]*//;s/[ \t]*$//')
echo $account_grandpa
account_grandpa=$(printf "%s" "$output_grandpa" | grep "SS58 Address:" | awk '{$1=$2=""; print $0}' | sed 's/^[ \t]*//;s/[ \t]*$//')
echo "$account_grandpa"
if [ ! -f "/internal/.secrets/account_grandpa.txt" ] || [ "$account_grandpa" != "$(cat /internal/.secrets/account_grandpa.txt)" ]; then
echo "$account_grandpa" > /internal/.secrets/account_grandpa.txt
printf "%s" "$account_grandpa" > /internal/.secrets/account_grandpa.txt
fi

node_key=$(cat /internal/.secrets/node_key.txt)
node_key=$(xargs < '/internal/.secrets/node_key.txt')
if [ "$secret_phrase_changed" -ne 0 ] || [ "$blox_seed_changed" -ne 0 ] || [ ! -d "/internal/keys/" ] || [ -z "$(ls -A /internal/keys/)" ]; then

#Remove saved keys
rm -rf /internal/keys/*

#Add Aura key to keystore
/sugarfunge-node key insert --base-path=/uniondrive/chain --keystore-path=/internal/keys --chain /customSpecRaw.json --scheme Sr25519 --suri "$secret_phrase" --password="$(cat '/internal/.secrets/password.txt')" --key-type aura
/sugarfunge-node key insert --base-path=/uniondrive/chain --keystore-path=/internal/keys --chain /customSpecRaw.json --scheme Sr25519 --suri "$secret_phrase" --password="$(xargs < '/internal/.secrets/password.txt')" --key-type aura

#Add Grandpa key to keystore
./sugarfunge-node key insert --base-path=/uniondrive/chain --keystore-path=/internal/keys --chain /customSpecRaw.json --scheme Ed25519 --suri "$secret_phrase" --password="$(cat '/internal/.secrets/password.txt')" --key-type gran
./sugarfunge-node key insert --base-path=/uniondrive/chain --keystore-path=/internal/keys --chain /customSpecRaw.json --scheme Ed25519 --suri "$secret_phrase" --password="$(xargs < '/internal/.secrets/password.txt')" --key-type gran
fi

# Wait for network availability
Expand Down Expand Up @@ -202,9 +187,10 @@ while :; do
fi
done

secret_seed=$(cat /internal/.secrets/secret_seed.txt)
aura_account=$(cat /internal/.secrets/account.txt)
grandpa_account=$(cat /internal/.secrets/account_grandpa.txt)
secret_seed=$(xargs < '/internal/.secrets/secret_seed.txt')
aura_account=$(xargs < '/internal/.secrets/account.txt')
grandpa_account=$(xargs < '/internal/.secrets/account_grandpa.txt')
echo "secret_seed=$secret_seed and aura_account=$aura_account and grandpa_account=$grandpa_account"
if [ -n "$secret_seed" ] && [ -n "$aura_account" ] && [ -n "$grandpa_account" ]; then
/proof-engine -- "$secret_seed" "$aura_account" "$grandpa_account" &
PROOF_ENGINE_PID=$!
Expand All @@ -223,12 +209,12 @@ while :; do
exit_code=$?
break
fi
if ! kill -0 $PROOF_ENGINE_PID 2>/dev/null; then
if ! kill -0 "$PROOF_ENGINE_PID" 2>/dev/null; then
exit_code=$?
break
fi
sleep 5
done

# Exit with status of process that exited first
exit $exit_code
exit "$exit_code"

0 comments on commit 6256068

Please sign in to comment.