install-osrf.sh: set ejabberd host correctly during registration on prod
[sitka/sitka-tools.git] / patching / patch-eg.sh
CommitLineData
8b032c9e
SC
1#!/bin/bash
2# Author: schan@sitka.bclibraries.ca
3
4Help() {
5 cat<<____
6
7DESCRIPTION:
8Apply or unapply a patch to remote Evergreen hosts
9
10USAGE:
11./patch-eg.sh -p ID [ -b backup_label ] [ -P rule ] [ -y ] [ -R ] [ -h ] host1 host2...
12
13OPTIONS:
14 -p SHA1 ID of patch to apply (mandatory)
15 -b Backup label of patch
16 Defaults to patch ID
dcae1c18 17 -P Patch rule (web, tt2, xul, bin, perl, util) to determine patch directory, user, and strip level
8b032c9e
SC
18 Defaults to 'web'
19 -y Yes, apply the patch instead of doing a dry run
20 -R Reverse or undo the patch instead of applying it
21 -h Print this message and exit
22____
23}
24
25# Parse command line for run-time options
26while getopts 'p:b:P:yRh' Option; do
27 case $Option in
28 (p) Patch_ID=$OPTARG;;
29 (b) Patch_Backup=$OPTARG;;
30 (P) Patch_Rule=$OPTARG;;
31 (y) Runit='';;
32 (R) Reverse='-R';;
33 (h) Help && exit 0;;
34 esac
35done
36shift $(( $OPTIND - 1 ))
37
38# Calculate default options
39Remote_Host=${1:?}
40Patch_ID=${Patch_ID:?}
41Patch_Backup=${Patch_Backup:-$Patch_ID}
42Patch_Rule=${Patch_Rule:-web}
43Runit=${Runit-'--dry-run'}
44Reverse=${Reverse-''}
45
46# Define function to echo the intended action for a host
47Echo_Intention() {
48 [[ $Runit == '--dry-run' ]] && echo -ne '\nDry run: '
49 if [[ $Reverse == '-R' ]]; then echo -n 'Reversing '; else echo -n 'Applying '; fi
50 echo -e "patch $Patch_ID to $1 and backing up as $Patch_Backup"
51}
52
53# Associate the patch rule to patch path, strip level, and sudo user
54# If patch rule is 'perl', the patch path will be dynamically determined from the host
55declare -A Rule
56case $Patch_Rule in
dcae1c18
SC
57 (web) Rule=( [path]=/srv/openils/var [level]=2 [sudo]=opensrf );;
58 (tt2) Rule=( [path]=/srv/openils/var [level]=3 [sudo]=opensrf );;
59 (xul) Rule=( [path]=/srv/openils/var/web/xul [level]=4 [sudo]=opensrf );;
60 (bin) Rule=( [path]=/srv/openils/bin [level]=4 [sudo]=opensrf );;
61 (perl) Rule=( [path]=/usr/local/share/perl/5.14.2 [level]=5 [sudo]=root );;
62 (util) Rule=( [path]=/srv/openils/var/web/xul/server [level]=6 [sudo]=opensrf );;
8b032c9e
SC
63esac
64
65
66# Set local environment
67
68# Domain name of patch host (Evergreen)
69#declare -r SOURCE="git.evergreen-ils.org/?p=Evergreen.git"
70# Domain name of patch host (Sitka)
71declare -r SOURCE="git.sitka.bclibraries.ca/gitweb/?p=sitka/evergreen.git"
72# Domain name of target hosts
73declare -r TARGET=sitka.bclibraries.ca
74# Name of ssh and sudo user
75declare -r LOGIN=sitkastaff
76
77
78# Calculate command text strings
79declare -r SUDO="sudo -p '' -S -u ${Rule[sudo]}"
80declare -r Patch_URL="http://$SOURCE;a=patch;h=$Patch_ID"
81declare -r WGET="wget -q -O - '$Patch_URL'"
82declare -r Backup_Policy="-b -B .patch/$Patch_Backup/ -V simple"
83declare -r PATCH="patch $Runit $Reverse -r - $Backup_Policy -p${Rule[level]}"
84declare -r OPENILS_DIR="perl -MOpenILS -e 'print \$INC{qq(OpenILS.pm)}'"
85
86# Define function to find directory name of the OpenILS Perl module at a host
87Perl_Dir() { dirname $(ssh "$LOGIN@$1.$TARGET" $OPENILS_DIR); }
88
89# Define function to patch a directory at a host via ssh sudo
90Patch_Dir() { ssh "$LOGIN@$1.$TARGET" "$SUDO bash -c \"$WGET | $PATCH -d $2 \""; }
91
92# Ask the user once for sudo password
93read -s -p "$LOGIN's sudo password: " Password ; echo
94
95for Host; do
96 Echo_Intention $Host
97 [[ $Patch_Rule == 'perl' ]] && Rule[path]=$(Perl_Dir $Host)
98 Patch_Dir $Host ${Rule[path]} <<< $Password
99done