Skip to content

Commit

Permalink
Add exist_ok param for PKIServer.copy()
Browse files Browse the repository at this point in the history
  • Loading branch information
edewata committed Apr 22, 2024
1 parent c90ce15 commit ef5c1ed
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 36 deletions.
43 changes: 30 additions & 13 deletions base/server/python/pki/server/__init__.py
Expand Up @@ -666,9 +666,13 @@ def symlink(self, source, dest, exist_ok=False):
gid=self.gid,
exist_ok=exist_ok)

def copy(self, source, dest, force=False):
def copy(self, source, dest, exist_ok=False, force=False):

logger.info('Creating %s', dest)
if os.path.exists(dest) and exist_ok:
logger.info('Reusing %s', dest)
return

logger.info('Copying %s to %s', source, dest)

pki.util.copy(
source,
Expand Down Expand Up @@ -727,7 +731,11 @@ def create(self, force=False):
self.makedirs(self.certs_dir, exist_ok=True)

catalina_policy = os.path.join(Tomcat.CONF_DIR, 'catalina.policy')
self.copy(catalina_policy, self.catalina_policy, force=force)
self.copy(
catalina_policy,
self.catalina_policy,
exist_ok=True,
force=force)

catalina_properties = os.path.join(
PKIServer.SHARE_DIR, 'server', 'conf', 'catalina.properties')
Expand All @@ -740,7 +748,11 @@ def create(self, force=False):
self.create_server_xml()

# copy /etc/tomcat/tomcat.conf
self.copy(Tomcat.TOMCAT_CONF, self.tomcat_conf, force=force)
self.copy(
Tomcat.TOMCAT_CONF,
self.tomcat_conf,
exist_ok=True,
force=force)

tomcat_conf = pki.PropertyFile(self.tomcat_conf, quote='"')
tomcat_conf.read()
Expand Down Expand Up @@ -780,7 +792,11 @@ def create(self, force=False):
self.symlink(target, link, exist_ok=True)

service_conf = os.path.join(SYSCONFIG_DIR, 'tomcat')
self.copy(service_conf, self.service_conf, force=force)
self.copy(
service_conf,
self.service_conf,
exist_ok=True,
force=force)

with open(self.service_conf, 'a', encoding='utf-8') as f:
print('CATALINA_BASE="%s"' % self.base_dir, file=f)
Expand Down Expand Up @@ -818,17 +834,18 @@ def create_logs_dir(self, exist_ok=False):
def create_logging_properties(self, force=False):

logging_properties = os.path.join(Tomcat.CONF_DIR, 'logging.properties')
self.copy(logging_properties, self.logging_properties, force=force)
self.copy(
logging_properties,
self.logging_properties,
exist_ok=True,
force=force)

def create_server_xml(self):

if os.path.exists(self.server_xml):
logger.info('Updating %s', self.server_xml)
else:
logger.info('Creating %s', self.server_xml)
self.copy(
pki.server.Tomcat.SERVER_XML,
self.server_xml)
self.copy(
pki.server.Tomcat.SERVER_XML,
self.server_xml,
exist_ok=True)

server_config = self.get_server_config()

Expand Down
18 changes: 15 additions & 3 deletions base/server/python/pki/server/cli/acme.py
Expand Up @@ -137,15 +137,27 @@ def execute(self, argv):

database_template = os.path.join(acme_share_dir, 'conf', 'database.conf')
database_conf = os.path.join(acme_conf_dir, 'database.conf')
instance.copy(database_template, database_conf, force=force)
instance.copy(
database_template,
database_conf,
exist_ok=True,
force=force)

issuer_template = os.path.join(acme_share_dir, 'conf', 'issuer.conf')
issuer_conf = os.path.join(acme_conf_dir, 'issuer.conf')
instance.copy(issuer_template, issuer_conf, force=force)
instance.copy(
issuer_template,
issuer_conf,
exist_ok=True,
force=force)

realm_template = os.path.join(acme_share_dir, 'conf', 'realm.conf')
realm_conf = os.path.join(acme_conf_dir, 'realm.conf')
instance.copy(realm_template, realm_conf, force=force)
instance.copy(
realm_template,
realm_conf,
exist_ok=True,
force=force)


class ACMERemoveCLI(pki.cli.CLI):
Expand Down
11 changes: 4 additions & 7 deletions base/server/python/pki/server/deployment/__init__.py
Expand Up @@ -261,13 +261,10 @@ def flatten_master_dict(self):

def configure_server_xml(self):

if os.path.exists(self.instance.server_xml):
logger.info('Updating %s', self.instance.server_xml)
else:
logger.info('Creating %s', self.instance.server_xml)
self.instance.copy(
pki.server.Tomcat.SERVER_XML,
self.instance.server_xml)
self.instance.copy(
pki.server.Tomcat.SERVER_XML,
self.instance.server_xml,
exist_ok=True)

server_config = self.instance.get_server_config()

Expand Down
Expand Up @@ -91,7 +91,8 @@ def spawn(self, deployer):

instance.copy(
pki_source_registry_cfg,
pki_target_registry_cfg)
pki_target_registry_cfg,
exist_ok=True)

