From 8d31ec2ad775c97c4b860eb24a3481092b6e88e3 Mon Sep 17 00:00:00 2001 From: umohnani8 Date: Wed, 6 Dec 2017 15:42:06 -0500 Subject: Add Artifacts directory to containers Create an artifacts directory in the container's static directory so store container information coming from outside of libpod to specified files An example is to hold data from user specified flags in kpod run/create such as --cap-add, --ipcMode, etc... Signed-off-by: umohnani8 Closes: #108 Approved by: mheon --- libpod/container.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/libpod/container.go b/libpod/container.go index bd7455147..40aa689ff 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -50,6 +50,8 @@ const ( ContainerStateStopped ContainerState = iota // ContainerStatePaused indicates that the container has been paused ContainerStatePaused ContainerState = iota + // name of the directory holding the artifacts + artifactsDir = "artifacts" ) // Container is a single OCI container @@ -383,6 +385,11 @@ func (c *Container) setupStorage() error { c.config.StaticDir = containerInfo.Dir c.state.RunDir = containerInfo.RunDir + artifacts := filepath.Join(c.config.StaticDir, artifactsDir) + if err := os.MkdirAll(artifacts, 0755); err != nil { + return errors.Wrapf(err, "error creating artifacts directory %q", artifacts) + } + return nil } @@ -396,6 +403,11 @@ func (c *Container) teardownStorage() error { return errors.Wrapf(ErrCtrStateInvalid, "cannot remove storage for container %s as it is running or paused", c.ID()) } + artifacts := filepath.Join(c.config.StaticDir, artifactsDir) + if err := os.RemoveAll(artifacts); err != nil { + return errors.Wrapf(err, "error removing artifacts %q", artifacts) + } + if err := c.cleanupStorage(); err != nil { return errors.Wrapf(err, "failed to cleanup container %s storage", c.ID()) } @@ -718,6 +730,25 @@ func (c *Container) Export(path string) error { return err } +// AddArtifact creates and writes to an artifact file for the container +func (c *Container) AddArtifact(name string, data []byte) error { + return ioutil.WriteFile(c.getArtifactPath(name), data, 0740) +} + +// GetArtifact reads the specified artifact file from the container +func (c *Container) GetArtifact(name string) ([]byte, error) { + return ioutil.ReadFile(c.getArtifactPath(name)) +} + +// RemoveArtifact deletes the specified artifacts file +func (c *Container) RemoveArtifact(name string) error { + return os.Remove(c.getArtifactPath(name)) +} + +func (c *Container) getArtifactPath(name string) string { + return filepath.Join(c.config.StaticDir, artifactsDir, name) +} + // Commit commits the changes between a container and its image, creating a new // image func (c *Container) Commit() (*storage.Image, error) { -- cgit v1.2.3-54-g00ecf