|
| 1 | +import argparse |
| 2 | +import abc |
| 3 | +import signal |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +import tempfile |
| 7 | +import time |
| 8 | +from abc import ABC |
| 9 | +from pathlib import Path |
| 10 | +from threading import Event |
| 11 | + |
| 12 | + |
| 13 | +class Storage(ABC): |
| 14 | + pass |
| 15 | + |
| 16 | + |
| 17 | +class MySQL(Storage): |
| 18 | + def __init__(self): |
| 19 | + # Set up data directories. |
| 20 | + self.temp_dir = tempfile.TemporaryDirectory() |
| 21 | + temp_dir = Path(self.temp_dir.name) |
| 22 | + self.db_dir = temp_dir / 'data' |
| 23 | + self.db_dir.mkdir() |
| 24 | + self.db_dir.chmod(0o750) |
| 25 | + |
| 26 | + self.socket_path = temp_dir / 'mysqld.sock' |
| 27 | + self.user = 'selfoss' |
| 28 | + self.password = 'password' |
| 29 | + self.database = 'selfoss' |
| 30 | + |
| 31 | + def start(self): |
| 32 | + subprocess.check_call([ |
| 33 | + 'mysql_install_db', |
| 34 | + # Prevent defaulting to --user=mysql. |
| 35 | + '--no-defaults', |
| 36 | + f'--datadir={self.db_dir}', |
| 37 | + ]) |
| 38 | + |
| 39 | + # Start the server |
| 40 | + subprocess.check_call([ |
| 41 | + 'mysqld_safe', |
| 42 | + # Prevent trying to use /var/log for logs. |
| 43 | + '--no-defaults', |
| 44 | + f'--datadir={self.db_dir}', |
| 45 | + f'--socket={self.socket_path}', |
| 46 | + '--skip-networking', |
| 47 | + '--no-auto-restart', |
| 48 | + ]) |
| 49 | + |
| 50 | + # Create user and database. |
| 51 | + # Waiting does not seem to work. |
| 52 | + time.sleep(2) |
| 53 | + subprocess.check_call(['mysql', '--wait', f'--socket={self.socket_path}', f"--execute=CREATE USER '{self.user}'@'localhost' IDENTIFIED BY '{self.password}';"]) |
| 54 | + subprocess.check_call(['mysql', '--wait', f'--socket={self.socket_path}', f'--execute=CREATE DATABASE {self.database};']) |
| 55 | + subprocess.check_call(['mysql', '--wait', f'--socket={self.socket_path}', f"--execute=GRANT ALL PRIVILEGES ON *.* TO '{self.user}'@'localhost';"]) |
| 56 | + |
| 57 | + def stop(self): |
| 58 | + subprocess.check_call(['mysqladmin', f'--socket={self.socket_path}', 'shutdown']) |
| 59 | + self.temp_dir.cleanup() |
| 60 | + |
| 61 | + def get_config(self): |
| 62 | + return { |
| 63 | + 'db_type': 'mysql', |
| 64 | + 'db_socket': self.socket_path, |
| 65 | + 'db_username': self.user, |
| 66 | + 'db_password': self.password, |
| 67 | + 'db_database': self.database, |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | +class PostgreSQL(Storage): |
| 72 | + def __init__(self): |
| 73 | + # Set up data directories. |
| 74 | + self.temp_dir = tempfile.TemporaryDirectory() |
| 75 | + temp_dir = Path(self.temp_dir.name) |
| 76 | + self.db_dir = temp_dir / 'data' |
| 77 | + self.db_dir.mkdir() |
| 78 | + self.db_dir.chmod(0o750) |
| 79 | + |
| 80 | + self.socket_dir_path = temp_dir |
| 81 | + self.user = 'selfoss' |
| 82 | + self.database = 'selfoss' |
| 83 | + |
| 84 | + def start(self): |
| 85 | + subprocess.check_call(['initdb', self.db_dir]) |
| 86 | + |
| 87 | + # Start the server |
| 88 | + subprocess.check_call([ |
| 89 | + 'pg_ctl', |
| 90 | + 'start', |
| 91 | + f'--pgdata={self.db_dir}', |
| 92 | + # Intentionally passing options as a string |
| 93 | + f'--options=-k {self.socket_dir_path} -c listen_addresses=', |
| 94 | + ]) |
| 95 | + |
| 96 | + # Create users |
| 97 | + # Using a “template1” database since it is guaranteed to be present. |
| 98 | + subprocess.check_call(['psql', f'--host={self.socket_dir_path}', '--dbname=template1', '--tuples-only', '--no-align', f'--command=CREATE USER "{self.user}"']) |
| 99 | + subprocess.check_call(['psql', f'--host={self.socket_dir_path}', '--dbname=template1', '--tuples-only', '--no-align', f'--command=CREATE DATABASE "{self.database}" WITH OWNER = "{self.user}"']) |
| 100 | + |
| 101 | + def stop(self): |
| 102 | + subprocess.check_call(['pg_ctl', 'stop', f'--pgdata={self.db_dir}']) |
| 103 | + self.temp_dir.cleanup() |
| 104 | + |
| 105 | + def get_config(self): |
| 106 | + return { |
| 107 | + 'db_type': 'pgsql', |
| 108 | + 'db_host': self.socket_dir_path, |
| 109 | + 'db_username': self.user, |
| 110 | + 'db_database': self.database, |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | +class SQLite(Storage): |
| 115 | + def __init__(self): |
| 116 | + # Set up data directory. |
| 117 | + self.temp_dir = tempfile.TemporaryDirectory() |
| 118 | + temp_dir = Path(self.temp_dir.name) |
| 119 | + self.file = temp_dir / 'selfoss.db' |
| 120 | + |
| 121 | + def start(self): |
| 122 | + pass |
| 123 | + |
| 124 | + def stop(self): |
| 125 | + self.temp_dir.cleanup() |
| 126 | + |
| 127 | + def get_config(self): |
| 128 | + return { |
| 129 | + 'db_type': 'sqlite', |
| 130 | + 'db_file': self.file, |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == '__main__': |
| 135 | + parser = argparse.ArgumentParser( |
| 136 | + description='Runs a storage server for purposes of testing', |
| 137 | + ) |
| 138 | + parser.add_argument( |
| 139 | + 'backend', |
| 140 | + nargs='?', |
| 141 | + default='sqlite', |
| 142 | + help='Database backend to start' |
| 143 | + ) |
| 144 | + args = parser.parse_args() |
| 145 | + storage_backend = args.backend |
| 146 | + |
| 147 | + if storage_backend == 'mysql': |
| 148 | + storage_server = MySQL() |
| 149 | + elif storage_backend == 'postgresql': |
| 150 | + storage_server = PostgreSQL() |
| 151 | + elif storage_backend == 'sqlite': |
| 152 | + storage_server = SQLite() |
| 153 | + else: |
| 154 | + raise Exception(f'Unknown storage backend type: {storage_backend}') |
| 155 | + |
| 156 | + termination_event = Event() |
| 157 | + |
| 158 | + def exit_gracefully(*args): |
| 159 | + storage_server.stop() |
| 160 | + termination_event.set() |
| 161 | + |
| 162 | + signal.signal(signal.SIGINT, exit_gracefully) |
| 163 | + signal.signal(signal.SIGTERM, exit_gracefully) |
| 164 | + |
| 165 | + storage_server.start() |
| 166 | + |
| 167 | + print(storage_server.get_config()) |
| 168 | + |
| 169 | + termination_event.wait() |
0 commit comments