>From 93d3b5f399078eeafafc47e27f02a8cd643dbba6 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Thu, 22 Dec 2016 05:01:38 +0100
Subject: [PATCH] Refactored deployment system variables.

To improve reusability the deployment system variables have been
converted from global variables in pkiconfig.py into attributes in
PKIDeployer.
---
 .../python/pki/server/deployment/__init__.py       | 16 ++++++++++++++++
 .../python/pki/server/deployment/pkiconfig.py      |  6 ------
 .../python/pki/server/deployment/pkiparser.py      |  8 ++++----
 base/server/sbin/pkidestroy                        | 22 ----------------------
 base/server/sbin/pkispawn                          | 21 ---------------------
 5 files changed, 20 insertions(+), 53 deletions(-)

diff --git a/base/server/python/pki/server/deployment/__init__.py b/base/server/python/pki/server/deployment/__init__.py
index b5db8867a728e2af0016190108219a83542753ae..3d719ded2b991bc11b0aeb66ab002e8246fb2b89 100644
--- a/base/server/python/pki/server/deployment/__init__.py
+++ b/base/server/python/pki/server/deployment/__init__.py
@@ -20,6 +20,9 @@
 
 from __future__ import absolute_import
 import os
+import socket
+import struct
+import subprocess
 import time
 from time import strftime as date
 from lxml import etree
@@ -71,6 +74,19 @@ class PKIDeployer:
         self.log_timestamp = date('%Y%m%d%H%M%S', time.localtime(ticks))
         self.certificate_timestamp = date('%Y-%m-%d %H:%M:%S', time.localtime(ticks))
 
+        # Obtain the architecture bit-size
+        self.architecture = struct.calcsize("P") * 8
+
+        # Retrieve hostname
+        self.hostname = socket.getfqdn()
+
+        # Retrieve DNS domainname
+        self.dns_domainname = subprocess.check_output(["dnsdomainname"])
+        self.dns_domainname = self.dns_domainname.decode('ascii').rstrip('\n')
+
+        if not len(self.dns_domainname):
+            self.dns_domainname = self.hostname
+
     def init(self):
 
         # Utility objects
diff --git a/base/server/python/pki/server/deployment/pkiconfig.py b/base/server/python/pki/server/deployment/pkiconfig.py
index 8cf830c1309b177b7015c41955569578f837383b..9e1cab57ca59ca2bcbaf52e38a51b7f6c308260f 100644
--- a/base/server/python/pki/server/deployment/pkiconfig.py
+++ b/base/server/python/pki/server/deployment/pkiconfig.py
@@ -93,12 +93,6 @@ PKI_DEPLOYMENT_DEFAULT_TOMCAT_HTTPS_PORT = 8443
 PKI_DEPLOYMENT_DEFAULT_TOMCAT_SERVER_PORT = 8005
 PKI_DEPLOYMENT_DEFAULT_TOMCAT_AJP_PORT = 8009
 
-# PKI Deployment Global Variables
-pki_architecture = None
-pki_hostname = None
-pki_dns_domainname = None
-
-
 # PKI Deployment Command-Line Variables
 pki_deployment_executable = None
 
diff --git a/base/server/python/pki/server/deployment/pkiparser.py b/base/server/python/pki/server/deployment/pkiparser.py
index 14a1a1e0d4a5be561dbc40c3b4145214d9b7bb10..15e48baf05f132b7ba9810ba40a59bbeb8ce981f 100644
--- a/base/server/python/pki/server/deployment/pkiparser.py
+++ b/base/server/python/pki/server/deployment/pkiparser.py
@@ -208,7 +208,7 @@ class PKIConfigParser:
             'pki_instance_name': default_instance_name,
             'pki_http_port': default_http_port,
             'pki_https_port': default_https_port,
-            'pki_dns_domainname': config.pki_dns_domainname,
+            'pki_dns_domainname': self.deployer.dns_domainname,
             'pki_subsystem': self.deployer.subsystem_name,
             'pki_subsystem_type': self.deployer.subsystem_name.lower(),
             'pki_root_prefix': config.pki_root_prefix,
@@ -216,7 +216,7 @@ class PKIConfigParser:
             'resteasy_lib': resteasy_lib,
             'jni_jar_dir': jni_jar_dir,
             'home_dir': os.path.expanduser("~"),
-            'pki_hostname': config.pki_hostname})
+            'pki_hostname': self.deployer.hostname})
 
         # Make keys case-sensitive!
         self.deployer.main_config.optionxform = str
