FreedomBox/actions/minidlna
James Valleroy 3c85846fa2
minidlna: Move sysctl config to /etc/sysctl.d/50-freedombox.conf
/etc/sysctl.conf is owned by procps package.

Test: Run minidlna install on fresh image.
- /etc/sysctl.conf is not modified.
- /etc/sysctl.d/50-freedombox.conf has the expected content.
- /proc/sys/fs/inotify/max_user_watches contains 100000.

- Running with these changes upgrades app version and triggers a setup. Changes
in /etc/sysctl.conf are removed. After undoing the changes /etc/sysctl.conf is
identical to pristine version installed from procps package. This can be
obtained by running; rm -f /etc/sysctl.conf ; apt install --reinstall procps -o
Dpkg::Options::=--force-confmiss

Closes #1802.

Signed-off-by: James Valleroy <jvalleroy@mailbox.org>
[sunil: Undo changes done in /etc/sysctl.conf in older versions]
[sunil: Increment app version to trigger configuration migration]
[sunil: Ensure that app is not re-enabled during migration]
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2020-05-18 08:49:24 -04:00

120 lines
3.5 KiB
Python
Executable File

#!/usr/bin/python3
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Configuration actions for the minidlna server.
"""
import argparse
from tempfile import mkstemp
from shutil import move
import subprocess
from os import fdopen, remove, chmod, stat
import augeas
from plinth import action_utils
from plinth.utils import grep
CONFIG_PATH = '/etc/minidlna.conf'
SYSCTL_CONF = '''# This file is managed and overwritten by FreedomBox.
# Helps minidlna monitor changes in large media directories
fs.inotify.max_user_watches = 100000
'''
def parse_arguments():
"""Return parsed command line arguments as dictionary."""
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
subparsers.add_parser('setup', help='Setup SSH server')
subparsers.add_parser('get-media-dir', help='Get media directory')
set_media_dir = subparsers.add_parser('set-media-dir',
help='Set custom media directory')
set_media_dir.add_argument('--dir')
subparsers.required = True
return parser.parse_args()
def _undo_old_configuration_changes():
"""Restore /etc/sysctl.conf to before our changes.
Older version of minidlna app in FreedomBox < 20.9 wrote to
/etc/sysctl.conf directly. This will cause conffile prompt during upgrade
of procps package. Undo the changes so that upgrade can happen smoothly.
"""
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
augeas.Augeas.NO_MODL_AUTOLOAD)
aug.set('/augeas/load/Sysctl/lens', 'Sysctl.lns')
aug.set('/augeas/load/Sysctl/incl[last() + 1]', '/etc/sysctl.conf')
aug.load()
key_path = '/files/etc/sysctl.conf/fs.inotify.max_user_watches'
if aug.get(key_path) == '100000':
aug.remove(key_path)
aug.save()
def subcommand_setup(arguments):
"""
Increase inotify watches per folder to allow minidlna to
monitor changes in large media-dirs.
"""
_undo_old_configuration_changes()
with open('/etc/sysctl.d/50-freedombox-minidlna.conf', 'w') as conf:
conf.write(SYSCTL_CONF)
subprocess.run(['systemctl', 'restart', 'systemd-sysctl'], check=True)
def subcommand_get_media_dir(arguments):
"""Retrieve media directory from minidlna.conf"""
line = grep('^media_dir=', CONFIG_PATH)
print(line[0].split("=")[1])
def subcommand_set_media_dir(arguments):
"""Set media directory in minidlna.conf"""
line = grep('^media_dir=', CONFIG_PATH)[0]
new_line = 'media_dir=%s\n' % arguments.dir
replace_in_config_file(CONFIG_PATH, line, new_line)
if action_utils.service_is_running('minidlna'):
action_utils.service_restart('minidlna')
def replace_in_config_file(file_path, pattern, subst):
"""
Create a temporary minidlna.conf file,
replace the media dir config,
remove original one and move the temporary file.
Preserve permissions as the original file.
"""
temp_file, temp_file_path = mkstemp()
with fdopen(temp_file, 'w') as new_file:
with open(file_path) as old_file:
for line in old_file:
new_file.write(line.replace(pattern, subst))
old_st_mode = stat(file_path).st_mode
remove(file_path)
move(temp_file_path, file_path)
chmod(file_path, old_st_mode)
def main():
"""Parse arguments and perform all duties."""
arguments = parse_arguments()
subcommand = arguments.subcommand.replace('-', '_')
subcommand_method = globals()['subcommand_' + subcommand]
subcommand_method(arguments)
if __name__ == '__main__':
main()