mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-27 10:44:33 +00:00
zoph: Uninstall fully so that reinstall works
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
17e84419b8
commit
74fc8e08da
@ -110,6 +110,12 @@ class ZophApp(app_module.App):
|
|||||||
if self.get_component('webserver-zoph').is_enabled():
|
if self.get_component('webserver-zoph').is_enabled():
|
||||||
self.enable()
|
self.enable()
|
||||||
|
|
||||||
|
def uninstall(self):
|
||||||
|
"""De-configure and uninstall the app."""
|
||||||
|
# Before package uninstall, so that config file is still available
|
||||||
|
privileged.uninstall()
|
||||||
|
super().uninstall()
|
||||||
|
|
||||||
|
|
||||||
class ZophBackupRestore(BackupRestore):
|
class ZophBackupRestore(BackupRestore):
|
||||||
"""Component to backup/restore Zoph database"""
|
"""Component to backup/restore Zoph database"""
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
import configparser
|
import configparser
|
||||||
import os
|
import os
|
||||||
|
import pathlib
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
@ -10,6 +11,7 @@ from plinth import action_utils
|
|||||||
from plinth.actions import privileged
|
from plinth.actions import privileged
|
||||||
|
|
||||||
APACHE_CONF = '/etc/apache2/conf-available/zoph.conf'
|
APACHE_CONF = '/etc/apache2/conf-available/zoph.conf'
|
||||||
|
DB_CONF = pathlib.Path('/etc/zoph.ini')
|
||||||
DB_BACKUP_FILE = '/var/lib/plinth/backups-data/zoph-database.sql'
|
DB_BACKUP_FILE = '/var/lib/plinth/backups-data/zoph-database.sql'
|
||||||
|
|
||||||
|
|
||||||
@ -18,7 +20,9 @@ def pre_install():
|
|||||||
"""Preseed debconf values before packages are installed."""
|
"""Preseed debconf values before packages are installed."""
|
||||||
action_utils.debconf_set_selections([
|
action_utils.debconf_set_selections([
|
||||||
'zoph zoph/dbconfig-install boolean true',
|
'zoph zoph/dbconfig-install boolean true',
|
||||||
'zoph zoph/mysql/admin-user string root'
|
'zoph zoph/dbconfig-upgrade boolean true',
|
||||||
|
'zoph zoph/dbconfig-remove boolean true',
|
||||||
|
'zoph zoph/mysql/admin-user string root',
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
@ -58,13 +62,16 @@ def setup():
|
|||||||
_zoph_configure('maps.provider', 'osm')
|
_zoph_configure('maps.provider', 'osm')
|
||||||
|
|
||||||
|
|
||||||
def _get_db_name():
|
def _get_db_config():
|
||||||
"""Return the name of the database configured by dbconfig."""
|
"""Return the name of the database configured by dbconfig."""
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
with open('/etc/zoph.ini', 'r', encoding='utf-8') as file_handle:
|
with DB_CONF.open('r', encoding='utf-8') as file_handle:
|
||||||
config.read_file(file_handle)
|
config.read_file(file_handle)
|
||||||
|
|
||||||
return config['zoph']['db_name'].strip('"')
|
return {
|
||||||
|
'db_name': config['zoph']['db_name'].strip('"'),
|
||||||
|
'db_user': config['zoph']['db_user'].strip('"'),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@privileged
|
@privileged
|
||||||
@ -88,8 +95,8 @@ def set_configuration(enable_osm: bool | None = None,
|
|||||||
query = f"UPDATE zoph_users SET user_name='{admin_user}' \
|
query = f"UPDATE zoph_users SET user_name='{admin_user}' \
|
||||||
WHERE user_name='admin';"
|
WHERE user_name='admin';"
|
||||||
|
|
||||||
subprocess.run(['mysql', _get_db_name()], input=query.encode(),
|
subprocess.run(['mysql', _get_db_config()['db_name']],
|
||||||
check=True)
|
input=query.encode(), check=True)
|
||||||
|
|
||||||
|
|
||||||
@privileged
|
@privileged
|
||||||
@ -107,7 +114,7 @@ def dump_database():
|
|||||||
May be called when app is disabled.
|
May be called when app is disabled.
|
||||||
"""
|
"""
|
||||||
with action_utils.service_ensure_running('mysql'):
|
with action_utils.service_ensure_running('mysql'):
|
||||||
db_name = _get_db_name()
|
db_name = _get_db_config()['db_name']
|
||||||
os.makedirs(os.path.dirname(DB_BACKUP_FILE), exist_ok=True)
|
os.makedirs(os.path.dirname(DB_BACKUP_FILE), exist_ok=True)
|
||||||
with open(DB_BACKUP_FILE, 'w', encoding='utf-8') as db_backup_file:
|
with open(DB_BACKUP_FILE, 'w', encoding='utf-8') as db_backup_file:
|
||||||
subprocess.run(['mysqldump', db_name], stdout=db_backup_file,
|
subprocess.run(['mysqldump', db_name], stdout=db_backup_file,
|
||||||
@ -121,9 +128,30 @@ def restore_database():
|
|||||||
May be called when app is disabled.
|
May be called when app is disabled.
|
||||||
"""
|
"""
|
||||||
with action_utils.service_ensure_running('mysql'):
|
with action_utils.service_ensure_running('mysql'):
|
||||||
db_name = _get_db_name()
|
db_name = _get_db_config()['db_name']
|
||||||
subprocess.run(['mysqladmin', '--force', 'drop', db_name], check=False)
|
subprocess.run(['mysqladmin', '--force', 'drop', db_name], check=False)
|
||||||
subprocess.run(['mysqladmin', 'create', db_name], check=True)
|
subprocess.run(['mysqladmin', 'create', db_name], check=True)
|
||||||
with open(DB_BACKUP_FILE, 'r', encoding='utf-8') as db_restore_file:
|
with open(DB_BACKUP_FILE, 'r', encoding='utf-8') as db_restore_file:
|
||||||
subprocess.run(['mysql', db_name], stdin=db_restore_file,
|
subprocess.run(['mysql', db_name], stdin=db_restore_file,
|
||||||
check=True)
|
check=True)
|
||||||
|
|
||||||
|
|
||||||
|
@privileged
|
||||||
|
def uninstall():
|
||||||
|
"""Drop database, database user and database configuration.
|
||||||
|
|
||||||
|
May be called when app is disabled.
|
||||||
|
"""
|
||||||
|
with action_utils.service_ensure_running('mysql'):
|
||||||
|
try:
|
||||||
|
config = _get_db_config()
|
||||||
|
subprocess.run(
|
||||||
|
['mysqladmin', '--force', 'drop', config['db_name']],
|
||||||
|
check=False)
|
||||||
|
|
||||||
|
query = f'DROP USER IF EXISTS {config["db_user"]}@localhost;'
|
||||||
|
subprocess.run(['mysql'], input=query.encode(), check=False)
|
||||||
|
except FileNotFoundError: # Database configuration not found
|
||||||
|
pass
|
||||||
|
|
||||||
|
DB_CONF.unlink(missing_ok=True)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user