]> git.evergreen-ils.org Git - contrib/pines.git/blob - update-libips.sh
add local git tar script
[contrib/pines.git] / update-libips.sh
1 #!/bin/bash
2
3 # Copyright (C) 2012 Georgia Public Library Service
4 # Chris Sharp <csharp@georgialibraries.org>
5 #    
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License as published by
8 #    the Free Software Foundation, either version 3 of the License, or
9 #    (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15 #
16 #    You should have received a copy of the GNU General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 OSRF_CONF_DIR="/openils/conf"
21 LIB_IPS_TXT="$OSRF_CONF_DIR/lib_ips.txt"
22 BRICK_HEADS="6" # number of brick heads you have - script assumes you're running on brick01-head
23 EDITOR="/usr/bin/editor"
24
25 [[ $(whoami) != "opensrf" ]] && echo "Must be opensrf user to run this utility." && exit
26
27 QueryFile () {
28 echo "Would you like to query by library name or by IP address?"
29 echo
30 echo "[1] Library name"
31 echo "[2] IP Address"
32 echo
33 read -p "Please choose option 1 or 2: " CHOICE
34 if [ "$CHOICE" = "1" ]; then
35         read -p "What is the partial or full library shortname (e.g., ECGR-CCO, WGRL) for which you'd like to see the IP range? " SHORTNAME
36         grep -i "$SHORTNAME" $LIB_IPS_TXT | sort;
37 elif [ "$CHOICE" = "2" ]; then
38         read -p "What is the partial or full IP address (e.g. 172.16.10.1, 10.1.1) for which you'd like to see the library name? " IP_ADDR
39         grep $IP_ADDR $LIB_IPS_TXT | sort;
40 else
41         echo "No valid response received.  Aborting."
42         exit;
43 fi
44 }
45
46 UpdateFile () {
47 echo
48 echo "This function currently only adds new libraries and ranges."
49 echo
50 read -p "Please enter the library shortname (e.g., WGRL-HQ): " NEW_SHORTNAME
51 read -p "Please enter the start IP of the range (if a single IP, just enter that): " START_IP
52 read -p "Please enter the end IP of the range (if a single IP, just enter that again): " END_IP
53 echo
54 echo "The new line will be:"
55 echo -e "$NEW_SHORTNAME\t$START_IP\t$END_IP"
56 echo
57 read -p "Is this correct? (y/n) " ANSWER
58 if [ "$ANSWER" = "y" ]; then
59         echo "Backing up the original file..."
60         cp $LIB_IPS_TXT $LIB_IPS_TXT.`date +%Y%m%d%H%M%S`
61         echo "Adding the new entry to the file..."
62         echo "$NEW_SHORTNAME $START_IP $END_IP" >> "$LIB_IPS_TXT"
63         echo "Re-sorting file with new entry in place..."
64         sort $LIB_IPS_TXT -o $LIB_IPS_TXT
65         echo "Done!"
66 else
67         echo "Aborting."
68         exit;
69 fi
70 }
71
72 CopyFile () {
73 # works best if the opensrf user is keyed for opensrf on the other brick heads
74 echo
75 echo "Copying file to the other brick heads..."
76 for i in `seq 2 $BRICK_HEADS`; do 
77         ssh opensrf@brick0$i-head cp $LIB_IPS_TXT $LIB_IPS_TXT.`date +%Y%m%d%H%M%S`; 
78         scp $LIB_IPS_TXT opensrf@brick0$i-head:$LIB_IPS_TXT; 
79         echo "brick0$i-head complete"; 
80 done;
81 echo "You will need to restart Apache on all brick heads for the changes to take effect."
82 }
83
84 EditCopy () {
85 cp $LIB_IPS_TXT $LIB_IPS_TXT.`date +%Y%m%d%H%M%S`
86 $EDITOR $LIB_IPS_TXT
87 read -p "Would you like to copy the file to the other brick heads? (y/n) " COPYFILE
88 if [ "$COPYFILE" = "y" ]; then
89         CopyFile
90 elif [ "$COPYFILE" = "n" ]; then
91         echo "Not copying file."
92         exit;
93 else 
94         echo "No valid response received.  Not copying file."
95 fi
96 }
97
98
99 echo "This program allows you to query and/or update the Evergreen Library IP redirection file on each of the application bricks."
100 echo
101 echo "What would you like to do?"
102 echo
103 echo "[1] Query the lib_ips.txt file by library name or IP address"
104 echo "[2] Update the file with a new entry"
105 echo "[3] Manually edit, then optionally copy the file to the other brick heads"
106 echo
107 read -p "Please choose option 1, 2 or 3: " RESPONSE
108 if [ "$RESPONSE" = "1" ]; then
109         QueryFile
110 elif [ "$RESPONSE" = "2" ]; then
111         UpdateFile && echo "The file is updated" && CopyFile
112 elif [ "$RESPONSE" = "3" ]; then
113         EditCopy
114 else
115         echo "No valid response received.  Aborting."
116         exit;
117 fi