gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/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  	"gvisor.dev/gvisor/pkg/log"
    24  	"gvisor.dev/gvisor/runsc/cmd/util"
    25  	"gvisor.dev/gvisor/runsc/config"
    26  	"gvisor.dev/gvisor/runsc/container"
    27  	"gvisor.dev/gvisor/runsc/flag"
    28  )
    29  
    30  // Delete implements subcommands.Command for the "delete" command.
    31  type Delete struct {
    32  	// force indicates that the container should be terminated if running.
    33  	force bool
    34  }
    35  
    36  // Name implements subcommands.Command.Name.
    37  func (*Delete) Name() string {
    38  	return "delete"
    39  }
    40  
    41  // Synopsis implements subcommands.Command.Synopsis.
    42  func (*Delete) Synopsis() string {
    43  	return "delete resources held by a container"
    44  }
    45  
    46  // Usage implements subcommands.Command.Usage.
    47  func (*Delete) Usage() string {
    48  	return `delete [flags] <container ids>`
    49  }
    50  
    51  // SetFlags implements subcommands.Command.SetFlags.
    52  func (d *Delete) SetFlags(f *flag.FlagSet) {
    53  	f.BoolVar(&d.force, "force", false, "terminate container if running")
    54  }
    55  
    56  // Execute implements subcommands.Command.Execute.
    57  func (d *Delete) Execute(_ context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus {
    58  	if f.NArg() == 0 {
    59  		f.Usage()
    60  		return subcommands.ExitUsageError
    61  	}
    62  
    63  	conf := args[0].(*config.Config)
    64  	if err := d.execute(f.Args(), conf); err != nil {
    65  		util.Fatalf("%v", err)
    66  	}
    67  	return subcommands.ExitSuccess
    68  }
    69  
    70  func (d *Delete) execute(ids []string, conf *config.Config) error {
    71  	for _, id := range ids {
    72  		c, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, container.LoadOpts{})
    73  		if err != nil {
    74  			if os.IsNotExist(err) && d.force {
    75  				log.Warningf("couldn't find container %q: %v", id, err)
    76  				return nil
    77  			}
    78  			return fmt.Errorf("loading container %q: %v", id, err)
    79  		}
    80  		if !d.force && c.Status != container.Created && c.Status != container.Stopped {
    81  			return fmt.Errorf("cannot delete container that is not stopped without --force flag")
    82  		}
    83  		if err := c.Destroy(); err != nil {
    84  			return fmt.Errorf("destroying container: %v", err)
    85  		}
    86  	}
    87  	return nil
    88  }