From f221bd693f3a64a2f9f364a2f6dbc90242ba282f Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Tue, 25 Feb 2014 12:01:55 -0500
Subject: [PATCH] Direct deployment for TPS.

The deployment tool has been modified to deploy TPS directly from the
share folder. This way the TPS UI can be upgraded automatically with
RPM upgrade without having to write upgrade scripts.

For this to work, the TPS web application files cannot contain any
customization. So, the cfgPath parameter has been removed from web.xml,
and the CMSStartServlet has been modified such that if the parameter is
missing it would generate a default path matching the original value in
web.xml. Also, the velocity.properties has been modified to use a fixed
value for the file.resource.loader.path parameter pointing to the share
folder.

In the future other subsystems may be modified to use the same
deployment mechanism.

Ticket #748, #752, #499
---
 .../netscape/cms/servlet/base/CMSStartServlet.java | 23 +++--
 .../deployment/scriptlets/slot_substitution.py     | 12 ++-
 .../deployment/scriptlets/webapp_deployment.py     | 97 ++++++++--------------
 .../shared/conf/Catalina/localhost/tps.xml         |  2 +-
 .../shared/webapps/tps/WEB-INF/velocity.properties |  2 +-
 base/tps-tomcat/shared/webapps/tps/WEB-INF/web.xml |  4 -
 specs/pki-core.spec                                | 45 ++++++++++
 7 files changed, 106 insertions(+), 79 deletions(-)

diff --git a/base/server/cms/src/com/netscape/cms/servlet/base/CMSStartServlet.java b/base/server/cms/src/com/netscape/cms/servlet/base/CMSStartServlet.java
index 60230dbab2875ccd91794ac9e0548ffb64fde530..a256e9f14e8ba6043c16d470583652a259c21026 100644
--- a/base/server/cms/src/com/netscape/cms/servlet/base/CMSStartServlet.java
+++ b/base/server/cms/src/com/netscape/cms/servlet/base/CMSStartServlet.java
@@ -21,7 +21,6 @@ import java.io.File;
 import java.io.IOException;
 import java.io.PrintWriter;
 
-import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
@@ -46,9 +45,18 @@ public class CMSStartServlet extends HttpServlet {
     private static final long serialVersionUID = 515623839479425172L;
     public final static String PROP_CMS_CFG = "cfgPath";
 
-    public void init(ServletConfig config) throws ServletException {
-        super.init(config);
-        String path = config.getInitParameter(PROP_CMS_CFG);
+    public void init() throws ServletException {
+
+        String path = getServletConfig().getInitParameter(PROP_CMS_CFG);
+        String context = getServletContext().getContextPath();
+        String subsystem = context.startsWith("/") ? context.substring(1) : context;
+
+        // if path not specified, use default path
+        if (path == null) {
+            String instanceDir = System.getProperty("catalina.base");
+            path = instanceDir + File.separator + "conf" + File.separator +
+                    subsystem + File.separator + "CS.cfg";
+        }
 
         File f = new File(path);
         String old_path = "";
@@ -88,6 +96,7 @@ public class CMSStartServlet extends HttpServlet {
                         }
                     }
                 } catch (Exception e) {
+                    e.printStackTrace();
                 }
             }
         }
@@ -95,12 +104,11 @@ public class CMSStartServlet extends HttpServlet {
         try {
             CMS.start(path);
         } catch (EBaseException e) {
+            e.printStackTrace();
         }
 
         // Register realm for this subsystem
-        String context = getServletContext().getContextPath();
-        if (context.startsWith("/")) context = context.substring(1);
-        ProxyRealm.registerRealm(context, new PKIRealm());
+        ProxyRealm.registerRealm(subsystem, new PKIRealm());
     }
 
     public void doGet(HttpServletRequest req, HttpServletResponse res)
@@ -125,6 +133,5 @@ public class CMSStartServlet extends HttpServlet {
      */
     public void destroy() {
         CMS.shutdown();
-        super.destroy();
     }
 }
