mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-01-28 08:03:36 +00:00
To avoid Apache restart during installation. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
175 lines
5.1 KiB
Python
Executable File
175 lines
5.1 KiB
Python
Executable File
#!/usr/bin/python3
|
|
#
|
|
# This file is part of Plinth.
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
# License, or (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
#
|
|
"""
|
|
Configuration helper for searx.
|
|
"""
|
|
|
|
import argparse
|
|
import augeas
|
|
import os
|
|
import secrets
|
|
import shutil
|
|
|
|
import yaml
|
|
|
|
from plinth import action_utils
|
|
from plinth.utils import gunzip
|
|
|
|
SETTINGS_FILE = '/etc/searx/settings.yml'
|
|
|
|
UWSGI_FILE = '/etc/uwsgi/apps-available/searx.ini'
|
|
|
|
|
|
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('enable', help='Enable Searx')
|
|
subparsers.add_parser('disable', help='Disable Searx')
|
|
subparsers.add_parser(
|
|
'setup', help='Perform post-installation operations for Searx')
|
|
|
|
safe_search = subparsers.add_parser(
|
|
'set-safe-search',
|
|
help='Set the default filter for safe search on Searx')
|
|
safe_search.add_argument(
|
|
'filter', type=int,
|
|
help='Filter results. 0: None, 1: Moderate, 2: Strict')
|
|
|
|
subparsers.add_parser('get-safe-search',
|
|
help='Print the value of the safe search setting.')
|
|
|
|
subparsers.required = True
|
|
return parser.parse_args()
|
|
|
|
|
|
def _copy_uwsgi_configuration():
|
|
"""Copy example uwsgi configuration
|
|
|
|
Copy the example uwsgi configuration shipped with Searx documentation to
|
|
the appropriate uwsgi directory.
|
|
"""
|
|
example_config = ('/usr/share/doc/searx/examples/'
|
|
'uwsgi/apps-available/searx.ini')
|
|
if not os.path.exists(UWSGI_FILE):
|
|
shutil.copy(example_config, os.path.dirname(UWSGI_FILE))
|
|
|
|
|
|
def _update_uwsgi_configuration():
|
|
"""Fix uwsgi configuration.
|
|
|
|
uwsgi 2.0.15-debian crashes when trying to autoload.
|
|
"""
|
|
aug = augeas.Augeas(
|
|
flags=augeas.Augeas.NO_LOAD + augeas.Augeas.NO_MODL_AUTOLOAD)
|
|
aug.set('/augeas/load/inifile/lens', 'Puppet.lns')
|
|
aug.set('/augeas/load/inifile/incl[last() + 1]', UWSGI_FILE)
|
|
aug.load()
|
|
aug.set('/files/etc/uwsgi/apps-available/searx.ini/uwsgi/autoload', 'false')
|
|
aug.save()
|
|
|
|
|
|
def _generate_secret_key(settings):
|
|
"""Generate a secret key for the Searx installation."""
|
|
secret_key = secrets.token_hex(64)
|
|
settings['server']['secret_key'] = secret_key
|
|
|
|
|
|
def _set_title(settings):
|
|
"""Set the page title to '{box_name} Web Search'."""
|
|
title = 'FreedomBox Web Search'
|
|
settings['general']['instance_name'] = title
|
|
|
|
|
|
def _set_timeout(settings):
|
|
"""Set timeout to 20 seconds."""
|
|
settings['outgoing']['request_timeout'] = 20.0
|
|
|
|
|
|
def subcommand_set_safe_search(arguments):
|
|
"""Set safe search filter for search results."""
|
|
value = arguments.filter
|
|
settings = read_settings()
|
|
settings['search']['safe_search'] = value
|
|
write_settings(settings)
|
|
|
|
|
|
def subcommand_get_safe_search(_):
|
|
"""Print the value of the safe search setting."""
|
|
if os.path.exists(SETTINGS_FILE):
|
|
settings = read_settings()
|
|
print(settings['search']['safe_search'])
|
|
else:
|
|
print(0)
|
|
|
|
|
|
def read_settings():
|
|
"""Load settings as dictionary from YAML config file."""
|
|
with open(SETTINGS_FILE, 'rb') as settings_file:
|
|
return yaml.load(settings_file)
|
|
|
|
|
|
def write_settings(settings):
|
|
"""Write settings from dictionary to YAML config file."""
|
|
with open(SETTINGS_FILE, 'w') as settings_file:
|
|
yaml.dump(settings, settings_file)
|
|
|
|
|
|
def subcommand_setup(_):
|
|
"""Post installation actions for Searx"""
|
|
_copy_uwsgi_configuration()
|
|
_update_uwsgi_configuration()
|
|
|
|
if not os.path.exists(SETTINGS_FILE):
|
|
example_settings_file = '/usr/share/doc/searx/examples/settings.yml.gz'
|
|
gunzip(example_settings_file, SETTINGS_FILE)
|
|
|
|
settings = read_settings()
|
|
_generate_secret_key(settings)
|
|
_set_title(settings)
|
|
_set_timeout(settings)
|
|
write_settings(settings)
|
|
|
|
action_utils.service_restart('uwsgi')
|
|
|
|
|
|
def subcommand_enable(_):
|
|
"""Enable web configuration and reload."""
|
|
action_utils.uwsgi_enable('searx')
|
|
action_utils.webserver_enable('searx-freedombox')
|
|
|
|
|
|
def subcommand_disable(_):
|
|
"""Disable web configuration and reload."""
|
|
action_utils.webserver_disable('searx-freedombox')
|
|
action_utils.uwsgi_disable('searx')
|
|
|
|
|
|
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()
|