mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-01-21 07:55:00 +00:00
users: Move dpkg reconfigure to action utils
In the hope that it will be useful to other actions. Also delete the named temporary file after configuration.
This commit is contained in:
parent
6c3d3888c1
commit
8e28afe160
@ -22,9 +22,7 @@ Configuration helper for the LDAP user directory
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
import augeas
|
||||
from plinth import action_utils
|
||||
@ -55,12 +53,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'})
|
||||
action_utils.dpkg_reconfigure('slapd', {'domain': 'thisbox'})
|
||||
action_utils.dpkg_reconfigure('nslcd', {'ldap-uris': 'ldapi:///',
|
||||
'ldap-base': 'dc=thisbox',
|
||||
'ldap-auth-type': 'SASL',
|
||||
'ldap-sasl-mech': 'EXTERNAL'})
|
||||
action_utils.dpkg_reconfigure('libnss-ldapd',
|
||||
{'nsswitch': 'group, passwd, shadow'})
|
||||
|
||||
was_running = action_utils.service_is_running('slapd')
|
||||
if not was_running:
|
||||
@ -147,29 +146,6 @@ 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 tempfile.NamedTemporaryFile(mode='w', delete=False) as override_file:
|
||||
override_file.write(override_data)
|
||||
|
||||
env = os.environ.copy()
|
||||
env['DEBCONF_DB_OVERRIDE'] = 'File{' + override_file.name + \
|
||||
' readonly:true}'
|
||||
env['DEBIAN_FRONTEND'] = 'noninteractive'
|
||||
subprocess.run(['dpkg-reconfigure', package], env=env)
|
||||
|
||||
|
||||
def main():
|
||||
"""Parse arguments and perform all duties"""
|
||||
arguments = parse_arguments()
|
||||
|
||||
@ -26,6 +26,7 @@ import logging
|
||||
import psutil
|
||||
import socket
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -417,3 +418,31 @@ def get_ip_addresses():
|
||||
def get_hostname():
|
||||
"""Return the current hostname."""
|
||||
return subprocess.check_output(['hostname']).decode().strip()
|
||||
|
||||
|
||||
def dpkg_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 tempfile.NamedTemporaryFile(mode='w', delete=False) as override_file:
|
||||
override_file.write(override_data)
|
||||
|
||||
env = os.environ.copy()
|
||||
env['DEBCONF_DB_OVERRIDE'] = 'File{' + override_file.name + \
|
||||
' readonly:true}'
|
||||
env['DEBIAN_FRONTEND'] = 'noninteractive'
|
||||
subprocess.run(['dpkg-reconfigure', package], env=env)
|
||||
|
||||
try:
|
||||
os.remove(override_file)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user