github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/runsc/cmd/list.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 "encoding/json" 20 "fmt" 21 "os" 22 "text/tabwriter" 23 "time" 24 25 "github.com/google/subcommands" 26 specs "github.com/opencontainers/runtime-spec/specs-go" 27 "github.com/SagerNet/gvisor/pkg/log" 28 "github.com/SagerNet/gvisor/runsc/config" 29 "github.com/SagerNet/gvisor/runsc/container" 30 "github.com/SagerNet/gvisor/runsc/flag" 31 ) 32 33 // List implements subcommands.Command for the "list" command for the "list" command. 34 type List struct { 35 quiet bool 36 format string 37 } 38 39 // Name implements subcommands.command.name. 40 func (*List) Name() string { 41 return "list" 42 } 43 44 // Synopsis implements subcommands.Command.Synopsis. 45 func (*List) Synopsis() string { 46 return "list containers started by runsc with the given root" 47 } 48 49 // Usage implements subcommands.Command.Usage. 50 func (*List) Usage() string { 51 return `list [flags]` 52 } 53 54 // SetFlags implements subcommands.Command.SetFlags. 55 func (l *List) SetFlags(f *flag.FlagSet) { 56 f.BoolVar(&l.quiet, "quiet", false, "only list container ids") 57 f.StringVar(&l.format, "format", "text", "output format: 'text' (default) or 'json'") 58 } 59 60 // Execute implements subcommands.Command.Execute. 61 func (l *List) Execute(_ context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus { 62 if f.NArg() != 0 { 63 f.Usage() 64 return subcommands.ExitUsageError 65 } 66 67 conf := args[0].(*config.Config) 68 ids, err := container.List(conf.RootDir) 69 if err != nil { 70 Fatalf("%v", err) 71 } 72 73 if l.quiet { 74 for _, id := range ids { 75 fmt.Println(id.ContainerID) 76 } 77 return subcommands.ExitSuccess 78 } 79 80 // Collect the containers. 81 var containers []*container.Container 82 for _, id := range ids { 83 c, err := container.Load(conf.RootDir, id, container.LoadOpts{Exact: true}) 84 if err != nil { 85 log.Warningf("Skipping container %q: %v", id, err) 86 continue 87 } 88 containers = append(containers, c) 89 } 90 91 switch l.format { 92 case "text": 93 // Print a nice table. 94 w := tabwriter.NewWriter(os.Stdout, 12, 1, 3, ' ', 0) 95 fmt.Fprint(w, "ID\tPID\tSTATUS\tBUNDLE\tCREATED\tOWNER\n") 96 for _, c := range containers { 97 fmt.Fprintf(w, "%s\t%d\t%s\t%s\t%s\t%s\n", 98 c.ID, 99 c.SandboxPid(), 100 c.Status, 101 c.BundleDir, 102 c.CreatedAt.Format(time.RFC3339Nano), 103 c.Owner) 104 } 105 w.Flush() 106 case "json": 107 // Print just the states. 108 var states []specs.State 109 for _, c := range containers { 110 states = append(states, c.State()) 111 } 112 if err := json.NewEncoder(os.Stdout).Encode(states); err != nil { 113 Fatalf("marshaling container state: %v", err) 114 } 115 default: 116 Fatalf("unknown list format %q", l.format) 117 } 118 return subcommands.ExitSuccess 119 }