diff --git a/base/server/python/pki/server/deployment/scriptlets/slot_substitution.py b/base/server/python/pki/server/deployment/scriptlets/slot_substitution.py
index a6c21e166bb64aa8cca14b934dfcc60f98167b91..d0a69870bc2da6456a4674aef14b9bc6078b4e51 100644
--- a/base/server/python/pki/server/deployment/scriptlets/slot_substitution.py
+++ b/base/server/python/pki/server/deployment/scriptlets/slot_substitution.py
@@ -67,10 +67,14 @@ class PkiScriptlet(pkiscriptlet.AbstractBasePkiScriptlet):
                 deployer.master_dict['pki_source_tomcat_conf'],
                 deployer.master_dict['pki_target_tomcat_conf'],
                 overwrite_flag=True)
-            deployer.file.apply_slot_substitution(
-                deployer.master_dict['pki_target_velocity_properties'])
-            deployer.file.apply_slot_substitution(
-                deployer.master_dict['pki_target_subsystem_web_xml'])
+
+            # Customize web application files except for TPS.
+            if deployer.master_dict['pki_subsystem'] != "TPS":
+                deployer.file.apply_slot_substitution(
+                    deployer.master_dict['pki_target_velocity_properties'])
+                deployer.file.apply_slot_substitution(
+                    deployer.master_dict['pki_target_subsystem_web_xml'])
+
             # Strip "<filter>" section from subsystem "web.xml"
             # This is ONLY necessary because XML comments cannot be "nested"!
             # deployer.file.copy(deployer.master_dict['pki_target_subsystem_web_xml'],
diff --git a/base/server/python/pki/server/deployment/scriptlets/webapp_deployment.py b/base/server/python/pki/server/deployment/scriptlets/webapp_deployment.py
index 975028d73c67ff00c088d11c69640cc74d3256bf..8c301dd7d0c5040572415117b00b0f6f7591a0ed 100644
--- a/base/server/python/pki/server/deployment/scriptlets/webapp_deployment.py
+++ b/base/server/python/pki/server/deployment/scriptlets/webapp_deployment.py
@@ -44,69 +44,43 @@ class PkiScriptlet(pkiscriptlet.AbstractBasePkiScriptlet):
             config.pki_log.info(log.WEBAPP_DEPLOYMENT_SPAWN_1, __name__,
                                 extra=config.PKI_INDENTATION_LEVEL_1)
 
-            # Copy /usr/share/pki/server/webapps/pki/admin
-            # to <instance>/webapps/<subsystem>/admin
-            # TODO: common templates should be deployed in common webapp
-            deployer.directory.create(deployer.master_dict['pki_tomcat_webapps_subsystem_path'])
-            deployer.directory.copy(
-                os.path.join(
-                    config.PKI_DEPLOYMENT_SOURCE_ROOT,
-                    "server",
-                    "webapps",
-                    "pki",
-                    "admin"),
-                os.path.join(
+            # For subsystems other than TPS, copy the web application files.
+            # For TPS, deploy directly from /usr/share/pki.
+            if deployer.master_dict['pki_subsystem'] != "TPS":
+
+                deployer.directory.create(deployer.master_dict['pki_tomcat_webapps_subsystem_path'])
+
+                # Copy /usr/share/pki/<subsystem>/webapps/<subsystem>
+                # to <instance>/webapps/<subsystem>
+                deployer.directory.copy(
+                    os.path.join(
+                        config.PKI_DEPLOYMENT_SOURCE_ROOT,
+                        deployer.master_dict['pki_subsystem'].lower(),
+                        "webapps",
+                        deployer.master_dict['pki_subsystem'].lower()),
                     deployer.master_dict['pki_tomcat_webapps_subsystem_path'],
-                    "admin"),
-                overwrite_flag=True)
+                    overwrite_flag=True)
 
