dockersetup: import database dump before running migrations

If the database dump is older than the application, there may be
migrations to run, so we need to run them after importing and not
beforehand.

Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com>
This commit is contained in:
Paul Eggleton 2019-01-22 15:38:40 +13:00
parent 303d7ca235
commit b99143d2e3

View File

@ -482,21 +482,45 @@ if return_code != 0:
print("docker-compose up failed") print("docker-compose up failed")
sys.exit(1) sys.exit(1)
# Apply any pending layerindex migrations / initialize the database. Database might not be ready yet; have to wait then poll. # Database might not be ready yet; have to wait then poll.
time.sleep(8) time.sleep(8)
while True: while True:
time.sleep(2) time.sleep(2)
# Pass credentials through environment for slightly better security # Pass credentials through environment for slightly better security
# (avoids password being visible through ps or /proc/<pid>/cmdline) # (avoids password being visible through ps or /proc/<pid>/cmdline)
env = os.environ.copy() env = os.environ.copy()
env['DATABASE_USER'] = 'root' env['MYSQL_PWD'] = dbapassword
env['DATABASE_PASSWORD'] = dbapassword # Dummy command, we just want to establish that the db can be connected to
return_code = subprocess.call("docker-compose run --rm -e DATABASE_USER -e DATABASE_PASSWORD layersapp /opt/migrate.sh", shell=True, env=env) return_code = subprocess.call("echo | docker exec -i -e MYSQL_PWD layersdb mysql -uroot layersdb", shell=True, env=env)
if return_code == 0: if return_code == 0:
break break
else: else:
print("Database server may not be ready; will try again.") print("Database server may not be ready; will try again.")
if not updatemode:
# Import the user's supplied data
if dbfile:
return_code = subprocess.call("gunzip -t %s > /dev/null 2>&1" % dbfile, shell=True)
if return_code == 0:
catcmd = 'zcat'
else:
catcmd = 'cat'
env = os.environ.copy()
env['MYSQL_PWD'] = dbapassword
return_code = subprocess.call("%s %s | docker exec -i -e MYSQL_PWD layersdb mysql -uroot layersdb" % (catcmd, dbfile), shell=True, env=env)
if return_code != 0:
print("Database import failed")
sys.exit(1)
# Apply any pending layerindex migrations / initialize the database.
env = os.environ.copy()
env['DATABASE_USER'] = 'root'
env['DATABASE_PASSWORD'] = dbapassword
return_code = subprocess.call("docker-compose run --rm -e DATABASE_USER -e DATABASE_PASSWORD layersapp /opt/migrate.sh", shell=True, env=env)
if return_code != 0:
print("Applying migrations failed")
sys.exit(1)
if not updatemode: if not updatemode:
# Create normal database user for app to use # Create normal database user for app to use
with tempfile.NamedTemporaryFile('w', dir=os.getcwd(), delete=False) as tf: with tempfile.NamedTemporaryFile('w', dir=os.getcwd(), delete=False) as tf:
@ -517,20 +541,6 @@ if not updatemode:
finally: finally:
os.remove(sqlscriptfile) os.remove(sqlscriptfile)
# Import the user's supplied data
if dbfile:
return_code = subprocess.call("gunzip -t %s > /dev/null 2>&1" % dbfile, shell=True)
if return_code == 0:
catcmd = 'zcat'
else:
catcmd = 'cat'
env = os.environ.copy()
env['MYSQL_PWD'] = dbapassword
return_code = subprocess.call("%s %s | docker exec -i -e MYSQL_PWD layersdb mysql -uroot layersdb" % (catcmd, dbfile), shell=True, env=env)
if return_code != 0:
print("Database import failed")
sys.exit(1)
## For a fresh database, create an admin account ## For a fresh database, create an admin account
print("Creating database superuser. Input user name, email, and password when prompted.") print("Creating database superuser. Input user name, email, and password when prompted.")
return_code = subprocess.call("docker-compose run --rm layersapp /opt/layerindex/manage.py createsuperuser", shell=True) return_code = subprocess.call("docker-compose run --rm layersapp /opt/layerindex/manage.py createsuperuser", shell=True)