3
3
import subprocess
4
4
import signal
5
5
import sys
6
+ import os
6
7
from pathlib import Path
8
+ import tarfile
9
+ import yaml
7
10
8
11
import click
9
12
import requests
@@ -32,24 +35,51 @@ def graceful_exit(signum, frame):
32
35
signal .signal (signal .SIGINT , graceful_exit )
33
36
signal .signal (signal .SIGTERM , graceful_exit )
34
37
38
+ def find_chart_yaml (tar , path = '' ):
39
+ # Iterate through the members of the tarfile
40
+ for member in tar .getmembers ():
41
+ # If the member is a directory, recursively search within it
42
+ if member .isdir ():
43
+ find_chart_yaml (tar , os .path .join (path , member .name ))
44
+ # If the member is a file and its name is 'chart.yaml', return its path
45
+ if "Chart.yaml" in member .name :
46
+ return os .path .join (path , member .name )
47
+
48
+ def read_chart_version (chart_tgz_path ):
49
+ # Open the chart tgz file
50
+ with tarfile .open (chart_tgz_path , 'r:gz' ) as tar :
51
+ # Find the path to chart.yaml within the tarball
52
+ chart_yaml_path = find_chart_yaml (tar )
53
+ if chart_yaml_path :
54
+ # Extract the chart.yaml file
55
+ chart_yaml_file = tar .extractfile (chart_yaml_path )
56
+ if chart_yaml_file is not None :
57
+ # Load the YAML content from chart.yaml
58
+ chart_data = yaml .safe_load (chart_yaml_file )
59
+ # Read the version from chart.yaml
60
+ version = chart_data .get ('version' )
61
+ name = chart_data .get ('name' )
62
+ return name , version
63
+ else :
64
+ raise Exception ("Failed to read chart.yaml from the chart tgz file. filename {}" .format (chart_tgz_path ))
65
+ else :
66
+ raise Exception ("chart.yaml not found in the chart tgz file. filename {}" .format (chart_tgz_path ))
67
+
35
68
class ChartV2 :
36
69
37
70
def __init__ (self , filepath :Path ):
38
71
self .filepath = filepath
39
72
self .project = self .filepath .parts [- 2 ]
40
- parts = self . filepath . stem . split ( '-' )
41
- flag = False
73
+ self . name = ""
74
+ self . version = ""
42
75
try :
43
- for i in range (len (parts )- 1 , - 1 , - 1 ):
44
- if parts [i ][0 ].isnumeric () or ((parts [i ][0 ]== 'v' or parts [i ][0 ]== 'v' ) and parts [i ][1 ].isnumeric ()) :
45
- self .name , self .version = '-' .join (parts [:i ]), '-' .join (parts [i :])
46
- flag = True
47
- break
48
- if not flag :
76
+ self .name , self .version = read_chart_version (filepath )
77
+ if self .name == "" or self .version == "" or self .name is None or self .version is None :
49
78
raise Exception ('chart name: {} is illegal' .format ('-' .join (parts )))
50
79
except Exception as e :
51
80
click .echo ("Skipped chart: {} due to illegal chart name. Error: {}" .format (filepath , e ), err = True )
52
81
return
82
+
53
83
def __check_exist (self , hostname , username , password ):
54
84
return requests .get (CHART_URL_PATTERN .format (
55
85
host = hostname ,
@@ -90,6 +120,9 @@ def migrate(hostname, username, password):
90
120
item_show_func = lambda x : "{}/{}:{} total errors: {}" .format (x .project , x .name , x .version , len (errs )) if x else '' ) as bar :
91
121
for chart in bar :
92
122
try :
123
+ if chart .name == "" or chart .version == "" :
124
+ print ("skip the chart {} has no name or version info" .format (chart .filepath ))
125
+ continue
93
126
result = chart .migrate (hostname , username , password )
94
127
if result .stderr :
95
128
errs .append ("chart: {name}:{version} in {project} has err: {err}" .format (
@@ -99,10 +132,11 @@ def migrate(hostname, username, password):
99
132
err = result .stderr
100
133
))
101
134
except Exception as e :
102
- errs .append ("chart: {name}:{version} in {project} has err: {err}" .format (
135
+ errs .append ("chart: {name}:{version} in {project}, path {path} has err: {err}" .format (
103
136
name = chart .name ,
104
137
version = chart .version ,
105
138
project = chart .project ,
139
+ path = chart .filepath ,
106
140
err = e ))
107
141
click .echo ("Migration is Done." )
108
142
print_exist_errs ()
0 commit comments