github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/runsc/cmd/trace/delete.go (about)

     1  // Copyright 2020 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 trace
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/MerlinKodo/gvisor/runsc/cmd/util"
    22  	"github.com/MerlinKodo/gvisor/runsc/config"
    23  	"github.com/MerlinKodo/gvisor/runsc/container"
    24  	"github.com/MerlinKodo/gvisor/runsc/flag"
    25  	"github.com/google/subcommands"
    26  )
    27  
    28  // delete implements subcommands.Command for the "delete" command.
    29  type delete struct {
    30  	sessionName string
    31  }
    32  
    33  // Name implements subcommands.Command.
    34  func (*delete) Name() string {
    35  	return "delete"
    36  }
    37  
    38  // Synopsis implements subcommands.Command.
    39  func (*delete) Synopsis() string {
    40  	return "delete a trace session"
    41  }
    42  
    43  // Usage implements subcommands.Command.
    44  func (*delete) Usage() string {
    45  	return `delete [flags] <sandbox id> - delete a trace session
    46  `
    47  }
    48  
    49  // SetFlags implements subcommands.Command.
    50  func (l *delete) SetFlags(f *flag.FlagSet) {
    51  	f.StringVar(&l.sessionName, "name", "", "name of session to be deleted")
    52  }
    53  
    54  // Execute implements subcommands.Command.
    55  func (l *delete) Execute(_ context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus {
    56  	if f.NArg() != 1 {
    57  		f.Usage()
    58  		return subcommands.ExitUsageError
    59  	}
    60  	if len(l.sessionName) == 0 {
    61  		f.Usage()
    62  		return util.Errorf("missing session name, please set --name")
    63  	}
    64  
    65  	id := f.Arg(0)
    66  	conf := args[0].(*config.Config)
    67  
    68  	opts := container.LoadOpts{
    69  		SkipCheck:     true,
    70  		RootContainer: true,
    71  	}
    72  	c, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, opts)
    73  	if err != nil {
    74  		util.Fatalf("loading sandbox: %v", err)
    75  	}
    76  
    77  	if err := c.Sandbox.DeleteTraceSession(l.sessionName); err != nil {
    78  		util.Fatalf("deleting session: %v", err)
    79  	}
    80  
    81  	fmt.Printf("Trace session %q deleted.\n", l.sessionName)
    82  	return subcommands.ExitSuccess
    83  }