Changeset 2825 for trunk/server/fedora


Ignore:
Timestamp:
Feb 23, 2017, 1:18:37 AM (5 years ago)
Author:
andersk
Message:
export-scripts-certs: use a lock file; reload Apache on changes
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/server/fedora/config/etc/httpd/export-scripts-certs

    r2821 r2825  
    22
    33import base64
     4import errno
     5import fcntl
    46import hashlib
    57import ldap
    68import os
     9import subprocess
    710import sys
    811import textwrap
     
    98101        yield '</VirtualHost>\n'
    99102
    100 with open(os.path.join(CERTS_DIR, 'vhosts.conf.new'), 'w') as vhosts_file:
    101     vhosts_file.write('# Generated by {}.  Manual changes will be lost.\n\n'.format(os.path.realpath(__file__)))
    102     vhosts_file.write(''.join(l for dn, vhost in vhosts for l in conf(vhost)))
    103 os.rename(os.path.join(CERTS_DIR, 'vhosts.conf.new'), os.path.join(CERTS_DIR, 'vhosts.conf'))
     103with open(os.path.join(CERTS_DIR, '.lock'), 'w') as lock_file:
     104    fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX)
    104105
    105 for filename in os.listdir(CERTS_DIR):
    106     if filename.endswith('.pem') and filename not in cert_filenames:
    107         os.remove(os.path.join(CERTS_DIR, filename))
     106    new_vhosts_conf = \
     107        '# Generated by {}.  Manual changes will be lost.\n\n'.format(os.path.realpath(__file__)) + \
     108        ''.join(l for dn, vhost in vhosts for l in conf(vhost))
     109
     110    try:
     111        with open(os.path.join(CERTS_DIR, 'vhosts.conf')) as vhosts_file:
     112            old_vhosts_conf = vhosts_file.read()
     113    except IOError as e:
     114        if e.errno == errno.ENOENT:
     115            old_vhosts_conf = None
     116        else:
     117            raise
     118
     119    if old_vhosts_conf is not None and new_vhosts_conf != old_vhosts_conf:
     120        with open(os.path.join(CERTS_DIR, 'vhosts.conf.new'), 'w') as new_vhosts_file:
     121            new_vhosts_file.write(new_vhosts_conf)
     122        os.rename(os.path.join(CERTS_DIR, 'vhosts.conf.new'), os.path.join(CERTS_DIR, 'vhosts.conf'))
     123
     124        configtest = subprocess.Popen(['apachectl', 'configtest'], stderr=subprocess.PIPE)
     125        e = configtest.communicate()[1]
     126        if configtest.returncode == 0 and e == 'Syntax OK\n':
     127            subprocess.check_call(['apachectl', 'graceful'])
     128        else:
     129            err('apachectl configtest failed:\n' + e)
     130
     131    for filename in os.listdir(CERTS_DIR):
     132        if filename.endswith('.pem') and filename not in cert_filenames:
     133            os.remove(os.path.join(CERTS_DIR, filename))
     134
     135    fcntl.flock(lock_file.fileno(), fcntl.LOCK_UN)
    108136
    109137sys.exit(1 if error else 0)
Note: See TracChangeset for help on using the changeset viewer.