| 1 |
/* |
| 2 |
Copyright The containerd 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 commands |
| 18 |
|
| 19 |
import ( |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
|
| 23 |
"github.com/containerd/containerd/cmd/ctr/commands" |
| 24 |
"github.com/containerd/containerd/images/converter" |
| 25 |
"github.com/containerd/containerd/platforms" |
| 26 |
"github.com/containerd/stargz-snapshotter/ipfs" |
| 27 |
estargzconvert "github.com/containerd/stargz-snapshotter/nativeconverter/estargz" |
| 28 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 29 |
"github.com/sirupsen/logrus" |
| 30 |
"github.com/urfave/cli" |
| 31 |
) |
| 32 |
|
| 33 |
// IPFSPushCommand pushes an image to IPFS |
| 34 |
var IPFSPushCommand = cli.Command{ |
| 35 |
Name: "ipfs-push", |
| 36 |
Usage: "push an image to IPFS (experimental)", |
| 37 |
ArgsUsage: "[flags] <image_ref>", |
| 38 |
Flags: []cli.Flag{ |
| 39 |
// platform flags |
| 40 |
cli.StringSliceFlag{ |
| 41 |
Name: "platform", |
| 42 |
Usage: "Add content for a specific platform", |
| 43 |
Value: &cli.StringSlice{}, |
| 44 |
}, |
| 45 |
cli.BoolFlag{ |
| 46 |
Name: "all-platforms", |
| 47 |
Usage: "Add content for all platforms", |
| 48 |
}, |
| 49 |
cli.BoolTFlag{ |
| 50 |
Name: "estargz", |
| 51 |
Usage: "Convert the image into eStargz", |
| 52 |
}, |
| 53 |
}, |
| 54 |
Action: func(context *cli.Context) error { |
| 55 |
srcRef := context.Args().Get(0) |
| 56 |
if srcRef == "" { |
| 57 |
return errors.New("image need to be specified") |
| 58 |
} |
| 59 |
|
| 60 |
var platformMC platforms.MatchComparer |
| 61 |
if context.Bool("all-platforms") { |
| 62 |
platformMC = platforms.All |
| 63 |
} else { |
| 64 |
if pss := context.StringSlice("platform"); len(pss) > 0 { |
| 65 |
var all []ocispec.Platform |
| 66 |
for _, ps := range pss { |
| 67 |
p, err := platforms.Parse(ps) |
| 68 |
if err != nil { |
| 69 |
return fmt.Errorf("invalid platform %q: %w", ps, err) |
| 70 |
} |
| 71 |
all = append(all, p) |
| 72 |
} |
| 73 |
platformMC = platforms.Ordered(all...) |
| 74 |
} else { |
| 75 |
platformMC = platforms.DefaultStrict() |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
client, ctx, cancel, err := commands.NewClient(context) |
| 80 |
if err != nil { |
| 81 |
return err |
| 82 |
} |
| 83 |
defer cancel() |
| 84 |
|
| 85 |
var layerConvert converter.ConvertFunc |
| 86 |
if context.Bool("estargz") { |
| 87 |
layerConvert = estargzconvert.LayerConvertFunc() |
| 88 |
} |
| 89 |
p, err := ipfs.Push(ctx, client, srcRef, layerConvert, platformMC) |
| 90 |
if err != nil { |
| 91 |
return err |
| 92 |
} |
| 93 |
logrus.WithField("CID", p).Infof("Pushed") |
| 94 |
fmt.Println(p) |
| 95 |
|
| 96 |
return nil |
| 97 |
}, |
| 98 |
} |