>From 51958c82a064cb4a4146fc55641117bc6b155e3a Mon Sep 17 00:00:00 2001
From: Ade Lee <alee@redhat.com>
Date: Wed, 29 Apr 2015 12:46:19 -0400
Subject: [PATCH] Fix interactive install to not reprompt for ports

Ports are already set when deploying into an existing instance.
Having a user re-enter these is repetitious and error prone.
---
 base/server/man/man8/pkispawn.8                    |  2 ++
 .../python/pki/server/deployment/pkiparser.py      | 20 ++++++++++++
 base/server/sbin/pkispawn                          | 38 ++++++++++++++++++----
 3 files changed, 53 insertions(+), 7 deletions(-)

diff --git a/base/server/man/man8/pkispawn.8 b/base/server/man/man8/pkispawn.8
index 1ef9b26bec977a442b4d628b9b30075345c24147..33c36e3380950b1b499f4e3d644e0d59c7a80597 100644
--- a/base/server/man/man8/pkispawn.8
+++ b/base/server/man/man8/pkispawn.8
@@ -116,6 +116,8 @@ the AJP port of the Tomcat instance. The default value is 8009.
 .TP
 \fIManagement port:\fP
 the management port of the Tomcat instance. The default value is 8005.
+.TP
+\fBNote:\fP When deploying a new subsystem into an existing instance, pkispawn will attempt to read the ports from \fBdeployment.cfg\fP files stored for previously installed subsystems for this instance.  If successful, the installer will not prompt for these ports. 
 .PP
 \fBAdministrative User Parameters\fP
 .TP
diff --git a/base/server/python/pki/server/deployment/pkiparser.py b/base/server/python/pki/server/deployment/pkiparser.py
index b39075f6c8a1e6dcaccb51abdad20c2877acb814..39cef9413171f6a22bb2292edc1f7a18d07257fc 100644
--- a/base/server/python/pki/server/deployment/pkiparser.py
+++ b/base/server/python/pki/server/deployment/pkiparser.py
@@ -1259,3 +1259,23 @@ class PKIConfigParser:
         except ConfigParser.ParsingError, err:
             rv = err
         return rv
+
+    @staticmethod
+    def read_existing_deployment_data(instance_name):
+        data = {}
+        instance_root = os.path.join('/var/lib/pki', instance_name)
+        if not os.path.exists(instance_root):
+            return data
+        deployment_root = os.path.join('/etc/sysconfig/pki/tomcat',
+                                       instance_name)
+
+        for root, _dirs, names in os.walk(deployment_root):
+            if 'deployment.cfg' in names:
+                deployment_file = os.path.join(root, 'deployment.cfg')
+                data = PKIConfigParser.read_simple_configuration_file(
+                    deployment_file)
+                break
+
+        return data
+
+
diff --git a/base/server/sbin/pkispawn b/base/server/sbin/pkispawn
index 965918f224b754e4f406161d98bda1e644e22470..97806549ef073a677459ab19d5f5d7695d457e90 100755
--- a/base/server/sbin/pkispawn
+++ b/base/server/sbin/pkispawn
@@ -147,13 +147,30 @@ def main(argv):
             parser.indent = 2
 
             print "Tomcat:"
-            parser.read_text('Instance', 'DEFAULT', 'pki_instance_name')
-            parser.read_text('HTTP port', config.pki_subsystem, 'pki_http_port')
-            parser.read_text('Secure HTTP port', config.pki_subsystem,
-                             'pki_https_port')
-            parser.read_text('AJP port', config.pki_subsystem, 'pki_ajp_port')
-            parser.read_text('Management port', config.pki_subsystem,
-                             'pki_tomcat_server_port')
+            instance_name = parser.read_text(
+                'Instance', 'DEFAULT', 'pki_instance_name')
+            existing_data = parser.read_existing_deployment_data(instance_name)
+
+            set_port(parser,
+                     'pki_http_port',
+                     'HTTP port',
+                     existing_data)
+
+            set_port(parser,
+                     'pki_https_port',
+                     'Secure HTTP port',
+                     existing_data)
+
+            set_port(parser,
+                     'pki_ajp_port',
+                     'AJP port',
+                     existing_data)
+
+            set_port(parser,
+                     'pki_tomcat_server_port',
+                     'Management port',
+                     existing_data)
+
             print
 
             print "Administrator:"
@@ -586,6 +603,13 @@ def main(argv):
     print_install_information(parser.mdict)
 
 
+def set_port(parser, tag, prompt, existing_data):
+    if existing_data[tag]:
+        parser.set_property(config.pki_subsystem, tag, existing_data[tag])
+    else:
+        parser.read_text(prompt, config.pki_subsystem, tag)
+
+
 def print_install_information(mdict):
 
     skip_configuration = config.str2bool(mdict['pki_skip_configuration'])
-- 
1.9.3

