Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-09 10:47:23

0001 #!/usr/bin/env python3
0002 """Run approved local SWF migrations via PostgreSQL peer authentication.
0003 
0004 Applications keep a non-owner database login. The root deployment process
0005 loads the release environment, then invokes Django as the postgres OS user.
0006 No administrator password is stored in the application environment.
0007 """
0008 import os
0009 from pathlib import Path
0010 import subprocess
0011 import sys
0012 
0013 
0014 def main():
0015     if os.geteuid() != 0:
0016         raise SystemExit('Run this migration helper as root during an approved deployment.')
0017     env_file = Path(os.environ.get('SWF_ENV_FILE', '/opt/swf-monitor/config/env/production.env'))
0018     env = os.environ.copy()
0019     for line in env_file.read_text().splitlines():
0020         line = line.strip()
0021         if not line or line.startswith('#') or '=' not in line:
0022             continue
0023         key, value = line.removeprefix('export ').split('=', 1)
0024         value = value.strip()
0025         if len(value) >= 2 and value[0] == value[-1] and value[0] in "\"'":
0026             value = value[1:-1]
0027         env[key.strip()] = value
0028     if env.get('DB_HOST', 'localhost') not in ('localhost', '127.0.0.1', '::1', ''):
0029         raise SystemExit('This helper requires the local SWF PostgreSQL server.')
0030     env.update(DB_USER='postgres', DB_PASSWORD='', DB_HOST='/var/run/postgresql',
0031                DJANGO_SETTINGS_MODULE='swf_monitor_project.settings', DJANGO_LOGGING_MODE='none')
0032     # Use exactly the environment read above. The postgres OS user must not need
0033     # read permission on application credential files discovered by AutoConfig.
0034     bootstrap = '''
0035 import decouple
0036 from decouple import Config, RepositoryEmpty
0037 decouple.config.config = Config(RepositoryEmpty())
0038 from django.core.management import execute_from_command_line
0039 import sys
0040 execute_from_command_line(['manage.py', 'migrate', '--noinput'] + sys.argv[1:])
0041 '''
0042     return subprocess.call(['/usr/sbin/runuser', '-u', 'postgres', '--', sys.executable,
0043                             '-c', bootstrap, *sys.argv[1:]], env=env)
0044 
0045 
0046 if __name__ == '__main__':
0047     sys.exit(main())