Déploiement d'application sur plusieurs serveurs

From Tuxunix
Jump to: navigation, search

Déploiement d'application sur plusieurs serveurs

ScreenDeploy.jpg

Informations

CONFIGGEN.CFG : Fichier contenant les paramètres généraux, comme la date de réalisation de MEP, le nom de la MEP...

MEPGEN.sh : Script qui réalise l’ensemble des actions, copie des livrables sur les serveurs et exécutions des commandes à distance.

010DB.CFG : Fichier contenant les pools des serveurs concerné, répertoire/fichier à copier (source/destination), définition de la commande.

TEMPLATE : Répertoire contenant des exemples de fichier de configuration.

LIVRABLES : Répertoire contenant les fichiers à copier sur les serveurs distant pensé à créer des sous répertoires pour chaque pool de serveur !


CONFIGGEN.CFG

#!/bin/sh

#
#@Name          MEPCONFIGGEN.CFG
#@Fontion       Init var to MEP
#@Version       0.5
#@Authors       P.T
#@Modify by     X.X
#

numMepFileGen="MEP001"
dateMep="20101116"

#OTHERS
#######
mepFolder="/var/MEP/${numMepFileGen}"
logFile="${mepFolder}/${numMepFileGen}.log"
dateNow="$(date +%Y%m%d)"
dateTimeNow="$(date +%Y%m%d_%H%M%S)"
defaultUser="root"
#userCmd=""
serverType=""
FileTmpTestCopie="/tmp/test.txt"


#COLOR
######
red='\E[31;31m'
green='\E[32;32m'
yellow='\E[33;33m'
init='\033[0m'


#INIT VAR
#########
poolSrv=""
#source
folderRoot=""
#destination
folderTarget=""
cmd=""
errorCmd="/tmp/mep.${RANDOM}.log"

MEPGEN.sh

#!/bin/sh

#
#@Name          MEPGEN.sh
#@Fontion       Launch MEP
#@Version       0.5
#@Authors       P.T
#@Modify by     P.T (add testCopie + check error2 + add localCmd)
#

function initVars(){
       #Call MEPCONFIG file
       [ -f CONFIGGEN.CFG ] && {
               . CONFIGGEN.CFG
               } || {
               echo -e "Configuration file (CONFIGGEN.CFG) not found!!" | tee -a ${logFile}
               exit 1
               }
       #si rep non connue
       [ ! -e "${mepFolder}" ] && {
               echo -e "${red} Unknow MEP Folder!! ${init}" | tee -a ${logFile}
               exit 1
       }
       #si presence de fichier log on bckp.
       [ -e "${logFile}" ] && [ -s "${logFile}" ] && {
               echo -e "${yellow} Log file exist ${logFile} ! Move file log... ${init}" | tee -a ${logFile}
               /bin/mv ${logFile} ${logFile}.${dateTimeNow}.log
       }
       #Eraze logFile
       > ${logFile}

       [ -x "/usr/bin/tput" ] && {
               /usr/bin/tput clear | tee -a ${logFile}
               }

} #init

function resultCmd(){
       if [ "${1}" != "0" ]; then
               echo -e "Result cmd for ${2} : ${red} Error (${1}) ${init}"
               echo -e "Erreur execution appeler initiateur MEP"
               echo "ERROR_EXEC_MEP" > ${errorCmd}
               #break
               exit 1
       else
               echo -e "Result cmd for ${2} : ${green} Ok (${1}) ${init}"
               errorCmd="0"
       fi
} #resultCmd

function checkLaunchDate() {

[ "${dateMep}" != "${dateNow}" ] && {
       echo -e "${red} Mauvaise date execution, appeler la personne ayant fait la MEP ou la DMD ${init}" |tee -a ${logFile}
       exit 1
       }

} #checkLaunchDate


function endMep(){


echo "clear tmp file" | tee -a ${logFile}
rm -rf /tmp/mep.*.log
[ "$?" == "0" ] && echo -e "${green} clear tmp file [Done].${init}" || echo -e "${yellow} clear tmp file [Done].${init}"
echo | tee -a ${logFile}
echo "FIN DU SCRIPT : verifier les logs et si OK, passez a etape suivante" | tee -a ${logFile}
echo | tee -a ${logFile}

} #executionMep