-            # Copy /usr/share/pki/<subsystem>/webapps/<subsystem>
-            # to <instance>/webapps/<subsystem>
-            deployer.directory.copy(
-                os.path.join(
-                    config.PKI_DEPLOYMENT_SOURCE_ROOT,
-                    deployer.master_dict['pki_subsystem'].lower(),
-                    "webapps",
-                    deployer.master_dict['pki_subsystem'].lower()),
-                deployer.master_dict['pki_tomcat_webapps_subsystem_path'],
-                overwrite_flag=True)
+                # Copy /usr/share/pki/server/webapps/pki/admin
+                # to <instance>/webapps/<subsystem>/admin
+                # TODO: common templates should be deployed in common webapp
+                deployer.directory.copy(
+                    os.path.join(
+                        config.PKI_DEPLOYMENT_SOURCE_ROOT,
+                        "server",
+                            "webapps",
+                        "pki",
+                        "admin"),
+                    os.path.join(
+                        deployer.master_dict['pki_tomcat_webapps_subsystem_path'],
+                        "admin"),
+                    overwrite_flag=True)
 
-            deployer.directory.create(
-                deployer.master_dict['pki_tomcat_webapps_subsystem_webinf_classes_path'])
-            deployer.directory.create(
-                deployer.master_dict['pki_tomcat_webapps_subsystem_webinf_lib_path'])
-            # establish Tomcat webapps subsystem WEB-INF lib symbolic links
-            deployer.symlink.create(deployer.master_dict['pki_certsrv_jar'],
-                deployer.master_dict['pki_certsrv_jar_link'])
-            deployer.symlink.create(deployer.master_dict['pki_cmsbundle'],
-                deployer.master_dict['pki_cmsbundle_jar_link'])
-            deployer.symlink.create(deployer.master_dict['pki_cmscore'],
-                deployer.master_dict['pki_cmscore_jar_link'])
-            deployer.symlink.create(deployer.master_dict['pki_cms'],
-                deployer.master_dict['pki_cms_jar_link'])
-            deployer.symlink.create(deployer.master_dict['pki_cmsutil'],
-                deployer.master_dict['pki_cmsutil_jar_link'])
-            deployer.symlink.create(deployer.master_dict['pki_nsutil'],
-                deployer.master_dict['pki_nsutil_jar_link'])
-            if deployer.master_dict['pki_subsystem'] == "CA":
-                deployer.symlink.create(deployer.master_dict['pki_ca_jar'],
-                                    deployer.master_dict['pki_ca_jar_link'])
-            elif deployer.master_dict['pki_subsystem'] == "KRA":
-                deployer.symlink.create(deployer.master_dict['pki_kra_jar'],
-                                    deployer.master_dict['pki_kra_jar_link'])
-            elif deployer.master_dict['pki_subsystem'] == "OCSP":
-                deployer.symlink.create(deployer.master_dict['pki_ocsp_jar'],
-                                    deployer.master_dict['pki_ocsp_jar_link'])
-            elif deployer.master_dict['pki_subsystem'] == "TKS":
-                deployer.symlink.create(deployer.master_dict['pki_tks_jar'],
-                                    deployer.master_dict['pki_tks_jar_link'])
-            elif deployer.master_dict['pki_subsystem'] == "TPS":
-                deployer.symlink.create(deployer.master_dict['pki_tps_jar'],
-                                    deployer.master_dict['pki_tps_jar_link'])
-            # set ownerships, permissions, and acls
-            deployer.directory.set_mode(deployer.master_dict['pki_tomcat_webapps_subsystem_path'])
+                # set ownerships, permissions, and acls
+                deployer.directory.set_mode(deployer.master_dict['pki_tomcat_webapps_subsystem_path'])
 
-            # Copy /usr/share/pki/<subsystem>/conf/Catalina/localhost/<subsystem>.xml
+            # Copy web application context file
+            # from /usr/share/pki/<subsystem>/conf/Catalina/localhost/<subsystem>.xml
             # to <instance>/conf/Catalina/localhost/<subsystem>.xml
             deployer.file.copy(
                 os.path.join(
@@ -137,7 +111,8 @@ class PkiScriptlet(pkiscriptlet.AbstractBasePkiScriptlet):
                     "localhost",
                     deployer.master_dict['pki_subsystem'].lower() + ".xml"))
 
