>From 5ccc84860f21c77c8f6aa6b9a0c4dbde947062e7 Mon Sep 17 00:00:00 2001
From: Ade Lee <alee@redhat.com>
Date: Wed, 20 Mar 2013 15:21:46 -0400
Subject: [PATCH] Add status servlet to all subsystems

Also, add code to installer to check status before continuing
with configuration, to ensure tomcat is actually up.
---
 base/common/python/pki/account.py             |  4 +--
 base/common/python/pki/client.py              | 10 +++++---
 base/common/python/pki/system.py              | 17 ++++++++++--
 base/deploy/src/scriptlets/configuration.py   |  7 +++++
 base/deploy/src/scriptlets/pkihelper.py       | 37 +++++++++++++++++++++++++++
 base/kra/shared/webapps/kra/WEB-INF/web.xml   | 17 ++++++++++++
 base/ocsp/shared/webapps/ocsp/WEB-INF/web.xml | 16 ++++++++++++
 base/tks/shared/webapps/tks/WEB-INF/web.xml   | 16 ++++++++++++
 8 files changed, 116 insertions(+), 8 deletions(-)

diff --git a/base/common/python/pki/account.py b/base/common/python/pki/account.py
index 84f2d0ef03936f921afc15c687b3b9779e026f3d..be87f83438395fab94aa581b55e9f1e409a1afea 100644
--- a/base/common/python/pki/account.py
+++ b/base/common/python/pki/account.py
@@ -25,7 +25,7 @@ class AccountClient:
         self.connection = connection
 
     def login(self):
-        self.connection.get('account/login')
+        self.connection.get('/rest/account/login')
 
     def logout(self):
-        self.connection.get('account/logout')
+        self.connection.get('/rest/account/logout')
diff --git a/base/common/python/pki/client.py b/base/common/python/pki/client.py
index 43b78e1af2760b62e8567a6beb2230701bb61db9..83bba62d6a3751f74cb034ea685d6e60c3fc2191 100644
--- a/base/common/python/pki/client.py
+++ b/base/common/python/pki/client.py
@@ -27,7 +27,8 @@ class PKIConnection:
             protocol='http',
             hostname='localhost',
             port=80,
-            subsystem='ca'):
+            subsystem='ca',
+            accept='application/json'):
 
         self.protocol = protocol
         self.hostname = hostname
@@ -39,7 +40,8 @@ class PKIConnection:
             self.subsystem
 
         self.session = requests.Session()
-        self.session.headers.update({'Accept': 'application/json'})
+        if accept != None:
+            self.session.headers.update({'Accept': accept})
 
     def authenticate(self, username=None, password=None):
         if username is not None and password is not None:
@@ -47,14 +49,14 @@ class PKIConnection:
 
     def get(self, path):
         r = self.session.get(
-            self.serverURI + '/rest/' + path,
+            self.serverURI + path,
             verify=False)
         r.raise_for_status()
         return r
 
     def post(self, path, payload, headers):
         r = self.session.post(
-                self.serverURI + '/rest/' + path,
+                self.serverURI + path,
                 verify=False,
                 data=payload,
                 headers=headers)
diff --git a/base/common/python/pki/system.py b/base/common/python/pki/system.py
index 51e899f98cfe828fb627450710d6d441611708b4..dbf549220f2189f8099f590d6becfa5a606f63df 100644
--- a/base/common/python/pki/system.py
+++ b/base/common/python/pki/system.py
@@ -30,7 +30,7 @@ class SecurityDomainClient:
         self.connection = connection
 
     def getSecurityDomainInfo(self):
-        r = self.connection.get('securityDomain/domainInfo')
+        r = self.connection.get('/rest/securityDomain/domainInfo')
         j = r.json()
 
         info = SecurityDomainInfo()
@@ -66,6 +66,19 @@ class SystemConfigClient:
     def configure(self, data):
         headers = {'Content-type': 'application/json',
                    'Accept': 'application/json'}
-        r = self.connection.post('installer/configure', data, headers)
+        r = self.connection.post('/rest/installer/configure', data, headers)
         info = r.json()['ConfigurationResponse']
         return info
