Configure users module packages after installation

Preseeding settings with debconf won't have any effect if the packages
are already installed. Instead, provide an override database to
dpkg-reconfigure.
This commit is contained in:
James Valleroy 2016-06-14 18:25:59 -04:00 committed by Sunil Mohan Adapa
parent 438d5850f2
commit 956416ef37
No known key found for this signature in database
GPG Key ID: 36C361440C9BC971
2 changed files with 30 additions and 33 deletions

View File

@ -22,6 +22,7 @@ Configuration helper for the LDAP user directory
"""
import argparse
import os
import subprocess
import augeas
@ -36,43 +37,11 @@ def parse_arguments():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
subparsers.add_parser(
'pre-install',
help='Preseed debconf values before packages are installed')
subparsers.add_parser('setup', help='Setup LDAP')
return parser.parse_args()
def subcommand_pre_install(_):
"""Preseed debconf values before packages are installed."""
subprocess.run(
['debconf-set-selections'],
input=b'slapd slapd/domain string thisbox',
check=True)
subprocess.run(
['debconf-set-selections'],
input=b'nslcd nslcd/ldap-uris string ldapi:///',
check=True)
subprocess.run(
['debconf-set-selections'],
input=b'nslcd nslcd/ldap-base string dc=thisbox',
check=True)
subprocess.run(
['debconf-set-selections'],
input=b'nslcd nslcd/ldap-auth-type select SASL',
check=True)
subprocess.run(
['debconf-set-selections'],
input=b'nslcd nslcd/ldap-sasl-mech select EXTERNAL',
check=True)
subprocess.run(
['debconf-set-selections'],
input=b'libnss-ldapd libnss-ldapd/nsswitch multiselect '
b'group, passwd, shadow',
check=True)
def subcommand_setup(_):
"""Setup LDAP."""
# Update pam configs for access and mkhomedir.
@ -85,6 +54,13 @@ def subcommand_setup(_):
def configure_slapd():
"""Configure LDAP authentication and basic structure."""
_reconfigure('slapd', {'domain': 'thisbox'})
_reconfigure('nslcd', {'ldap-uris': 'ldapi:///',
'ldap-base': 'dc=thisbox',
'ldap-auth-type': 'SASL',
'ldap-sasl-mech': 'EXTERNAL'})
_reconfigure('libnss-ldapd', {'nsswitch': 'group, passwd, shadow'})
was_running = action_utils.service_is_running('slapd')
if not was_running:
action_utils.service_start('slapd')
@ -170,6 +146,28 @@ def configure_ldapscripts():
aug.save()
def _reconfigure(package, config):
"""Reconfigure package using debconf database override."""
override_template = '''
Name: {package}/{key}
Template: {package}/{key}
Value: {value}
Owners: {package}
'''
override_data = ''
for key, value in config.items():
override_data += override_template.format(
package=package, key=key, value=value)
with open('/tmp/override.dat', 'w') as override_file:
override_file.write(override_data)
env = os.environ.copy()
env['DEBCONF_DB_OVERRIDE'] = 'File{/tmp/override.dat readonly:true}'
env['DEBIAN_FRONTEND'] = 'noninteractive'
subprocess.run(['dpkg-reconfigure', package], env=env)
def main():
"""Parse arguments and perform all duties"""
arguments = parse_arguments()

View File

@ -46,7 +46,6 @@ def init():
def setup(helper, old_version=None):
"""Install and configure the module."""
helper.call('pre', actions.superuser_run, 'users', ['pre-install'])
helper.install(managed_packages)
helper.call('post', actions.superuser_run, 'users', ['setup'])