2016-08-25 22:05:30 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import subprocess
|
2016-08-26 10:30:53 +02:00
|
|
|
import psycopg2
|
2020-06-09 13:33:39 +02:00
|
|
|
import urllib2
|
2018-08-08 15:09:45 +02:00
|
|
|
import re
|
2016-08-25 22:05:30 +02:00
|
|
|
|
2019-01-10 23:25:02 +01:00
|
|
|
import time
|
2016-08-25 22:05:30 +02:00
|
|
|
|
2016-08-25 22:10:24 +02:00
|
|
|
ckan_ini = os.environ.get('CKAN_INI', '/srv/app/production.ini')
|
2016-08-25 22:05:30 +02:00
|
|
|
|
2016-08-26 10:30:53 +02:00
|
|
|
RETRY = 5
|
|
|
|
|
|
|
|
def check_db_connection(retry=None):
|
2016-08-26 10:44:49 +02:00
|
|
|
|
2020-02-17 15:30:52 +01:00
|
|
|
print('[prerun] Start check_db_connection...')
|
2019-01-10 23:25:02 +01:00
|
|
|
|
2016-08-26 10:30:53 +02:00
|
|
|
if retry is None:
|
|
|
|
retry = RETRY
|
|
|
|
elif retry == 0:
|
2020-02-17 15:30:52 +01:00
|
|
|
print('[prerun] Giving up after 5 tries...')
|
2016-08-26 10:30:53 +02:00
|
|
|
sys.exit(1)
|
2016-08-26 10:44:49 +02:00
|
|
|
|
2016-08-26 10:30:53 +02:00
|
|
|
conn_str = os.environ.get('CKAN_SQLALCHEMY_URL', '')
|
|
|
|
try:
|
|
|
|
connection = psycopg2.connect(conn_str)
|
2016-08-26 10:44:49 +02:00
|
|
|
|
2016-08-26 10:30:53 +02:00
|
|
|
except psycopg2.Error as e:
|
2020-02-17 15:30:52 +01:00
|
|
|
print((str(e)))
|
|
|
|
print('[prerun] Unable to connect to the database...try again in a while.')
|
2016-08-26 10:30:53 +02:00
|
|
|
import time
|
|
|
|
time.sleep(10)
|
|
|
|
check_db_connection(retry = retry - 1)
|
|
|
|
else:
|
|
|
|
connection.close()
|
2016-08-26 10:44:49 +02:00
|
|
|
|
2016-08-26 10:30:53 +02:00
|
|
|
def check_solr_connection(retry=None):
|
2016-08-26 10:44:49 +02:00
|
|
|
|
2020-02-17 15:30:52 +01:00
|
|
|
print('[prerun] Start check_solr_connection...')
|
2019-01-10 23:25:02 +01:00
|
|
|
|
2016-08-26 10:30:53 +02:00
|
|
|
if retry is None:
|
|
|
|
retry = RETRY
|
|
|
|
elif retry == 0:
|
2020-02-17 15:30:52 +01:00
|
|
|
print('[prerun] Giving up after 5 tries...')
|
2016-08-26 10:30:53 +02:00
|
|
|
sys.exit(1)
|
2016-08-26 10:44:49 +02:00
|
|
|
|
2016-08-26 10:30:53 +02:00
|
|
|
url = os.environ.get('CKAN_SOLR_URL', '')
|
|
|
|
search_url = '{url}/select/?q=*&wt=json'.format(url=url)
|
2016-08-26 10:44:49 +02:00
|
|
|
|
2016-08-26 10:30:53 +02:00
|
|
|
try:
|
2020-06-09 13:33:39 +02:00
|
|
|
connection = urllib2.urlopen(search_url)
|
|
|
|
except urllib2.URLError as e:
|
2020-02-17 15:30:52 +01:00
|
|
|
print((str(e)))
|
|
|
|
print('[prerun] Unable to connect to solr...try again in a while.')
|
2016-08-26 10:30:53 +02:00
|
|
|
import time
|
|
|
|
time.sleep(10)
|
|
|
|
check_solr_connection(retry = retry - 1)
|
|
|
|
else:
|
2019-01-10 23:25:02 +01:00
|
|
|
import re
|
|
|
|
conn_info = connection.read()
|
2020-06-09 13:33:39 +02:00
|
|
|
conn_info = re.sub(r'"zkConnected":true', '"zkConnected":True', conn_info)
|
2019-01-10 23:25:02 +01:00
|
|
|
eval(conn_info)
|
2016-08-25 22:05:30 +02:00
|
|
|
|
|
|
|
def init_db():
|
|
|
|
|
2020-02-17 15:30:52 +01:00
|
|
|
print('[prerun] Start init_db...')
|
2019-01-10 23:25:02 +01:00
|
|
|
|
2020-06-09 13:33:39 +02:00
|
|
|
db_command = ['paster', '--plugin=ckan', 'db', 'init', '-c', ckan_ini]
|
2019-02-18 22:51:35 +01:00
|
|
|
|
2020-06-09 13:33:39 +02:00
|
|
|
print('[prerun] Initializing or upgrading db - start using paster db init')
|
2016-08-25 22:05:30 +02:00
|
|
|
try:
|
2019-01-10 23:25:02 +01:00
|
|
|
# run init scripts
|
2016-08-25 22:05:30 +02:00
|
|
|
subprocess.check_output(db_command, stderr=subprocess.STDOUT)
|
2019-02-18 22:51:35 +01:00
|
|
|
|
2020-02-17 15:30:52 +01:00
|
|
|
print('[prerun] Initializing or upgrading db - end')
|
|
|
|
except subprocess.CalledProcessError as e:
|
2016-08-25 22:05:30 +02:00
|
|
|
if 'OperationalError' in e.output:
|
2020-02-17 15:30:52 +01:00
|
|
|
print((e.output))
|
|
|
|
print('[prerun] Database not ready, waiting a bit before exit...')
|
2016-08-25 22:05:30 +02:00
|
|
|
import time
|
|
|
|
time.sleep(5)
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
2020-02-17 15:30:52 +01:00
|
|
|
print((e.output))
|
2016-08-25 22:05:30 +02:00
|
|
|
raise e
|
2020-02-17 15:30:52 +01:00
|
|
|
print('[prerun] Initializing or upgrading db - finish')
|
2016-08-25 22:05:30 +02:00
|
|
|
|
2019-02-18 22:51:35 +01:00
|
|
|
|
|
|
|
def init_datastore():
|
|
|
|
|
|
|
|
conn_str = os.environ.get('CKAN_DATASTORE_WRITE_URL')
|
|
|
|
if not conn_str:
|
2020-02-17 15:30:52 +01:00
|
|
|
print('[prerun] Skipping datastore initialization')
|
2019-02-18 22:51:35 +01:00
|
|
|
return
|
|
|
|
|
2020-06-09 13:33:39 +02:00
|
|
|
datastore_perms_command = ['paster', '--plugin=ckan', 'datastore',
|
|
|
|
'set-permissions', '-c', ckan_ini]
|
2019-02-18 22:51:35 +01:00
|
|
|
|
|
|
|
connection = psycopg2.connect(conn_str)
|
|
|
|
cursor = connection.cursor()
|
|
|
|
|
2020-02-17 15:30:52 +01:00
|
|
|
print('[prerun] Initializing datastore db - start')
|
2019-02-18 22:51:35 +01:00
|
|
|
try:
|
|
|
|
datastore_perms = subprocess.Popen(
|
|
|
|
datastore_perms_command,
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
|
|
|
|
perms_sql = datastore_perms.stdout.read()
|
|
|
|
# Remove internal pg command as psycopg2 does not like it
|
2020-02-17 15:30:52 +01:00
|
|
|
perms_sql = re.sub('\\\\connect \"(.*)\"', '', perms_sql.decode('utf-8'))
|
2019-02-18 22:51:35 +01:00
|
|
|
cursor.execute(perms_sql)
|
|
|
|
for notice in connection.notices:
|
2020-02-17 15:30:52 +01:00
|
|
|
print(notice)
|
2019-02-18 22:51:35 +01:00
|
|
|
|
|
|
|
connection.commit()
|
|
|
|
|
2020-02-17 15:30:52 +01:00
|
|
|
print('[prerun] Initializing datastore db - end')
|
|
|
|
print((datastore_perms.stdout.read()))
|
2019-02-18 22:51:35 +01:00
|
|
|
except psycopg2.Error as e:
|
2020-02-17 15:30:52 +01:00
|
|
|
print('[prerun] Could not initialize datastore')
|
|
|
|
print((str(e)))
|
2019-02-18 22:51:35 +01:00
|
|
|
|
2020-02-17 15:30:52 +01:00
|
|
|
except subprocess.CalledProcessError as e:
|
2019-02-18 22:51:35 +01:00
|
|
|
if 'OperationalError' in e.output:
|
2020-02-17 15:30:52 +01:00
|
|
|
print((e.output))
|
|
|
|
print('[prerun] Database not ready, waiting a bit before exit...')
|
2019-02-18 22:51:35 +01:00
|
|
|
time.sleep(5)
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
2020-02-17 15:30:52 +01:00
|
|
|
print((e.output))
|
2019-02-18 22:51:35 +01:00
|
|
|
raise e
|
|
|
|
finally:
|
|
|
|
cursor.close()
|
|
|
|
connection.close()
|
|
|
|
|
|
|
|
|
2016-08-25 22:05:30 +02:00
|
|
|
def create_sysadmin():
|
|
|
|
|
2020-02-17 15:30:52 +01:00
|
|
|
print('[prerun] Start create_sysadmin...')
|
2019-01-10 23:25:02 +01:00
|
|
|
|
2016-08-25 22:05:30 +02:00
|
|
|
name = os.environ.get('CKAN_SYSADMIN_NAME')
|
|
|
|
password = os.environ.get('CKAN_SYSADMIN_PASSWORD')
|
|
|
|
email = os.environ.get('CKAN_SYSADMIN_EMAIL')
|
|
|
|
|
|
|
|
if name and password and email:
|
|
|
|
|
|
|
|
# Check if user exists
|
2020-06-09 13:33:39 +02:00
|
|
|
command = ['paster', '--plugin=ckan', 'user', name, '-c', ckan_ini]
|
2016-08-25 22:05:30 +02:00
|
|
|
|
|
|
|
out = subprocess.check_output(command)
|
2020-02-17 15:30:52 +01:00
|
|
|
if 'User:None' not in re.sub(r'\s', '', out.decode('utf-8')):
|
|
|
|
print('[prerun] Sysadmin user exists, skipping creation')
|
2016-08-25 22:05:30 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
# Create user
|
2020-06-09 13:33:39 +02:00
|
|
|
command = ['paster', '--plugin=ckan', 'user', 'add',
|
2016-08-25 22:05:30 +02:00
|
|
|
name,
|
|
|
|
'password=' + password,
|
2020-06-09 13:33:39 +02:00
|
|
|
'email=' + email,
|
|
|
|
'-c', ckan_ini]
|
2016-08-25 22:05:30 +02:00
|
|
|
|
|
|
|
subprocess.call(command)
|
2020-02-17 15:30:52 +01:00
|
|
|
print(('[prerun] Created user {0}'.format(name)))
|
2016-08-25 22:05:30 +02:00
|
|
|
|
|
|
|
# Make it sysadmin
|
2020-06-09 13:33:39 +02:00
|
|
|
command = ['paster', '--plugin=ckan', 'sysadmin', 'add',
|
|
|
|
name,
|
|
|
|
'-c', ckan_ini]
|
2016-08-25 22:05:30 +02:00
|
|
|
|
|
|
|
subprocess.call(command)
|
2020-02-17 15:30:52 +01:00
|
|
|
print(('[prerun] Made user {0} a sysadmin'.format(name)))
|
2016-08-25 22:05:30 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
maintenance = os.environ.get('MAINTENANCE_MODE', '').lower() == 'true'
|
|
|
|
|
|
|
|
if maintenance:
|
2020-02-17 15:30:52 +01:00
|
|
|
print('[prerun] Maintenance mode, skipping setup...')
|
2016-08-25 22:05:30 +02:00
|
|
|
else:
|
2016-08-26 10:30:53 +02:00
|
|
|
check_db_connection()
|
|
|
|
check_solr_connection()
|
2016-08-25 22:05:30 +02:00
|
|
|
init_db()
|
2019-02-18 22:51:35 +01:00
|
|
|
if os.environ.get('CKAN_DATASTORE_WRITE_URL'):
|
|
|
|
init_datastore()
|
2016-08-25 22:05:30 +02:00
|
|
|
create_sysadmin()
|
2019-01-10 23:25:02 +01:00
|
|
|
#time.sleep(60000) # don't end the prerun script to allow container dock and debug
|