github.com/demonoid81/containerd@v1.3.4/cmd/ctr/commands/commands.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package commands
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"os"
    23  	"path/filepath"
    24  	"strings"
    25  
    26  	"github.com/containerd/containerd/defaults"
    27  	"github.com/urfave/cli"
    28  )
    29  
    30  var (
    31  	// SnapshotterFlags are cli flags specifying snapshotter names
    32  	SnapshotterFlags = []cli.Flag{
    33  		cli.StringFlag{
    34  			Name:   "snapshotter",
    35  			Usage:  "snapshotter name. Empty value stands for the default value.",
    36  			EnvVar: "CONTAINERD_SNAPSHOTTER",
    37  		},
    38  	}
    39  
    40  	// LabelFlag is a cli flag specifying labels
    41  	LabelFlag = cli.StringSliceFlag{
    42  		Name:  "label",
    43  		Usage: "labels to attach to the image",
    44  	}
    45  
    46  	// RegistryFlags are cli flags specifying registry options
    47  	RegistryFlags = []cli.Flag{
    48  		cli.BoolFlag{
    49  			Name:  "skip-verify,k",
    50  			Usage: "skip SSL certificate validation",
    51  		},
    52  		cli.BoolFlag{
    53  			Name:  "plain-http",
    54  			Usage: "allow connections using plain HTTP",
    55  		},
    56  		cli.StringFlag{
    57  			Name:  "user,u",
    58  			Usage: "user[:password] Registry user and password",
    59  		},
    60  		cli.StringFlag{
    61  			Name:  "refresh",
    62  			Usage: "refresh token for authorization server",
    63  		},
    64  	}
    65  
    66  	// ContainerFlags are cli flags specifying container options
    67  	ContainerFlags = []cli.Flag{
    68  		cli.StringFlag{
    69  			Name:  "config,c",
    70  			Usage: "path to the runtime-specific spec config file",
    71  		},
    72  		cli.StringFlag{
    73  			Name:  "cwd",
    74  			Usage: "specify the working directory of the process",
    75  		},
    76  		cli.StringSliceFlag{
    77  			Name:  "env",
    78  			Usage: "specify additional container environment variables (i.e. FOO=bar)",
    79  		},
    80  		cli.StringFlag{
    81  			Name:  "env-file",
    82  			Usage: "specify additional container environment variables in a file(i.e. FOO=bar, one per line)",
    83  		},
    84  		cli.StringSliceFlag{
    85  			Name:  "label",
    86  			Usage: "specify additional labels (i.e. foo=bar)",
    87  		},
    88  		cli.StringSliceFlag{
    89  			Name:  "mount",
    90  			Usage: "specify additional container mount (ex: type=bind,src=/tmp,dst=/host,options=rbind:ro)",
    91  		},
    92  		cli.BoolFlag{
    93  			Name:  "net-host",
    94  			Usage: "enable host networking for the container",
    95  		},
    96  		cli.BoolFlag{
    97  			Name:  "privileged",
    98  			Usage: "run privileged container",
    99  		},
   100  		cli.BoolFlag{
   101  			Name:  "read-only",
   102  			Usage: "set the containers filesystem as readonly",
   103  		},
   104  		cli.StringFlag{
   105  			Name:  "runtime",
   106  			Usage: "runtime name",
   107  			Value: defaults.DefaultRuntime,
   108  		},
   109  		cli.BoolFlag{
   110  			Name:  "tty,t",
   111  			Usage: "allocate a TTY for the container",
   112  		},
   113  		cli.StringSliceFlag{
   114  			Name:  "with-ns",
   115  			Usage: "specify existing Linux namespaces to join at container runtime (format '<nstype>:<path>')",
   116  		},
   117  		cli.StringFlag{
   118  			Name:  "pid-file",
   119  			Usage: "file path to write the task's pid",
   120  		},
   121  		cli.IntFlag{
   122  			Name:  "gpus",
   123  			Usage: "add gpus to the container",
   124  		},
   125  		cli.BoolFlag{
   126  			Name:  "allow-new-privs",
   127  			Usage: "turn off OCI spec's NoNewPrivileges feature flag",
   128  		},
   129  		cli.Uint64Flag{
   130  			Name:  "memory-limit",
   131  			Usage: "memory limit (in bytes) for the container",
   132  		},
   133  		cli.StringSliceFlag{
   134  			Name:  "device",
   135  			Usage: "add a device to a container",
   136  		},
   137  		cli.BoolFlag{
   138  			Name:  "seccomp",
   139  			Usage: "enable the default seccomp profile",
   140  		},
   141  	}
   142  )
   143  
   144  // ObjectWithLabelArgs returns the first arg and a LabelArgs object
   145  func ObjectWithLabelArgs(clicontext *cli.Context) (string, map[string]string) {
   146  	var (
   147  		first        = clicontext.Args().First()
   148  		labelStrings = clicontext.Args().Tail()
   149  	)
   150  
   151  	return first, LabelArgs(labelStrings)
   152  }
   153  
   154  // LabelArgs returns a map of label key,value pairs
   155  func LabelArgs(labelStrings []string) map[string]string {
   156  	labels := make(map[string]string, len(labelStrings))
   157  	for _, label := range labelStrings {
   158  		parts := strings.SplitN(label, "=", 2)
   159  		key := parts[0]
   160  		value := "true"
   161  		if len(parts) > 1 {
   162  			value = parts[1]
   163  		}
   164  
   165  		labels[key] = value
   166  	}
   167  
   168  	return labels
   169  }
   170  
   171  // PrintAsJSON prints input in JSON format
   172  func PrintAsJSON(x interface{}) {
   173  	b, err := json.MarshalIndent(x, "", "    ")
   174  	if err != nil {
   175  		fmt.Fprintf(os.Stderr, "can't marshal %+v as a JSON string: %v\n", x, err)
   176  	}
   177  	fmt.Println(string(b))
   178  }
   179  
   180  // WritePidFile writes the pid atomically to a file
   181  func WritePidFile(path string, pid int) error {
   182  	path, err := filepath.Abs(path)
   183  	if err != nil {
   184  		return err
   185  	}
   186  	tempPath := filepath.Join(filepath.Dir(path), fmt.Sprintf(".%s", filepath.Base(path)))
   187  	f, err := os.OpenFile(tempPath, os.O_RDWR|os.O_CREATE|os.O_EXCL|os.O_SYNC, 0666)
   188  	if err != nil {
   189  		return err
   190  	}
   191  	_, err = fmt.Fprintf(f, "%d", pid)
   192  	f.Close()
   193  	if err != nil {
   194  		return err
   195  	}
   196  	return os.Rename(tempPath, path)
   197  }