diff options
author | Matthew Heon <matthew.heon@gmail.com> | 2018-03-07 11:15:00 -0500 |
---|---|---|
committer | Atomic Bot <atomic-devel@projectatomic.io> | 2018-03-08 16:40:21 +0000 |
commit | d23b9fd4ed96cbf03c5da3af6bc5837d9998a75a (patch) | |
tree | 6871bb77a274e8a17e847080e6cf11e03fb97be8 /libpod/container_internal.go | |
parent | 04d56c9fe30235c9f99cf816fc6078fa5b2f1512 (diff) | |
download | podman-d23b9fd4ed96cbf03c5da3af6bc5837d9998a75a.tar.gz podman-d23b9fd4ed96cbf03c5da3af6bc5837d9998a75a.tar.bz2 podman-d23b9fd4ed96cbf03c5da3af6bc5837d9998a75a.zip |
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
Diffstat (limited to 'libpod/container_internal.go')
-rw-r--r-- | libpod/container_internal.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/libpod/container_internal.go b/libpod/container_internal.go index c346fd27f..6fbaa4546 100644 --- a/libpod/container_internal.go +++ b/libpod/container_internal.go @@ -1,6 +1,7 @@ package libpod import ( + "encoding/json" "fmt" "io" "io/ioutil" @@ -730,3 +731,36 @@ func (c *Container) addImageVolumes(g *generate.Generator) error { } 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 +} |