From c376fd45f6e58ccb381ad3a16bc4fee48de4cd7e Mon Sep 17 00:00:00 2001 From: Kenfe-Mickael Laventure Date: Fri, 9 Dec 2016 10:47:29 -0800 Subject: [PATCH] Cleanup statedir a little Signed-off-by: Kenfe-Mickael Laventure --- execution/executors/oci/oci.go | 14 +++++++------- execution/statedir.go | 23 ++++++++++++++--------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/execution/executors/oci/oci.go b/execution/executors/oci/oci.go index e5a0737..4f07cee 100644 --- a/execution/executors/oci/oci.go +++ b/execution/executors/oci/oci.go @@ -111,11 +111,11 @@ func (r *OCIRuntime) Create(ctx context.Context, id string, o execution.CreateOp } }(container) - initDir, err := container.StateDir().NewProcess() + initProcID, initStateDir, err := container.StateDir().NewProcess() if err != nil { return nil, err } - pidFile := filepath.Join(initDir, "pid") + pidFile := filepath.Join(initStateDir, "pid") err = r.runc.Create(ctx, id, o.Bundle, &runc.CreateOpts{ PidFile: pidFile, Console: consolePath, @@ -135,7 +135,7 @@ func (r *OCIRuntime) Create(ctx context.Context, id string, o execution.CreateOp if err != nil { return nil, err } - process, err := newProcess(filepath.Base(initDir), pid) + process, err := newProcess(initProcID, pid) if err != nil { return nil, err } @@ -261,17 +261,17 @@ func (r *OCIRuntime) StartProcess(ctx context.Context, c *execution.Container, o }() } - processStateDir, err := c.StateDir().NewProcess() + procID, procStateDir, err := c.StateDir().NewProcess() if err != nil { return nil, err } defer func() { if err != nil { - c.StateDir().DeleteProcess(filepath.Base(processStateDir)) + c.StateDir().DeleteProcess(procID) } }() - pidFile := filepath.Join(processStateDir, "pid") + pidFile := filepath.Join(procStateDir, "pid") if err := r.runc.Exec(ctx, c.ID(), o.Spec, &runc.ExecOpts{ PidFile: pidFile, Detach: false, @@ -286,7 +286,7 @@ func (r *OCIRuntime) StartProcess(ctx context.Context, c *execution.Container, o return nil, err } - process, err := newProcess(filepath.Base(processStateDir), pid) + process, err := newProcess(procID, pid) if err != nil { return nil, err } diff --git a/execution/statedir.go b/execution/statedir.go index 5642edb..eb72fcb 100644 --- a/execution/statedir.go +++ b/execution/statedir.go @@ -6,7 +6,7 @@ import ( "path/filepath" ) -const processesDir = "processes" +const processesDirName = "processes" type StateDir string @@ -15,7 +15,7 @@ func NewStateDir(root, id string) (StateDir, error) { if err := os.Mkdir(path, 0700); err != nil { return "", err } - if err := os.Mkdir(filepath.Join(path, processesDir), 0700); err != nil { + if err := os.Mkdir(StateDir(path).processesDir(), 0700); err != nil { os.RemoveAll(path) return "", err } @@ -26,20 +26,25 @@ func (s StateDir) Delete() error { return os.RemoveAll(string(s)) } -func (s StateDir) NewProcess() (string, error) { - return ioutil.TempDir(s.processDir(), "") +func (s StateDir) NewProcess() (id, dir string, err error) { + dir, err = ioutil.TempDir(s.processesDir(), "") + if err != nil { + return "", "", err + } + + return filepath.Base(dir), dir, err } func (s StateDir) ProcessDir(id string) string { - return filepath.Join(string(s), id) + return filepath.Join(s.processesDir(), id) } func (s StateDir) DeleteProcess(id string) error { - return os.RemoveAll(filepath.Join(s.processDir(), id)) + return os.RemoveAll(filepath.Join(s.processesDir(), id)) } func (s StateDir) Processes() ([]string, error) { - procsDir := s.processDir() + procsDir := s.processesDir() dirs, err := ioutil.ReadDir(procsDir) if err != nil { return nil, err @@ -54,6 +59,6 @@ func (s StateDir) Processes() ([]string, error) { return paths, nil } -func (s StateDir) processDir() string { - return filepath.Join(string(s), processesDir) +func (s StateDir) processesDir() string { + return filepath.Join(string(s), processesDirName) }