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

sshpc_run.sh

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

text/x-sh sshpc_run.sh — 5 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"; }

check_running_dir() {
  ### Checks for instruction files already running or to run
  local mac="$1"

  if [[ -n $(find "${SSHPC_DIRECTORY}/running/" -name "${mac}*") ]]; then
    local instruction_file=$(find "${SSHPC_DIRECTORY}/running/" -name "${mac}*" | sort | head -n 1) 

  elif [[ -n "$(ls ${SSHPC_DIRECTORY}/run/)" ]]; then
    local instruction_file=$(find "${SSHPC_DIRECTORY}/run/" -type f -printf '%T+ %p\n' | sort | head -n 1 | cut -d ' ' -f 2- )
    mv "${instruction_file}" "${SSHPC_DIRECTORY}/running/${mac}_$(basename ${instruction_file})" 
    local instruction_file="${SSHPC_DIRECTORY}/running/${mac}_$(basename ${instruction_file})" 
  else
    debugging "No files to be run"
    exit 0
  fi

  echo "${instruction_file}"
}

send_files() {
  ### ENV
  debugging "ENV"
  debugging "Sending files: ${comm} to client ${mac} ip: ${ip}, port: ${port}"
  if $debug
  then local env_command="scp -P ${port} \"${comm}\" sshpc@${ip}:~"
  else local env_command="scp -q -P ${port} \"${comm}\" sshpc@${ip}:~"; fi

  eval "${env_command}" || exit 1
}

send_compacted_files() {
  ### BEN
  debugging "BEN"
  debugging "Sending compacted files"
  local ben_command='tar cjf - \"${comm}\" | ssh -n -p ${port} bioinfo@150.165.131.12 "tar xjf -"'
  eval "${ben_command}" || exit 1
  debugging "Compacted files sent"
}

get_files() {
  ### REC
  debugging "REC"
  debugging "Gettings files from client"
  if $debug
  then local rec_command="scp -P ${port} sshpc@${ip}:~/${comm}"
  else local rec_command="scp -q -P ${port} sshpc@${ip}:~/${comm}"; fi
  eval "${rec_command}" || exit 1
  debugging "Files loaded from client"
}

compact_client_files() {
  ### BRE
  debugging "BRE"
  debugging "Compacting files for client"
  local bre_command='ssh -n -p ${port} bioinfo@150.165.131.12 "tar zcf - \"${comm}\"" | tar zxf -'
  eval "${bre_command}" || exit 1
  debugging "Files for client compacted"
}

execute_command() {
  ### EXE
  debugging "EXE"
  debugging "Executing files on Client"
  debugging "${comm}"
  local comm=$(echo "${comm}" | sed "s/cpu_from_client/${1}/")
  local exe_command="ssh -n -p ${port} sshpc@${ip} ${comm}"
  eval "${exe_command}" || exit 1
  debugging "exe_commands on client executed"
}

execute_on_server() {
  ### SRV
  debugging "SRV"
  debugging "Executing files on Server"
  debugging "${comm}"
  local exe_command="${comm}"
  eval "${exe_command}" || exit 1
  debugging "exe_commands on client executed"
}

delete_files() {
  ### DEL
  debugging "DEL"
  debugging "Deleting client files"
  debugging "${comm}"
  local del_command="ssh -n -p ${port} sshpc@${ip} rm \"${comm}\""
  eval "${del_command}"
  debugging "Client files deleted"
}

append_to_sshpc_log() {
  local file="$1"
  local ip="$2"
  local mac="$3"
  local lab="$4"

  rm "${file}"
  echo "$(date +"%d/%m/%y%t%H:%M")"$'\t'"${lab}"$'\t'"${ip}"$'\t'"$(basename "${file}")" >> "/home/sshpc/runned/$(date +"%Y%m")_sshpclog.txt"
}

iterate_command_file() {
  ### Iterates through command file
  local file="$1"
  local ip="$2"
  local mac="$3"
  local lab="$4"
  local free_cores="$5"
  local port="9123"

  debugging "\nlab: $lab\nmac: $mac\navailable cores: $free_cores\nip: $ip\nfile: $file\nport: $port\n"

  if [ ! -f "${file}" ]; then debugging "${file} instruction file does not exist" && exit 1; else
    while read -r seq ins comm; do
      [[ "$seq" =~ ^# ]] && continue
      case $ins in 
        ENV)
            send_files;;
        BEN)
            send_compacted_files;;
        REC)
          get_files;;
        BRE)
          compact_client_files;;
        EXE)
          execute_command "${free_cores}";;
        SRV)
          execute_on_server;;
        DEL)
          delete_files;;
      esac
      sed -i "s/${seq}/#${seq}/" "${file}"
    done<"$file"
    append_to_sshpc_log "${file}" "${ip}" "${mac}" "${lab}"
  fi
}

main () {
  # Debug 
  local debug=true
  while true; do
      case "$1" in
          -q | --quiet) debug=false; shift;;
          --) break;;
          *) break;;
      esac
  done
  debugging "=============================\n|||    Running ${PROGNAME}    |||\n============================="

  # Variables sent from verify.sh
  local ip=$1
  local mac=$2
  local free_cores=$3
  local lab=$4

  local file=$(check_running_dir "${mac}")
  debugging "file: ${file}\nset to: ${mac}"

  iterate_command_file "${file}" "${ip}" "${mac}" "${lab}" "${free_cores}"
}

main "$@"