mirror of
https://github.com/containers/podman.git
synced 2025-07-18 01:57:24 +08:00
Refactor saving OCI spec to disk into separate function
It will be needed for restarting containers Signed-off-by: Matthew Heon <matthew.heon@gmail.com> Closes: #462 Approved by: baude
This commit is contained in:
@ -94,8 +94,6 @@ func (ns LinuxNS) String() string {
|
|||||||
type Container struct {
|
type Container struct {
|
||||||
config *ContainerConfig
|
config *ContainerConfig
|
||||||
|
|
||||||
runningSpec *spec.Spec
|
|
||||||
|
|
||||||
state *containerState
|
state *containerState
|
||||||
|
|
||||||
// Locked indicates that a container has been locked as part of a
|
// Locked indicates that a container has been locked as part of a
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
package libpod
|
package libpod
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
gosignal "os/signal"
|
gosignal "os/signal"
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -63,22 +61,6 @@ func (c *Container) Init() (err error) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// If the OCI spec already exists, we need to replace it
|
|
||||||
// Cannot guarantee some things, e.g. network namespaces, have the same
|
|
||||||
// paths
|
|
||||||
jsonPath := filepath.Join(c.bundlePath(), "config.json")
|
|
||||||
if _, err := os.Stat(jsonPath); err != nil {
|
|
||||||
if !os.IsNotExist(err) {
|
|
||||||
return errors.Wrapf(err, "error doing stat on container %s spec", c.ID())
|
|
||||||
}
|
|
||||||
// The spec does not exist, we're fine
|
|
||||||
} else {
|
|
||||||
// The spec exists, need to remove it
|
|
||||||
if err := os.Remove(jsonPath); err != nil {
|
|
||||||
return errors.Wrapf(err, "error replacing runtime spec for container %s", c.ID())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy /etc/resolv.conf to the container's rundir
|
// Copy /etc/resolv.conf to the container's rundir
|
||||||
runDirResolv, err := c.generateResolvConf()
|
runDirResolv, err := c.generateResolvConf()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -101,20 +83,11 @@ func (c *Container) Init() (err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
c.runningSpec = spec
|
|
||||||
|
|
||||||
// Save the OCI spec to disk
|
// Save the OCI spec to disk
|
||||||
fileJSON, err := json.Marshal(c.runningSpec)
|
if err := c.saveSpec(spec); err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
return errors.Wrapf(err, "error exporting runtime spec for container %s to JSON", c.ID())
|
|
||||||
}
|
}
|
||||||
if err := ioutil.WriteFile(jsonPath, fileJSON, 0644); err != nil {
|
|
||||||
return errors.Wrapf(err, "error writing runtime spec JSON to file for container %s", c.ID())
|
|
||||||
}
|
|
||||||
|
|
||||||
logrus.Debugf("Created OCI spec for container %s at %s", c.ID(), jsonPath)
|
|
||||||
|
|
||||||
c.state.ConfigPath = jsonPath
|
|
||||||
|
|
||||||
// With the spec complete, do an OCI create
|
// With the spec complete, do an OCI create
|
||||||
if err := c.runtime.ociRuntime.createContainer(c, c.config.CgroupParent); err != nil {
|
if err := c.runtime.ociRuntime.createContainer(c, c.config.CgroupParent); err != nil {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package libpod
|
package libpod
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@ -730,3 +731,36 @@ func (c *Container) addImageVolumes(g *generate.Generator) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save OCI spec to disk, replacing any existing specs for the container
|
||||||
|
func (c *Container) saveSpec(spec *spec.Spec) error {
|
||||||
|
// If the OCI spec already exists, we need to replace it
|
||||||
|
// Cannot guarantee some things, e.g. network namespaces, have the same
|
||||||
|
// paths
|
||||||
|
jsonPath := filepath.Join(c.bundlePath(), "config.json")
|
||||||
|
if _, err := os.Stat(jsonPath); err != nil {
|
||||||
|
if !os.IsNotExist(err) {
|
||||||
|
return errors.Wrapf(err, "error doing stat on container %s spec", c.ID())
|
||||||
|
}
|
||||||
|
// The spec does not exist, we're fine
|
||||||
|
} else {
|
||||||
|
// The spec exists, need to remove it
|
||||||
|
if err := os.Remove(jsonPath); err != nil {
|
||||||
|
return errors.Wrapf(err, "error replacing runtime spec for container %s", c.ID())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fileJSON, err := json.Marshal(spec)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "error exporting runtime spec for container %s to JSON", c.ID())
|
||||||
|
}
|
||||||
|
if err := ioutil.WriteFile(jsonPath, fileJSON, 0644); err != nil {
|
||||||
|
return errors.Wrapf(err, "error writing runtime spec JSON for container %s to disk", c.ID())
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.Debugf("Created OCI spec for container %s at %s", c.ID(), jsonPath)
|
||||||
|
|
||||||
|
c.state.ConfigPath = jsonPath
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user