mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-01-21 07:55:00 +00:00
143 lines
4.6 KiB
Python
Executable File
143 lines
4.6 KiB
Python
Executable File
#!/usr/bin/python3
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
"""
|
|
Configuration helper for Tiny Tiny RSS.
|
|
"""
|
|
|
|
import argparse
|
|
import os
|
|
import subprocess
|
|
|
|
import augeas
|
|
|
|
from plinth import action_utils
|
|
|
|
CONFIG_FILE = '/etc/tt-rss/config.php'
|
|
DEFAULT_FILE = '/etc/default/tt-rss'
|
|
DATABASE_FILE = '/etc/tt-rss/database.php'
|
|
DB_BACKUP_FILE = '/var/lib/plinth/backups-data/ttrss-database.sql'
|
|
|
|
|
|
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('pre-setup', help='Perform pre-setup operations')
|
|
subparsers.add_parser('setup', help='Setup Tiny Tiny RSS configuration')
|
|
subparsers.add_parser('enable-api-access', help='Enable Tiny Tiny RSS API')
|
|
subparsers.add_parser('dump-database', help='Dump database to file')
|
|
subparsers.add_parser('restore-database',
|
|
help='Restore database from file')
|
|
|
|
subparsers.required = True
|
|
return parser.parse_args()
|
|
|
|
|
|
def subcommand_pre_setup(_):
|
|
"""Preseed debconf values before packages are installed."""
|
|
action_utils.debconf_set_selections(
|
|
['tt-rss tt-rss/database-type string pgsql'])
|
|
|
|
|
|
def subcommand_setup(_):
|
|
"""Setup Tiny Tiny RSS configuration."""
|
|
aug = load_augeas()
|
|
|
|
aug.set('/files' + DEFAULT_FILE + '/DISABLED', '0')
|
|
|
|
skip_self_url_path_exists = False
|
|
|
|
for match in aug.match('/files' + CONFIG_FILE + '/define'):
|
|
if aug.get(match) == 'SELF_URL_PATH':
|
|
aug.set(match + '/value', "'http://localhost/tt-rss/'")
|
|
elif aug.get(match) == 'PLUGINS':
|
|
aug.set(match + '/value', "'auth_remote, note'")
|
|
elif aug.get(match) == '_SKIP_SELF_URL_PATH_CHECKS':
|
|
skip_self_url_path_exists = True
|
|
aug.set(match + '/value', 'true')
|
|
|
|
if not skip_self_url_path_exists:
|
|
aug.set('/files' + CONFIG_FILE + '/define[last() + 1]',
|
|
'_SKIP_SELF_URL_PATH_CHECKS')
|
|
aug.set('/files' + CONFIG_FILE + '/define[last()]/value', 'true')
|
|
|
|
aug.save()
|
|
|
|
if action_utils.service_is_enabled('tt-rss'):
|
|
action_utils.service_restart('tt-rss')
|
|
|
|
|
|
def subcommand_enable_api_access(_):
|
|
"""Enable API access so that tt-rss can be accessed through mobile app."""
|
|
import psycopg2 # Only available post installation
|
|
|
|
aug = load_augeas()
|
|
|
|
def get_value(variable_name):
|
|
"""Return the value of a variable from database configuration file."""
|
|
return aug.get('/files' + DATABASE_FILE + '/$' + variable_name) \
|
|
.strip("'\"")
|
|
|
|
user = get_value('dbuser')
|
|
password = get_value('dbpass')
|
|
database = get_value('dbname')
|
|
host = get_value('dbserver')
|
|
|
|
connection = psycopg2.connect(database=database, user=user,
|
|
password=password, host=host)
|
|
cursor = connection.cursor()
|
|
|
|
cursor.execute("UPDATE ttrss_prefs SET def_value=true "
|
|
"WHERE pref_name='ENABLE_API_ACCESS';")
|
|
|
|
connection.commit()
|
|
connection.close()
|
|
|
|
|
|
def subcommand_dump_database(_):
|
|
"""Dump database to file."""
|
|
os.makedirs(os.path.dirname(DB_BACKUP_FILE), exist_ok=True)
|
|
with open(DB_BACKUP_FILE, 'w') as db_backup_file:
|
|
_run_as_postgres(['pg_dump', 'ttrss'], stdout=db_backup_file)
|
|
|
|
|
|
def subcommand_restore_database(_):
|
|
"""Restore database from file."""
|
|
_run_as_postgres(['dropdb', 'ttrss'])
|
|
_run_as_postgres(['createdb', 'ttrss'])
|
|
with open(DB_BACKUP_FILE, 'r') as db_restore_file:
|
|
_run_as_postgres(['psql', '--dbname', 'ttrss'], stdin=db_restore_file)
|
|
|
|
|
|
def _run_as_postgres(command, stdin=None, stdout=None):
|
|
"""Run a command as postgres user."""
|
|
command = ['sudo', '--user', 'postgres'] + command
|
|
subprocess.run(command, stdin=stdin, stdout=stdout, check=True)
|
|
|
|
|
|
def load_augeas():
|
|
"""Initialize Augeas."""
|
|
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
|
|
augeas.Augeas.NO_MODL_AUTOLOAD)
|
|
aug.set('/augeas/load/Shellvars/lens', 'Shellvars.lns')
|
|
aug.set('/augeas/load/Shellvars/incl[last() + 1]', DEFAULT_FILE)
|
|
aug.set('/augeas/load/Phpvars/lens', 'Phpvars.lns')
|
|
aug.set('/augeas/load/Phpvars/incl[last() + 1]', CONFIG_FILE)
|
|
aug.set('/augeas/load/Phpvars/incl[last() + 1]', DATABASE_FILE)
|
|
aug.load()
|
|
return aug
|
|
|
|
|
|
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()
|