function launchMep(){
       for f in ${PWD}/CONF/*.CFG ; do
               #Check error
               [ "$(cat "${errorCmd}" 2>/dev/null)" == "ERROR_EXEC_MEP" ] && {
                       echo -e "${red} Error de commande MEP stop... ${init}"
                       echo -e "${red} Appeler initiateur de la MEP. ${init}"
                       break
                       }
               [ -x "${f}" ] && {
                       echo "Init all variable..." | tee -a ${logFile}
                       . CONFIGGEN.CFG
                       echo "Load file : ${f}..." | tee -a ${logFile}
                       . ${f}
               } || {
                       echo -e "Configuration file ${f} not found or not executable!!" | tee -a ${logFile}
                       exit 1
               }
               #Charge fichier par fichier
               #verifie si fichier source exist
               [ -z "${folderRoot}" ] || [ -z "$(ls -A "${folderRoot}")" ] && {
                       echo -e "${yellow} No file to transfert exist!!${init}" | tee -a ${logFile}
                       }
               for i in ${poolSrv} ; do
                       #Check error
                       [ "$(cat "${errorCmd}" 2>/dev/null))" == "ERROR_EXEC_MEP" ] && {
                               echo -e "${red} Error de commande MEP stop... ${init}"
                               echo -e "${red} Appeler initiateur de la MEP. ${init}"
                               #break
                               exit
                               }
                       ping -c3 "${i}" >> ${logFile} 2>&1
                       [ "$?" != "0" ] && {
                                       echo -e "${red}Host ${i} no connection!${init}" | tee -a ${logFile}
                                       exit 1
                               } || {
                               if [ ! -z "${folderRoot}" ]; then
                                       #Copy files
                                       for j in ${folderRoot}/* ; do
                                               [ -z "${j}" ] && {
                                                       echo "No file found in ${folderRoot} to copy..." | tee -a ${logFile}
                                                       continue
                                                       }

                                               MDA5SUM_SRC=$(md5sum ${j} | cut -f1 -d' ')

                                               #Copie file
                                               scp -r ${j} ${i}:${folderTarget} >> ${logFile} 2>&1
                                               resultCmd $? CopieFile_${j} | tee -a ${logFile}
                                               #Check error
                                               [ "$(cat "${errorCmd}" 2>/dev/null))" == "ERROR_EXEC_MEP" ] && {
                                                       echo -e "${red} Error de commande MEP stop... ${init}"
                                                       echo -e "${red} Appeler initiateur de la MEP. ${init}"
                                                       #break
                                                       exit
                                                       }

                                               MDA5SUM_TRG=$(ssh ${i} "md5sum ${folderTarget}/${j##*/} | cut -f1 -d' '")

                                               if [ "${MDA5SUM_TRG}" != "${MDA5SUM_SRC}" ]; then
                                                       echo -e ${dateTimeNow}" - Erreur sur le controle MD5SUM de ${j##*/} - ${red} NOK ${init}" | tee -a ${logFile}
                                                       exit 1
                                               else
                                                       echo -e ${dateTimeNow}" - Controle MD5SUM ${j##*/} ${green} OK ${init}" | tee -a ${logFile}
                                               fi
                                       done
                               fi
                                       #Check error
                                       [ "$(cat "${errorCmd}" 2>/dev/null))" == "ERROR_EXEC_MEP" ] && {
                                               echo -e "${red} Error de commande MEP stop... ${init}"
                                               echo -e "${red} Appeler initiateur de la MEP. ${init}"
                                               #break
                                               exit
                                               }
                                       #Execute cmd distant
                                       [ ! -z "${cmd}" ] && {
                                               ssh ${i} "${cmd}" >> ${logFile} 2>&1
                                               resultCmd $? Command_${cmd} | tee -a ${logFile}
                                       } || {
                                               echo -e "${yellow}For ${i} command: ${cmd} empty...${init}" | tee -a ${logFile}
                                               #continue
                                       }
                                       #Execute cmd local
                                       [ ! -z "${localCmd}" ] && {
                                               ${localCmd} >> ${logFile} 2>&1
                                               resultCmd $? Command_${localCmd} | tee -a ${logFile}
                                       } || {
                                               echo -e "${yellow} local command: ${localCmd} empty...${init}" | tee -a ${logFile}
                                               continue
                                       }
                               }
               done
       done
} #transfert

