Skip to content

Commit

Permalink
Merge pull request #8094 from rhatdan/cidfile
Browse files Browse the repository at this point in the history
The cidfile should be created when the container is created
  • Loading branch information
openshift-merge-robot authored Oct 27, 2020
2 parents dbbd598 + 32af1be commit 5c08495
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 43 deletions.
32 changes: 3 additions & 29 deletions cmd/podman/containers/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ import (
"github.com/containers/podman/v2/cmd/podman/utils"
"github.com/containers/podman/v2/libpod/define"
"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"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -94,15 +92,6 @@ func create(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
cidFile, err := openCidFile(cliVals.CIDFile)
if err != nil {
return err
}

if cidFile != nil {
defer errorhandling.CloseQuiet(cidFile)
defer errorhandling.SyncQuiet(cidFile)
}

if err := createInit(cmd); err != nil {
return err
Expand Down Expand Up @@ -139,10 +128,9 @@ func create(cmd *cobra.Command, args []string) error {
return err
}

if cidFile != nil {
_, err = cidFile.WriteString(report.Id)
if err != nil {
logrus.Error(err)
if cliVals.CIDFile != "" {
if err := util.CreateCidFile(cliVals.CIDFile, report.Id); err != nil {
return err
}
}

Expand Down Expand Up @@ -269,20 +257,6 @@ func pullImage(imageName string) (string, error) {
return imageName, nil
}

func openCidFile(cidfile string) (*os.File, error) {
if cidfile == "" {
return nil, nil
}
cidFile, err := util.OpenExclusiveFile(cidfile)
if err != nil && os.IsExist(err) {
return nil, errors.Errorf("container id file exists. Ensure another container is not using it or delete %s", cidfile)
}
if err != nil {
return nil, errors.Errorf("error opening cidfile %s", cidfile)
}
return cidFile, nil
}

// createPodIfNecessary automatically creates a pod when requested. if the pod name
// has the form new:ID, the pod ID is created and the name in the spec generator is replaced
// with ID.
Expand Down
15 changes: 1 addition & 14 deletions cmd/podman/containers/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,8 @@ func run(cmd *cobra.Command, args []string) error {
return errors.Wrapf(err, "error checking authfile path %s", af)
}
}
cidFile, err := openCidFile(cliVals.CIDFile)
if err != nil {
return err
}

if cidFile != nil {
defer errorhandling.CloseQuiet(cidFile)
defer errorhandling.SyncQuiet(cidFile)
}
runOpts.CIDFile = cliVals.CIDFile
runOpts.Rm = cliVals.Rm
if err := createInit(cmd); err != nil {
return err
Expand Down Expand Up @@ -193,12 +186,6 @@ func run(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
if cidFile != nil {
_, err = cidFile.WriteString(report.Id)
if err != nil {
logrus.Error(err)
}
}

if runOpts.Detach {
fmt.Println(report.Id)
Expand Down
1 change: 1 addition & 0 deletions pkg/domain/entities/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions pkg/domain/infra/abi/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pkg/domain/infra/tunnel/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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}

Expand Down
15 changes: 15 additions & 0 deletions pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 5c08495

Please sign in to comment.