mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 18:35:44 +03:00
AllocDirBuilder that creates the alloc directory structure
This commit is contained in:
65
client/allocdir/alloc_dir.go
Normal file
65
client/allocdir/alloc_dir.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package allocdir
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/hashicorp/nomad/nomad/structs"
|
||||
)
|
||||
|
||||
var (
|
||||
// The name of the directory that is shared across tasks in a task group.
|
||||
SharedAllocName = "alloc"
|
||||
|
||||
// The set of directories that exist inside eache shared alloc directory.
|
||||
SharedAllocDirs = []string{"logs", "tmp", "data"}
|
||||
|
||||
// The name of the directory that exists inside each task directory
|
||||
// regardless of driver.
|
||||
TaskLocal = "local"
|
||||
)
|
||||
|
||||
// Builds the necessary directory structure for running an alloc.
|
||||
type AllocDirBuilder interface {
|
||||
// Given a list of a task build the correct alloc structure.
|
||||
Build([]*structs.Task) error
|
||||
|
||||
// Tears down previously build directory structure.
|
||||
Destroy() error
|
||||
|
||||
// Returns the directory of a task if it was created, otherwise an error is
|
||||
// returned.
|
||||
TaskDir(task string) (string, error)
|
||||
}
|
||||
|
||||
type AllocDir struct {
|
||||
// AllocDir is the directory used for storing any state
|
||||
// of this allocation. It will be purged on alloc destroy.
|
||||
AllocDir string
|
||||
|
||||
// The shared directory is available to all tasks within the same task
|
||||
// group.
|
||||
SharedDir string
|
||||
|
||||
// TaskDirs is a mapping of task names to their non-shared directory.
|
||||
TaskDirs map[string]string
|
||||
}
|
||||
|
||||
func NewAllocDir(allocDir string) *AllocDir {
|
||||
d := &AllocDir{AllocDir: allocDir, TaskDirs: make(map[string]string)}
|
||||
d.SharedDir = filepath.Join(d.AllocDir, SharedAllocName)
|
||||
return d
|
||||
}
|
||||
|
||||
func (d *AllocDir) Destroy() error {
|
||||
return os.RemoveAll(d.AllocDir)
|
||||
}
|
||||
|
||||
func (d *AllocDir) TaskDir(task string) (string, error) {
|
||||
if dir, ok := d.TaskDirs[task]; ok {
|
||||
return dir, nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("Task directory doesn't exist for task %v", task)
|
||||
}
|
||||
98
client/allocdir/alloc_dir_linux.go
Normal file
98
client/allocdir/alloc_dir_linux.go
Normal file
@@ -0,0 +1,98 @@
|
||||
// +build !windows
|
||||
|
||||
package allocdir
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hashicorp/nomad/nomad/structs"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (d *AllocDir) Build(tasks []*structs.Task) error {
|
||||
// Make the alloc directory, owned by the nomad process.
|
||||
if err := os.Mkdir(d.AllocDir, 0700); err != nil {
|
||||
return fmt.Errorf("Failed to make the alloc directory %v: %v", d.AllocDir, err)
|
||||
}
|
||||
|
||||
nobody, err := user.Lookup("nobody")
|
||||
if err != nil {
|
||||
return fmt.Errorf("Could not set owner/group on shared alloc directory: %v", err)
|
||||
}
|
||||
|
||||
uid, err := getUid(nobody)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
gid, err := getGid(nobody)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Make the shared directory and make it availabe to all user/groups.
|
||||
if err := mkOwnedDir(d.SharedDir, uid, gid, 0777); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, dir := range SharedAllocDirs {
|
||||
p := filepath.Join(d.SharedDir, dir)
|
||||
if err := mkOwnedDir(p, uid, gid, 0777); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Make the task directories.
|
||||
for _, t := range tasks {
|
||||
p := filepath.Join(d.AllocDir, t.Name)
|
||||
if err := mkOwnedDir(p, uid, gid, 0777); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create a local directory that each task can use.
|
||||
local := filepath.Join(p, TaskLocal)
|
||||
if err := mkOwnedDir(local, uid, gid, 0777); err != nil {
|
||||
return err
|
||||
}
|
||||
d.TaskDirs[t.Name] = local
|
||||
|
||||
// TODO: Mount the shared alloc dir into each task dir.
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// mkOwnedDir creates the directory specified by the path with the passed
|
||||
// permissions. It also sets the passed uid and gid. It returns an error if any
|
||||
// of these operations fail.
|
||||
func mkOwnedDir(path string, uid, gid int, perm os.FileMode) error {
|
||||
if err := os.Mkdir(path, perm); err != nil {
|
||||
return fmt.Errorf("Failed to make directory %v: %v", path, err)
|
||||
}
|
||||
|
||||
if err := os.Chown(path, uid, gid); err != nil {
|
||||
return fmt.Errorf("Couldn't change owner/group of %v to (uid: %v, gid: %v): %v", path, uid, gid, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getUid(u *user.User) (int, error) {
|
||||
uid, err := strconv.Atoi(u.Uid)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("Unable to convert Uid to an int: %v", err)
|
||||
}
|
||||
|
||||
return uid, nil
|
||||
}
|
||||
|
||||
func getGid(u *user.User) (int, error) {
|
||||
gid, err := strconv.Atoi(u.Gid)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("Unable to convert Gid to an int: %v", err)
|
||||
}
|
||||
|
||||
return gid, nil
|
||||
}
|
||||
8
client/allocdir/alloc_dir_windows.go
Normal file
8
client/allocdir/alloc_dir_windows.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package allocdir
|
||||
|
||||
import "github.com/hashicorp/nomad/nomad/structs"
|
||||
|
||||
func (r *AllocRunner) Build(tasks []*structs.Task) error {
|
||||
// TODO: Need to figure out how to do mounts on windows.
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user