aboutsummaryrefslogtreecommitdiff
path: root/sshtool
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2022-04-20 13:01:51 +0200
committerAlex Auvolat <alex@adnab.me>2022-04-20 13:03:29 +0200
commit9c9c776213478023d4cab6290efcb6adfdbbbe86 (patch)
tree85ae8d2c3dac9c01daf5a1524b8a4ff83b84df70 /sshtool
parent50e9f0b589b6387d193fcb420ddc045c0bc6d632 (diff)
downloadnixcfg-9c9c776213478023d4cab6290efcb6adfdbbbe86.tar.gz
nixcfg-9c9c776213478023d4cab6290efcb6adfdbbbe86.zip
Refactor deployment scripts
Diffstat (limited to 'sshtool')
-rwxr-xr-xsshtool83
1 files changed, 83 insertions, 0 deletions
diff --git a/sshtool b/sshtool
new file mode 100755
index 0000000..94a3ea0
--- /dev/null
+++ b/sshtool
@@ -0,0 +1,83 @@
+#!/usr/bin/env bash
+
+cd $(dirname $0)
+
+CMDFILE="$1"
+shift 1
+
+CLUSTER="$1"
+if [ -z "$CLUSTER" ] || [ ! -d "cluster/$CLUSTER" ]; then
+ echo "Usage: $CMDFILE <cluster name>"
+ echo "The cluster name must be the name of a subdirectory of cluster/"
+ exit 1
+fi
+shift 1
+
+if [ -z "$1" ]; then
+ NIXHOSTLIST=$(ls cluster/$CLUSTER/node | grep -v '\.site\.')
+else
+ NIXHOSTLIST="$@"
+fi
+
+if [ -z "$ROOT_PASS" ]; then
+ read -s -p "Enter remote root password: " ROOT_PASS
+ echo
+fi
+
+SSH_CONFIG=cluster/$CLUSTER/ssh_config
+
+function header {
+ cat <<EOF
+export DEPLOYTOOL_ROOT_PASSWORD=$ROOT_PASS
+cat > /tmp/deploytool_askpass <<EOG
+#!/usr/bin/env sh
+echo \$DEPLOYTOOL_ROOT_PASSWORD
+EOG
+chmod +x /tmp/deploytool_askpass
+export SUDO_ASKPASS=/tmp/deploytool_askpass
+sudo -A sh - <<EOEVERYTHING
+EOF
+}
+
+function footer {
+ echo EOEVERYTHING
+}
+
+function message {
+ echo "echo '$@'"
+}
+
+function cmd {
+ echo "echo '- run $@'"
+ echo "$@"
+}
+
+function set_env {
+ echo "echo '- set $@'"
+ echo "export $@"
+}
+
+function copy {
+ local FROM=$1
+ local TO=$2
+ cat <<EOF
+echo '- write $TO from $FROM'
+base64 -d <<EOG | tee $TO > /dev/null
+$(base64 <$FROM)
+EOG
+EOF
+}
+
+for NIXHOST in $NIXHOSTLIST; do
+ NIXHOST=${NIXHOST%.*}
+
+ if [ -z "$SSH_USER" ]; then
+ SSH_DEST=$NIXHOST
+ else
+ SSH_DEST=$SSH_USER@$NIXHOST
+ fi
+
+ echo "==== DOING $NIXHOST ===="
+
+ (header; . $CMDFILE; footer) | ssh -F $SSH_CONFIG $SSH_DEST sh -
+done