-            # Delete <instance>/webapps/<subsystem>
-            deployer.directory.delete(deployer.master_dict['pki_tomcat_webapps_subsystem_path'])
+            # For subsystems other than TPS, delete <instance>/webapps/<subsystem>.
+            if deployer.master_dict['pki_subsystem'] != "TPS":
+                deployer.directory.delete(deployer.master_dict['pki_tomcat_webapps_subsystem_path'])
 
         return self.rv
diff --git a/base/tps-tomcat/shared/conf/Catalina/localhost/tps.xml b/base/tps-tomcat/shared/conf/Catalina/localhost/tps.xml
index e838503a64e5442e114cf4e18f616fdffe39b647..d80c1296dbd6a88a9263c50b351a31516682a59a 100644
--- a/base/tps-tomcat/shared/conf/Catalina/localhost/tps.xml
+++ b/base/tps-tomcat/shared/conf/Catalina/localhost/tps.xml
@@ -22,7 +22,7 @@
   See the License for the specific language governing permissions and
   limitations under the License.
 -->
-<Context crossContext="true" allowLinking="true">
+<Context docBase="/usr/share/pki/tps/webapps/tps" crossContext="true" allowLinking="true">
 
     <Manager
         secureRandomProvider="Mozilla-JSS" secureRandomAlgorithm="pkcs11prng"/>
diff --git a/base/tps-tomcat/shared/webapps/tps/WEB-INF/velocity.properties b/base/tps-tomcat/shared/webapps/tps/WEB-INF/velocity.properties
index 5cd0454cce3a0a93dccd01fbf4c55cc71b77fc2a..c1c6e4ea9501642bac654c8a4666608a69e61601 100644
--- a/base/tps-tomcat/shared/webapps/tps/WEB-INF/velocity.properties
+++ b/base/tps-tomcat/shared/webapps/tps/WEB-INF/velocity.properties
@@ -5,7 +5,7 @@
 #
 resource.loader = file
 file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader
-file.resource.loader.path = [PKI_INSTANCE_PATH]/[PKI_WEBAPPS_NAME]/[PKI_SUBSYSTEM_TYPE]
+file.resource.loader.path = /usr/share/pki/server/webapps/pki
 file.resource.loader.cache = true
 file.resource.loader.modificationCheckInterval = 2
 input.encoding=UTF-8
diff --git a/base/tps-tomcat/shared/webapps/tps/WEB-INF/web.xml b/base/tps-tomcat/shared/webapps/tps/WEB-INF/web.xml
index 0fe3df4dc3cc41f3bdae7ea752d057d56e13595d..11964a00ba5ebd98d1d1e67d400d037809cb3b49 100644
--- a/base/tps-tomcat/shared/webapps/tps/WEB-INF/web.xml
+++ b/base/tps-tomcat/shared/webapps/tps/WEB-INF/web.xml
@@ -43,10 +43,6 @@
             <param-value>BasicAclAuthz</param-value>
         </init-param>
         <init-param>
-            <param-name>cfgPath</param-name>
-            <param-value>[PKI_INSTANCE_PATH]/conf/[PKI_SUBSYSTEM_TYPE]/CS.cfg</param-value>
-        </init-param>
-        <init-param>
             <param-name>ID</param-name>
             <param-value>tpsstart</param-value>
         </init-param>
diff --git a/specs/pki-core.spec b/specs/pki-core.spec
index cb1912124f8d92a5cdf2e673db7dc0c666914209..3ed6af72fa7f987c00a56992434a49a756956a86 100644
--- a/specs/pki-core.spec
+++ b/specs/pki-core.spec
@@ -522,6 +522,51 @@ cd build
 cd build
 %{__make} install DESTDIR=%{buildroot} INSTALL="install -p"
 
