Project

General

Profile

1
#!/bin/bash
2
#This script will install openjdk-7 and tomcat7.
3
#It will update the alternatives for java, javac, keytool and javaws to openjdk-7.
4
#It will modify the /etc/tomcat7/catalina.properties to allow DataONE idenifiers.
5
#It will modify the workers.properties file for apache-tomcat connector.
6
#It will move Metacat and other web applications from the old context directory to the new context directory.
7
#The user running the script should have the sudo permission.
8

    
9
APACHE_ENABLED_SITES_DIR=/etc/apache2/sites-enabled
10
APACHE_AVAILABLE_SITES_DIR=/etc/apache2/sites-available
11
NEW_JDK_PACKAGE=openjdk-7-jdk
12
NEW_JDK_HOME=/usr/lib/jvm/java-7-openjdk-amd64
13

    
14
JK_CONF=/etc/apache2/mods-enabled/jk.conf
15

    
16
OLD_TOMCAT=tomcat6
17
OLD_TOMCAT_BASE=/var/lib/${OLD_TOMCAT}
18

    
19
NEW_TOMCAT=tomcat7
20
NEW_TOMCAT_COMMON=${NEW_TOMCAT}-common
21
NEW_TOMCAT_LIB=lib${NEW_TOMCAT}-java
22
NEW_CATALINA_PROPERTIES=/etc/${NEW_TOMCAT}/catalina.properties
23
NEW_TOMCAT_HOME=/usr/share/${NEW_TOMCAT}
24
NEW_TOMCAT_BASE=/var/lib/${NEW_TOMCAT}
25
NEW_TOMCAT_SERVER_CONIF=$NEW_TOMCAT_BASE/conf/server.xml
26
NEW_TOMCAT_CONTEXT_CONF=$NEW_TOMCAT_BASE/conf/context.xml
27

    
28
KNB=knb
29
SSL=ssl
30
METACAT=metacat
31
WEBAPPS=webapps
32
METACAT_DATA_DIR=/var/metacat
33
TOMCAT_CONFIG_SLASH='org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true'
34
TOMCAT_CONFIG_BACKSLASH='org.apache.catalina.connector.CoyoteAdapter.ALLOW_BACKSLASH=true'
35
INIT_START_DIR=/etc/init.d
36

    
37

    
38
if [ $# -ne 1 ]; then
39
   echo "This script should take one and only one parameter as the name of the host.";
40
   exit 1;
41
fi
42
HOST_NAME=$1
43
echo "Host name is $HOST_NAME"
44

    
45
echo "instal xmlstarlet"
46
sudo apt-get install xmlstarlet
47

    
48
sudo /etc/init.d/apache2 stop
49
echo "install ${NEW_JDK_PACKAGE}"
50
sudo apt-get install ${NEW_JDK_PACKAGE}
51
sleep 3
52
echo "configure java, java, keytool and javaws"
53
sudo update-alternatives --set java ${NEW_JDK_HOME}/jre/bin/java
54
sudo update-alternatives --set javac ${NEW_JDK_HOME}/bin/javac
55
sudo update-alternatives --set keytool ${NEW_JDK_HOME}/jre/bin/keytool
56
sudo update-alternatives --set javaws ${NEW_JDK_HOME}/jre/bin/javaws
57

    
58
echo "install ${NEW_TOMCAT}"
59
sudo ${INIT_START_DIR}/${OLD_TOMCAT} stop
60
sudo apt-get install ${NEW_TOMCAT_LIB}
61
sudo apt-get install ${NEW_TOMCAT_COMMON}
62
sudo apt-get install ${NEW_TOMCAT}
63
echo "configure ${NEW_TOMCAT}"
64
if grep -q "${TOMCAT_CONFIG_SLASH}" ${NEW_CATALINA_PROPERTIES}; then  
65
echo "${TOMCAT_CONFIG_SLASH} exists and don't need to do anything."  
66
else  
67
   echo "${TOMCAT_CONFIG_SLASH} don't exist and add it."
68
   sudo sed -i.bak "$ a\\${TOMCAT_CONFIG_SLASH}" ${NEW_CATALINA_PROPERTIES} 
69
fi
70
if grep -q "${TOMCAT_CONFIG_BACKSLASH}" ${NEW_CATALINA_PROPERTIES}; then
71
echo "${TOMCAT_CONFIG_BACKSLASH} exists and don't need to do anything."  
72
else
73
   echo "${TOMCAT_CONFIG_BACKSLASH} don't exist and add it."
74
   sudo sed -i "$ a\\${TOMCAT_CONFIG_BACKSLASH}" ${NEW_CATALINA_PROPERTIES}
75
fi
76

    
77
echo "add an attribute useHttpOnly='false' to the element Context if it doesn't have one in the $NEW_TOMCAT_CONTEXT_CONF"
78
sudo cp $NEW_TOMCAT_CONTEXT_CONF $NEW_TOMCAT_CONTEXT_CONF.bak
79
useHttpOnly=$(sudo xmlstarlet sel -t --value-of "/Context/@useHttpOnly" $NEW_TOMCAT_CONTEXT_CONF)
80
echo "the uerHttpOnly is $useHttpOnly"
81
if [[ -n $useHttpOnly ]]; then
82
	if [[ $useHttpOnly == 'false' ]]; then
83
		echo "Attribute useHttpOnly was set to false and we don't need to do anything"
84
        else
85
		echo "Update the attribute useHttpOnly's value to false"
86
		sudo xmlstarlet ed -L -P -u "/Context/@useHttpOnly" -v false $NEW_TOMCAT_CONTEXT_CONF
87
	fi
88
else 
89
	echo "Attribute useHttpOnly hasn't been set and we will add one"
90
	sudo xmlstarlet ed -L -P -s "/Context" --type attr -n useHttpOnly -v false $NEW_TOMCAT_CONTEXT_CONF
91
fi
92

    
93
echo "remove the 8080 ports and add the 8009 ports to the tomcat7 server.xml"
94
sudo cp $NEW_TOMCAT_SERVER_CONIF $NEW_TOMCAT_SERVER_CONIF.bak
95
sudo xmlstarlet ed -L -P -d "//Connector[@port='8080']" $NEW_TOMCAT_SERVER_CONIF
96
#echo "the configuration file is $NEW_TOMCAT_SERVER_CONIF"
97
result=$(sudo xmlstarlet sel -t --value-of "/Server/Service[@name='Catalina']/Connector[@protocol='AJP/1.3']/@port" $NEW_TOMCAT_SERVER_CONIF)
98
#echo "the result is $result"
99
if [[ -n $result ]]; then
100
  echo "An ajp 1.3 connector exists and we don't need to do anything."
101
else
102
  echo "No aip 1.3 connector found and we should add one"
103
  sudo xmlstarlet ed -L -P -s "/Server/Service[@name='Catalina']" -t elem -name Connector -v "" $NEW_TOMCAT_SERVER_CONIF
104
  sudo xmlstarlet ed -L -P -s "/Server/Service/Connector[not(@port)]" --type attr -n port -v 8009 $NEW_TOMCAT_SERVER_CONIF
105
  sudo xmlstarlet ed -L -P -s "/Server/Service/Connector[not(@protocol)]" --type attr -n protocol -v AJP/1.3 $NEW_TOMCAT_SERVER_CONIF
106
  sudo xmlstarlet ed -L -P -s "/Server/Service/Connector[not(@redirectPort)]" --type attr -n redirectPort -v 8443 $NEW_TOMCAT_SERVER_CONIF
107
fi
108

    
109

    
110
echo "move Metacat and other web applications from $OLD_TOMCAT to $NEW_TOMCAT"
111
sudo ${INIT_START_DIR}/${NEW_TOMCAT} stop
112
sudo rm -rf ${NEW_TOMCAT_BASE}/${WEBAPPS}/*
113
sudo cp -R ${OLD_TOMCAT_BASE}/${WEBAPPS}/*  ${NEW_TOMCAT_BASE}/${WEBAPPS}/.
114
sudo chown -R ${NEW_TOMCAT}:${NEW_TOMCAT} ${NEW_TOMCAT_BASE}/${WEBAPPS}/*
115
echo "change the value of the application.deployDir in the metacat.properties file"
116
SAFE_NEW_TOMCAT_WEBAPPS=$(printf '%s\n' "$NEW_TOMCAT_BASE/$WEBAPPS" | sed 's/[[\.*^$(){}?+|/]/\\&/g')
117
#echo "the escaped webpass value is ${SAFE_NEW_TOMCAT_WEBAPPS}"
118
if [ -f "$NEW_TOMCAT_BASE/$WEBAPPS/$KNB/WEB-INF/metacat.properties" ]; then
119
	echo "$NEW_TOMCAT_BASE/$WEBAPPS/$KNB/WEB-INF/metacat.properties exists and the application.deployDir will be updated"	
120
	sudo sed -i.bak --regexp-extended "s/(application\.deployDir=).*/\1${SAFE_NEW_TOMCAT_WEBAPPS}/;" $NEW_TOMCAT_BASE/$WEBAPPS/$KNB/WEB-INF/metacat.properties
121
        sudo sed -i --regexp-extended "s/(geoserver\.GEOSERVER_DATA_DIR=).*/\1${SAFE_NEW_TOMCAT_WEBAPPS}\/${KNB}\/spatial\/geoserver\/data/;" $NEW_TOMCAT_BASE/$WEBAPPS/$KNB/WEB-INF/metacat.properties
122
else
123
	echo "$NEW_TOMCAT_BASE/$WEBAPPS/$KNB/WEB-INF/metacat.properties does NOT exists and the application.deployDir will NOT be updated"
124
fi
125

    
126
if [ -f "$NEW_TOMCAT_BASE/$WEBAPPS/$METACAT/WEB-INF/metacat.properties" ]; then
127
                echo "$NEW_TOMCAT_BASE/$WEBAPPS/$METACAT/WEB-INF/metacat.properties eixsts and the application.deployDir will be updated" 
128
                sudo sed -i.bak --regexp-extended "s/(application\.deployDir=).*/\1${SAFE_NEW_TOMCAT_WEBAPPS}/;" $NEW_TOMCAT_BASE/$WEBAPPS/$METACAT/WEB-INF/metacat.properties
129
		sudo sed -i --regexp-extended "s/(geoserver\.GEOSERVER_DATA_DIR=).*/\1${SAFE_NEW_TOMCAT_WEBAPPS}\/${METACAT}\/spatial\/geoserver\/data/;" $NEW_TOMCAT_BASE/$WEBAPPS/$METACAT/WEB-INF/metacat.properties
130
else 
131
  echo "$NEW_TOMCAT_BASE/$WEBAPPS/$METACAT/WEB-INF/metacat.properties doesn't eixt and the application.deployDir will NOT be updated"
132
fi
133

    
134
echo "change the ownership of $METACAT_DATA_DIR to $NEW_TOMCAT"
135
sudo chown -R ${NEW_TOMCAT}:${NEW_TOMCAT} ${METACAT_DATA_DIR}
136

    
137

    
138
echo "Change somethings on apache configuration"
139
echo "read the location of the workers.properties file from the jk_conf"
140
while read f1 f2 
141
do
142
        if [ "$f1" = "JkWorkersFile" ]; then
143
        JK_WORKER_PATH="$f2"    
144
    fi
145
done < ${JK_CONF}
146
echo "the jk workers.properties location is $JK_WORKER_PATH"
147

    
148
echo "update the tomcat home and java home in workers.properties file"
149
SAFE_NEW_TOMCAT_HOME=$(printf '%s\n' "$NEW_TOMCAT_HOME" | sed 's/[[\.*^$(){}?+|/]/\\&/g')
150
SAFE_NEW_JDK_HOME=$(printf '%s\n' "$NEW_JDK_HOME" | sed 's/[[\.*^$(){}?+|/]/\\&/g')
151
sudo sed -i.bak --regexp-extended "s/(workers\.tomcat_home=).*/\1${SAFE_NEW_TOMCAT_HOME}/;
152
                s/(workers\.java_home=).*/\1${SAFE_NEW_JDK_HOME}/;"\
153
                $JK_WORKER_PATH
154

    
155
echo "we need to do some work since the new version of apache only load the site files with .conf extension in the sites-enabled directory"
156
echo "delete all links which doesn't end with .conf in the site-enabled directory since they can't be loaded"
157
sudo find $APACHE_ENABLED_SITES_DIR -type l ! -name "*.conf" -delete
158

    
159
echo "add .conf to the files which don't end with .conf or .bak or .org"
160
for i in $(sudo find $APACHE_AVAILABLE_SITES_DIR -type f \( ! -name "*.conf" -a ! -name "*.bak" -a ! -name "*.org" \)); 
161
do
162
    sudo mv "$i" "${i}".conf 
163
done
164

    
165
echo "update the apache site files by replacing $OLD_TOMCAT by $NEW_TOMCAT"
166
for j in $(sudo find $APACHE_AVAILABLE_SITES_DIR -type f -name "*.conf")
167
do
168
    sudo sed -i.bak "s/${OLD_TOMCAT}/${NEW_TOMCAT}/;" $j
169
done
170

    
171
echo "rename the site file knb to $HOST_NAME and knb-ssl to $HOST_NAME-ssl"
172
sudo mv $APACHE_AVAILABLE_SITES_DIR/$KNB.conf $APACHE_AVAILABLE_SITES_DIR/$HOST_NAME.conf
173
sudo mv $APACHE_AVAILABLE_SITES_DIR/$KNB-ssl.conf $APACHE_AVAILABLE_SITES_DIR/$HOST_NAME-ssl.conf
174

    
175
echo "current redirect rules doesn't work. we need to change it"
176
sudo sed -i "s|\("RewriteCond" * *\).*|\1%{HTTPS} off|" $APACHE_AVAILABLE_SITES_DIR/$HOST_NAME.conf
177
sudo sed -i "s|\("RewriteRule" * *\).*|\1(.*) https://%{HTTP_HOST}%{REQUEST_URI}|" $APACHE_AVAILABLE_SITES_DIR/$HOST_NAME.conf
178

    
179
echo "enable the two sites $HOST_NAME and $HOST_NAME-ssl"
180
sudo a2ensite $HOST_NAME
181
sudo a2ensite $HOST_NAME-ssl
182

    
183
sudo /etc/init.d/apache2 start
184
sudo /etc/init.d/tomcat7 start
185

    
186
exit 0
(6-6/12)