github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/runsc/cmd/delete.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cmd
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"os"
    21  
    22  	"github.com/google/subcommands"
    23  	"github.com/SagerNet/gvisor/pkg/log"
    24  	"github.com/SagerNet/gvisor/runsc/config"
    25  	"github.com/SagerNet/gvisor/runsc/container"
    26  	"github.com/SagerNet/gvisor/runsc/flag"
    27  )
    28  
    29  // Delete implements subcommands.Command for the "delete" command.
    30  type Delete struct {
    31  	// force indicates that the container should be terminated if running.
    32  	force bool
    33  }
    34  
    35  // Name implements subcommands.Command.Name.
    36  func (*Delete) Name() string {
    37  	return "delete"
    38  }
    39  
    40  // Synopsis implements subcommands.Command.Synopsis.
    41  func (*Delete) Synopsis() string {
    42  	return "delete resources held by a container"
    43  }
    44  
    45  // Usage implements subcommands.Command.Usage.
    46  func (*Delete) Usage() string {
    47  	return `delete [flags] <container ids>`
    48  }
    49  
    50  // SetFlags implements subcommands.Command.SetFlags.
    51  func (d *Delete) SetFlags(f *flag.FlagSet) {
    52  	f.BoolVar(&d.force, "force", false, "terminate container if running")
    53  }
    54  
    55  // Execute implements subcommands.Command.Execute.
    56  func (d *Delete) Execute(_ context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
    57  	if f.NArg() == 0 {
    58  		f.Usage()
    59  		return subcommands.ExitUsageError
    60  	}
    61  
    62  	conf := args[0].(*config.Config)
    63  	if err := d.execute(f.Args(), conf); err != nil {
    64  		Fatalf("%v", err)
    65  	}
    66  	return subcommands.ExitSuccess
    67  }
    68  
    69  func (d *Delete) execute(ids []string, conf *config.Config) error {
    70  	for _, id := range ids {
    71  		c, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, container.LoadOpts{})
    72  		if err != nil {
    73  			if os.IsNotExist(err) && d.force {
    74  				log.Warningf("couldn't find container %q: %v", id, err)
    75  				return nil
    76  			}
    77  			return fmt.Errorf("loading container %q: %v", id, err)
    78  		}
    79  		if !d.force && c.Status != container.Created && c.Status != container.Stopped {
    80  			return fmt.Errorf("cannot delete container that is not stopped without --force flag")
    81  		}
    82  		if err := c.Destroy(); err != nil {
    83  			return fmt.Errorf("destroying container: %v", err)
    84  		}
    85  	}
    86  	return nil
    87  }