Skip to content

Commit

Permalink
Move PKIServerFactory into pki.server
Browse files Browse the repository at this point in the history
  • Loading branch information
edewata committed Dec 14, 2023
1 parent 0c7cade commit b0df74e
Show file tree
Hide file tree
Showing 35 changed files with 200 additions and 228 deletions.
49 changes: 49 additions & 0 deletions base/server/python/pki/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2056,3 +2056,52 @@ def __init__(self, message, exception=None,

self.instance = instance
self.subsystem = subsystem


class PKIServerFactory(object):

@classmethod
def create(cls, name):
'''
This method creates PKIServer object based on the
optional service type specified in the service name.
The default type is 'pki-tomcatd'.
:param name: Server name in this format: [<type>@]<name>[.service]
'''

if name.endswith('.service'):
name = name[0:-8]

parts = name.split('@')

if len(parts) == 1: # no type
instance_type = 'pki-tomcatd'
instance_name = name

else: # with type
instance_type = parts[0]
instance_name = parts[1]

sysconfig_file = os.path.join('/etc/sysconfig', instance_name)

if os.path.isfile(sysconfig_file):

with open(sysconfig_file, encoding='utf-8') as f:
nuxwdog_status = re.search('^USE_NUXWDOG=\"(.*)\"', f.read(), re.MULTILINE)

# Check if the regex was matched and then check if nuxwdog is enabled.
if nuxwdog_status and nuxwdog_status.group(1) == "true":
instance_type += '-nuxwdog'

logger.info('Loading instance type: %s', instance_type)

if instance_type == 'tomcat':
return pki.server.PKIServer(instance_name)

if instance_type.startswith('pki-tomcatd'):
module = __import__('pki.server.instance', fromlist=['PKIInstance'])
clazz = getattr(module, 'PKIInstance')
return clazz(instance_name, instance_type=instance_type)

raise Exception('Unsupported instance type: %s' % instance_type)
15 changes: 7 additions & 8 deletions base/server/python/pki/server/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import pki.server.cli.tps
import pki.server.cli.upgrade
import pki.server.cli.webapp
import pki.server.instance
import pki.util

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -369,7 +368,7 @@ def execute(self, argv):
if len(args) > 0:
instance_name = args[0]

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)

if not force and instance.exists():
logger.error('Instance already exists: %s', instance_name)
Expand Down Expand Up @@ -439,7 +438,7 @@ def execute(self, argv):
if len(args) > 0:
instance_name = args[0]

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)

if not force and not instance.exists():
logger.error('Invalid instance: %s', instance_name)
Expand Down Expand Up @@ -495,7 +494,7 @@ def execute(self, argv):
if len(args) > 0:
instance_name = args[0]

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)

if not instance.exists():
logger.error('Invalid instance: %s', instance_name)
Expand Down Expand Up @@ -567,7 +566,7 @@ def execute(self, argv):
if len(args) > 0:
instance_name = args[0]

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)

if not instance.exists():
logger.error('Invalid instance: %s', instance_name)
Expand Down Expand Up @@ -641,7 +640,7 @@ def execute(self, argv):
if len(args) > 0:
instance_name = args[0]

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)

if not instance.exists():
logger.error('Invalid instance: %s', instance_name)
Expand Down Expand Up @@ -715,7 +714,7 @@ def execute(self, argv):
if len(args) > 0:
instance_name = args[0]

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)

if not instance.exists():
logger.error('Invalid instance: %s', instance_name)
Expand Down Expand Up @@ -797,7 +796,7 @@ def execute(self, argv):
if len(args) > 0:
instance_name = args[0]

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)

if not instance.exists():
logger.error('Invalid instance: %s', instance_name)
Expand Down
25 changes: 12 additions & 13 deletions base/server/python/pki/server/cli/acme.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import pki.cli
import pki.server
import pki.server.instance
import pki.server.cli.subsystem

# TODO: auto-populate this map from /usr/share/pki/acme/database
Expand Down Expand Up @@ -124,7 +123,7 @@ def execute(self, argv):
if len(args) > 0:
name = args[0]

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)

if not instance.exists():
raise Exception('Invalid instance: %s' % instance_name)
Expand Down Expand Up @@ -206,7 +205,7 @@ def execute(self, argv):
if len(args) > 0:
name = args[0]

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)

if not instance.exists():
raise Exception('Invalid instance: %s' % instance_name)
Expand Down Expand Up @@ -285,7 +284,7 @@ def execute(self, argv):
if len(args) > 0:
name = args[0]

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)

if not instance.exists():
raise Exception('Invalid instance: %s' % instance_name)
Expand Down Expand Up @@ -374,7 +373,7 @@ def execute(self, argv):
if len(args) > 0:
name = args[0]

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)

