-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathexport.go
190 lines (170 loc) · 5.57 KB
/
export.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package export
import (
"archive/tar"
"compress/gzip"
"context"
"fmt"
"io"
"kitops/pkg/artifact"
"kitops/pkg/lib/constants"
"kitops/pkg/lib/filesystem"
"kitops/pkg/lib/repo"
"kitops/pkg/output"
"os"
"path"
"path/filepath"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content"
"oras.land/oras-go/v2/registry"
"sigs.k8s.io/yaml"
)
func exportModel(ctx context.Context, store oras.Target, ref *registry.Reference, options *exportOptions) error {
manifestDesc, err := store.Resolve(ctx, ref.Reference)
if err != nil {
return fmt.Errorf("failed to resolve local reference: %w", err)
}
manifest, config, err := repo.GetManifestAndConfig(ctx, store, manifestDesc)
if err != nil {
return fmt.Errorf("failed to read local model: %s", err)
}
if options.exportConf.exportConfig {
if err := exportConfig(config, options.exportDir, options.overwrite); err != nil {
return err
}
}
// Since there might be multiple models, etc. we need to synchronously iterate
// through the config's relevant field to get the correct path for exporting
var codeIdx, datasetIdx int
for _, layerDesc := range manifest.Layers {
layerDir := ""
switch layerDesc.MediaType {
case constants.ModelLayerMediaType:
if !options.exportConf.exportModels {
continue
}
modelEntry := config.Model
layerDir = filepath.Join(options.exportDir, modelEntry.Path)
output.Infof("Exporting model %s to %s", modelEntry.Name, layerDir)
case constants.CodeLayerMediaType:
if !options.exportConf.exportCode {
continue
}
codeEntry := config.Code[codeIdx]
layerDir = filepath.Join(options.exportDir, codeEntry.Path)
output.Infof("Exporting code to %s", layerDir)
codeIdx += 1
case constants.DataSetLayerMediaType:
if !options.exportConf.exportDatasets {
continue
}
datasetEntry := config.DataSets[datasetIdx]
layerDir = filepath.Join(options.exportDir, datasetEntry.Path)
output.Infof("Exporting dataset %s to %s", datasetEntry.Name, layerDir)
datasetIdx += 1
}
if err := exportLayer(ctx, store, layerDesc, layerDir, options.overwrite); err != nil {
return err
}
}
output.Debugf("Exported %d code layers", codeIdx)
output.Debugf("Exported %d dataset layers", datasetIdx)
return nil
}
func exportConfig(config *artifact.KitFile, exportDir string, overwrite bool) error {
configPath := path.Join(exportDir, constants.DefaultKitFileName)
if fi, exists := filesystem.PathExists(configPath); exists {
if !overwrite {
return fmt.Errorf("failed to export config: path %s already exists", configPath)
} else if !fi.Mode().IsRegular() {
return fmt.Errorf("failed to export config: path %s exists and is not a regular file", configPath)
}
}
configBytes, err := yaml.Marshal(config)
if err != nil {
return fmt.Errorf("failed to export config: %w", err)
}
output.Infof("Exporting config to %s", configPath)
if err := os.WriteFile(configPath, configBytes, 0644); err != nil {
return fmt.Errorf("failed to write config file: %w", err)
}
return nil
}
func exportLayer(ctx context.Context, store content.Storage, desc ocispec.Descriptor, exportDir string, overwrite bool) error {
rc, err := store.Fetch(ctx, desc)
if err != nil {
return fmt.Errorf("failed get layer %s: %w", desc.Digest, err)
}
defer rc.Close()
gzr, err := gzip.NewReader(rc)
if err != nil {
return fmt.Errorf("error extracting gzipped file: %w", err)
}
defer gzr.Close()
tr := tar.NewReader(gzr)
if fi, exists := filesystem.PathExists(exportDir); exists {
if !overwrite {
return fmt.Errorf("failed to export: path %s already exists", exportDir)
} else if !fi.IsDir() {
return fmt.Errorf("failed to export: path %s exists and is not a directory", exportDir)
}
output.Debugf("Directory %s already exists", exportDir)
}
if err := os.MkdirAll(exportDir, 0755); err != nil {
return fmt.Errorf("failed to create directory %s: %w", exportDir, err)
}
return extractTar(tr, exportDir, overwrite)
}
func extractTar(tr *tar.Reader, dir string, overwrite bool) error {
for {
header, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
outPath := path.Join(dir, header.Name)
switch header.Typeflag {
case tar.TypeDir:
if fi, exists := filesystem.PathExists(outPath); exists {
if !overwrite {
return fmt.Errorf("path '%s' already exists", outPath)
}
if !fi.IsDir() {
return fmt.Errorf("path '%s' already exists and is not a directory", outPath)
}
output.Debugf("Path %s already exists", outPath)
}
output.Debugf("Creating directory %s", outPath)
if err := os.MkdirAll(outPath, header.FileInfo().Mode()); err != nil {
return fmt.Errorf("failed to create directory %s: %w", outPath, err)
}
case tar.TypeReg:
if fi, exists := filesystem.PathExists(outPath); exists {
if !overwrite {
return fmt.Errorf("path '%s' already exists", outPath)
}
if !fi.Mode().IsRegular() {
return fmt.Errorf("path '%s' already exists and is not a regular file", outPath)
}
}
output.Debugf("Extracting file %s", outPath)
file, err := os.OpenFile(outPath, os.O_CREATE|os.O_TRUNC|os.O_RDWR, header.FileInfo().Mode())
if err != nil {
return fmt.Errorf("failed to create file %s: %w", outPath, err)
}
defer file.Close()
written, err := io.Copy(file, tr)
if err != nil {
return fmt.Errorf("failed to write file %s: %w", outPath, err)
}
if written != header.Size {
return fmt.Errorf("could not extract file %s", outPath)
}
default:
return fmt.Errorf("Unrecognized type in archive: %s", header.Name)
}
}
return nil
}