mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-03-11 09:04:54 +00:00
Signed-off-by: Joseph Nuthalapati <njoseph@thoughtworks.com> Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
119 lines
3.6 KiB
Python
Executable File
119 lines
3.6 KiB
Python
Executable File
#!/usr/bin/python3
|
|
# -*- mode: python -*-
|
|
#
|
|
# 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 os
|
|
import secrets
|
|
import shutil
|
|
|
|
from plinth import action_utils, cfg
|
|
from plinth.utils import YAMLFile, gunzip
|
|
|
|
SETTINGS_FILE = '/etc/searx/settings.yml'
|
|
|
|
|
|
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")
|
|
|
|
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')
|
|
destination = '/etc/uwsgi/apps-available/'
|
|
|
|
if not os.path.exists(os.path.join(destination, 'searx.ini')):
|
|
shutil.copy(example_config, destination)
|
|
action_utils.webserver_enable('uwsgi', kind='module')
|
|
|
|
|
|
def _generate_secret_key():
|
|
""" Generate a secret key for the Searx installation."""
|
|
|
|
if not os.path.exists(SETTINGS_FILE):
|
|
example_settings_file = '/usr/share/doc/searx/examples/settings.yml.gz'
|
|
gunzip(example_settings_file, SETTINGS_FILE)
|
|
|
|
# Generate and set a secret key
|
|
with YAMLFile(SETTINGS_FILE) as settings:
|
|
secret_key = secrets.token_hex(32)
|
|
settings['server']['secret_key'] = secret_key
|
|
|
|
action_utils.service_restart('uwsgi')
|
|
|
|
|
|
def _set_title():
|
|
"""Set the page title to '{box_name} Web Search'."""
|
|
with YAMLFile(SETTINGS_FILE) as settings:
|
|
title = 'FreedomBox Web Search'
|
|
settings['general']['instance_name'] = title
|
|
|
|
action_utils.service_restart('uwsgi')
|
|
|
|
|
|
def subcommand_setup(_):
|
|
"""Post installation actions for Searx"""
|
|
_copy_uwsgi_configuration()
|
|
_generate_secret_key()
|
|
_set_title()
|
|
|
|
|
|
def subcommand_enable(_):
|
|
"""Enable web configuration and reload."""
|
|
if not os.path.exists('/etc/uwsgi/apps-enabled/searx.ini'):
|
|
os.symlink('/etc/uwsgi/apps-available/searx.ini',
|
|
'/etc/uwsgi/apps-enabled/searx.ini')
|
|
action_utils.webserver_enable('searx-freedombox')
|
|
|
|
|
|
def subcommand_disable(_):
|
|
"""Disable web configuration and reload."""
|
|
action_utils.webserver_disable('searx-freedombox')
|
|
os.unlink('/etc/uwsgi/apps-enabled/searx.ini')
|
|
|
|
|
|
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()
|