Merge pull request #5112 from wy65701436/migrate-clair

Add clair data migration in DB migrator
This commit is contained in:
Daniel Jiang 2018-06-12 14:33:13 +08:00 committed by GitHub
commit bb847ce745
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 11 deletions

View File

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

View File

@ -55,7 +55,7 @@ function up_notary {
WARNING: Notary migration will only allow anyone haven't migrated notary or
launched harbor yet.
If you want to migrate notary data, please delete all the notaryserver
and notarysigner DB tables in pgsql manually fistly.
and notarysigner DB tables in pgsql manually firstly.
*******************************************************************************
EOF
exit 0
@ -79,4 +79,36 @@ EOF
stop_mysql root
stop_pgsql $1
fi
}
function up_clair {
# clair DB info: user: 'postgres' database: 'postgres'
set +e
if [[ $(psql -U $1 -d postgres -t -c "select count(*) from vulnerability;") -eq 0 ]]; then
echo "no vulnerability data needs to be updated."
return 0
else
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
## it's not a clean clair db, so cannot execute the import step.
## fail at here to call user to clean DB, then to run clair db migration.
if [[ $(psql -U $1 -d postgres -t -c "select count(*) from pg_tables where schemaname='public';") -ne 0 ]]; then
cat >&2 <<-EOF
*******************************************************************************
WARNING: Clair migration will only allow anyone haven't migrated clair or
launched harbor yet.
If you want to migrate clair data, please delete all the clair DB tables
in pgsql manually firstly.
*******************************************************************************
EOF
exit 0
fi
set -e
psql -U $1 -f /harbor-migration/db/schema/clair.pgsql
stop_pgsql $1
fi
}

View File

@ -41,16 +41,20 @@ if [ "${1:0:1}" = '-' ]; then
fi
function launch_pgsql {
local pg_data=$2
if [ -z $2 ]; then
pg_data=$PGDATA
fi
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
if [ ! -s "$PGDATA/PG_VERSION" ]; then
if [ ! -s "$pg_data/PG_VERSION" ]; then
file_env 'POSTGRES_INITDB_ARGS'
if [ "$POSTGRES_INITDB_XLOGDIR" ]; then
export POSTGRES_INITDB_ARGS="$POSTGRES_INITDB_ARGS --xlogdir $POSTGRES_INITDB_XLOGDIR"
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
# messes it up
file_env 'POSTGRES_PASSWORD'
@ -66,10 +70,10 @@ function launch_pgsql {
{
echo
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
# 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_DB' "$POSTGRES_USER"
@ -107,19 +111,23 @@ EOSQL
done
#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 'PostgreSQL init process complete; ready for start up.'
echo
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
}
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 {