Refactor prerun.py.override to improve database connection checks and ownership of storage directory

This commit is contained in:
mjanez 2024-09-18 11:20:15 +02:00
parent ec0b2fa8df
commit e082565a85
No known key found for this signature in database
GPG Key ID: 7A62FDE17C5A4929
1 changed files with 19 additions and 11 deletions

View File

@ -33,6 +33,7 @@ def check_main_db_connection(retry=None):
conn_str = os.environ.get("CKAN_SQLALCHEMY_URL")
if not conn_str:
print("[prerun] CKAN_SQLALCHEMY_URL not defined, not checking db")
return
return check_db_connection(conn_str, retry)
@ -41,6 +42,7 @@ def check_datastore_db_connection(retry=None):
conn_str = os.environ.get("CKAN_DATASTORE_WRITE_URL")
if not conn_str:
print("[prerun] CKAN_DATASTORE_WRITE_URL not defined, not checking db")
return
return check_db_connection(conn_str, retry)
@ -83,29 +85,29 @@ def check_solr_connection(retry=None):
time.sleep(10)
check_solr_connection(retry=retry - 1)
else:
import re
conn_info = connection.read()
schema_name = json.loads(conn_info)
if 'ckan' in schema_name['name']:
print('[prerun] Succesfully connected to solr and CKAN schema loaded')
else:
import re
conn_info = connection.read()
schema_name = json.loads(conn_info)
if 'ckan' in schema_name['name']:
print('[prerun] Succesfully connected to solr and CKAN schema loaded')
else:
print('[prerun] Succesfully connected to solr, but CKAN schema not found')
def init_db():
db_command = ["ckan", "-c", ckan_ini, "db", "init"]
db_command = ["ckan", "-c", ckan_ini, "db", "upgrade"]
print("[prerun] Initializing or upgrading db - start")
try:
subprocess.check_output(db_command, stderr=subprocess.STDOUT)
print("[prerun] Initializing or upgrading db - end")
except subprocess.CalledProcessError as e:
if "OperationalError" in e.output:
print(e.output)
if "OperationalError" in str(e.output):
print("[prerun] Database not ready, waiting a bit before exit...")
time.sleep(5)
sys.exit(1)
else:
print(str(e))
print(e.output)
raise e
@ -194,6 +196,13 @@ def create_sysadmin():
subprocess.call(command)
print("[prerun] Made user {0} a sysadmin".format(name))
# cleanup permissions
# We're running as root before pivoting to uwsgi and dropping privs
data_dir = "%s/storage" % os.environ['CKAN_STORAGE_PATH']
command = ["chown", "-R", "ckan:ckan", data_dir]
subprocess.call(command)
print("[prerun] Ensured storage directory is owned by ckan")
if __name__ == "__main__":
@ -208,5 +217,4 @@ if __name__ == "__main__":
check_datastore_db_connection()
init_datastore_db()
check_solr_connection()
create_sysadmin()
create_sysadmin()