github.com/dctrud/umoci@v0.4.3-0.20191016193643-05a1d37de015/cmd/umoci/gc.go (about)

     1  /*
     2   * umoci: Umoci Modifies Open Containers' Images
     3   * Copyright (C) 2016, 2017, 2018 SUSE LLC.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *    http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package main
    19  
    20  import (
    21  	"github.com/openSUSE/umoci/oci/cas/dir"
    22  	"github.com/openSUSE/umoci/oci/casext"
    23  	"github.com/pkg/errors"
    24  	"github.com/urfave/cli"
    25  	"golang.org/x/net/context"
    26  )
    27  
    28  var gcCommand = cli.Command{
    29  	Name:  "gc",
    30  	Usage: "garbage-collects an OCI image's blobs",
    31  	ArgsUsage: `--layout <image-path>
    32  
    33  Where "<image-path>" is the path to the OCI image.
    34  
    35  This command will do a mark-and-sweep garbage collection of the provided OCI
    36  image, only retaining blobs which can be reached by a descriptor path from the
    37  root set of references. All other blobs will be removed.`,
    38  
    39  	// create modifies an image layout.
    40  	Category: "layout",
    41  
    42  	Before: func(ctx *cli.Context) error {
    43  		if _, ok := ctx.App.Metadata["--image-path"]; !ok {
    44  			return errors.Errorf("missing mandatory argument: --layout")
    45  		}
    46  		return nil
    47  	},
    48  
    49  	Action: gc,
    50  }
    51  
    52  func gc(ctx *cli.Context) error {
    53  	imagePath := ctx.App.Metadata["--image-path"].(string)
    54  
    55  	// Get a reference to the CAS.
    56  	engine, err := dir.Open(imagePath)
    57  	if err != nil {
    58  		return errors.Wrap(err, "open CAS")
    59  	}
    60  	engineExt := casext.NewEngine(engine)
    61  	defer engine.Close()
    62  
    63  	// Run the GC.
    64  	return errors.Wrap(engineExt.GC(context.Background()), "gc")
    65  }