|
| 1 | +/* |
| 2 | + * Copyright 2024 The CNAI Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package backend |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "fmt" |
| 23 | + "path/filepath" |
| 24 | + |
| 25 | + "github.com/CloudNativeAI/modctl/pkg/archiver" |
| 26 | + "github.com/CloudNativeAI/modctl/pkg/storage" |
| 27 | + modelspec "github.com/CloudNativeAI/model-spec/specs-go/v1" |
| 28 | + |
| 29 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 30 | +) |
| 31 | + |
| 32 | +// Extract extracts the model artifact. |
| 33 | +func (b *backend) Extract(ctx context.Context, target string, output string) error { |
| 34 | + // parse the repository and tag from the target. |
| 35 | + ref, err := ParseReference(target) |
| 36 | + if err != nil { |
| 37 | + return fmt.Errorf("failed to parse the target: %w", err) |
| 38 | + } |
| 39 | + |
| 40 | + repo, tag := ref.Repository(), ref.Tag() |
| 41 | + // pull the manifest from the storage. |
| 42 | + manifestRaw, _, err := b.store.PullManifest(ctx, repo, tag) |
| 43 | + if err != nil { |
| 44 | + return fmt.Errorf("failed to pull the manifest from storage: %w", err) |
| 45 | + } |
| 46 | + // unmarshal the manifest. |
| 47 | + var manifest ocispec.Manifest |
| 48 | + if err := json.Unmarshal(manifestRaw, &manifest); err != nil { |
| 49 | + return fmt.Errorf("failed to unmarshal the manifest: %w", err) |
| 50 | + } |
| 51 | + |
| 52 | + return exportModelArtifact(ctx, b.store, manifest, repo, output) |
| 53 | +} |
| 54 | + |
| 55 | +// exportModelArtifact exports the target model artifact to the output directory, which will open the artifact and extract to restore the orginal repo structure. |
| 56 | +func exportModelArtifact(ctx context.Context, store storage.Storage, manifest ocispec.Manifest, repo, output string) error { |
| 57 | + for _, layer := range manifest.Layers { |
| 58 | + // pull the blob from the storage. |
| 59 | + reader, err := store.PullBlob(ctx, repo, layer.Digest.String()) |
| 60 | + if err != nil { |
| 61 | + return fmt.Errorf("failed to pull the blob from storage: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + defer reader.Close() |
| 65 | + |
| 66 | + targetDir := output |
| 67 | + // get the original filepath in order to restore the original repo structure. |
| 68 | + originalFilePath := layer.Annotations[modelspec.AnnotationFilepath] |
| 69 | + if dir := filepath.Dir(originalFilePath); dir != "" { |
| 70 | + targetDir = filepath.Join(targetDir, dir) |
| 71 | + } |
| 72 | + |
| 73 | + // untar the blob to the target directory. |
| 74 | + if err := archiver.Untar(reader, targetDir); err != nil { |
| 75 | + return fmt.Errorf("failed to untar the blob to output directory: %w", err) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return nil |
| 80 | +} |
0 commit comments