-
Notifications
You must be signed in to change notification settings - Fork 0
/
koha-vh-ports
executable file
·116 lines (104 loc) · 3.55 KB
/
koha-vh-ports
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash
# ------------------------------------------------------------------
# Author: Lennon Mazonde
# GitHub: @grandmaestr
# Title:koha-vhost-ports
# Description:
# This script updates the VirtualHost config to listen to on the specified port.
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------
# Script version
VERSION=0.1.0
# Set the name of the script to variable
SCRIPT_NAME="$(basename ${0})"
SUBJECT=koha-vhosts-ports
# ------Help--------------------------------------------------------
Help(){
# Display Help
cat <<EOF
Usage: $SCRIPT_NAME -ihv args
Options:
i - instanceid. The name of the koha instance. One instance id per option.
h - help. Print this help.
v - version. Print the script version.
Example:
To run the script on a single instance called "library", run:
$SCRIPT_NAME -i library
For multiple instances, run
$SCRIPT_NAME -i library1 -i library2 ...
EOF
}
# --- Options processing -------------------------------------------
if [ $# == 0 ] ; then
Help
exit 1;
fi
while getopts ":i:vh" optname; do
case "$optname" in
v)
echo "Version $VERSION"
exit 0;
;;
i)
instanceid+=("$OPTARG")
;;
h)
Help
exit 0;
;;
\?)
echo "Unknown option $OPTARG"
Help
exit 0;
;;
:)
echo "Error: you must provide at least one instance name for option -$OPTARG"
exit 0;
;;
*)
echo "Unknown error while processing options"
Help
exit 0;
;;
esac
done
shift $(($OPTIND - 1))
param1=$1
param2=$2
# --- Backup Original Config -------------------------------------
Backup(){
# Backup
sudo rsync -a /etc/apache2/sites-enabled/$val.conf /etc/apache2/sites-enabled/$val.conf.bkp.$(date +"%d_%m_%Y_%T")
}
# --- Modify vhosts Config -------------------------------------
Replace(){
# Set the first port
# Replace the vhost with the new port in the range
sudo sed -i "/.*VirtualHost .*/c\\<VirtualHost \*:$port\>" /etc/apache2/sites-enabled/$val.conf ;
# Insert port in ports.conf
sudo sed -i "0,/^Listen .*/s//Listen $port\n&/" /etc/apache2/ports.conf
# Augment by 1
((port=port+1))
sudo sed -i "0,/.*VirtualHost .*/s//\<VirtualHost \*:$port\>/" /etc/apache2/sites-enabled/$val.conf ;
# Insert port in ports.conf
sudo sed -i "0,/^Listen .*/s//Listen $port\n&/" /etc/apache2/ports.conf
# Augment by 1
((port=port+1))
}
# --- Locks -------------------------------------------------------
LOCK_FILE=/tmp/$SUBJECT.lock
if [ -f "$LOCK_FILE" ]; then
echo "Script is already running"
exit
fi
trap "rm -f $LOCK_FILE" EXIT
touch $LOCK_FILE
# --- Body --------------------------------------------------------
set -x
# Set the inital port numberdd
read -p "Enter the port number to be used in the sequence (e.g. 8100): " initial_portnumber
port=$initial_portnumber
for val in "${instanceid[@]}"; do
Backup
Replace
done