Handle error in migrate script (#18664)

Dump the total amount of errors

Signed-off-by: stonezdj <daojunz@vmware.com>
This commit is contained in:
stonezdj(Daojun Zhang) 2023-05-17 10:03:23 +08:00 committed by GitHub
parent cf1af0d450
commit 44faccf01b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 19 deletions

View File

@ -4,9 +4,9 @@ ENV HELM_EXPERIMENTAL_OCI=1
ENV REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt ENV REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt
COPY ./migrate_chart.py ./migrate_chart.sh / COPY ./migrate_chart.py ./migrate_chart.sh /
ADD https://get.helm.sh/helm-v3.2.4-linux-amd64.tar.gz / ADD https://get.helm.sh/helm-v3.9.1-linux-amd64.tar.gz /
RUN tar zxvf /helm-v3.2.4-linux-amd64.tar.gz && \ RUN tar zxvf /helm-v3.9.1-linux-amd64.tar.gz && \
pip install click==7.1.2 && \ pip install click==7.1.2 && \
pip install requests==2.24.0 && \ pip install requests==2.24.0 && \
chmod +x /migrate_chart.sh ./migrate_chart.py chmod +x /migrate_chart.sh ./migrate_chart.py

View File

@ -18,9 +18,12 @@ errs = []
def print_exist_errs(): def print_exist_errs():
if errs: if errs:
click.echo("Following errors exist", err=True) click.echo("There are {} errors exist".format(len(errs)), err=True)
for e in errs: for e in errs:
click.echo(e, err=True) click.echo(e, err=True)
# Write the error to file
with open("/chart_storage/migration_errors.txt", "a") as f:
f.write(e + "\n")
def graceful_exit(signum, frame): def graceful_exit(signum, frame):
print_exist_errs() print_exist_errs()
@ -36,14 +39,17 @@ class ChartV2:
self.project = self.filepath.parts[-2] self.project = self.filepath.parts[-2]
parts = self.filepath.stem.split('-') parts = self.filepath.stem.split('-')
flag = False flag = False
try:
for i in range(len(parts)-1, -1, -1): for i in range(len(parts)-1, -1, -1):
if parts[i][0].isnumeric(): if parts[i][0].isnumeric() or ((parts[i][0]=='v' or parts[i][0]=='v') and parts[i][1].isnumeric()) :
self.name, self.version = '-'.join(parts[:i]), '-'.join(parts[i:]) self.name, self.version = '-'.join(parts[:i]), '-'.join(parts[i:])
flag = True flag = True
break break
if not flag: if not flag:
raise Exception('chart name: {} is illegal'.format('-'.join(parts))) raise Exception('chart name: {} is illegal'.format('-'.join(parts)))
except Exception as e:
click.echo("Skipped chart: {} due to illegal chart name. Error: {}".format(filepath, e), err=True)
return
def __check_exist(self, hostname, username, password): def __check_exist(self, hostname, username, password):
return requests.get(CHART_URL_PATTERN.format( return requests.get(CHART_URL_PATTERN.format(
host=hostname, host=hostname,
@ -59,11 +65,9 @@ class ChartV2:
if res.status_code == 401: if res.status_code == 401:
raise Exception(res.reason) raise Exception(res.reason)
oci_ref = "{host}/{project}/{name}:{version}".format( oci_ref = "oci://{host}/{project}".format(
host=hostname, host=hostname,
project=self.project, project=self.project)
name=self.name,
version=self.version)
return subprocess.run([MIGRATE_CHART_SCRIPT, HELM_CMD, self.filepath, oci_ref], return subprocess.run([MIGRATE_CHART_SCRIPT, HELM_CMD, self.filepath, oci_ref],
text=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) text=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
@ -81,7 +85,7 @@ def migrate(hostname, username, password):
raise Exception('This operation only allowed for admin') raise Exception('This operation only allowed for admin')
subprocess.run([CA_UPDATE_CMD]) subprocess.run([CA_UPDATE_CMD])
subprocess.run([HELM_CMD, 'registry', 'login', hostname, '--username', username, '--password', password]) subprocess.run([HELM_CMD, 'registry', 'login', hostname, '--username', username, '--password', password])
charts = [ChartV2(c) for p in CHART_SOURCE_DIR.iterdir() if p.is_dir() for c in p.iterdir() if c.is_file() and c.name != "index-cache.yaml"] charts = [ChartV2(c) for p in CHART_SOURCE_DIR.iterdir() if p.is_dir() for c in p.iterdir() if c.is_file() and c.name.endswith(".tgz")]
with click.progressbar(charts, label="Migrating chart ...", length=len(charts), with click.progressbar(charts, label="Migrating chart ...", length=len(charts),
item_show_func=lambda x: "{}/{}:{} total errors: {}".format(x.project, x.name, x.version, len(errs)) if x else '') as bar: item_show_func=lambda x: "{}/{}:{} total errors: {}".format(x.project, x.name, x.version, len(errs)) if x else '') as bar:
for chart in bar: for chart in bar:

4
tools/migrate_chart/migrate_chart.sh Normal file → Executable file
View File

@ -4,6 +4,4 @@ HELM_CMD=$1
V2_CHART_PATH=$2 V2_CHART_PATH=$2
OCI_REF=$3 OCI_REF=$3
${HELM_CMD} chart save ${V2_CHART_PATH} ${OCI_REF} ${HELM_CMD} push ${V2_CHART_PATH} ${OCI_REF}
${HELM_CMD} chart push ${OCI_REF}
${HELM_CMD} chart remove ${OCI_REF}