2016-05-09 08:11:34 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2016-05-13 11:53:50 +02:00
|
|
|
export PYTHONPATH=$PYTHONPATH:/harbor-migration
|
2016-10-27 09:01:46 +02:00
|
|
|
if [ -z "$DB_USR" -o -z "$DB_PWD" ]; then
|
|
|
|
echo "DB_USR or DB_PWD not set, exiting..."
|
|
|
|
exit 1
|
|
|
|
fi
|
2016-05-13 11:53:50 +02:00
|
|
|
|
2016-10-27 09:01:46 +02:00
|
|
|
source ./alembic.tpl > ./alembic.ini
|
2016-05-09 08:11:34 +02:00
|
|
|
|
2016-10-27 09:01:46 +02:00
|
|
|
DBCNF="-hlocalhost -u${DB_USR}"
|
2016-05-09 08:11:34 +02:00
|
|
|
|
|
|
|
#prevent shell to print insecure message
|
2016-10-27 09:01:46 +02:00
|
|
|
export MYSQL_PWD="${DB_PWD}"
|
2016-05-09 08:11:34 +02:00
|
|
|
|
|
|
|
if [[ $1 = "help" || $1 = "h" || $# = 0 ]]; then
|
|
|
|
echo "Usage:"
|
|
|
|
echo "backup perform database backup"
|
|
|
|
echo "restore perform database restore"
|
|
|
|
echo "up, upgrade perform database schema upgrade"
|
2016-05-13 11:53:50 +02:00
|
|
|
echo "test test database connection"
|
2016-05-09 08:11:34 +02:00
|
|
|
echo "h, help usage help"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2016-10-27 09:01:46 +02:00
|
|
|
if [[ ( $1 = "up" || $1 = "upgrade" ) && ${SKIP_CONFIRM} != "y" ]]; then
|
2016-05-09 08:11:34 +02:00
|
|
|
echo "Please backup before upgrade."
|
|
|
|
read -p "Enter y to continue updating or n to abort:" ans
|
|
|
|
case $ans in
|
|
|
|
[Yy]* )
|
|
|
|
;;
|
2017-07-25 04:48:12 +02:00
|
|
|
[Nn]* )
|
2016-05-09 08:11:34 +02:00
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
* ) echo "illegal answer: $ans. Upgrade abort!!"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo 'Trying to start mysql server...'
|
|
|
|
DBRUN=0
|
2016-10-27 09:01:46 +02:00
|
|
|
mysqld &
|
2017-10-26 11:08:12 +02:00
|
|
|
for i in {60..0}; do
|
|
|
|
mysqladmin -u$DB_USR -p$DB_PWD processlist >/dev/null 2>&1
|
|
|
|
if [ $? = 0 ]; then
|
2016-05-09 08:11:34 +02:00
|
|
|
break
|
|
|
|
fi
|
2017-10-26 11:08:12 +02:00
|
|
|
echo 'Waiting for MySQL start...'
|
|
|
|
sleep 1
|
2016-05-09 08:11:34 +02:00
|
|
|
done
|
2017-10-26 11:08:12 +02:00
|
|
|
if [ "$i" = 0 ]; then
|
2016-05-09 08:11:34 +02:00
|
|
|
echo "timeout. Can't run mysql server."
|
2016-05-13 11:53:50 +02:00
|
|
|
if [[ $1 = "test" ]]; then
|
|
|
|
echo "test failed."
|
|
|
|
fi
|
2016-05-09 08:11:34 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
2016-05-13 11:53:50 +02:00
|
|
|
if [[ $1 = "test" ]]; then
|
|
|
|
echo "test passed."
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2016-05-09 08:11:34 +02:00
|
|
|
key="$1"
|
|
|
|
case $key in
|
|
|
|
up|upgrade)
|
|
|
|
VERSION="$2"
|
|
|
|
if [[ -z $VERSION ]]; then
|
|
|
|
VERSION="head"
|
|
|
|
echo "Version is not specified. Default version is head."
|
|
|
|
fi
|
|
|
|
echo "Performing upgrade ${VERSION}..."
|
|
|
|
if [[ $(mysql $DBCNF -N -s -e "select count(*) from information_schema.tables \
|
|
|
|
where table_schema='registry' and table_name='alembic_version';") -eq 0 ]]; then
|
|
|
|
echo "table alembic_version does not exist. Trying to initial alembic_version."
|
|
|
|
mysql $DBCNF < ./alembic.sql
|
|
|
|
#compatible with version 0.1.0 and 0.1.1
|
|
|
|
if [[ $(mysql $DBCNF -N -s -e "select count(*) from information_schema.tables \
|
|
|
|
where table_schema='registry' and table_name='properties'") -eq 0 ]]; then
|
|
|
|
echo "table properties does not exist. The version of registry is 0.1.0"
|
|
|
|
else
|
|
|
|
echo "The version of registry is 0.1.1"
|
|
|
|
mysql $DBCNF -e "insert into registry.alembic_version values ('0.1.1')"
|
|
|
|
fi
|
|
|
|
fi
|
2017-08-22 09:47:53 +02:00
|
|
|
alembic -c ./alembic.ini current
|
2016-05-09 08:11:34 +02:00
|
|
|
alembic -c ./alembic.ini upgrade ${VERSION}
|
2017-08-22 09:47:53 +02:00
|
|
|
rc="$?"
|
|
|
|
alembic -c ./alembic.ini current
|
2016-05-09 08:11:34 +02:00
|
|
|
echo "Upgrade performed."
|
2017-08-22 09:47:53 +02:00
|
|
|
echo $rc
|
|
|
|
exit $rc
|
2016-05-09 08:11:34 +02:00
|
|
|
;;
|
|
|
|
backup)
|
|
|
|
echo "Performing backup..."
|
|
|
|
mysqldump $DBCNF --add-drop-database --databases registry > ./backup/registry.sql
|
|
|
|
echo "Backup performed."
|
|
|
|
;;
|
2017-07-25 04:48:12 +02:00
|
|
|
export)
|
|
|
|
echo "Performing export..."
|
|
|
|
./export --dbuser ${DB_USR} --dbpwd ${DB_PWD} --exportpath ${EXPORTPATH}
|
|
|
|
rc="$?"
|
|
|
|
echo "Export performed."
|
|
|
|
echo $rc
|
|
|
|
exit $rc
|
|
|
|
;;
|
2017-09-07 16:50:38 +02:00
|
|
|
mapprojects)
|
|
|
|
echo "Performing map projects..."
|
|
|
|
./mapprojects --dbuser ${DB_USR} --dbpwd ${DB_PWD} --mapprojectsfile ${MAPPROJECTFILE}
|
|
|
|
rc="$?"
|
|
|
|
echo "Map projects performed."
|
|
|
|
echo $rc
|
|
|
|
exit $rc
|
|
|
|
;;
|
2016-05-09 08:11:34 +02:00
|
|
|
restore)
|
|
|
|
echo "Performing restore..."
|
|
|
|
mysql $DBCNF < ./backup/registry.sql
|
|
|
|
echo "Restore performed."
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "unknown option"
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
esac
|