Add clair data migration in DB migrator

This commit is contained in:
wangyan 2018-06-08 01:15:12 -07:00
parent 7254b438ca
commit c7a54e115c
3 changed files with 40 additions and 10 deletions

View File

@ -23,6 +23,7 @@ set -e
ISMYSQL=false ISMYSQL=false
ISPGSQL=false ISPGSQL=false
ISNOTARY=false ISNOTARY=false
ISCLAIR=false
cur_version="" cur_version=""
PGSQL_USR="postgres" PGSQL_USR="postgres"
@ -45,6 +46,10 @@ function init {
ISPGSQL=true ISPGSQL=true
fi fi
if [ -d "/clair-db" ]; then
ISCLAIR=true
fi
if [ $ISMYSQL == false ] && [ $ISPGSQL == false ]; then if [ $ISMYSQL == false ] && [ $ISPGSQL == false ]; then
echo "No database has been mounted for the migration. Use '-v' to set it in 'docker run'." echo "No database has been mounted for the migration. Use '-v' to set it in 'docker run'."
exit 1 exit 1
@ -65,7 +70,11 @@ function init {
fi fi
if [ $ISPGSQL == true ]; then if [ $ISPGSQL == true ]; then
launch_pgsql $PGSQL_USR if [ $ISCLAIR == true ]; then
launch_pgsql $PGSQL_USR "/clair-db"
else
launch_pgsql $PGSQL_USR
fi
fi fi
} }
@ -130,6 +139,8 @@ function validate {
function upgrade { function upgrade {
if [ $ISNOTARY == true ];then if [ $ISNOTARY == true ];then
up_notary $PGSQL_USR up_notary $PGSQL_USR
elif [ $ISCLAIR == true ];then
up_clair $PGSQL_USR
else else
up_harbor $1 up_harbor $1
fi fi

View File

@ -80,3 +80,14 @@ EOF
stop_pgsql $1 stop_pgsql $1
fi fi
} }
function up_clair {
# clair DB info: user: 'postgres' database: 'postgres'
pg_dump -U postgres postgres > /harbor-migration/db/schema/clair.pgsql
stop_pgsql postgres "/clair-db"
# it's harbor DB on pgsql.
launch_pgsql $1
psql -U $1 -f /harbor-migration/db/schema/clair.pgsql
stop_pgsql $1
}

View File

@ -41,16 +41,20 @@ if [ "${1:0:1}" = '-' ]; then
fi fi
function launch_pgsql { function launch_pgsql {
local pg_data=$2
if [ -z $2 ]; then
pg_data=$PGDATA
fi
if [ "$1" = 'postgres' ]; then if [ "$1" = 'postgres' ]; then
chown -R postgres:postgres $PGDATA chown -R postgres:postgres $pg_data
# look specifically for PG_VERSION, as it is expected in the DB dir # look specifically for PG_VERSION, as it is expected in the DB dir
if [ ! -s "$PGDATA/PG_VERSION" ]; then if [ ! -s "$pg_data/PG_VERSION" ]; then
file_env 'POSTGRES_INITDB_ARGS' file_env 'POSTGRES_INITDB_ARGS'
if [ "$POSTGRES_INITDB_XLOGDIR" ]; then if [ "$POSTGRES_INITDB_XLOGDIR" ]; then
export POSTGRES_INITDB_ARGS="$POSTGRES_INITDB_ARGS --xlogdir $POSTGRES_INITDB_XLOGDIR" export POSTGRES_INITDB_ARGS="$POSTGRES_INITDB_ARGS --xlogdir $POSTGRES_INITDB_XLOGDIR"
fi fi
su - $1 -c "initdb -D $PGDATA -U postgres -E UTF-8 --lc-collate=en_US.UTF-8 --lc-ctype=en_US.UTF-8 $POSTGRES_INITDB_ARGS" su - $1 -c "initdb -D $pg_data -U postgres -E UTF-8 --lc-collate=en_US.UTF-8 --lc-ctype=en_US.UTF-8 $POSTGRES_INITDB_ARGS"
# check password first so we can output the warning before postgres # check password first so we can output the warning before postgres
# messes it up # messes it up
file_env 'POSTGRES_PASSWORD' file_env 'POSTGRES_PASSWORD'
@ -66,10 +70,10 @@ function launch_pgsql {
{ {
echo echo
echo "host all all all $authMethod" echo "host all all all $authMethod"
} >> "$PGDATA/pg_hba.conf" } >> "$pg_data/pg_hba.conf"
# internal start of server in order to allow set-up using psql-client # internal start of server in order to allow set-up using psql-client
# does not listen on external TCP/IP and waits until start finishes # does not listen on external TCP/IP and waits until start finishes
su - $1 -c "pg_ctl -D \"$PGDATA\" -o \"-c listen_addresses='localhost'\" -w start" su - $1 -c "pg_ctl -D \"$pg_data\" -o \"-c listen_addresses='localhost'\" -w start"
file_env 'POSTGRES_USER' 'postgres' file_env 'POSTGRES_USER' 'postgres'
file_env 'POSTGRES_DB' "$POSTGRES_USER" file_env 'POSTGRES_DB' "$POSTGRES_USER"
@ -107,19 +111,23 @@ EOSQL
done done
#PGUSER="${PGUSER:-postgres}" \ #PGUSER="${PGUSER:-postgres}" \
#su - $1 -c "pg_ctl -D \"$PGDATA\" -m fast -w stop" #su - $1 -c "pg_ctl -D \"$pg_data\" -m fast -w stop"
echo echo
echo 'PostgreSQL init process complete; ready for start up.' echo 'PostgreSQL init process complete; ready for start up.'
echo echo
else else
su - $PGSQL_USR -c "pg_ctl -D \"$PGDATA\" -o \"-c listen_addresses='localhost'\" -w start" su - $PGSQL_USR -c "pg_ctl -D \"$pg_data\" -o \"-c listen_addresses='localhost'\" -w start"
fi fi
fi fi
} }
function stop_pgsql { function stop_pgsql {
su - $1 -c "pg_ctl -D \"/var/lib/postgresql/data\" -w stop" local pg_data=$2
if [ -z $2 ]; then
pg_data=$PGDATA
fi
su - $1 -c "pg_ctl -D \"$pg_data\" -w stop"
} }
function get_version_pgsql { function get_version_pgsql {