github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/pod_create.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/containers/libpod/cmd/podman/cliconfig"
     8  	"github.com/containers/libpod/cmd/podman/shared"
     9  	"github.com/containers/libpod/cmd/podman/shared/parse"
    10  	"github.com/containers/libpod/libpod/define"
    11  	"github.com/containers/libpod/pkg/adapter"
    12  	"github.com/containers/libpod/pkg/errorhandling"
    13  	"github.com/containers/libpod/pkg/util"
    14  	"github.com/pkg/errors"
    15  	"github.com/sirupsen/logrus"
    16  	"github.com/spf13/cobra"
    17  )
    18  
    19  var (
    20  	// Kernel namespaces shared by default within a pod
    21  
    22  	podCreateCommand cliconfig.PodCreateValues
    23  
    24  	podCreateDescription = `After creating the pod, the pod ID is printed to stdout.
    25  
    26    You can then start it at any time with the  podman pod start <pod_id> command. The pod will be created with the initial state 'created'.`
    27  
    28  	_podCreateCommand = &cobra.Command{
    29  		Use:   "create",
    30  		Args:  noSubArgs,
    31  		Short: "Create a new empty pod",
    32  		Long:  podCreateDescription,
    33  		RunE: func(cmd *cobra.Command, args []string) error {
    34  			podCreateCommand.InputArgs = args
    35  			podCreateCommand.GlobalFlags = MainGlobalOpts
    36  			podCreateCommand.Remote = remoteclient
    37  			return podCreateCmd(&podCreateCommand)
    38  		},
    39  	}
    40  )
    41  
    42  func init() {
    43  	podCreateCommand.Command = _podCreateCommand
    44  	podCreateCommand.SetHelpTemplate(HelpTemplate())
    45  	podCreateCommand.SetUsageTemplate(UsageTemplate())
    46  	flags := podCreateCommand.Flags()
    47  	flags.SetInterspersed(false)
    48  	flags.AddFlagSet(getNetFlags())
    49  	flags.StringVar(&podCreateCommand.CgroupParent, "cgroup-parent", "", "Set parent cgroup for the pod")
    50  	flags.BoolVar(&podCreateCommand.Infra, "infra", true, "Create an infra container associated with the pod to share namespaces with")
    51  	flags.StringVar(&podCreateCommand.InfraImage, "infra-image", define.DefaultInfraImage, "The image of the infra container to associate with the pod")
    52  	flags.StringVar(&podCreateCommand.InfraCommand, "infra-command", define.DefaultInfraCommand, "The command to run on the infra container when the pod is started")
    53  	flags.StringSliceVar(&podCreateCommand.LabelFile, "label-file", []string{}, "Read in a line delimited file of labels")
    54  	flags.StringSliceVarP(&podCreateCommand.Labels, "label", "l", []string{}, "Set metadata on pod (default [])")
    55  	flags.StringVarP(&podCreateCommand.Name, "name", "n", "", "Assign a name to the pod")
    56  	flags.StringVarP(&podCreateCommand.Hostname, "hostname", "", "", "Set a hostname to the pod")
    57  	flags.StringVar(&podCreateCommand.PodIDFile, "pod-id-file", "", "Write the pod ID to the file")
    58  	flags.StringVar(&podCreateCommand.Share, "share", shared.DefaultKernelNamespaces, "A comma delimited list of kernel namespaces the pod will share")
    59  
    60  }
    61  func podCreateCmd(c *cliconfig.PodCreateValues) error {
    62  	var (
    63  		err       error
    64  		podIdFile *os.File
    65  	)
    66  
    67  	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
    68  	if err != nil {
    69  		return errors.Wrapf(err, "error creating libpod runtime")
    70  	}
    71  	defer runtime.DeferredShutdown(false)
    72  
    73  	if len(c.StringSlice("publish")) > 0 {
    74  		if !c.Infra {
    75  			return errors.Errorf("you must have an infra container to publish port bindings to the host")
    76  		}
    77  	}
    78  
    79  	if !c.Infra && c.Flag("share").Changed && c.Share != "none" && c.Share != "" {
    80  		return errors.Errorf("You cannot share kernel namespaces on the pod level without an infra container")
    81  	}
    82  	if c.Flag("pod-id-file").Changed {
    83  		podIdFile, err = util.OpenExclusiveFile(c.PodIDFile)
    84  		if err != nil && os.IsExist(err) {
    85  			return errors.Errorf("pod id file exists. Ensure another pod is not using it or delete %s", c.PodIDFile)
    86  		}
    87  		if err != nil {
    88  			return errors.Errorf("error opening pod-id-file %s", c.PodIDFile)
    89  		}
    90  		defer errorhandling.CloseQuiet(podIdFile)
    91  		defer errorhandling.SyncQuiet(podIdFile)
    92  	}
    93  
    94  	labels, err := parse.GetAllLabels(c.LabelFile, c.Labels)
    95  	if err != nil {
    96  		return errors.Wrapf(err, "unable to process labels")
    97  	}
    98  
    99  	podID, err := runtime.CreatePod(getContext(), c, labels)
   100  	if err != nil {
   101  		return errors.Wrapf(err, "unable to create pod")
   102  	}
   103  	if podIdFile != nil {
   104  		_, err = podIdFile.WriteString(podID)
   105  		if err != nil {
   106  			logrus.Error(err)
   107  		}
   108  	}
   109  	fmt.Printf("%s\n", podID)
   110  	return nil
   111  }