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_NAMEW=$1
42

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

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

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

    
87

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

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

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

    
116

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

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

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

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

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

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

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

    
158
sudo /etc/init.d/apache2 start
159
sudo /etc/init.d/tomcat7 start
(5-5/10)