|
| 1 | +#!/bin/bash |
| 2 | +# this script "connects" the leader to the worker. |
| 3 | +# first argument is the name of the new/hostname pioreactor worker |
| 4 | +# second optional argument is the worker password, default "raspberry" |
| 5 | +# third optional argument is the address of the new Pioreactor, default "<hostname>.local" |
| 6 | + |
| 7 | +set -x |
| 8 | +set -e |
| 9 | +export LC_ALL=C |
| 10 | + |
| 11 | +HOSTNAME=$1 |
| 12 | +SSHPASS=${2:-raspberry} |
| 13 | +ADDRESS=${3:-"$HOSTNAME".local} |
| 14 | + |
| 15 | +USERNAME=pioreactor |
| 16 | + |
| 17 | +LEADER_ADDRESS=$(crudini --get /home/$USERNAME/.pioreactor/config.ini cluster.topology leader_address) |
| 18 | + |
| 19 | + |
| 20 | +# remove from known_hosts if already present |
| 21 | +ssh-keygen -R "$ADDRESS" >/dev/null 2>&1 |
| 22 | +ssh-keygen -R "$HOSTNAME" >/dev/null 2>&1 |
| 23 | +ssh-keygen -R "$(getent hosts "$ADDRESS" | cut -d' ' -f1)" >/dev/null 2>&1 |
| 24 | + |
| 25 | + |
| 26 | +# allow us to SSH in, but make sure we can first before continuing. |
| 27 | +# check we have .pioreactor folder to confirm the device has the pioreactor image |
| 28 | +N=120 |
| 29 | +counter=0 |
| 30 | + |
| 31 | +while ! sshpass -p "$SSHPASS" ssh "$USERNAME"@"$ADDRESS" "test -d /home/$USERNAME/.pioreactor && echo 'exists'" |
| 32 | +do |
| 33 | + echo "Connection to $ADDRESS missed - $(date)" |
| 34 | + |
| 35 | + if sshpass -v -p "$SSHPASS" ssh "$USERNAME"@"$ADDRESS" |& grep "Wrong password"; then |
| 36 | + echo "Wrong password provided." |
| 37 | + fi |
| 38 | + |
| 39 | + counter=$((counter + 1)) |
| 40 | + |
| 41 | + if [ "$counter" -eq "$N" ]; then |
| 42 | + echo "Attempted to connect $N times, but failed. Exiting." |
| 43 | + exit 1 |
| 44 | + fi |
| 45 | + |
| 46 | + sleep 1 |
| 47 | +done |
| 48 | + |
| 49 | + |
| 50 | +# Verify exact hostname match |
| 51 | +ACTUAL_HOSTNAME=$(sshpass -p "$SSHPASS" ssh "$USERNAME"@"$ADDRESS" "hostname") |
| 52 | +if [ "$ACTUAL_HOSTNAME" != "$HOSTNAME" ]; then |
| 53 | + echo "Hostname mismatch: expected '$HOSTNAME', but got '$ACTUAL_HOSTNAME'. Exiting." |
| 54 | + exit 1 |
| 55 | +fi |
| 56 | + |
| 57 | +# copy public key over |
| 58 | +sshpass -p "$SSHPASS" ssh-copy-id "$USERNAME"@"$ADDRESS" |
| 59 | + |
| 60 | +# remove any existing config (for idempotent) |
| 61 | +# we do this first so the user can see it on the Pioreactors/ page |
| 62 | +UNIT_CONFIG=/home/$USERNAME/.pioreactor/config_"$HOSTNAME".ini |
| 63 | + |
| 64 | +rm -f "$UNIT_CONFIG" |
| 65 | +touch "$UNIT_CONFIG" |
| 66 | +echo -e "# Any settings here are specific to $HOSTNAME, and override the settings in shared config.ini" >> "$UNIT_CONFIG" |
| 67 | + |
| 68 | +# add worker's address to config |
| 69 | +CONFIG=/home/$USERNAME/.pioreactor/config.ini |
| 70 | +crudini --set "$CONFIG" cluster.addresses "$HOSTNAME" "$ADDRESS" |
| 71 | + |
| 72 | +# add worker to known hosts on leader |
| 73 | +ssh-keyscan "$ADDRESS" >> "/home/$USERNAME/.ssh/known_hosts" |
| 74 | + |
| 75 | + |
| 76 | +# sync-configs |
| 77 | +pios sync-configs --units "$HOSTNAME" --skip-save |
| 78 | +sleep 1 |
| 79 | + |
| 80 | +# check we have config.ini file to confirm the device has the necessary configuration |
| 81 | +N=120 |
| 82 | +counter=0 |
| 83 | + |
| 84 | +while ! sshpass -p "$SSHPASS" ssh "$USERNAME"@"$ADDRESS" "test -f /home/$USERNAME/.pioreactor/config.ini && echo 'exists'" |
| 85 | +do |
| 86 | + echo "Looking for config.ini - $(date)" |
| 87 | + |
| 88 | + counter=$((counter + 1)) |
| 89 | + |
| 90 | + if [ "$counter" -eq "$N" ]; then |
| 91 | + echo "Attempted to find config.ini $N times, but failed. Exiting." |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | + |
| 95 | + sleep 1 |
| 96 | +done |
| 97 | + |
| 98 | +# sync date & times, specifically for LAP see https://github.com/Pioreactor/pioreactor/issues/269 |
| 99 | +ssh "$USERNAME"@"$ADDRESS" "sudo date --set \"$(date)\" && sudo fake-hwclock save" |
| 100 | +ssh "$USERNAME"@"$ADDRESS" "echo \"server $LEADER_ADDRESS iburst prefer\" | sudo tee -a /etc/chrony/chrony.conf || :" |
| 101 | + |
| 102 | + |
| 103 | +# reboot to set configuration |
| 104 | +# the || true is because the connection fails, which returns as -1. |
| 105 | +ssh "$USERNAME"@"$ADDRESS" 'sudo reboot;' || true |
| 106 | + |
| 107 | +exit 0 |
0 commit comments