2016-12-05 22:15:03 +00:00
|
|
|
package execution
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
const processesDir = "processes"
|
|
|
|
|
|
|
|
type StateDir string
|
|
|
|
|
|
|
|
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 {
|
2016-12-05 22:15:03 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2016-12-05 23:38:32 +00:00
|
|
|
if err := os.Mkdir(filepath.Join(path, processesDir), 0700); err != nil {
|
2016-12-05 22:15:03 +00:00
|
|
|
os.RemoveAll(path)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return StateDir(path), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s StateDir) Delete() error {
|
|
|
|
return os.RemoveAll(string(s))
|
|
|
|
}
|
|
|
|
|
2016-12-05 23:38:32 +00:00
|
|
|
func (s StateDir) NewProcess() (string, error) {
|
|
|
|
return ioutil.TempDir(s.processDir(), "")
|
2016-12-05 22:15:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s StateDir) ProcessDir(id string) string {
|
|
|
|
return filepath.Join(string(s), id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s StateDir) DeleteProcess(id string) error {
|
2016-12-05 23:38:32 +00:00
|
|
|
return os.RemoveAll(filepath.Join(s.processDir(), id))
|
2016-12-05 22:15:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s StateDir) Processes() ([]string, error) {
|
2016-12-05 23:38:32 +00:00
|
|
|
procsDir := s.processDir()
|
|
|
|
dirs, err := ioutil.ReadDir(procsDir)
|
2016-12-05 22:15:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
func (s StateDir) processDir() string {
|
|
|
|
return filepath.Join(string(s), processesDir)
|
|
|
|
}
|