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
sudo /etc/init.d/apache2 stop
46
echo "install ${NEW_JDK_PACKAGE}"
47
sudo apt-get install ${NEW_JDK_PACKAGE}
48
sleep 3
49
echo "configure java, java, keytool and javaws"
50
sudo update-alternatives --set java ${NEW_JDK_HOME}/jre/bin/java
51
sudo update-alternatives --set javac ${NEW_JDK_HOME}/bin/javac
52
sudo update-alternatives --set keytool ${NEW_JDK_HOME}/jre/bin/keytool
53
sudo update-alternatives --set javaws ${NEW_JDK_HOME}/jre/bin/javaws
54

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

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

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

    
106

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

    
123
if [ -f "$NEW_TOMCAT_BASE/$WEBAPPS/$METACAT/WEB-INF/metacat.properties" ]; then
124
                echo "$NEW_TOMCAT_BASE/$WEBAPPS/$METACAT/WEB-INF/metacat.properties eixsts and the application.deployDir will be updated" 
125
                sudo sed -i.bak --regexp-extended "s/(application\.deployDir=).*/\1${SAFE_NEW_TOMCAT_WEBAPPS}/;" $NEW_TOMCAT_BASE/$WEBAPPS/$METACAT/WEB-INF/metacat.properties
126
		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
127
else 
128
  echo "$NEW_TOMCAT_BASE/$WEBAPPS/$METACAT/WEB-INF/metacat.properties doesn't eixt and the application.deployDir will NOT be updated"
129
fi
130

    
131
echo "change the ownership of $METACAT_DATA_DIR to $NEW_TOMCAT"
132
sudo chown -R ${NEW_TOMCAT}:${NEW_TOMCAT} ${METACAT_DATA_DIR}
133

    
134

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

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

    
152
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"
153
echo "delete all links which doesn't end with .conf in the site-enabled directory since they can't be loaded"
154
sudo find $APACHE_ENABLED_SITES_DIR -type f ! -name "*.conf" -delete
155

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

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

    
168
echo "rename the site file knb to $HOST_NAME and knb-ssl to $HOST_NAME-ssl"
169
sudo mv $APACHE_AVAILABLE_SITES_DIR/$KNB.conf $APACHE_AVAILABLE_SITES_DIR/$HOST_NAME.conf
170
sudo mv $APACHE_AVAILABLE_SITES_DIR/$KNB-ssl.conf $APACHE_AVAILABLE_SITES_DIR/$HOST_NAME-ssl.conf
171

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

    
176
echo "enable the two sites $HOST_NAME and $HOST_NAME-ssl"
177
sudo a2ensite $HOST_NAME
178
sudo a2ensite $HOST_NAME-ssl
179

    
180
sudo /etc/init.d/apache2 start
181
sudo /etc/init.d/tomcat7 start
182

    
183
exit 0
(5-5/10)