Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [root@p3plsysops001 ~]# cat /usr/local/sbin/gdpatch.sh
- #!/bin/bash
- #Enable compatability mode for bash 3.2 regex matching
- shopt -s compat31
- #Include conf files with presidence on local directories conf file.
- . /opt/gdpatch/gdpatch.conf
- #Take input variables
- spacewalkgroup=$1
- patchtime=$2
- reboottime=$3
- if [ $# -lt 1 ]
- then
- echo "Usage: `basename $0` spacewalkgroup [patchtime] [reboottime] [emai llist]"
- echo "EX: myspacewalkgroup 20130131T22:00:00 20130131T23:55:00 '\"root\" <root@localhost>'"
- exit 1
- fi
- if [ -z $2 ]
- then
- echo "No patchtime provided, patching will be scheduled to happen now"
- patchtime="`date +%Y%m%dT%k:%M:%S`"
- fi
- if [ -z $3 ]
- then
- echo "No reboot provided, restarts will be scheduled to happen now"
- reboottime="`date +%Y%m%dT%k:%M:%S`"
- fi
- if [ ! -z "$4" ]
- then
- echo "using user provided email list:"
- echo "$4"
- EMAILLIST=$4
- else
- echo "No emaillist provided default email list in confs will be used"
- fi
- if [[ $patchtime =~ ^[0-9]\{8\}T[0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} ]]
- then
- echo ""
- else
- echo "patchtime format does not match YYYYMMDDTHH:MM:SS EX: `date +%Y%m% dT%k:%M:%S`"
- exit 1
- fi
- if [[ $reboottime =~ ^[0-9]\{8\}T[0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} ]]
- then
- echo ""
- else
- echo "reboottime format does not match YYYYMMDDTHH:MM:SS EX: `date +%Y%m %dT%k:%M:%S`"
- exit 1
- fi
- echo "Script running for $spacewalkgroup"
- encryptedpassword=`j-encrypt "$PASSWORD"`
- #setup functions used for snapshots, sending email, and patching.
- preppatchingemail (){
- spacewalkgroup=$1
- EMAILLIST=$2
- patchtime=$3
- #Make a new copy of the email template
- cp /opt/gdpatch/email-body-template.txt /opt/gdpatch/$$.modified-email-body.txt
- #Add date, time, and email list to the email file.
- sed -i "s/\--ENVIRONMENT--/$spacewalkgroup/" /opt/gdpatch/$$.modified-email-body .txt
- sed -i "s/To: .*/To: $EMAILLIST/" /opt/gdpatch/$$.modified-email-body.txt
- sed -i "s/\$date/$patchtime/" /opt/gdpatch/$$.modified-email-body.txt
- }
- removesnapshotsfor (){
- spacewalkgroup="$1"
- VSPHEREHOST="$2"
- username="$3"
- encryptedpassword="$4"
- echo "starting snapshot removal."
- processes=`pgrep "snapshotmanager" | wc -l`
- for host in `swutil systemgroup-list -g $spacewalkgroup --raw | awk -F '.' '{pri nt $1}'`
- do
- while [ $processes -gt 4 ]
- do
- echo "number of current jobs is $processes sleeping for 3 second s"
- sleep 3
- processes=`pgrep "snapshotmanager" | wc -l`
- done
- echo "Removing last patching snapshot for: $host"
- nice -n 15 snapshotmanager.pl --url "$VSPHEREHOST" --username "$username " --password "$encryptedpassword" --operation remove --vmname "$host" --snapshot name pre-"$spacewalkgroup"-patching-snapshot --children 1 > /dev/null 2>&1 &
- sleep 1
- processes=`pgrep "snapshotmanager" | wc -l`
- done
- }
- waitforsnapshotmanager (){
- processes=`pgrep "snapshotmanager" | wc -l`
- while [ $processes -gt 0 ]
- do
- echo "waiting for all snapshotmanager jobs to complete before continuing"
- sleep 5
- processes=`pgrep "snapshotmanager" | wc -l`
- done
- }
- createsnapshotsfor (){
- spacewalkgroup="$1"
- VSPHEREHOST="$2"
- username="$3"
- encryptedpassword="$4"
- processes=`pgrep "snapshotmanager" | wc -l`
- for host in `swutil systemgroup-list -g $spacewalkgroup --raw | awk -F '.' '{pri nt $1}'`
- do
- while [ $processes -gt 4 ]
- do
- echo "number of current jobs is $processes sleeping for 3 second s"
- sleep 3
- processes=`pgrep "snapshotmanager" | wc -l`
- done
- echo "Creating new patching snapshot for: $host"
- nice -n 15 snapshotmanager.pl --url "$VSPHEREHOST" --username "$username " --password "$encryptedpassword" --operation create --vmname "$host" --snapshot name pre-"$spacewalkgroup"-patching-snapshot > /dev/null 2>&1 &
- echo "$host <br>" >> /opt/gdpatch/$$.hosts.txt
- sleep 1
- processes=`pgrep "snapshotmanager" | wc -l`
- done
- }
- sendpatchingnoticeemail (){
- echo "<br></body></html>" >> /opt/gdpatch/$$.hosts.txt
- cat /opt/gdpatch/$$.modified-email-body.txt /opt/gdpatch/$$.hosts.txt /opt/gdpat ch/sysopsimage.txt > /opt/gdpatch/$$.email.txt
- /usr/sbin/sendmail -i -t < /opt/gdpatch/$$.email.txt
- echo "Email sent"
- rm -f /opt/gdpatch/$$.email.txt
- rm -f /opt/gdpatch/$$.modified-email-body.txt
- rm -f /opt/gdpatch/$$.hosts.txt
- }
- schedulepatching (){
- spacewalkgroup=$1
- patchtime=$2
- swutil patch-server -g "$spacewalkgroup" -D "$patchtime"
- }
- schedulereboots (){
- spacewalkgroup=$1
- reboottime=$2
- swutil reboot-server -g $spacewalkgroup -D "$reboottime"
- echo $reboottime
- }
- sendpatchingcompletionemail (){
- EMAILLIST=$1
- reboottime=$2
- spacewalkgroup=$3
- mydir=`pwd`
- # Use this magic to schedule with spacewalk.
- sed "s/To: .*/To: $EMAILLIST/" /opt/gdpatch/patching-completion-email-template.t xt | sed "s/\--ENVIRONMENT--/$spacewalkgroup/" > /opt/gdpatch/$$.patching-comple tion-email.txt
- emailcontents=`cat /opt/gdpatch/$$.patching-completion-email.txt`
- echo -e "#! /bin/bash \n /usr/sbin/sendmail -i -t << ENDOFMYFILE\n$emailcontents \nENDOFMYFILE" > /opt/gdpatch/$$.patching-completion-email.sh
- swutil run-script -s `hostname` -f /opt/gdpatch/$$.patching-completion-email.sh -D $reboottime
- rm -f /opt/gdpatch/$$.patching-completion-email.txt
- rm -f /opt/gdpatch/$$.patching-completion-email.sh
- }
- preppatchingemail "$spacewalkgroup" "$EMAILLIST" "$patchtime"
- removesnapshotsfor "$spacewalkgroup" "$VSPHEREHOST" "$USERNAME" "$encryptedpassw ord"
- waitforsnapshotmanager
- createsnapshotsfor "$spacewalkgroup" "$VSPHEREHOST" "$USERNAME" "$encryptedpassw ord"
- waitforsnapshotmanager
- sendpatchingnoticeemail
- echo "$spacewalkgroup"
- echo "$patchtime"
- schedulepatching "$spacewalkgroup" "$patchtime"
- schedulereboots "$spacewalkgroup" "$reboottime"
- sendpatchingcompletionemail "$EMAILLIST" "$reboottime" "$spacewalkgroup"
- [root@p3plsysops001 ~]#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement