mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-26 02:28:06 +01:00
c8265a8d53
Default target version is 1.5.0 This is mainly for VIC-appliance upgrade, and should be considered experimental for oss due to limited test. Tested with 1.2 and 1.3 harbor.cfg from VIC appliance.
248 lines
7.7 KiB
Plaintext
Executable File
248 lines
7.7 KiB
Plaintext
Executable File
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
The script is to map the project ID of Harbor and Admiral, and only for VIC 1.2 migration.
|
|
|
|
In VIC 1.2, proejct is managed by Admiral rather than Harbor, as part of migration,
|
|
it needs to unify the proejct ID of Admiral and Harbor.
|
|
"""
|
|
|
|
import json
|
|
import fileinput
|
|
from optparse import OptionParser
|
|
import os
|
|
import MySQLdb
|
|
import sys
|
|
|
|
|
|
class Parameters(object):
|
|
|
|
def __init__(self):
|
|
self.dbuser = ''
|
|
self.dbpwd = ''
|
|
self.mapprojectsfile = ''
|
|
self.init_from_input()
|
|
|
|
@staticmethod
|
|
def parse_input():
|
|
usage = \
|
|
'usage: %prog [options] <dbuser> <dbpwd> <mapprojectsfile>'
|
|
parser = OptionParser(usage)
|
|
parser.add_option('-u', '--dbuser', dest='dbuser',
|
|
help='db user')
|
|
parser.add_option('-p', '--dbpwd', dest='dbpwd',
|
|
help='db password')
|
|
parser.add_option('-m', '--mapprojectsfile',
|
|
dest='mapprojectsfile',
|
|
help='the path of mapping projects file')
|
|
|
|
(options, args) = parser.parse_args()
|
|
return (options.dbuser, options.dbpwd, options.mapprojectsfile)
|
|
|
|
def init_from_input(self):
|
|
(self.dbuser, self.dbpwd, self.mapprojectsfile) = \
|
|
Parameters.parse_input()
|
|
|
|
|
|
class AccessLog:
|
|
|
|
def __init__(self, log_id, project_id):
|
|
self.log_id = log_id
|
|
self.project_id = project_id
|
|
|
|
|
|
class Repository:
|
|
|
|
def __init__(self, repository_id, project_id):
|
|
self.repository_id = repository_id
|
|
self.project_id = project_id
|
|
|
|
|
|
class ReplicationPolicy:
|
|
|
|
def __init__(self, replication_policy_id, project_id):
|
|
self.replication_policy_id = replication_policy_id
|
|
self.project_id = project_id
|
|
|
|
|
|
class Project:
|
|
|
|
def __init__(
|
|
self,
|
|
project_id,
|
|
name,
|
|
index_id,
|
|
):
|
|
|
|
self.project_id = project_id
|
|
self.project_name = name
|
|
self.index_id = index_id
|
|
|
|
|
|
class HarborUtil:
|
|
|
|
def __init__(self, dbuser, dbpwd):
|
|
self.serverName = 'localhost'
|
|
self.user = dbuser
|
|
self.password = dbpwd
|
|
self.port = '3306'
|
|
self.subDB = 'registry'
|
|
self.db = None
|
|
self.cursor = None
|
|
|
|
def connect(self):
|
|
try:
|
|
self.db = MySQLdb.connect(host=self.serverName,
|
|
user=self.user, passwd=self.password, db=self.subDB)
|
|
self.db.autocommit(False)
|
|
self.cursor = self.db.cursor()
|
|
except Exception, e:
|
|
raise Exception(e)
|
|
|
|
def close(self):
|
|
try:
|
|
self.db.commit()
|
|
self.cursor.close()
|
|
self.db.close()
|
|
except Exception, e:
|
|
print str(e)
|
|
|
|
def enable_foreign_key_check(self):
|
|
try:
|
|
self.cursor.execute('SET FOREIGN_KEY_CHECKS=1')
|
|
except Exception, e:
|
|
print str(e)
|
|
|
|
def disable_foreign_key_check(self):
|
|
try:
|
|
self.cursor.execute('SET FOREIGN_KEY_CHECKS=0')
|
|
except Exception, e:
|
|
print str(e)
|
|
|
|
def get_index_id(self, projects, project_id):
|
|
for project in projects:
|
|
if project.project_id == project_id:
|
|
return project.index_id
|
|
return ''
|
|
|
|
def update_access_log_table(self, projects):
|
|
access_logs = []
|
|
try:
|
|
query_sccess_log = \
|
|
'SELECT log_id, project_id from registry.access_log'
|
|
self.cursor.execute(query_sccess_log)
|
|
self.cursor.fetchall()
|
|
for result in self.cursor:
|
|
access_logs.append(AccessLog(result[0], result[1]))
|
|
except Exception, e:
|
|
raise Exception(e)
|
|
|
|
for item in access_logs:
|
|
index_id = self.get_index_id(projects, item.project_id)
|
|
if index_id != '':
|
|
try:
|
|
update_access_log_project_id = \
|
|
'UPDATE registry.access_log SET project_id=%s where log_id=%s' \
|
|
% (index_id, item.log_id)
|
|
self.cursor.execute(update_access_log_project_id)
|
|
except Exception, e:
|
|
raise Exception(e)
|
|
|
|
def update_repository_table(self, projects):
|
|
repositories = []
|
|
try:
|
|
query_repository = \
|
|
'SELECT repository_id, project_id from registry.repository'
|
|
self.cursor.execute(query_repository)
|
|
self.cursor.fetchall()
|
|
for result in self.cursor:
|
|
repositories.append(Repository(result[0], result[1]))
|
|
except Exception, e:
|
|
raise Exception(e)
|
|
|
|
for item in repositories:
|
|
index_id = self.get_index_id(projects, item.project_id)
|
|
if index_id != '':
|
|
try:
|
|
update_repository_project_id = \
|
|
'UPDATE registry.repository SET project_id=%s where repository_id=%s' \
|
|
% (index_id, item.repository_id)
|
|
self.cursor.execute(update_repository_project_id)
|
|
except Exception, e:
|
|
raise Exception(e)
|
|
|
|
def update_replication_policy_table(self, projects):
|
|
replication_policies = []
|
|
try:
|
|
query_replication_policy = \
|
|
'SELECT id, project_id from registry.replication_policy'
|
|
self.cursor.execute(query_replication_policy)
|
|
self.cursor.fetchall()
|
|
for result in self.cursor:
|
|
replication_policies.append(ReplicationPolicy(result[0],
|
|
result[1]))
|
|
except Exception, e:
|
|
raise Exception(e)
|
|
|
|
for item in replication_policies:
|
|
index_id = self.get_index_id(projects, item.project_id)
|
|
if index_id != '':
|
|
try:
|
|
update_replication_policy_id = \
|
|
'UPDATE registry.replication_policy SET project_id=%s where id=%s' \
|
|
% (index_id, item.replication_policy_id)
|
|
self.cursor.execute(update_replication_policy_id)
|
|
except Exception, e:
|
|
raise Exception(e)
|
|
|
|
|
|
def main():
|
|
commandline_input = Parameters()
|
|
harbor = HarborUtil(commandline_input.dbuser,
|
|
commandline_input.dbpwd)
|
|
|
|
try:
|
|
harbor.connect()
|
|
harbor.disable_foreign_key_check()
|
|
|
|
with open(commandline_input.mapprojectsfile, 'r') as \
|
|
project_mapping_file:
|
|
project_mapping_data = json.load(project_mapping_file)
|
|
|
|
projects_mapping_list = []
|
|
for item in project_mapping_data['map_projects']:
|
|
projects_mapping_list.append(Project(item['project_id'],
|
|
item['project_name'], item['index_id']))
|
|
|
|
harbor.update_access_log_table(projects_mapping_list)
|
|
harbor.update_repository_table(projects_mapping_list)
|
|
harbor.update_replication_policy_table(projects_mapping_list)
|
|
except Exception, e:
|
|
print e
|
|
sys.exit(1)
|
|
finally:
|
|
harbor.enable_foreign_key_check()
|
|
harbor.close()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|
|
|
|
|
|
|