|
| 1 | +package rofl |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + v1 "github.com/opencontainers/image-spec/specs-go/v1" |
| 10 | + oras "oras.land/oras-go/v2" |
| 11 | + "oras.land/oras-go/v2/content/file" |
| 12 | + "oras.land/oras-go/v2/registry/remote" |
| 13 | + "oras.land/oras-go/v2/registry/remote/auth" |
| 14 | + "oras.land/oras-go/v2/registry/remote/credentials" |
| 15 | + "oras.land/oras-go/v2/registry/remote/retry" |
| 16 | + |
| 17 | + "github.com/oasisprotocol/oasis-core/go/runtime/bundle" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + ociTypeOrcConfig = "application/vnd.oasis.orc.config.v1+json" |
| 22 | + ociTypeOrcLayer = "application/vnd.oasis.orc.layer.v1" |
| 23 | + ociTypeOrcArtifact = "application/vnd.oasis.orc" |
| 24 | +) |
| 25 | + |
| 26 | +// PushBundleToOciRepository pushes an ORC bundle to the given remote OCI repository. |
| 27 | +func PushBundleToOciRepository(bundleFn, dst, tag string) error { |
| 28 | + ctx := context.Background() |
| 29 | + |
| 30 | + // Open the bundle. |
| 31 | + bnd, err := bundle.Open(bundleFn) |
| 32 | + if err != nil { |
| 33 | + return fmt.Errorf("failed to open bundle: %w", err) |
| 34 | + } |
| 35 | + defer bnd.Close() |
| 36 | + |
| 37 | + // Create a temporary file store to build the OCI layers. |
| 38 | + tmpDir, err := os.MkdirTemp("", "oasis-orc2oci") |
| 39 | + if err != nil { |
| 40 | + return fmt.Errorf("failed to create temporary directory: %w", err) |
| 41 | + } |
| 42 | + defer os.RemoveAll(tmpDir) |
| 43 | + |
| 44 | + storeDir := filepath.Join(tmpDir, "oci") |
| 45 | + store, err := file.New(storeDir) |
| 46 | + if err != nil { |
| 47 | + return fmt.Errorf("failed to create temporary OCI store: %w", err) |
| 48 | + } |
| 49 | + defer store.Close() |
| 50 | + |
| 51 | + bundleDir := filepath.Join(tmpDir, "bundle") |
| 52 | + if err = bnd.WriteExploded(bundleDir); err != nil { |
| 53 | + return fmt.Errorf("failed to explode bundle: %w", err) |
| 54 | + } |
| 55 | + |
| 56 | + // Generate the config object from the manifest. |
| 57 | + const manifestName = "META-INF/MANIFEST.MF" |
| 58 | + configDsc, err := store.Add(ctx, manifestName, ociTypeOrcConfig, filepath.Join(bundleDir, manifestName)) |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("failed to add config object from manifest: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + // Add other files as layers. |
| 64 | + layers := make([]v1.Descriptor, 0, len(bnd.Data)-1) |
| 65 | + for fn := range bnd.Data { |
| 66 | + if fn == manifestName { |
| 67 | + continue |
| 68 | + } |
| 69 | + |
| 70 | + layerDsc, err := store.Add(ctx, fn, ociTypeOrcLayer, filepath.Join(bundleDir, fn)) |
| 71 | + if err != nil { |
| 72 | + return fmt.Errorf("failed to add OCI layer: %w", err) |
| 73 | + } |
| 74 | + |
| 75 | + layers = append(layers, layerDsc) |
| 76 | + } |
| 77 | + |
| 78 | + // Pack the OCI manifest. |
| 79 | + opts := oras.PackManifestOptions{ |
| 80 | + Layers: layers, |
| 81 | + ConfigDescriptor: &configDsc, |
| 82 | + } |
| 83 | + manifestDescriptor, err := oras.PackManifest(ctx, store, oras.PackManifestVersion1_1, ociTypeOrcArtifact, opts) |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("failed to pack OCI manifest: %w", err) |
| 86 | + } |
| 87 | + |
| 88 | + // Tag the manifest. |
| 89 | + if err = store.Tag(ctx, manifestDescriptor, tag); err != nil { |
| 90 | + return fmt.Errorf("failed to tag OCI manifest: %w", err) |
| 91 | + } |
| 92 | + |
| 93 | + // Connect to remote repository. |
| 94 | + repo, err := remote.NewRepository(dst) |
| 95 | + if err != nil { |
| 96 | + return fmt.Errorf("failed to init remote OCI repository: %w", err) |
| 97 | + } |
| 98 | + creds, err := credentials.NewStoreFromDocker(credentials.StoreOptions{}) |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("failed to init OCI credential store: %w", err) |
| 101 | + } |
| 102 | + repo.Client = &auth.Client{ |
| 103 | + Client: retry.DefaultClient, |
| 104 | + Cache: auth.NewCache(), |
| 105 | + Credential: credentials.Credential(creds), |
| 106 | + } |
| 107 | + |
| 108 | + // Push to remote repository. |
| 109 | + if _, err = oras.Copy(ctx, store, tag, repo, tag, oras.DefaultCopyOptions); err != nil { |
| 110 | + return fmt.Errorf("failed to push to remote OCI repository: %w", err) |
| 111 | + } |
| 112 | + |
| 113 | + return nil |
| 114 | +} |
0 commit comments