+%{__mkdir_p} %{buildroot}%{_datadir}/pki/ca/webapps/ca/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-nsutil.jar %{buildroot}%{_datadir}/pki/ca/webapps/ca/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-cmsutil.jar %{buildroot}%{_datadir}/pki/ca/webapps/ca/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-certsrv.jar %{buildroot}%{_datadir}/pki/ca/webapps/ca/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-cms.jar %{buildroot}%{_datadir}/pki/ca/webapps/ca/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-cmscore.jar %{buildroot}%{_datadir}/pki/ca/webapps/ca/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-cmsbundle.jar %{buildroot}%{_datadir}/pki/ca/webapps/ca/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-ca.jar %{buildroot}%{_datadir}/pki/ca/webapps/ca/WEB-INF/lib
+
+%{__mkdir_p} %{buildroot}%{_datadir}/pki/kra/webapps/kra/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-nsutil.jar %{buildroot}%{_datadir}/pki/kra/webapps/kra/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-cmsutil.jar %{buildroot}%{_datadir}/pki/kra/webapps/kra/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-certsrv.jar %{buildroot}%{_datadir}/pki/kra/webapps/kra/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-cms.jar %{buildroot}%{_datadir}/pki/kra/webapps/kra/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-cmscore.jar %{buildroot}%{_datadir}/pki/kra/webapps/kra/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-cmsbundle.jar %{buildroot}%{_datadir}/pki/kra/webapps/kra/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-kra.jar %{buildroot}%{_datadir}/pki/kra/webapps/kra/WEB-INF/lib
+
+%{__mkdir_p} %{buildroot}%{_datadir}/pki/ocsp/webapps/ocsp/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-nsutil.jar %{buildroot}%{_datadir}/pki/ocsp/webapps/ocsp/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-cmsutil.jar %{buildroot}%{_datadir}/pki/ocsp/webapps/ocsp/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-certsrv.jar %{buildroot}%{_datadir}/pki/ocsp/webapps/ocsp/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-cms.jar %{buildroot}%{_datadir}/pki/ocsp/webapps/ocsp/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-cmscore.jar %{buildroot}%{_datadir}/pki/ocsp/webapps/ocsp/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-cmsbundle.jar %{buildroot}%{_datadir}/pki/ocsp/webapps/ocsp/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-ocsp.jar %{buildroot}%{_datadir}/pki/ocsp/webapps/ocsp/WEB-INF/lib
+
+%{__mkdir_p} %{buildroot}%{_datadir}/pki/tks/webapps/tks/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-nsutil.jar %{buildroot}%{_datadir}/pki/tks/webapps/tks/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-cmsutil.jar %{buildroot}%{_datadir}/pki/tks/webapps/tks/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-certsrv.jar %{buildroot}%{_datadir}/pki/tks/webapps/tks/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-cms.jar %{buildroot}%{_datadir}/pki/tks/webapps/tks/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-cmscore.jar %{buildroot}%{_datadir}/pki/tks/webapps/tks/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-cmsbundle.jar %{buildroot}%{_datadir}/pki/tks/webapps/tks/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-tks.jar %{buildroot}%{_datadir}/pki/tks/webapps/tks/WEB-INF/lib
+
+%{__mkdir_p} %{buildroot}%{_datadir}/pki/tps/webapps/tps/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-nsutil.jar %{buildroot}%{_datadir}/pki/tps/webapps/tps/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-cmsutil.jar %{buildroot}%{_datadir}/pki/tps/webapps/tps/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-certsrv.jar %{buildroot}%{_datadir}/pki/tps/webapps/tps/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-cms.jar %{buildroot}%{_datadir}/pki/tps/webapps/tps/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-cmscore.jar %{buildroot}%{_datadir}/pki/tps/webapps/tps/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-cmsbundle.jar %{buildroot}%{_datadir}/pki/tps/webapps/tps/WEB-INF/lib
+ln -s %{_javadir}/pki/pki-tps.jar %{buildroot}%{_datadir}/pki/tps/webapps/tps/WEB-INF/lib
+
 %if %{with server}
 
 # Scanning the python code with pylint. A return value of 0 represents there are no
-- 
1.8.4.2

