2016-12-05 22:15:03 +00:00
|
|
|
package execution
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-01-12 18:32:09 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2016-12-05 22:15:03 +00:00
|
|
|
)
|
|
|
|
|
2016-12-09 18:47:29 +00:00
|
|
|
const processesDirName = "processes"
|
2016-12-05 22:15:03 +00:00
|
|
|
|
|
|
|
type StateDir string
|
|
|
|
|
2017-01-12 18:32:09 +00:00
|
|
|
func LoadStateDir(root, id string) (StateDir, error) {
|
|
|
|
path := filepath.Join(root, id)
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
return "", errors.Wrap(err, "could not find container statedir")
|
|
|
|
}
|
|
|
|
return StateDir(path), nil
|
|
|
|
}
|
|
|
|
|
2016-12-05 22:15:03 +00:00
|
|
|
func NewStateDir(root, id string) (StateDir, error) {
|
|
|
|
path := filepath.Join(root, id)
|
2016-12-05 23:38:32 +00:00
|
|
|
if err := os.Mkdir(path, 0700); err != nil {
|
2017-01-12 18:32:09 +00:00
|
|
|
return "", errors.Wrap(err, "could not create container statedir")
|
2016-12-05 22:15:03 +00:00
|
|
|
}
|
2016-12-09 18:47:29 +00:00
|
|
|
if err := os.Mkdir(StateDir(path).processesDir(), 0700); err != nil {
|
2016-12-05 22:15:03 +00:00
|
|
|
os.RemoveAll(path)
|
2017-01-12 18:32:09 +00:00
|
|
|
return "", errors.Wrap(err, "could not create processes statedir")
|
2016-12-05 22:15:03 +00:00
|
|
|
}
|
2016-12-06 00:17:46 +00:00
|
|
|
return StateDir(path), nil
|
2016-12-05 22:15:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s StateDir) Delete() error {
|
2017-01-12 18:32:09 +00:00
|
|
|
err := os.RemoveAll(string(s))
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to remove statedir %s", string(s))
|
|
|
|
}
|
|
|
|
return nil
|
2016-12-05 22:15:03 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 17:16:23 +00:00
|
|
|
func (s StateDir) NewProcess(id string) (dir string, err error) {
|
|
|
|
dir = filepath.Join(s.processesDir(), id)
|
|
|
|
if err = os.Mkdir(dir, 0700); err != nil {
|
2017-01-12 18:32:09 +00:00
|
|
|
return "", errors.Wrap(err, "could not create process statedir")
|
2016-12-09 18:47:29 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 17:16:23 +00:00
|
|
|
return dir, nil
|
2016-12-05 22:15:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s StateDir) ProcessDir(id string) string {
|
2016-12-09 18:47:29 +00:00
|
|
|
return filepath.Join(s.processesDir(), id)
|
2016-12-05 22:15:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s StateDir) DeleteProcess(id string) error {
|
2017-01-12 18:32:09 +00:00
|
|
|
err := os.RemoveAll(filepath.Join(s.processesDir(), id))
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to remove process %d statedir", id)
|
|
|
|
}
|
|
|
|
return nil
|
2016-12-05 22:15:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s StateDir) Processes() ([]string, error) {
|
2016-12-09 18:47:29 +00:00
|
|
|
procsDir := s.processesDir()
|
2016-12-05 23:38:32 +00:00
|
|
|
dirs, err := ioutil.ReadDir(procsDir)
|
2016-12-05 22:15:03 +00:00
|
|
|
if err != nil {
|
2017-01-12 18:32:09 +00:00
|
|
|
return nil, errors.Wrap(err, "could not list processes statedir")
|
2016-12-05 22:15:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
paths := make([]string, 0)
|
|
|
|
for _, d := range dirs {
|
|
|
|
if d.IsDir() {
|
2016-12-05 23:38:32 +00:00
|
|
|
paths = append(paths, filepath.Join(procsDir, d.Name()))
|
2016-12-05 22:15:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return paths, nil
|
|
|
|
}
|
2016-12-05 23:38:32 +00:00
|
|
|
|
2016-12-09 18:47:29 +00:00
|
|
|
func (s StateDir) processesDir() string {
|
|
|
|
return filepath.Join(string(s), processesDirName)
|
2016-12-05 23:38:32 +00:00
|
|
|
}
|