summaryrefslogtreecommitdiff
path: root/libpod/container_api.go
diff options
context:
space:
mode:
authorMatthew Heon <matthew.heon@gmail.com>2018-03-12 15:32:10 -0400
committerAtomic Bot <atomic-devel@projectatomic.io>2018-03-13 13:54:45 +0000
commit40d302be8f9e3d1832d851cb016b2f568780a950 (patch)
tree1ea7a7e09f5b6fab2a456855d9eb437286cb16b1 /libpod/container_api.go
parent58c35daea279bc4cac35fe7acc943315ec584bfd (diff)
downloadpodman-40d302be8f9e3d1832d851cb016b2f568780a950.tar.gz
podman-40d302be8f9e3d1832d851cb016b2f568780a950.tar.bz2
podman-40d302be8f9e3d1832d851cb016b2f568780a950.zip
Modify pod API to move Init() into Start()
Separate Init() and Start() does not make sense on the pod side, where we may have to start containers in order to initialize others due to dependency orders. Also adjusts internal containers API for more code sharing. Signed-off-by: Matthew Heon <matthew.heon@gmail.com> Closes: #478 Approved by: rhatdan
Diffstat (limited to 'libpod/container_api.go')
-rw-r--r--libpod/container_api.go69
1 files changed, 35 insertions, 34 deletions
diff --git a/libpod/container_api.go b/libpod/container_api.go
index 0a27da7c9..7e2197614 100644
--- a/libpod/container_api.go
+++ b/libpod/container_api.go
@@ -59,35 +59,11 @@ func (c *Container) Init() (err error) {
}
}()
- if err := c.makeBindMounts(); err != nil {
- return err
- }
-
- // Generate the OCI spec
- spec, err := c.generateSpec()
- if err != nil {
- return err
- }
-
- // Save the OCI spec to disk
- if err := c.saveSpec(spec); err != nil {
- return err
- }
-
- // With the spec complete, do an OCI create
- if err := c.runtime.ociRuntime.createContainer(c, c.config.CgroupParent); err != nil {
- return err
- }
-
- logrus.Debugf("Created container %s in OCI runtime", c.ID())
-
- c.state.State = ContainerStateCreated
-
- return c.save()
+ return c.init()
}
// Start starts a container
-func (c *Container) Start() error {
+func (c *Container) Start() (err error) {
if !c.locked {
c.lock.Lock()
defer c.lock.Unlock()
@@ -107,20 +83,33 @@ func (c *Container) Start() error {
return errors.Wrapf(ErrNotImplemented, "restarting a stopped container is not yet supported")
}
- // Mount storage for the container
+ // Mount storage for the container if necessary
if err := c.mountStorage(); err != nil {
return err
}
+ defer func() {
+ if err != nil {
+ if err2 := c.cleanupStorage(); err2 != nil {
+ logrus.Errorf("Error cleaning up storage for container %s: %v", c.ID(), err2)
+ }
+ }
+ }()
- if err := c.runtime.ociRuntime.startContainer(c); err != nil {
- return err
+ // Create a network namespace if necessary
+ if c.config.CreateNetNS && c.state.NetNS == nil {
+ if err := c.runtime.createNetNS(c); err != nil {
+ return err
+ }
}
+ defer func() {
+ if err != nil {
+ if err2 := c.runtime.teardownNetNS(c); err2 != nil {
+ logrus.Errorf("Error tearing down network namespace for container %s: %v", c.ID(), err2)
+ }
+ }
+ }()
- logrus.Debugf("Started container %s", c.ID())
-
- c.state.State = ContainerStateRunning
-
- return c.save()
+ return c.start()
}
// Stop uses the container's stop signal (or SIGTERM if no signal was specified)
@@ -138,6 +127,12 @@ func (c *Container) Stop() error {
}
}
+ if c.state.State == ContainerStateConfigured ||
+ c.state.State == ContainerStateUnknown ||
+ c.state.State == ContainerStatePaused {
+ return errors.Wrapf(ErrCtrStateInvalid, "can only stop created, running, or stopped containers")
+ }
+
return c.stop(c.config.StopTimeout)
}
@@ -154,6 +149,12 @@ func (c *Container) StopWithTimeout(timeout uint) error {
}
}
+ if c.state.State == ContainerStateConfigured ||
+ c.state.State == ContainerStateUnknown ||
+ c.state.State == ContainerStatePaused {
+ return errors.Wrapf(ErrCtrStateInvalid, "can only stop created, running, or stopped containers")
+ }
+
return c.stop(timeout)
}