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

sshpc_client_install.sh

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

text/x-sh sshpc_client_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_client_install.sh [OPTION]"
    echo "Installs sshpc on client"
    echo "Example sudo ./.sshpc_client_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
  if getent passwd sshpc > /dev/null; then
    debugging "sshpc user already exists, creating a new one\n"
    userdel -fr sshpc >& /dev/null
  else
    debugging "Creating sshpc user"
  fi

  useradd -mNr sshpc > /dev/null
  echo "sshpc:sshpc2022" | sudo chpasswd
  local port="9123"
  if [[ -f /etc/ssh/sshd_config ]]; then
    sed -E -i "s/.*Port [0-9]+/Port ${port}/" /etc/ssh/sshd_config
  else 
    debugging "OpenSSH-server required!\n install it, reboot the system and try again"
    exit 1 
  fi
}

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"
    touch "${SSHPC_DIRECTORY}/.ssh/authorized_keys"
    chown -R sshpc ${SSHPC_DIRECTORY}
    else debugging "Client directory already exists"; fi
}

create_config_file() {
  ### Checks for the config file
  if [ ! -f "${SSHPC_DIRECTORY}/.bin/sshpc.conf" ]; then
    touch ${SSHPC_DIRECTORY}/.bin/sshpc.conf && debugging "Creating config file"
    local lab
    local server
    local mac="$(cat /sys/class/net/en*/address | sed 's/://g')"
    local cores=$(nproc)
    local port=$(cat /etc/ssh/sshd_config | grep -Po "(?<=Port )(\d+)")

    echo -e "server=\"${server}\"\nport=\"${port}\"\nlab=\"${lab}\"\nmac=\"${mac}\"\ncores=\"${cores}\"" > "${SSHPC_DIRECTORY}/.bin/sshpc.conf"
  else
    debugging "Config file already exists"
  fi
}

install_bzip2() {
  ### Installing Bzip2
  apt-get install bzip2 && debugging "Installing bzip2"
}

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
}

set_crontab() {
  ### Sets worker to cron agenda to run every minute
  debugging "Adding routine to crontab"
  echo "* * * * * ${SSHPC_DIRECTORY}/.bin/sshpc_worker.sh -q" > "${SSHPC_DIRECTORY}/agenda.txt"
  crontab -u sshpc "${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_config_file
  install_bzip2
  install_sshpc_scripts
  set_crontab
  debugging "=============================\n|||    Installation complete    |||\n============================="
}

main "$@"