if deployer.subsystem_type == "CA":

Expand All @@ -103,9 +104,12 @@ def spawn(self, deployer):
subsystem_name,
'emails')

pki_dest_emails = os.path.join(subsystem.conf_dir, 'emails')

instance.copy(
pki_source_emails,
subsystem.conf_dir + '/emails')
pki_dest_emails,
exist_ok=True)

# Link /var/lib/pki/<instance>/ca/emails
# to /var/lib/pki/<instance>/conf/ca/emails
Expand All @@ -122,9 +126,12 @@ def spawn(self, deployer):
subsystem_name,
'profiles')

pki_dest_profiles = os.path.join(subsystem.conf_dir, 'profiles')

instance.copy(
pki_source_profiles,
subsystem.conf_dir + '/profiles')
pki_dest_profiles,
exist_ok=True)

# Link /var/lib/pki/<instance>/ca/profiles
# to /var/lib/pki/<instance>/conf/ca/profiles
Expand All @@ -147,7 +154,8 @@ def spawn(self, deployer):

instance.copy(
pki_source_flatfile_txt,
pki_target_flatfile_txt)
pki_target_flatfile_txt,
exist_ok=True)

# Copy /usr/share/pki/<subsystem>/conf/<type>AdminCert.profile
# to /var/lib/pki/<instance>/conf/<subsystem>/adminCert.profile
Expand All @@ -166,7 +174,8 @@ def spawn(self, deployer):

instance.copy(
pki_source_admincert_profile,
pki_target_admincert_profile)
pki_target_admincert_profile,
exist_ok=True)

# Copy /usr/share/pki/<subsystem>/conf/caAuditSigningCert.profile
# to /var/lib/pki/<instance>/conf/<subsystem>/caAuditSigningCert.profile
Expand All @@ -183,7 +192,8 @@ def spawn(self, deployer):

instance.copy(
pki_source_caauditsigningcert_profile,
pki_target_caauditsigningcert_profile)
pki_target_caauditsigningcert_profile,
exist_ok=True)

# Copy /usr/share/pki/<subsystem>/conf/caCert.profile
# to /var/lib/pki/<instance>/conf/<subsystem>/caCert.profile
Expand All @@ -200,7 +210,8 @@ def spawn(self, deployer):

instance.copy(
pki_source_cacert_profile,
pki_target_cacert_profile)
pki_target_cacert_profile,
exist_ok=True)

# Copy /usr/share/pki/<subsystem>/conf/caOCSPCert.profile
# to /var/lib/pki/<instance>/conf/<subsystem>/caOCSPCert.profile
Expand All @@ -217,7 +228,8 @@ def spawn(self, deployer):

instance.copy(
pki_source_caocspcert_profile,
pki_target_caocspcert_profile)
pki_target_caocspcert_profile,
exist_ok=True)

# Copy /usr/share/pki/<subsystem>/conf/<type>ServerCert.profile
# to /var/lib/pki/<instance>/conf/<subsystem>/serverCert.profile
Expand All @@ -236,7 +248,8 @@ def spawn(self, deployer):

instance.copy(
pki_source_servercert_profile,
pki_target_servercert_profile)
pki_target_servercert_profile,
exist_ok=True)

# Copy /usr/share/pki/<subsystem>/conf/<type>SubsystemCert.profile
# to /var/lib/pki/<instance>/conf/<subsystem>/subsystemCert.profile
Expand All @@ -255,7 +268,8 @@ def spawn(self, deployer):

instance.copy(
pki_source_subsystemcert_profile,
pki_target_subsystemcert_profile)
pki_target_subsystemcert_profile,
exist_ok=True)

# Copy /usr/share/pki/<subsystem>/conf/proxy.conf
# to /var/lib/pki/<instance>/conf/<subsystem>/proxy.conf
Expand Down
2 changes: 1 addition & 1 deletion base/server/python/pki/server/instance.py
Expand Up @@ -325,7 +325,7 @@ def create_libs(self, force=False):
dest = os.path.join(self.common_lib_dir, filename)

logger.info('Copying %s to %s', source, dest)
self.copy(source, dest, force=force)
self.copy(source, dest, exist_ok=True, force=force)

common_lib_dir = os.path.join(pki.server.PKIServer.SHARE_DIR, 'server', 'common', 'lib')

Expand Down
10 changes: 8 additions & 2 deletions base/server/python/pki/server/subsystem.py
Expand Up @@ -209,7 +209,10 @@ def create_conf(self, exist_ok=False):
'conf',
'registry.cfg')

self.instance.copy(registry_conf, self.registry_conf)
self.instance.copy(
registry_conf,
self.registry_conf,
exist_ok=True)

def create_logs(self, exist_ok=False):

Expand Down Expand Up @@ -247,7 +250,10 @@ def create_registry(self, exist_ok=False):
'etc',
'default.cfg')

self.instance.copy(default_cfg, self.default_cfg)
self.instance.copy(
default_cfg,
self.default_cfg,
exist_ok=True)

def load(self):

Expand Down

0 comments on commit ef5c1ed

Please sign in to comment.