+
+class SystemStatusClient:
+
+    def __init__(self, connection):
+        self.connection = connection
+
+    def getStatus(self):
+        r = self.connection.get('/admin/' +\
+                self.connection.subsystem + '/getStatus')
+        return r.text
+
+
+
diff --git a/base/deploy/src/scriptlets/configuration.py b/base/deploy/src/scriptlets/configuration.py
index 05016d7878f34372ddccd0b7685ab146b88a9312..8fc062dfbab2d090abf95cf66c40adb7d4f026bd 100644
--- a/base/deploy/src/scriptlets/configuration.py
+++ b/base/deploy/src/scriptlets/configuration.py
@@ -94,6 +94,13 @@ class PkiScriptlet(pkiscriptlet.AbstractBasePkiScriptlet):
             elif tomcat_instance_subsystems > 1:
                 util.systemd.restart()
 
+        # wait for startup
+        status = util.instance.wait_for_startup(60)
+        if status == None:
+            config.pki_log.error("server failed to restart",
+                    extra=config.PKI_INDENTATION_LEVEL_2)
+            sys.exit(1)
+
         # Optionally wait for debugger to attach (e. g. - 'eclipse'):
         if config.str2bool(master['pki_enable_java_debugger']):
             config.wait_to_attach_an_external_java_debugger()
diff --git a/base/deploy/src/scriptlets/pkihelper.py b/base/deploy/src/scriptlets/pkihelper.py
index d663859f436692256f5dfaa214e03ef9a7966267..9b67277a73814196a7c2851f339a4b02441a69ea 100644
--- a/base/deploy/src/scriptlets/pkihelper.py
+++ b/base/deploy/src/scriptlets/pkihelper.py
@@ -28,13 +28,16 @@ import fileinput
 import pickle
 import random
 import re
+import requests
 import shutil
 import string
 import subprocess
+import time
 from grp import getgrgid
 from grp import getgrnam
 from pwd import getpwnam
 from pwd import getpwuid
+import xml.etree.ElementTree as ET
 import zipfile
 import selinux
 if selinux.is_selinux_enabled():
@@ -989,6 +992,40 @@ class instance:
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             sys.exit(1)
 
+    def get_instance_status(self):
+        self.connection = pki.client.PKIConnection(
+            protocol='https',
+            hostname=master['pki_hostname'],
+            port=master['pki_https_port'],
+            subsystem=master['pki_subsystem_type'],
+            accept = 'application/xml')
+
+        try:
+            client = pki.system.SystemStatusClient(self.connection)
+            response = client.getStatus()
+            root = ET.fromstring(response)
+            status = root.findtext("Status")
+
+            config.pki_log.debug(log.PKI_CONFIG_RESPONSE_STATUS, 
+                                 str(status),
+                                 extra=config.PKI_INDENTATION_LEVEL_3)
+            return status
+        except requests.exceptions.ConnectionError, e:
+            config.pki_log.debug(log.PKI_CONFIG_RESPONSE_STATUS,
+                                 "Connection Error: " + str(e),
+                                 extra=config.PKI_INDENTATION_LEVEL_3)
+            return None
+
+    def wait_for_startup(self, timeout):
+        tries = 1   
+        status = self.get_instance_status()
+        while status != "running":
+            if tries >= timeout:
+                return None
+            time.sleep(1)
+            status = self.get_instance_status()
+            tries = tries + 1
+        return status
 
 # PKI Deployment Directory Class
 class directory:
diff --git a/base/kra/shared/webapps/kra/WEB-INF/web.xml b/base/kra/shared/webapps/kra/WEB-INF/web.xml
index 5ef28fc3183bfa4786e4489c5135df544f6e044f..57c62561f79b2d1f7eaaf6cf6cc4e69a7610ca46 100644
--- a/base/kra/shared/webapps/kra/WEB-INF/web.xml
+++ b/base/kra/shared/webapps/kra/WEB-INF/web.xml
@@ -680,6 +680,18 @@
                          <param-value> certServer.clone.configuration </param-value> </init-param>
    </servlet>
 
+   <servlet>
+      <servlet-name>  kraGetStatus  </servlet-name>
+      <servlet-class> com.netscape.cms.servlet.csadmin.GetStatus  </servlet-class>
+             <init-param><param-name>  GetClientCert  </param-name>
+                         <param-value> false       </param-value> </init-param>
+             <init-param><param-name>  authority   </param-name>
+                         <param-value> kra          </param-value> </init-param>
+             <init-param><param-name>  ID          </param-name>
+                         <param-value> kraGetStatus </param-value> </init-param>
+   </servlet>
+
+
    <!-- ==================== RESTEasy Configuration =============== -->
 
    <listener>
