Cleanup statedir a little

Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
This commit is contained in:
Kenfe-Mickael Laventure 2016-12-09 10:47:29 -08:00
parent 642ed8bb48
commit c376fd45f6
2 changed files with 21 additions and 16 deletions

View file

@ -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
}

View file

@ -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)
}