if not instance.exists():
raise Exception('Invalid instance: %s' % instance_name)
Expand Down Expand Up @@ -445,7 +444,7 @@ def execute(self, argv):
self.print_help()
sys.exit(1)

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)

if not instance.exists():
raise Exception('Invalid instance: %s' % instance_name)
Expand Down Expand Up @@ -528,7 +527,7 @@ def execute(self, argv):
self.print_help()
sys.exit(1)

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)

if not instance.exists():
raise Exception('Invalid instance: %s' % instance_name)
Expand Down Expand Up @@ -636,7 +635,7 @@ def execute(self, argv):
self.print_help()
sys.exit(1)

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)

if not instance.exists():
raise Exception('Invalid instance: %s' % instance_name)
Expand Down Expand Up @@ -768,7 +767,7 @@ def execute(self, argv):
self.print_help()
sys.exit(1)

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)

if not instance.exists():
raise Exception('Invalid instance: %s' % instance_name)
Expand Down Expand Up @@ -972,7 +971,7 @@ def execute(self, argv):
self.print_help()
sys.exit(1)

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)

if not instance.exists():
raise Exception('Invalid instance: %s' % instance_name)
Expand Down Expand Up @@ -1091,7 +1090,7 @@ def execute(self, argv):
self.print_help()
sys.exit(1)

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)

if not instance.exists():
raise Exception('Invalid instance: %s' % instance_name)
Expand Down Expand Up @@ -1273,7 +1272,7 @@ def execute(self, argv):
self.print_help()
sys.exit(1)

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)

if not instance.exists():
raise Exception('Invalid instance: %s' % instance_name)
Expand Down Expand Up @@ -1415,7 +1414,7 @@ def execute(self, argv):
self.print_help()
sys.exit(1)

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)

if not instance.exists():
raise Exception('Invalid instance: %s' % instance_name)
Expand Down
19 changes: 9 additions & 10 deletions base/server/python/pki/server/cli/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import tempfile

import pki.cli
import pki.server.instance

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -136,7 +135,7 @@ def execute(self, argv):
self.print_help()
sys.exit(1)

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)
if not instance.exists():
logger.error('Invalid instance %s.', instance_name)
sys.exit(1)
Expand Down Expand Up @@ -264,7 +263,7 @@ def execute(self, argv):
self.print_help()
sys.exit(1)

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)
if not instance.exists():
logger.error('Invalid instance %s.', instance_name)
sys.exit(1)
Expand Down Expand Up @@ -387,7 +386,7 @@ def execute(self, argv):
self.print_help()
sys.exit(1)

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)
if not instance.exists():
logger.error('Invalid instance %s.', instance_name)
sys.exit(1)
Expand Down Expand Up @@ -475,7 +474,7 @@ def execute(self, argv):

event_name = args[0]

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)
if not instance.exists():
logger.error('Invalid instance %s.', instance_name)
sys.exit(1)
Expand Down Expand Up @@ -550,7 +549,7 @@ def execute(self, argv):
raise getopt.GetoptError("Too many arguments specified.")
event_name = args[0]

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)
if not instance.exists():
logger.error('Invalid instance %s.', instance_name)
sys.exit(1)
Expand Down Expand Up @@ -644,7 +643,7 @@ def execute(self, argv):

event_name = args[0]

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)
if not instance.exists():
logger.error('Invalid instance %s.', instance_name)
sys.exit(1)
Expand Down Expand Up @@ -722,7 +721,7 @@ def execute(self, argv):

event_name = args[0]

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)
if not instance.exists():
logger.error('Invalid instance %s.', instance_name)
sys.exit(1)
Expand Down Expand Up @@ -803,7 +802,7 @@ def execute(self, argv):
self.print_help()
sys.exit(1)

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)
if not instance.exists():
logger.error('Invalid instance %s.', instance_name)
sys.exit(1)
Expand Down Expand Up @@ -880,7 +879,7 @@ def execute(self, argv):
self.print_help()
sys.exit(1)

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)
if not instance.exists():
logger.error('Invalid instance %s.', instance_name)
sys.exit(1)
Expand Down
5 changes: 2 additions & 3 deletions base/server/python/pki/server/cli/banner.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import sys

import pki.cli
import pki.server.instance

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -88,7 +87,7 @@ def execute(self, argv):
self.usage()
sys.exit(1)

instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)

if not instance.exists():
logger.error('Invalid instance %s.', instance_name)
Expand Down Expand Up @@ -168,7 +167,7 @@ def execute(self, argv):
else:

# load banner from instance
instance = pki.server.instance.PKIServerFactory.create(instance_name)
instance = pki.server.PKIServerFactory.create(instance_name)

if not instance.exists():
logger.error('Invalid instance %s.', instance_name)
Expand Down
Loading

0 comments on commit b0df74e

Please sign in to comment.