Você está aqui: Página Inicial > Contents > Documentos > sshpc > sshpc_server_install.sh
conteúdo

sshpc_server_install.sh

por EDSON - LAMBDA última modificação 13/06/2023 11h31

text/x-sh sshpc_server_install.sh — 4 KB

Conteúdo do arquivo

#!/bin/bash

#########################################################################################
## SSHPC - Cluster de processamento distribuído baseado em SSH                         ##
##                                                                                     ##
## Local:   Laboratório Multiusuário de Bioinformática e Análise de Dados (LAMBDA)     ##
##          Centro de Biotecnologia (CBiotec), Universidade Federal da Paraíba (UFPB)  ##
## Data:   30/05/2023                                                                  ##
## Coordenador: Edson Luiz Folador                                                     ##
## IC: Arthur Araújo de Lacerda                                                        ##
#########################################################################################

readonly PROGNAME=$(basename "$0")
readonly PROGDIR=$(readlink -m $(dirname "$0"))
readonly SSHPC_DIRECTORY="/home/sshpc"

# debugging flag
debugging() { ${debug} && echo -e "$1"; }

help() {
    echo "Usage: sudo ./sshpc_server_install.sh [OPTION]"
    echo "Installs sshpc on server"
    echo "Example sudo ./.sshpc_server_install.sh"
    echo
    echo "Options"
    echo -e "-q, --quiet\tDisables verbose"
    echo -e "-h, --help\tHelp function"
}

create_sshpc_user() {
  ### Creates sshpc system user
  # if user already exists, deletes user and procedes
  getent passwd sshpc > /dev/null && echo "user already exits" && exit 1

  useradd -mNr sshpc
  echo "sshpc:sshpc2022" | sudo chpasswd 
  local port="9123"
  sed -E -i "s/.*Port [0-9]+/Port ${port}/" /etc/ssh/sshd_config
  # TODO ajeitar isso
  # echo "ServerAliveInterval 60" >> /etc/ssh/sshd_config
}

create_bin() {
  ### Checks for .bin directory
  if [ ! -d "${SSHPC_DIRECTORY}/.bin" ]; then
    mkdir -p "${SSHPC_DIRECTORY}/.bin" && debugging "Creating client directories"
    mkdir -p "${SSHPC_DIRECTORY}/.ssh" && debugging "Creating ssh directories"
    chown -R sshpc /home/sshpc
    else debugging "Client directory already exists"; fi
}

create_run_directories() {
  ### Checks for necessary run, runned and running directories
  if [ ! -f "${SSHPC_DIRECTORY}/run" ] || [ ! -f "${SSHPC_DIRECTORY}/runned" ] || [ ! -f "${SSHPC_DIRECTORY}/running" ]; then
    mkdir -p ${SSHPC_DIRECTORY}/{run,runned,running} && debugging "Creating necessary directories"
    else debugging "Necessary directories already exist"; fi
}

install_sshpc_scripts() {
  ### install worker from server
  debugging "Downloading server scripts"

  declare -a scripts_to_download=("sshpc_worker.sh" "sshpc_client_pair.sh")

  for script in "${scripts_to_download[@]}"; do
    debugging "Downloading ${script} script"
    if $debug;
    then wget "https://www.ufpb.br/lambda/contents/documentos/sshpc/${script}" -O "${SSHPC_DIRECTORY}/.bin/${script}"
    else wget -q "https://www.ufpb.br/lambda/contents/documentos/sshpc/${script}" -O "${SSHPC_DIRECTORY}/.bin/${script}"; fi
    chmod +x "${SSHPC_DIRECTORY}/.bin/${script}"
  done
}

create_key() {
  ### Creates a ssh-key for the sshpc system user
  sudo -u sshpc ssh-keygen -t rsa -f "${SSHPC_DIRECTORY}"/.ssh/id_rsa -q -N ""
}

set_crontab() {
  ### Sets crontab routines

  # Sets verify to cron agenda to run every 5 minutes
  debugging "Adding verify routine to crontab"
  echo "* * * * * ${SSHPC_DIRECTORY}/.bin/sshpc_verify.sh -q" >> "${SSHPC_DIRECTORY}/agenda.txt"
  echo "@reboot find /home/sshpc/running -type f | xargs sed 's/#//' -i" >> "${SSHPC_DIRECTORY}/agenda.txt"
  echo "15 1 * * 1 reboot" >> "${SSHPC_DIRECTORY}/agenda.txt"
  crontab -u sshpc "${SSHPC_DIRECTORY}/agenda.txt" 
  rm ${SSHPC_DIRECTORY}/agenda.txt

  # Updates sshpc.log on reboot to root
  debugging "Adding sshpc.log updates to crontab"
  sudo -u sshpc touch "${SSHPC_DIRECTORY}/sshpc.log"
  sudo chmod 666 "${SSHPC_DIRECTORY}/sshpc.log"

  echo "@reboot tail -f /var/log/auth.log >> /home/sshpc/sshpc.log" >> "${SSHPC_DIRECTORY}/agenda.txt &"
  sudo -u root crontab "${SSHPC_DIRECTORY}/agenda.txt"
  rm ${SSHPC_DIRECTORY}/agenda.txt
}

main() {
  local debug=true
  while true; do
    case "$1" in
      -q | --quiet) debug=false; shift;;
      -h | --help) help; exit 0;;
      --) break;;
      *) break;;
    esac
  done

  create_sshpc_user
  create_bin
  create_run_directories
  install_sshpc_scripts
  set_crontab
  create_key
  debugging "=============================\n|||    Installation complete    |||\n============================="
}

main "$@"