From 32af1be01a37362d81baff47a2bb28e4c863a8c9 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Fri, 23 Oct 2020 06:58:41 -0400 Subject: The cidfile should be created when the container is created Currently if you run an interactive session of podman run and specifiy the --cidfile option, the cidfile will not get created until the container finishes running. If you run a detached container, it will get created right away. This Patch creates the cidfile as soon as the container is created. This could allow other tools to use the cidefile on all running containers. Signed-off-by: Daniel J Walsh --- pkg/domain/entities/containers.go | 1 + pkg/domain/infra/abi/containers.go | 7 +++++++ pkg/domain/infra/tunnel/containers.go | 6 ++++++ pkg/util/utils.go | 15 +++++++++++++++ 4 files changed, 29 insertions(+) (limited to 'pkg') diff --git a/pkg/domain/entities/containers.go b/pkg/domain/entities/containers.go index 46b169284..3fd7c79f4 100644 --- a/pkg/domain/entities/containers.go +++ b/pkg/domain/entities/containers.go @@ -294,6 +294,7 @@ type ContainerListOptions struct { // ContainerRunOptions describes the options needed // to run a container from the CLI type ContainerRunOptions struct { + CIDFile string Detach bool DetachKeys string ErrorStream *os.File diff --git a/pkg/domain/infra/abi/containers.go b/pkg/domain/infra/abi/containers.go index 60dbbce6c..98b886845 100644 --- a/pkg/domain/infra/abi/containers.go +++ b/pkg/domain/infra/abi/containers.go @@ -29,6 +29,7 @@ import ( "github.com/containers/podman/v2/pkg/signal" "github.com/containers/podman/v2/pkg/specgen" "github.com/containers/podman/v2/pkg/specgen/generate" + "github.com/containers/podman/v2/pkg/util" "github.com/containers/storage" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -846,6 +847,12 @@ func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.Conta return nil, err } + if opts.CIDFile != "" { + if err := util.CreateCidFile(opts.CIDFile, ctr.ID()); err != nil { + return nil, err + } + } + var joinPod bool if len(ctr.PodID()) > 0 { joinPod = true diff --git a/pkg/domain/infra/tunnel/containers.go b/pkg/domain/infra/tunnel/containers.go index 7913d79cd..8066e1c00 100644 --- a/pkg/domain/infra/tunnel/containers.go +++ b/pkg/domain/infra/tunnel/containers.go @@ -21,6 +21,7 @@ import ( "github.com/containers/podman/v2/pkg/domain/entities" "github.com/containers/podman/v2/pkg/errorhandling" "github.com/containers/podman/v2/pkg/specgen" + "github.com/containers/podman/v2/pkg/util" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -558,6 +559,11 @@ func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.Conta for _, w := range con.Warnings { fmt.Fprintf(os.Stderr, "%s\n", w) } + if opts.CIDFile != "" { + if err := util.CreateCidFile(opts.CIDFile, con.ID); err != nil { + return nil, err + } + } report := entities.ContainerRunReport{Id: con.ID} diff --git a/pkg/util/utils.go b/pkg/util/utils.go index 91aba9fa7..a9aad657d 100644 --- a/pkg/util/utils.go +++ b/pkg/util/utils.go @@ -638,3 +638,18 @@ func ValidateSysctls(strSlice []string) (map[string]string, error) { func DefaultContainerConfig() *config.Config { return containerConfig } + +func CreateCidFile(cidfile string, id string) error { + cidFile, err := OpenExclusiveFile(cidfile) + if err != nil { + if os.IsExist(err) { + return errors.Errorf("container id file exists. Ensure another container is not using it or delete %s", cidfile) + } + return errors.Errorf("error opening cidfile %s", cidfile) + } + if _, err = cidFile.WriteString(id); err != nil { + logrus.Error(err) + } + cidFile.Close() + return nil +} -- cgit v1.2.3-54-g00ecf