diff --git a/deploy_nix.sh b/deploy_nix.sh index e32ad4a..e5a1ef2 100755 --- a/deploy_nix.sh +++ b/deploy_nix.sh @@ -1,13 +1,47 @@ -#! /usr/bin/env bash - -if [ -z "${NODE:-}" ] - then - echo "Erreur : Si c'est la première fois que vous déployez le serveur, merci de faire: sudo NODE='votre_node' ./deploy_nix.sh" - exit 1 +if [ -z "$1" ]; then + # If no mode is specified it is local by default + MODE="local" +else + if [ "$1" = "local" ] || [ "$1" = "all" ]; then + MODE="$1" + else + echo "Mode $1 not recognized. Use either \`local\` for deployement on this node or \`all\` for deployement on the cluster." + exit 1 + fi fi -cp configuration.nix /etc/nixos/configuration.nix -cp cluster/nodes/$NODE.nix /etc/nixos/node.nix -cp wgautomesh.nix /etc/nixos/wgautomesh.nix -cp cluster.nix /etc/nixos/cluster.nix -nixos-rebuild switch +# Get hostname for local deployement +HOSTNAME=$(hostname) + +if [ "$MODE" = "local" ]; then + echo "====== Started deployement for LOCAL NODE... ======" + echo "Copying files ..." + sudo cp ./configuration.nix /etc/nixos/ + sudo cp ./cluster.nix /etc/nixos/ + sudo cp ./wgautomesh.nix /etc/nixos/ + sudo cp ./cluster/nodes/$HOSTNAME.nix /etc/nixos/node.nix + echo "Rebuilding nixOs ..." + sudo nixos-rebuild switch +elif [ "$MODE" = "all" ]; then + echo "====== Started deployement for ALL CLUSTER... ======" + read -r -s -p "Enter remote sudo password: " ROOT_PASS + # Get list of files in ./cluster/nodes to get hostnames of nodes + NIXHOSTLIST=$(ls cluster/nodes | grep '\.nix$') + for NIXHOST in $NIXHOSTLIST; do + NIXHOST=${NIXHOST%.*} + echo "" + echo "====== doing $NIXHOST ======" + echo "Copying files ..." + # Creating files and folder in /tmp/nixcfg with user permissions + # Because we cannot scp to root folder + echo "mkdir -p /tmp/nixcfg && rm -R /tmp/nixcfg/*" | ssh -p 1972 $NIXHOST.node.chokbar.bzh sh - + scp -P 1972 ./configuration.nix $NIXHOST.node.chokbar.bzh:/tmp/nixcfg/ + scp -P 1972 ./cluster.nix $NIXHOST.node.chokbar.bzh:/tmp/nixcfg/ + scp -P 1972 ./wgautomesh.nix $NIXHOST.node.chokbar.bzh:/tmp/nixcfg/ + scp -P 1972 ./cluster/nodes/$NIXHOST.nix $NIXHOST.node.chokbar.bzh:/tmp/nixcfg/node.nix + echo "Moving them to /etc/nixos ..." + echo "sudo -S <<< $ROOT_PASS mv /tmp/nixcfg/* /etc/nixos/" | ssh -p 1972 $NIXHOST.node.chokbar.bzh sh - + echo "Rebuilding nixOs ..." + echo "sudo -S <<< $ROOT_PASS nixos-rebuild switch" | ssh -p 1972 $NIXHOST.node.chokbar.bzh sh - + done +fi