-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.sh
121 lines (104 loc) · 3.56 KB
/
functions.sh
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
117
118
119
120
121
#!/bin/bash
### ====== ====== ====== Utility funcitons ====== ====== ======
# A utility function returning the root/mount directory
# of the mounted disk with the biggest free space.
function getMaxDiskSpaceDir()
{
# Get the metadata of all disks. For each disk returns
# (i) the number of available blocks and
# (ii) the disk's mount point
diskDefs=(`df -P | awk '{ print $4 " " $6 }'`)
# Number of elements in the def list
diskDefsLen=${#diskDefs[*]}
# Loop to find the index of the disk with the most Free space.
bestIdx=1
bestNumBlocks=0
for i in $(seq 2 2 $((diskDefsLen - 1)) )
do
availableBlocks=${diskDefs[$i]}
if [ $availableBlocks -gt $bestNumBlocks ]
then
bestNumBlocks=$availableBlocks
bestIdx=$i
fi
done
echo ${diskDefs[$((bestIdx+1))]}
}
# Escapes special characters (brackets, slashes, dollars etc.) from a string
function escapeString()
{
arg=$1
regex="\\\?\\\$|\[|\]|\(|\)|\/|\{|\}"
echo "$arg" | sed -r 's/('$regex')/\\\1/g'
}
# Replaces the value of a property in a file
# Arguements - prop name, prop value, file, assignment symbol ("=" if not specified)
function setProperty()
{
property=`escapeString $1`
val=`escapeString $2`
file=$3
# If the assignment symbol is not specified use "="
assignSymbol=${4-=}
sudo sed -i -e "s/^\(\s*\)$property\s*$assignSymbol.*/\1$property$assignSymbol$val/" $file
}
# Logs an installation header message, that delimits the installation stages
function logHeader()
{
arg=$1
logPrefix="\n\n == == == == == == == == == == == == == == == == == == == == == == == == \n -- -- -- "
logSuffix=" -- -- -- \n\n "
printf "$logPrefix$arg$logSuffix"
}
## Print installation details - direcotories, variables etc.
function logInstallDetails
{
logHeader "\n\n\n ======== ======== ======== ======== INSTALLATION SUMMARY ======== ======== ======== ======== \n\n"
printf "%-15s %s\n" "\$JAVA_HOME:" $JAVA_HOME
printf "%-15s %s\n" "\$JDK_HOME:" $JDK_HOME
printf "%-15s %s\n" "\$OLIO_HOME:" $OLIO_HOME
printf "%-15s %s\n" "\$FABAN_HOME:" $FABAN_HOME
printf "%-15s %s\n" "\$APP_DIR:" $APP_DIR
printf "%-15s %s\n" "\$FILESTORE:" $FILESTORE
printf "%-15s %s\n" "\$NFS_MOUNT_PATH:" $NFS_MOUNT_PATH
printf "%-15s %s\n" "\$CATALINA_HOME:" $CATALINA_HOME
printf "%-15s %s\n" "\$GEOCODER_HOME:" $GEOCODER_HOME
}
## Exports an environment variable and its value
function exportVar()
{
var=$1
val=$2
sudo bash -c "echo \"$var=$val\" >> /etc/environment"
export $var=$val
}
## Dynamically updates the load balancer's config file with the servers and their weigths.
## Arguements - pairs of DNS/IP addresses and weigths, file
function setServerFarmConfig()
{
tmpFile="$HOME/temp-setServerFarm-file.txt"
numParams=$#
params=( "$@" )
echo "" > "$tmpFile"
echo "upstream backend {" >> "$tmpFile"
for i in $(seq 0 2 $((numParams - 2)) )
do
addess=${params[$i]}
weigth=${params[$((i+1))]}
echo " server $addess weight=$weigth;" >> $tmpFile
done
echo "}" >> $tmpFile
file=${params[$((numParams-1))]}
sudo bash -c "cat $tmpFile >> $file"
rm $tmpFile
}
## Resets the load balancer to work with the specified servers and their weigths.
## Arguements - pairs of DNS/IP addresses and weigths
function resetLoadBalancer()
{
params=( "$@" )
cd /etc/nginx/sites-available/
sudo cp -f ./default-backup ./default
setServerFarmConfig ${params[*]} "./default"
cd - 1> /dev/null
}