diff --git a/scripts/mini-steward/README.md b/scripts/mini-steward/README.md new file mode 100644 index 0000000..0f171fa --- /dev/null +++ b/scripts/mini-steward/README.md @@ -0,0 +1,42 @@ +# mini-steward + +Loop over all lines of host ip addresses specified in the hosts.txt +file, and execute the the script with the id.rsa given as the input argument. +This script will run until done, to kill it use: + +```bash +CTRL+z +pkill -f loop-wrapper +``` + +## Overview + +- This is a helper script that will copy a script you want to run on one or more nodes, and execute it. +- The result of running the script will be returned in the `status.log` file. +- The script wil continue running until execution on all nodes are succesful. +- The result when successful run on a host will be written to the `done.log` file, with the output specified. + +## Usage + +Specify the the hosts to execute the script on in the `hosts.txt` file, notation `ip-address,node-name` separated by new lines. + +Example : + +```text +10.0.0.1,node1 +10.0.0.2,node2 +``` + +And start running the script with: + +```bash +./loop-wrapper +``` + +### The script to be executed + +To get the output written to `done.log` from the script that have been copied and ran on the nodes, it should write the desired output to `STDOUT` as it's last thing before calling `exit 0`. + +The same goes for error messages. + +See more details in the `test.sh` file for an example. diff --git a/scripts/mini-steward/hosts.txt b/scripts/mini-steward/hosts.txt new file mode 100644 index 0000000..e69de29 diff --git a/scripts/mini-steward/loop-wrapper.sh b/scripts/mini-steward/loop-wrapper.sh new file mode 100755 index 0000000..64642f9 --- /dev/null +++ b/scripts/mini-steward/loop-wrapper.sh @@ -0,0 +1,135 @@ +#!/bin/bash + +# Loop over all lines of host ip addresses specified in the hosts.txt +# file, and execute the the script given as the input argument. If no +# script is given as argument the scriptName specified below will be +# used. +# This script will run until done, to kill it use +# > CTRL+z +# > pkill -f loop-wrapper +### +# Info: change out the script in scriptName below with the executable +# script you want copied to host, and executed. +### +# Environmental variables can be passed on to the host if needed by +# embedding them directly in the ssh command. See below. +# +# sshOut=$(ssh -o ConnectTimeout=$sshTimeout -n -i $idRsaFile "$sshUser"@"$ipAddress" "sudo bash -c 'export NODENAME="$name"; $scpDestinationFolder/$scriptName'" 2>&1) +# Here we are exporting NODENAME set to the current value of $name when +# starting the ssh session, and then the script called on host can take +# use of it directly. + +if [ -z "$1" ]; then + echo "No script to call supplied" + echo "Usage: ./loop-wrapper " + exit 1 +else + scriptSourcePath=$(dirname "$1")"/" + echo "scriptSourcePath=$scriptSourcePath" + scriptName=$(basename "$1") + echo "scriptName=$scriptName" +fi + +if [ -z "$2" ]; then + echo "No id.rsa file supplied" + echo "Usage: ./loop-wrapper " + exit 1 +else + idRsaFile=$2 +fi + +pingTimeout=3 + +statusLog="./status.log" +touch $statusLog + +notDoneFile="./notdone.tmp" +doneFile="./done.log" +touch $doneFile + +# The file containing the ip adresses to work with. +# One address per line. +hostsFile="./hosts.txt" + +sshTimeout=30 +sshUser=ansible +scpDestinationFolder=/home/$sshUser + +# Loop until notDoneFile is empty. Checked in the end of loop. +while true; do + touch $notDoneFile + + # Read from the hosts.txt file one line at a time + # and execute.. + while IFS=, read -r ipAddress name; do + if [[ -z "$name" ]]; then + echo "error: missing name for ip $ipAddress in hosts.txt. Should be ," + exit 1 + fi + + echo working on :"$ipAddress","$name" + + # Check if host is available + if ping "$ipAddress" -c 1 -t $pingTimeout; then + echo ok info:ping ok:"$name", "$ipAddress" >>$statusLog + ## Put whatever to execute here... + ## copy files&scripts to run on host. + ## Execute the copied files/scripts on host. + # ---------------------------------------------- + # scp script file. + if scp -o ConnectTimeout=$sshTimeout -o StrictHostKeyChecking=no -i $idRsaFile "$scriptSourcePath$scriptName" "$sshUser"@"$ipAddress":; then + # Execute script file copied via ssh. + sshOut=$(ssh -o ConnectTimeout=$sshTimeout -n -i $idRsaFile "$sshUser"@"$ipAddress" "sudo bash -c 'export NODENAME=$name; $scpDestinationFolder/$scriptName'" 2>&1) + + if [ $? -eq 0 ]; then + echo ok info:succesfully executing ssh command: "$name", "$ipAddress" + echo ok info:succesfully executing ssh command: "$name", "$ipAddress" >>$statusLog + else + echo error:failed executing ssh command: "$sshOut", "$ipAddress", "$name" + echo error:failed executing ssh command: "$sshOut","$ipAddress", "$name" >>$statusLog + echo "$ipAddress","$name" >>$notDoneFile + continue + fi + else + echo error:failed scp copying, not running ssh command:"$ipAddress", "$name" + echo error:failed scp copying, not running ssh command:"$ipAddress", "$name" >>$statusLog + echo "$ipAddress","$name" >>$notDoneFile + continue + + fi + # ---------------------------------------------- + + echo "info: all done for host: " "$ipAddress", "$name" + echo "info: all done for host: " "$ipAddress", "$name" >>$statusLog + + echo "$ipAddress","$name","$sshOut" >>$doneFile + else + echo error: no ping reply: "$ipAddress", "$name" + echo error: no ping reply: "$ipAddress", "$name" >>$statusLog + + echo "$ipAddress","$name" >>$notDoneFile + continue + fi + + done <./hosts.txt + + # Check if notdone file is empty, loop if not empty, + # and break out and exit if empty. + if [ -s "$notDoneFile" ]; then + # The file is not-empty. + echo "the notDone file is not-empty, so we're looping" + cp $notDoneFile "$hostsFile" + rm $notDoneFile + else + # The file is empty, meaning.. we're done. + echo "info: all done for all hosts, exiting: " + echo "info: all done for all hosts, exiting: " >>$statusLog + + cp $notDoneFile "$hostsFile" + rm $notDoneFile + break + fi + + sleep 2 + +done diff --git a/scripts/mini-steward/test.sh b/scripts/mini-steward/test.sh new file mode 100755 index 0000000..946c7e8 --- /dev/null +++ b/scripts/mini-steward/test.sh @@ -0,0 +1,111 @@ +#!/bin/bash + +# Script to be copied over to the host, and executed. +# The script should exit with a line echoing out the +# error or success message, and should then be followed +# by an error code 0 if ok, and >0 if not ok. +## +# The HOSTNAME environmental variable is passed on to +# the host when the ssh session are initiated within +# the wrapper script. It is read from the hosts.txt +# file which should be in the format: +# 10.10.10.1,myhostnamehere + +workdir=/usr/local/steward + +# ------- Generate steward config + +mkdir -p $workdir/etc +touch $workdir/etc/config.toml + +# The ${NODENAME} is exported directly in the ssh command in the loop-wrapper script +cat >$workdir/etc/config.toml </dev/null 2>&1; then + echo "*" error: systemctl disable progName.service + exit 1 + fi + + if ! systemctl stop $progName.service >/dev/null 2>&1; then + echo "*" error: systemctl stop $progName.service + exit 1 + fi + + if ! rm $systemctlFile >/dev/null 2>&1; then + echo "*" error: removing $systemctlFile service file + exit 1 + fi +fi + +#echo "*" creating systemd $progName.service +cat >$systemctlFile <${progName} service +Documentation=https://github.com/RaaLabs/steward +After=network-online.target nss-lookup.target +Requires=network-online.target nss-lookup.target + +[Service] +ExecStart=env CONFIG_FOLDER=/usr/local/steward/etc /usr/local/steward/steward + +[Install] +WantedBy=multi-user.target +EOF + +# ------- generate nats keys + +errorMessage=$( + # Redirect stderr to stdout for all command with the closure. + { + # Generate private nk key + /usr/local/bin/nk -gen user >$workdir/seed.txt && + # Generate public nk key + /usr/local/bin/nk -inkey $workdir/seed.txt -pubout >$workdir/user.txt && + chmod 600 $workdir/seed.txt && + chmod 600 $workdir/user.txt && + systemctl enable $progName.service && + systemctl start $progName.service + } 2>&1 +) + +# ------- We're done, send output back to ssh client + +# Check if all went ok with the previous command sequence, or if any errors happened. +# We return any result back to the ssh sessions which called this script by echoing +# the result, and do an exit . +# We can return one line back, so if more values are needed to be returned, they have +# to be formated into the same line. +# +# All went ok. +if [ $? -eq 0 ]; then + user=$(cat $workdir/user.txt) + echo "nkey-user=$user" + exit 0 +# An error happened. +else + echo "failed executing script: $errorMessage" + exit 1 +fi