Commit 91279287 authored by Michael Salim's avatar Michael Salim
Browse files

started adding features to init postgres

parent 5f0b9cbc
......@@ -14,6 +14,9 @@ class ServerInfo:
else:
self.refresh()
if not self.data.get('balsamdb_path'):
self.update({'balsamdb_path': balsam_db_path})
if self.data.get('address') and os.environ.get('IS_SERVER_DAEMON')=='True':
raise RuntimeError(f"A running server address is already posted at {self.path}\n"
' Use "balsam dbserver --stop" to shut it down.\n'
......@@ -32,11 +35,24 @@ class ServerInfo:
address = f'tcp://{hostname}:{port}'
return address
def get_free_port(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('', 0))
port = int(sock.getsockname()[1])
sock.close()
return port
def get_sqlite3_info(self):
new_address = self.get_free_port_and_address()
info = dict(db_type='sqlite3', address=new_address)
return info
def get_postgres_info(self):
hostname = socket.gethostname()
port = self.get_free_port()
info = dict(host=hostname, port=port)
return info
def reset_server_address(self):
db = self['db_type']
info = getattr(self, f'get_{db}_info')()
......
......@@ -36,22 +36,29 @@ def resolve_db_path(path=None):
def configure_db_backend(db_path):
ENGINES = {
'sqlite3' : 'django.db.backends.sqlite3',
'postgres': 'django.db.backends.postgresql_psycopg2',
}
NAMES = {
'sqlite3' : 'db.sqlite3',
'sqlite3' : os.path.join(db_path, 'db.sqlite3'),
'postgres': 'balsam',
}
OPTIONS = {
'sqlite3' : {'timeout' : 5000},
'postgres' : {},
}
info = serverinfo.ServerInfo(db_path)
db_type = info['db_type']
user = info.get('user', '')
password = info.get('password', '')
db_name = os.path.join(db_path, NAMES[db_type])
host = info.get('host', '')
port = info.get('port', '')
db_name = NAMES[db_type]
db = dict(ENGINE=ENGINES[db_type], NAME=db_name,
OPTIONS=OPTIONS[db_type], USER=user, PASSWORD=password)
OPTIONS=OPTIONS[db_type], USER=user, PASSWORD=password,
HOST=host, PORT=port)
DATABASES = {'default':db}
return DATABASES
......
......@@ -317,7 +317,7 @@ def make_parser():
# --------
parser_init = subparsers.add_parser('init', help="Create new balsam DB")
parser_init.add_argument('path', help="Path to Balsam DB directory")
parser_init.add_argument('--db-type', choices=['sqlite3'],
parser_init.add_argument('--db-type', choices=['sqlite3', 'postgres'],
default='sqlite3', help="choose backend to use")
parser_init.set_defaults(func=init)
# -----------------
......
......@@ -410,7 +410,7 @@ def init(args):
serverinfo.update({'db_type': db_type})
fname = find_spec("balsam.scripts.init").origin
p = subprocess.Popen(f'BALSAM_DB_PATH={path} {sys.executable} {fname}',
p = subprocess.Popen(f'BALSAM_DB_PATH={path} {sys.executable} {fname} {path}',
shell=True)
p.wait()
......
from getpass import getuser
import os
import sys
import django
import time
import subprocess
from balsam.django_config.serverinfo import ServerInfo
os.environ['DJANGO_SETTINGS_MODULE'] = 'balsam.django_config.settings'
django.setup()
def sqlite3_init(serverInfo):
pass
from django import db
from django.core.management import call_command
from django.conf import settings
def postgres_init(serverInfo):
db_path = serverInfo['balsamdb_path']
db_path = os.path.join(db_path, 'balsamdb')
p = subprocess.Popen(f'initdb -D {db_path} -U $USER', shell=True)
retcode = p.wait()
if retcode != 0: raise RuntimeError("initdb failed")
db_path = db.connection.settings_dict['NAME']
print(f"Setting up new balsam database: {db_path}")
call_command('makemigrations', interactive=False, verbosity=0)
call_command('migrate', interactive=False, verbosity=0)
serverInfo.update({'user' : getuser()})
serverInfo.reset_server_address()
port = serverInfo['port']
with open(os.path.join(db_path, 'postgresql.conf'), 'a') as fp:
fp.write(f'port={port} # appended from balsam init\n')
new_path = settings.DATABASES['default']['NAME']
if os.path.exists(new_path):
print(f"Set up new DB at {new_path}")
else:
raise RuntimeError(f"Failed to created DB at {new_path}")
serv_proc = subprocess.Popen(f'pg_ctl -D {db_path} -w start', shell=True)
time.sleep(2)
create_proc = subprocess.Popen(f'createdb balsam -p {port}', shell=True)
retcode = create_proc.wait()
if retcode != 0: raise RuntimeError("createdb failed")
def postgres_post(serverInfo):
db_path = serverInfo['balsamdb_path']
db_path = os.path.join(db_path, 'balsamdb')
serv_proc = subprocess.Popen(f'pg_ctl -D {db_path} -w stop', shell=True)
serv_proc.wait()
def run_migrations():
import django
os.environ['DJANGO_SETTINGS_MODULE'] = 'balsam.django_config.settings'
django.setup()
from django import db
from django.core.management import call_command
from django.conf import settings
print(f"DB settings:", settings.DATABASES['default'])
db_path = db.connection.settings_dict['NAME']
print(f"Setting up new balsam database: {db_path}")
call_command('makemigrations', interactive=False, verbosity=0)
call_command('migrate', interactive=False, verbosity=0)
new_path = settings.DATABASES['default']['NAME']
if os.path.exists(new_path):
print(f"Set up new DB at {new_path}")
else:
raise RuntimeError(f"Failed to created DB at {new_path}")
if __name__ == "__main__":
serverInfo = ServerInfo(sys.argv[1])
db_type = serverInfo['db_type']
if db_type == 'sqlite3':
sqlite3_init(serverInfo)
elif db_type == 'postgres':
postgres_init(serverInfo)
else:
raise RuntimeError(f'init doesnt support DB type {db_type}')
run_migrations()
if db_type == 'postgres':
postgres_post(serverInfo)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment