From 4d4646d09b6fb1a8914f6a564baf7fbe4d683f9b Mon Sep 17 00:00:00 2001 From: Matthew Heon Date: Sat, 31 Mar 2018 21:13:03 -0400 Subject: Do not require Init() before Start() This will help dependency races Signed-off-by: Matthew Heon Closes: #577 Approved by: rhatdan --- libpod/container_api.go | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'libpod') diff --git a/libpod/container_api.go b/libpod/container_api.go index 6b30141a5..639696268 100644 --- a/libpod/container_api.go +++ b/libpod/container_api.go @@ -48,7 +48,9 @@ func (c *Container) Init() (err error) { } // Start starts a container -// Start can start created or stopped containers +// Start can start configured, created or stopped containers +// For configured containers, the container will be initialized first, then +// started // Stopped containers will be deleted and re-created in runc, undergoing a fresh // Init() func (c *Container) Start() (err error) { @@ -62,7 +64,9 @@ func (c *Container) Start() (err error) { } // Container must be created or stopped to be started - if !(c.state.State == ContainerStateCreated || c.state.State == ContainerStateStopped) { + if !(c.state.State == ContainerStateConfigured || + c.state.State == ContainerStateCreated || + c.state.State == ContainerStateStopped) { return errors.Wrapf(ErrCtrStateInvalid, "container %s must be in Created or Stopped state to be started", c.ID()) } @@ -77,11 +81,16 @@ func (c *Container) Start() (err error) { } }() - // Reinitialize the container if we need to if c.state.State == ContainerStateStopped { + // Reinitialize the container if we need to if err := c.reinit(); err != nil { return err } + } else if c.state.State == ContainerStateConfigured { + // Or initialize it for the first time if necessary + if err := c.init(); err != nil { + return err + } } // Start the container @@ -89,7 +98,9 @@ func (c *Container) Start() (err error) { } // StartAndAttach starts a container and attaches to it -// StartAndAttach can start created or stopped containers +// StartAndAttach can start configured, created or stopped containers +// For configured containers, the container will be initialized first, then +// started // Stopped containers will be deleted and re-created in runc, undergoing a fresh // Init() // If successful, an error channel will be returned containing the result of the @@ -107,7 +118,9 @@ func (c *Container) StartAndAttach(noStdin bool, keys string) (attachResChan <-c } // Container must be created or stopped to be started - if !(c.state.State == ContainerStateCreated || c.state.State == ContainerStateStopped) { + if !(c.state.State == ContainerStateConfigured || + c.state.State == ContainerStateCreated || + c.state.State == ContainerStateStopped) { return nil, errors.Wrapf(ErrCtrStateInvalid, "container %s must be in Created or Stopped state to be started", c.ID()) } @@ -122,11 +135,16 @@ func (c *Container) StartAndAttach(noStdin bool, keys string) (attachResChan <-c } }() - // Reinitialize the container if we need to if c.state.State == ContainerStateStopped { + // Reinitialize the container if we need to if err := c.reinit(); err != nil { return nil, err } + } else if c.state.State == ContainerStateConfigured { + // Or initialize it for the first time if necessary + if err := c.init(); err != nil { + return nil, err + } } attachChan := make(chan error) -- cgit v1.2.3-54-g00ecf