MigrationApp.sh: Difference between revisions
Jump to navigation
Jump to search
(Created page with "<syntaxhighlight lang="bash"> #!/bin/bash # migrationApp.sh <envCode> <RHELVersion> <hostName|appCode> [IPADDR] #pacheco at infocepo.com # if no parameters take from list if...") |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 2: | Line 2: | ||
#!/bin/bash | #!/bin/bash | ||
# migrationApp.sh <envCode> <RHELVersion> <hostName|appCode> [IPADDR] | # migrationApp.sh <envCode> <RHELVersion> <hostName|appCode> [IPADDR] | ||
# | #ynotopec at gmail.com | ||
# if no parameters take from list | # if no parameters take from list | ||
Line 226: | Line 226: | ||
</body></html>' | </body></html>' | ||
( echo "From: admin | ( echo "From: admin@company.com"; | ||
echo "To: customer@company.com"; | echo "To: customer@company.com"; | ||
echo "To: | echo "To: user@company.com"; | ||
echo "Cc: admin | echo "Cc: admin@company.com"; | ||
echo "Subject: ${envApp}app@${hostName}"; | echo "Subject: ${envApp}app@${hostName}"; | ||
echo "Content-Type: text/html"; | echo "Content-Type: text/html"; |
Latest revision as of 13:25, 31 January 2023
#!/bin/bash
# migrationApp.sh <envCode> <RHELVersion> <hostName|appCode> [IPADDR]
#ynotopec at gmail.com
# if no parameters take from list
if [ $# -eq 0 ] ;then
cat /exploitation/env/migrationAppCfg |grep . |grep -vE "^[[:space:]]*#" |sed -r 's#[[:space:]]+#;#g' |head -20 |while IFS=';' read envMy RHELVersion hostName IPADDR ;do
/exploitation/env/migrationApp.sh $envMy $RHELVersion $hostName $IPADDR &&sed -i -r 's/^(.*'${hostName}'.*)$/#\1/' /exploitation/env/migrationAppCfg
done
exit
fi
# 8 or 7
envMy=$1
RHELVersion=$2
hostName=$3
IPADDR=$4
# initialisation des variables
baseDir="$(realpath "$(dirname $0)"/..)"
letterEnv=$(echo $envMy |cut -c1 )
# Model
envKvm=kvm
if [ "${letterEnv}" == "e" ];then
envKvm=exkvm
modelName=APP"${RHELVersion}"EX
elif [ "${letterEnv}" == "p" ] ;then
modelName=APP"${RHELVersion}"PE
else
modelName=APP"${RHELVersion}"
fi
# init
if [ ${#hostName} -ne 3 ] ;then
install.sh "${envKvm}" "${modelName}" "${hostName}" "${IPADDR}"
# appli init
envApp="${envMy}""$(echo "${hostName}" |cut -c5-7 )"
else
install.sh "${envKvm}" "${modelName}" |tee /tmp/$$
# appli init
envApp="${envMy}${hostName}"
hostName="$(cat /tmp/$$ |grep ^hostName= |head -1 |cut -d= -f2 )"
fi
# Add app disk
devName=vdb
#sizeDisk=$(ssh.sh svc${envApp} "df |grep -w /${envApp} |grep -v dbbck$ |awk '{total = total + \$3}END{print int(total/1024/1024*1.25+.5)}'" |grep -v == )
sizeDisk=$(ssh.sh svc${envApp} "df |grep -w /${envApp} |grep -v dbbck$ |awk '{total = total + \$2}END{print int(total/1024/1024+.5)}'" |grep -v == )
#if PE/EX increase disk because BCK
if [ "${envMy}" == "pe" ] ||[ "${envMy}" == "ex" ] ;then
#sizeBck=$((${sizeDisk} / 4 ))
##audit real BCK and fix as necessary
#sizeBckReal=$(ssh.sh svc${envApp} "df |grep -w /${envApp} |grep -v // |grep dbbck$ |awk '{total = total + \$3}END{print int(total/1024/1024*1.25+.5)}'" |grep -v == )
#if [ $sizeBckReal -gt $sizeBck ] ;then
# sizeBck=$sizeBckReal
#fi
sizeBck=$(ssh.sh svc${envApp} "df |grep -w /${envApp} |grep -v // |grep dbbck$ |awk '{total = total + \$2}END{print int(total/1024/1024+.5)}'" |grep -v == )
.KVMdisk.py ${hostName} $((${sizeDisk} + ${sizeBck} ))
else
.KVMdisk.py ${hostName} ${sizeDisk}
fi
#* users
passwdUser=$(ssh.sh svc${envApp} "grep ${envApp} /etc/passwd" |grep -v == )
shadowUser=$(ssh.sh svc${envApp} "grep ${envApp} /etc/shadow" |grep -v == )
ssh.sh "${hostName}" "echo '$passwdUser' >>/etc/passwd"
[ ! -z "${shadowUser}" ] &&ssh.sh "${hostName}" "echo '$shadowUser' >>/etc/shadow"
# add user bch to admindata group
ssh.sh "${hostName}" '\
envApp=$(grep ^.....app: /etc/passwd |cut -c1-5 )
if [ ! -z "${envApp}" ] ;then
[ -z "$(grep -w ^admindata /etc/group)" ] &&(echo "admindata:x:500:${envApp}bch" >>/etc/group )
[ -z "$(grep -w ^exploit /etc/group)" ] &&(echo "exploit:x:501:${envApp}app" >>/etc/group )
[ -z "$(grep -E ^${envApp}bch:x:[0-9]+:50[0-1]: /etc/passwd)" ] &&(sed -ri "s#^(${envApp}bch:x:[0-9]+):50[0-9]:#\1:501:#g" /etc/passwd )
fi '
#change layout if PE/EX because BCK
if [ "${envMy}" == "pe" ] ||[ "${envMy}" == "ex" ] ;then
FSLVSize="/${envApp},lv_${envApp},L${sizeDisk}G
/${envApp}/${envMy}_siel/dbbck,lv_${envApp}bck,l+100%FREE"
else
FSLVSize="/${envApp},lv_${envApp},l+100%FREE"
fi
# LVM init
ssh.sh "${hostName}" "pvcreate /dev/$devName
vgcreate vg_$envApp /dev/$devName"
echo "${FSLVSize}" |grep . \
|while IFS=',' read FSPath lvName lvSize ;do
ssh.sh "${hostName}" "lvcreate -${lvSize} -n ${lvName} vg_${envApp} ;mkfs.xfs -f /dev/mapper/vg_${envApp}-${lvName}"
ssh.sh "${hostName}" "mkdir -p ${FSPath}"' ;echo "'"/dev/mapper/vg_${envApp}-${lvName} ${FSPath} xfs nofail,defaults 0 1"'" >>/etc/fstab ;'"mount ${FSPath}"
done
#deappbck or reappbck share
if [ "${letterEnv}" == "d" ] ||[ "${letterEnv}" == "i" ];then
envAppBck=deappbck
elif [ "${letterEnv}" == "r" ] ;then
envAppBck=reappbck
fi
if [ "${letterEnv}" != "p" ] &&[ "${letterEnv}" != "e" ];then
envAppBckUpper=$(echo $envAppBck |tr '[:lower:]' '[:upper:]' )
cp.sh 494 "${hostName}" /etc/.credentials_$envAppBck
ssh.sh "${hostName}" "mkdir -p /${envApp}/${envMy}_siel/dbbck"' ;\
echo "'"//${envAppBckUpper}.infocepo.com/${envAppBckUpper}\$/${envApp}bck /${envApp}/${envMy}_siel/dbbck cifs uid=,gid=500,forceuid,forcegid,noserverino,nodfs,credentials=/etc/.credentials_${envAppBck},setuids,file_mode=0774,dir_mode=0774 0 1"'" >>/etc/fstab ;\
grep uid=, /etc/fstab >/dev/null &&(uidMy=$(grep ^.....app: /etc/passwd |cut -d: -f3 ) ;sed -i.old "s#uid=,#uid=${uidMy},#" /etc/fstab ;mount -a )'
fi
ssh.sh "${hostName}" "chown ${envApp}app: -R /${envApp}
chmod u=rwx,g=rwx,o=,g+s -R /${envApp}"
# Listener
#ssh.sh "${hostName}" "ln -s /usr/local/bin/scripts_appcle/start_stop_app.sh /etc/rc.d/init.d/ln-${envApp}app"
#ssh.sh "${hostName}" 'echo "'"/etc/rc.d/init.d/ln-${envApp}app start"'" >>/etc/rc.d/rc.local
#chmod +x /etc/rc.d/rc.local'
#ssh.sh "${hostName}" "releaseV=\$(cat /etc/redhat-release |sed -rn 's#^.*release ([0-9]+).*\$#\1#p' )
#activationKey=ak_rhel\${releaseV}_dev_kvm
#subscription-manager register --org='infocepo.com' --activationkey="\${activationKey}" --force"
# appInst.loc
ssh.sh "${hostName}" 'echo "inventory_loc=/'"${envApp}"'/appInventory
inst_group=admindata" >/etc/appInst.loc
chown root:admindata /etc/appInst.loc
chcon -h system_u:object_r:etc_t:s0 /etc/appInst.loc
chmod 774 /etc/appInst.loc'
# tmp password
hostList.sh "${hostName}" >/tmp/hostList$$
ansible-playbook -i /tmp/hostList$$ --extra-vars '{"HOSTS":"all","USER":"admin","HASH":"'$(cat admin_hash_tmp )'"}' /etc/ansible/playbooks/passwd_update.yml
# APP start fix
hostList=$hostName
#cp.sh 2228 "${hostList}" /usr/local/bin/scripts_app/start_stop_app.sh
#cp.sh 2228 "${hostList}" /usr/local/bin/scripts_app/app.sh
#cp.sh 2228 "${hostList}" /usr/local/bin/scripts_app/applsnr.sh
# APPTAB
apptabUser=$(ssh.sh svc${envApp} "grep -i ${envApp} /etc/apptab" |grep -v == )
ssh.sh "${hostName}" "echo '$apptabUser' >>/etc/apptab
chown ${envApp}app: /etc/apptab
chmod 660 /etc/apptab
chcon -h system_u:object_r:etc_t:s0 /etc/apptab"
ssh.sh "${hostList}" "sed -i -r '"'s#(^[A-Z]+:/([a-z]{2})([a-z]{3})/product/app/[0-9\.]+:Y)$#\1:\2\3app:\U\2_\3#'"' /etc/apptab"
# APP startup
#ssh.sh "${hostList}" 'sed -rn '"'"'s#^([a-z]{5}app):.*$#\1#p'"'"' /etc/passwd |while read lineMy ;do
# echo "== ${lineMy} =="
# [ ! -L "/etc/rc.d/init.d/ln-${lineMy}-db" ] &&ln -s /usr/local/bin/scripts_app/start_stop_app.sh "/etc/rc.d/init.d/ln-${lineMy}-db"
#done'
# add VLANNNN (PE/EX) for TSM
ipAddrBCK="$(sed -rn 's#^'"${hostName}"'.*[[:space:]]+([[:graph:]]+)$#\1#p' /exploitation/env/migrationAppIPNNN.list )"
if [ ! -z "${ipAddrBCK}" ] ;then
hwAddrBCK="52:54:00:$(echo "${hostName}" |cut -c8-9 ):$(echo "${hostName}" |cut -c10-11 ):03"
network_name_BCK=BCK
ssh.sh "${hostName}" 'cat <<EOT >/etc/sysconfig/network-scripts/ifcfg-eth2
BOOTPROTO="none"
IPADDR="'${ipAddrBCK}'"
NETMASK="255.255.254.0"
DEVICE=eth2
HWADDR="'${hwAddrBCK}'"
ONBOOT=yes
EOT
sync
shutdown -h now'
sleep 3
ssh.sh "${hostName}" 'hostName=$(hostname -s )
if [ -e /opt/tivoli/tsm/client/ba/bin/dsm.sys ] ;then
hostNameCheck="$(sed -rn "s#^[[:space:]]*(NODENAME[[:space:]]+${hostName})\$#\\1#p" /opt/tivoli/tsm/client/ba/bin/dsm.sys)"
if [ -z "${hostNameCheck}" ] ;then
echo NOK ;sed -ri "s#^([[:space:]]*NODENAME).*\$#\\1 ${hostName}#" /opt/tivoli/tsm/client/ba/bin/dsm.sys
fi
fi'
.stopVM.py "${hostName}" >/dev/null
# HWADDR
ansible-playbook --extra-vars '{ "vm_name":"'${hostName}'","nic_name":"nic3","mac_address":"'${hwAddrBCK}'","network_name":"'${network_name_BCK}'" }' "${baseDir}"/ansible/playbooks/ovirt_nic.yml >/dev/null
sleep 2
# start KVM
.startVM.py "${hostName}" >/dev/null
# waiting network...
echo "starting" |tr -d '\n' &&\
while [ $(ping "${hostName}" -c 1 2>/dev/null >/dev/null ;echo $?) -ne 0 ] ;do
sleep 1
echo -e . |tr -d '\n'
done &&echo
fi
# new start (TODO template)
#cp.sh XXXX "${hostName}" '/etc/rc.d/rc.local'
# selinux permissive (TODO template)
cp.sh YYYY "${hostName}" /etc/selinux/config
# check FS
ssh.sh "${hostName}" "df -h |grep ${envApp}"
#mail
bodyHtml="Voici la connexion pour l'application ${envApp} :<br>
ssh ${envApp}app@${hostName}"
headHtml='<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
</head>
<body>
<p>Bonjour,</p><p>'
tailHtml='</p>Cordialement,<br>
Équipe Unix
</body></html>'
( echo "From: admin@company.com";
echo "To: customer@company.com";
echo "To: user@company.com";
echo "Cc: admin@company.com";
echo "Subject: ${envApp}app@${hostName}";
echo "Content-Type: text/html";
echo "MIME-Version: 1.0";
echo "";
echo "${headHtml}";
echo "${bodyHtml}";
echo "${tailHtml}";
) |sendmail -t