@@ -926,6 +938,11 @@
       <url-pattern>   /admin/console/config/savepkcs12 </url-pattern>
    </servlet-mapping>
 
+   <servlet-mapping>
+      <servlet-name>  kraGetStatus  </servlet-name>
+      <url-pattern>   /admin/kra/getStatus  </url-pattern>
+   </servlet-mapping>
+
    <!-- ==================== Default Session Configuration =============== -->
    <!-- You can set the default session timeout (in minutes) for all newly -->
    <!-- created sessions by modifying the value below.                     -->
diff --git a/base/ocsp/shared/webapps/ocsp/WEB-INF/web.xml b/base/ocsp/shared/webapps/ocsp/WEB-INF/web.xml
index eb29dc1549a1d7ff3a2afc5634501ccc09d0d4aa..570f4db6c44a83acf6b5738d3fb3b1e766cae7ca 100644
--- a/base/ocsp/shared/webapps/ocsp/WEB-INF/web.xml
+++ b/base/ocsp/shared/webapps/ocsp/WEB-INF/web.xml
@@ -393,6 +393,17 @@
                          <param-value> BasicAclAuthz </param-value> </init-param>
    </servlet>
 
+   <servlet>
+      <servlet-name>  ocspGetStatus  </servlet-name>
+      <servlet-class> com.netscape.cms.servlet.csadmin.GetStatus  </servlet-class>
+             <init-param><param-name>  GetClientCert  </param-name>
+                         <param-value> false       </param-value> </init-param>
+             <init-param><param-name>  authority   </param-name>
+                         <param-value> ocsp          </param-value> </init-param>
+             <init-param><param-name>  ID          </param-name>
+                         <param-value> ocspGetStatus </param-value> </init-param>
+   </servlet>
+
    <listener>
       <listener-class> org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap </listener-class>
    </listener>
@@ -560,6 +571,11 @@
       <url-pattern>   /admin/console/config/savepkcs12 </url-pattern>
    </servlet-mapping>
 
+   <servlet-name>  ocspGetStatus  </servlet-name>
+      <url-pattern>   /admin/ocsp/getStatus  </url-pattern>
+   </servlet-mapping>
+
+
    <!-- ==================== Default Session Configuration =============== -->
    <!-- You can set the default session timeout (in minutes) for all newly -->
    <!-- created sessions by modifying the value below.                     -->
diff --git a/base/tks/shared/webapps/tks/WEB-INF/web.xml b/base/tks/shared/webapps/tks/WEB-INF/web.xml
index 7cfb24b159013d83f734c918a19adb99f674bab3..bc1685ddb0dc5443f9a6e1a08b563fb6b1eaf0df 100644
--- a/base/tks/shared/webapps/tks/WEB-INF/web.xml
+++ b/base/tks/shared/webapps/tks/WEB-INF/web.xml
@@ -263,6 +263,17 @@
                          <param-value> certServer.clone.configuration.GetConfigEntries </param-value> </init-param>
    </servlet>
 
+   <servlet>
+      <servlet-name>  tksGetStatus  </servlet-name>
+      <servlet-class> com.netscape.cms.servlet.csadmin.GetStatus  </servlet-class>
+             <init-param><param-name>  GetClientCert  </param-name>
+                         <param-value> false       </param-value> </init-param>
+             <init-param><param-name>  authority   </param-name>
+                         <param-value> tks          </param-value> </init-param>
+             <init-param><param-name>  ID          </param-name>
+                         <param-value> tksGetStatus </param-value> </init-param>
+   </servlet>
+
    <listener>
       <listener-class> org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap </listener-class>
    </listener>
@@ -391,6 +402,11 @@
       <url-pattern>   /admin/console/config/savepkcs12 </url-pattern>
    </servlet-mapping>
 
+   <servlet-mapping>
+      <servlet-name>  tksGetStatus  </servlet-name>
+      <url-pattern>   /admin/tks/getStatus  </url-pattern>
+   </servlet-mapping>
+
    <!-- ==================== Default Session Configuration =============== -->
    <!-- You can set the default session timeout (in minutes) for all newly -->
    <!-- created sessions by modifying the value below.                     -->
-- 
1.8.1.4