function testCopie(){

for f in ${PWD}/CONF/*.CFG ; do
       [ -x "${f}" ] && {
               echo "Init all variable..." | tee -a ${logFile}
               . CONFIGGEN.CFG
               echo "Load file : ${f}..." | tee -a ${logFile}
               . ${f}
              } || {
               echo -e "Configuration file ${f} not found or not executable!!" | tee -a ${logFile}
               exit 1
              }
       #Charge fichier par fichier
       #verifie si fichier a copier sinon pas la peine de copier
       [ -z "${folderRoot}" ] || [ -z "$(ls -A "${folderRoot}")" ] && {
               echo -e "${yellow} No file to transfert exist!!${init}" | tee -a ${logFile}
               continue
               }

       for i in ${poolSrv} ; do
               ping -c3 "${i}" >> ${logFile} 2>&1
               [ "$?" != "0" ] && {
                               echo -e "${red}Host ${i} no connection!${init}" | tee -a ${logFile}
                               exit 1
                       } || {
                       #create test file
                       echo 'Test copie fichier sur serveur distant...' > ${FileTmpTestCopie}
                       if [ ! -z "${FileTmpTestCopie}" ]; then
                               #Copy files
                               MDA5SUM_SRC=$(md5sum ${FileTmpTestCopie} | cut -f1 -d' ')
                               #Copie file
                               scp -r ${FileTmpTestCopie} ${i}:${FileTmpTestCopie} >> ${logFile} 2>&1
                               resultCmd $? CopieFile_TestCopie | tee -a ${logFile}

                               MDA5SUM_TRG=$(ssh ${i} "md5sum ${FileTmpTestCopie} | cut -f1 -d' '")

                               if [ "${MDA5SUM_TRG}" != "${MDA5SUM_SRC}" ]; then
                                       echo -e ${dateTimeNow}" - Erreur sur le controle MD5SUM de ${j##*/} - ${red} NOK ${init}" | tee -a ${logFile}
                                       exit 1
                               else
                                       echo -e ${dateTimeNow}" - Controle MD5SUM ${j##*/} ${green} OK ${init}" | tee -a ${logFile}
                               fi
                       fi
                       }
       done
done

} #testCopie

function version(){

       echo -e "${0##*/}, version 0.2"

} #version

function help(){

echo -e ""
echo -e "Usage: ${0##*/} [-vsh] "
echo -e "-s --start [NOM_MEP]: Launch MEP"
echo -e "-t --test : verif cle ssh (copie srv)"
echo -e "-v --version : Version of ${0##*/}"
echo -e "-h --help : Usage run"
echo -e ""

} #help

#
#Main
#

[ ${#} == 0 ] && {
       echo "No arguments !"
       echo "Please, Usage: ${0##*/} --help"
       exit 1;
       }

initVars

while getopts ":vsth-:" opt; do
       case ${opt} in
               h)
               help
               ;;
               s)
               [ ! ${2} ] && {
                       echo "Numero MEP not found !" | tee -a ${logFile}
                       echo "Please, Usage: ${0##*/} --help" | tee -a ${logFile}
                       exit 1
                       } || {
                       [ "${2}" == "${numMepFileGen}" ] && {
                               numMep=${2}
                               checkLaunchDate
                               launchMep
                               endMep
                               } || {
                               echo "Error Numero MEP, check the num MEP!!" | tee -a ${logFile}
                               exit 1
                               }
                       }
               ;;
               t)
               testCopie
               ;;
               v)
               version
               ;;
               -) case ${OPTARG} in
                       help)
                       help
                       ;;
                       start)
                       [ ! ${2} ] && {
                               echo "Numero MEP not found !" | tee -a ${logFile}
                               echo "Please, Usage: ${0##*/} --help" | tee -a ${logFile}
                               exit 1
                               } || {
                               [ "${2}" == "${numMepFileGen}" ] && {
                                       numMep=${2}
                                       checkLaunchDate
                                       launchMep
                                       endMep
                               } || {
                                       echo "Error Numero MEP, check the num MEP!!" | tee -a ${logFile}
                                       exit 1
                               }
                       }
                       ;;
                       test)
                       testCopie
                       ;;
                       version)
                       version
                       ;;
                       *)
                       echo "Option illegale -${OPTARG}"
                       exit 1
                       ;;
               esac ;;
               *)
               echo "Option illegale ${opt}"
               exit 1
               ;;
       esac
done

CONF/010DB.CFG

  • Exemple de conf...
#!/bin/sh

#
#@Name          MEPCONFIGGEN.CFG
#@Fontion       Init var to MEP
#@Version       0.1
#@Authors       P.T
#@Modify by     X.X
#

#DB2
####
poolSrv="manpodb22"
#source
folderRoot="/var/MEP/${numMepFileGen}/LIVRABLES/DB"
fileRoot=""
#destination
folderTarget="/home/SQL"
fileTarget=""
cmd="cd /home/SQL && ./deployDB.sh db"