pelegant_setup.sh 1.52 KB
Newer Older
julian.gethmann's avatar
julian.gethmann committed
1 2 3
#!/bin/bash
# bash >= 4 required
# This script assumes to be run only once per day and overwrites files
4
# It also shouldn't be run for different hosts in parallel
julian.gethmann's avatar
julian.gethmann committed
5 6 7 8 9 10 11 12 13
# Search the "Edit"-line
# 
# - This script has not been tested so far
# - Mounting via sshfs is not included yet
set -euo pipefail
IFS=$'\n\t'

############################################################
# Edit:
14
declare -A host_cpus=( ["129.13.108.79"]="3" ["129.13.108.113"]="6" )
julian.gethmann's avatar
julian.gethmann committed
15 16
############################################################

17 18 19 20 21
# main_host=$(hostname -f)
main_host=$(ip -br -4 addr|\
	grep '129.13.108.'|\
	awk '{print $3}'|\
	cut -d"/" -f1)
julian.gethmann's avatar
julian.gethmann committed
22

23 24 25 26 27
printf "Generating SSH keys\n"
filename=~/.ssh/"id_Pelegant_$(date --iso)"
ssh-keygen -q -t ed25519 -o -f "${filename}" -C "Pelegant run from ${main_host} at $(date)"

printf "Creating mpihosts file\n"
julian.gethmann's avatar
julian.gethmann committed
28
if [ -f ~/.mpihosts ]; then
29
  /usr/bin/mv ~/.mpihosts ~/.mpihosts.$(date --iso)
julian.gethmann's avatar
julian.gethmann committed
30
fi
31
for host in "${!host_cpus[@]}"; do
32
  printf "%s:%s user=%s\n", "${host}" "${host_cpus[$host]}" "${USER}" >> ~/.mpihosts
julian.gethmann's avatar
julian.gethmann committed
33 34
done

35 36
printf "Copying files and mounting SSHFS\n"
for host in "${!host_cpus[@]}"; do
37
  printf "SSH copy from %s to %s (%s)", "${main_host}" "${host}" "${filename}"
38 39
  scp "${filename}" "${host}:~/.ssh/"
  scp "${filename}.pub" "${host}:~/.ssh/"
40 41
  ssh-copy-id -i "${filename}" "${host}"
  ssh-copy-id -i "${filename}" "${main_host}"
42 43 44 45

  scp ~/.mpihosts "${host}:.mpihosts"

  curdir=${PWD}
46 47 48
  ssh -i "${filename}" "${host}"\
	  "mkdir ${curdir} && "\
	  "sshfs ${main_host}:${curdir} ${curdir} -o IdentityFile=\"${filename}\""
julian.gethmann's avatar
julian.gethmann committed
49
done