@@ -574,7 +574,7 @@ class PKIConfigParser:
             self.mdict['pki_install_time'] = self.deployer.install_time
             self.mdict['pki_timestamp'] = self.deployer.log_timestamp
             self.mdict['pki_certificate_timestamp'] = self.deployer.certificate_timestamp
-            self.mdict['pki_architecture'] = config.pki_architecture
+            self.mdict['pki_architecture'] = self.deployer.architecture
             self.mdict['pki_default_deployment_cfg'] = \
                 config.default_deployment_cfg
             self.mdict['pki_user_deployment_cfg'] = config.user_deployment_cfg
@@ -1086,7 +1086,7 @@ class PKIConfigParser:
                 "+TLS_DHE_RSA_WITH_AES_128_CBC_SHA," + \
                 "+TLS_DHE_RSA_WITH_AES_256_CBC_SHA"
 
-            if config.pki_architecture == 64:
+            if self.deployer.architecture == 64:
                 self.mdict['NUXWDOG_JNI_PATH_SLOT'] = (
                     '/usr/lib64/nuxwdog-jni')
             else:
diff --git a/base/server/sbin/pkidestroy b/base/server/sbin/pkidestroy
index de7db951dbb35b209039839e90add05de792ccd6..4a0c6a0b8ae7830b164148f90e17838a6058219c 100755
--- a/base/server/sbin/pkidestroy
+++ b/base/server/sbin/pkidestroy
@@ -31,9 +31,6 @@ if not hasattr(sys, "hexversion") or sys.hexversion < 0x020700f0:
     sys.exit(1)
 try:
     import os
-    import socket
-    import struct
-    import subprocess
     import traceback
     import pki
     from pki.server.deployment import pkiconfig as config
@@ -70,25 +67,6 @@ def main(argv):
     # Set the umask
     os.umask(config.PKI_DEPLOYMENT_DEFAULT_UMASK)
 
-    # Obtain the architecture bit-size
-    config.pki_architecture = struct.calcsize("P") * 8
-
-    # Retrieve hostname
-    config.pki_hostname = socket.getfqdn()
-
-    # Retrieve DNS domainname
-    config.pki_dns_domainname = None
-    try:
-        dnsdomainname = subprocess.check_output(["dnsdomainname"])
-        dnsdomainname = dnsdomainname.decode('ascii').rstrip('\n')
-        config.pki_dns_domainname = dnsdomainname
-        if not len(config.pki_dns_domainname):
-            print(log.PKI_DNS_DOMAIN_NOT_SET)
-            config.pki_dns_domainname = config.pki_hostname
-    except subprocess.CalledProcessError as exc:
-        print(log.PKI_SUBPROCESS_ERROR_1 % exc)
-        sys.exit(1)
-
     # Read and process command-line arguments.
     parser = PKIConfigParser(
         'PKI Instance Removal',
diff --git a/base/server/sbin/pkispawn b/base/server/sbin/pkispawn
index 09b44741712eba7cd0b8329d65054a53d19b8661..e6e337b8d8c1f913f06617f8ae94aac55abab9cc 100755
--- a/base/server/sbin/pkispawn
+++ b/base/server/sbin/pkispawn
@@ -33,9 +33,6 @@ try:
     import ldap
     import os
     import requests
-    import socket
-    import struct
-    import subprocess
     import traceback
     import pki
     from pki.server.deployment import pkiconfig as config
@@ -73,24 +70,6 @@ def main(argv):
     # Set the umask
     os.umask(config.PKI_DEPLOYMENT_DEFAULT_UMASK)
 
-    # Obtain the architecture bit-size
-    config.pki_architecture = struct.calcsize("P") * 8
-
-    # Retrieve hostname
-    config.pki_hostname = socket.getfqdn()
-
-    # Retrieve DNS domainname
-    try:
-        dnsdomainname = subprocess.check_output(["dnsdomainname"])
-        dnsdomainname = dnsdomainname.decode('ascii').rstrip('\n')
-        config.pki_dns_domainname = dnsdomainname
-        if not len(config.pki_dns_domainname):
-            print(log.PKI_DNS_DOMAIN_NOT_SET)
-            config.pki_dns_domainname = config.pki_hostname
-    except subprocess.CalledProcessError as exc:
-        print(log.PKI_SUBPROCESS_ERROR_1 % exc)
-        sys.exit(1)
-
     # Read and process command-line arguments.
     parser = PKIConfigParser(
         'PKI Instance Installation and Configuration',
-- 
2.5.5

