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

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

    
36

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

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

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

    
72
echo "remove the 8080 ports and add the 8009 ports to the tomcat7 server.xml"
73
sudo cp $NEW_TOMCAT_SERVER_CONIF $NEW_TOMCAT_SERVER_CONIF.bak
74
sudo xmlstarlet ed -L -P -d "//Connector[@port='8080']" $NEW_TOMCAT_SERVER_CONIF
75
#echo "the configuration file is $NEW_TOMCAT_SERVER_CONIF"
76
result=$(sudo xmlstarlet sel -t --value-of "/Server/Service[@name='Catalina']/Connector[@protocol='AJP/1.3']/@port" $NEW_TOMCAT_SERVER_CONIF)
77
#echo "the result is $result"
78
if [[ -n $result ]]; then
79
  echo "An ajp 1.3 connector exists and we don't need to do anything."
80
else
81
  echo "No aip 1.3 connector found and we should add one"
82
  sudo xmlstarlet ed -L -P -s "/Server/Service[@name='Catalina']" -t elem -name Connector -v "" $NEW_TOMCAT_SERVER_CONIF
83
  sudo xmlstarlet ed -L -P -s "/Server/Service/Connector[not(@port)]" --type attr -n port -v 8009 $NEW_TOMCAT_SERVER_CONIF
84
  sudo xmlstarlet ed -L -P -s "/Server/Service/Connector[not(@protocol)]" --type attr -n protocol -v AJP/1.3 $NEW_TOMCAT_SERVER_CONIF
85
  sudo xmlstarlet ed -L -P -s "/Server/Service/Connector[not(@redirectPort)]" --type attr -n redirectPort -v 8443 $NEW_TOMCAT_SERVER_CONIF
86
fi
87

    
88

    
89
echo "move Metacat and other web applications from $OLD_TOMCAT to $NEW_TOMCAT"
90
sudo ${INIT_START_DIR}/${OLD_TOMCAT} stop
91
sudo ${INIT_START_DIR}/${NEW_TOMCAT} stop
92
sudo rm -rf ${NEW_TOMCAT_BASE}/${WEBAPPS}/*
93
sudo cp -R ${OLD_TOMCAT_BASE}/${WEBAPPS}/*  ${NEW_TOMCAT_BASE}/${WEBAPPS}/.
94
sudo chown -R ${NEW_TOMCAT}:${NEW_TOMCAT} ${NEW_TOMCAT_BASE}/${WEBAPPS}/*
95
echo "change the value of the application.deployDir in the metacat.properties file"
96
SAFE_NEW_TOMCAT_WEBAPPS=$(printf '%s\n' "$NEW_TOMCAT_BASE/$WEBAPPS" | sed 's/[[\.*^$(){}?+|/]/\\&/g')
97
#echo "the escaped webpass value is ${SAFE_NEW_TOMCAT_WEBAPPS}"
98
if [ -f "$NEW_TOMCAT_BASE/$WEBAPPS/$KNB/WEB-INF/metacat.properties" ]; then
99
	echo "$NEW_TOMCAT_BASE/$WEBAPPS/$KNB/WEB-INF/metacat.properties exists and the application.deployDir will be updated"	
100
	sudo sed -i.bak --regexp-extended "s/(application\.deployDir=).*/\1${SAFE_NEW_TOMCAT_WEBAPPS}/;" $NEW_TOMCAT_BASE/$WEBAPPS/$KNB/WEB-INF/metacat.properties
101
        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
102
else
103
	echo "$NEW_TOMCAT_BASE/$WEBAPPS/$KNB/WEB-INF/metacat.properties does NOT exists and the application.deployDir will NOT be updated"
104
fi
105

    
106
if [ -f "$NEW_TOMCAT_BASE/$WEBAPPS/$METACAT/WEB-INF/metacat.properties" ]; then
107
                echo "$NEW_TOMCAT_BASE/$WEBAPPS/$METACAT/WEB-INF/metacat.properties eixsts and the application.deployDir will be updated" 
108
                sudo sed -i.bak --regexp-extended "s/(application\.deployDir=).*/\1${SAFE_NEW_TOMCAT_WEBAPPS}/;" $NEW_TOMCAT_BASE/$WEBAPPS/$METACAT/WEB-INF/metacat.properties
109
		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
110
else 
111
  echo "$NEW_TOMCAT_BASE/$WEBAPPS/$METACAT/WEB-INF/metacat.properties doesn't eixt and the application.deployDir will NOT be updated"
112
fi
113

    
114
echo "change the ownership of $METACAT_DATA_DIR to $NEW_TOMCAT"
115
sudo chown -R ${NEW_TOMCAT}:${NEW_TOMCAT} ${METACAT_DATA_DIR}
116

    
117

    
118
echo "Change somethings on apache configuration"
119
echo "read the location of the workers.properties file from the jk_conf"
120
while read f1 f2 
121
do
122
        if [ "$f1" = "JkWorkersFile" ]; then
123
        JK_WORKER_PATH="$f2"    
124
    fi
125
done < ${JK_CONF}
126
echo "the jk workers.properties location is $JK_WORKER_PATH"
127

    
128
echo "update the tomcat home and java home in workers.properties file"
129
SAFE_NEW_TOMCAT_HOME=$(printf '%s\n' "$NEW_TOMCAT_HOME" | sed 's/[[\.*^$(){}?+|/]/\\&/g')
130
SAFE_NEW_JDK_HOME=$(printf '%s\n' "$NEW_JDK_HOME" | sed 's/[[\.*^$(){}?+|/]/\\&/g')
131
sudo sed -i.bak --regexp-extended "s/(workers\.tomcat_home=).*/\1${SAFE_NEW_TOMCAT_HOME}/;
132
                s/(workers\.java_home=).*/\1${SAFE_NEW_JDK_HOME}/;"\
133
                $JK_WORKER_PATH
134

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

    
139
echo "add .conf to the files which don't end with .conf or .bak or .org"
140
for i in $(sudo find $APACHE_AVAILABLE_SITES_DIR -type f \( ! -name "*.conf" -a ! -name "*.bak" -a ! -name "*.org" \)); 
141
do
142
    sudo mv "$i" "${i}".conf 
143
done
144

    
145
echo "update the apache site files by replacing $OLD_TOMCAT by $NEW_TOMCAT"
146
for j in $(sudo find $APACHE_AVAILABLE_SITES_DIR -type f -name "*.conf")
147
do
148
    sudo sed -i.bak "s/${OLD_TOMCAT}/${NEW_TOMCAT}/;" $j
149
done
150

    
151
echo "rename the site file knb to $HOST_NAME and knb-ssl to $HOST_NAME-ssl"
152
sudo mv $APACHE_AVAILABLE_SITES_DIR/$KNB.conf $APACHE_AVAILABLE_SITES_DIR/$HOST_NAME.conf
153
sudo mv $APACHE_AVAILABLE_SITES_DIR/$KNB-ssl.conf $APACHE_AVAILABLE_SITES_DIR/$HOST_NAME-ssl.conf
154

    
155
echo "enable the two sites $HOST_NAME and $HOST_NAME-ssl"
156
sudo a2ensite $HOST_NAME
157
sudo a2ensite $HOST_NAME-ssl
158

    
159
sudo /etc/init.d/apache2 start
160
sudo /etc/init.d/tomcat7 start
161

    
162
exit 0
(5-5/10)