2015-09-21 21:13:17 +00:00
|
|
|
package allocdir
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-09-23 04:56:29 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2015-09-21 21:13:17 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2015-09-25 23:49:14 +00:00
|
|
|
|
|
|
|
// A list of locations the shared alloc has been mounted to.
|
|
|
|
mounted []string
|
2015-09-21 21:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewAllocDir(allocDir string) *AllocDir {
|
|
|
|
d := &AllocDir{AllocDir: allocDir, TaskDirs: make(map[string]string)}
|
|
|
|
d.SharedDir = filepath.Join(d.AllocDir, SharedAllocName)
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2015-09-23 04:56:29 +00:00
|
|
|
// Tears down previously build directory structure.
|
2015-09-21 21:13:17 +00:00
|
|
|
func (d *AllocDir) Destroy() error {
|
2015-09-25 23:49:14 +00:00
|
|
|
// Unmount all mounted shared alloc dirs.
|
|
|
|
for _, m := range d.mounted {
|
|
|
|
if err := d.unmountSharedDir(m); err != nil {
|
|
|
|
return fmt.Errorf("Failed to unmount shared directory: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-21 21:13:17 +00:00
|
|
|
return os.RemoveAll(d.AllocDir)
|
|
|
|
}
|
|
|
|
|
2015-09-23 04:56:29 +00:00
|
|
|
// Given a list of a task build the correct alloc structure.
|
|
|
|
func (d *AllocDir) Build(tasks []*structs.Task) error {
|
|
|
|
// Make the alloc directory, owned by the nomad process.
|
|
|
|
if err := os.MkdirAll(d.AllocDir, 0700); err != nil {
|
|
|
|
return fmt.Errorf("Failed to make the alloc directory %v: %v", d.AllocDir, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the shared directory and make it availabe to all user/groups.
|
|
|
|
if err := os.Mkdir(d.SharedDir, 0777); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
// Make the shared directory have non-root permissions.
|
|
|
|
if err := d.dropDirPermissions(d.SharedDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-23 04:56:29 +00:00
|
|
|
for _, dir := range SharedAllocDirs {
|
|
|
|
p := filepath.Join(d.SharedDir, dir)
|
|
|
|
if err := os.Mkdir(p, 0777); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the task directories.
|
|
|
|
for _, t := range tasks {
|
|
|
|
taskDir := filepath.Join(d.AllocDir, t.Name)
|
|
|
|
if err := os.Mkdir(taskDir, 0777); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
// Make the task directory have non-root permissions.
|
|
|
|
if err := d.dropDirPermissions(taskDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-23 04:56:29 +00:00
|
|
|
// Create a local directory that each task can use.
|
|
|
|
local := filepath.Join(taskDir, TaskLocal)
|
|
|
|
if err := os.Mkdir(local, 0777); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-25 23:49:14 +00:00
|
|
|
|
|
|
|
if err := d.dropDirPermissions(local); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-23 04:56:29 +00:00
|
|
|
d.TaskDirs[t.Name] = taskDir
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Embed takes a mapping of absolute directory paths on the host to their
|
|
|
|
// intended, relative location within the task directory. Embed attempts
|
2015-09-25 23:49:14 +00:00
|
|
|
// hardlink and then defaults to copying. If the path exists on the host and
|
|
|
|
// can't be embeded an error is returned.
|
2015-09-23 04:56:29 +00:00
|
|
|
func (d *AllocDir) Embed(task string, dirs map[string]string) error {
|
|
|
|
taskdir, ok := d.TaskDirs[task]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Task directory doesn't exist for task %v", task)
|
|
|
|
}
|
|
|
|
|
|
|
|
subdirs := make(map[string]string)
|
|
|
|
for source, dest := range dirs {
|
2015-09-25 23:49:14 +00:00
|
|
|
// Check to see if directory exists on host.
|
2015-09-25 00:47:38 +00:00
|
|
|
s, err := os.Stat(source)
|
|
|
|
if os.IsNotExist(err) {
|
2015-09-25 23:49:14 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-09-23 04:56:29 +00:00
|
|
|
// Enumerate the files in source.
|
|
|
|
entries, err := ioutil.ReadDir(source)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Couldn't read directory %v: %v", source, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create destination directory.
|
|
|
|
destDir := filepath.Join(taskdir, dest)
|
2015-09-25 00:47:38 +00:00
|
|
|
if err := os.MkdirAll(destDir, s.Mode().Perm()); err != nil {
|
2015-09-23 04:56:29 +00:00
|
|
|
return fmt.Errorf("Couldn't create destination directory %v: %v", destDir, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
hostEntry := filepath.Join(source, entry.Name())
|
2015-09-25 23:49:14 +00:00
|
|
|
taskEntry := filepath.Join(destDir, filepath.Base(hostEntry))
|
2015-09-23 04:56:29 +00:00
|
|
|
if entry.IsDir() {
|
|
|
|
subdirs[hostEntry] = filepath.Join(dest, filepath.Base(hostEntry))
|
|
|
|
continue
|
|
|
|
} else if !entry.Mode().IsRegular() {
|
2015-09-24 23:17:58 +00:00
|
|
|
// If it is a symlink we can create it, otherwise we skip it.
|
2015-09-25 23:49:14 +00:00
|
|
|
if entry.Mode()&os.ModeSymlink == 0 {
|
2015-09-24 23:17:58 +00:00
|
|
|
continue
|
2015-09-25 23:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
link, err := os.Readlink(hostEntry)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Couldn't resolve symlink for %v: %v", source, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Symlink(link, taskEntry); err != nil {
|
|
|
|
return fmt.Errorf("Couldn't create symlink: %v", err)
|
|
|
|
}
|
|
|
|
continue
|
2015-09-23 04:56:29 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 00:47:38 +00:00
|
|
|
if err := d.linkOrCopy(hostEntry, taskEntry, entry.Mode().Perm()); err != nil {
|
2015-09-23 04:56:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recurse on self to copy subdirectories.
|
|
|
|
if len(subdirs) != 0 {
|
|
|
|
return d.Embed(task, subdirs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MountSharedDir mounts the shared directory into the specified task's
|
|
|
|
// directory. Mount is documented at an OS level in their respective
|
|
|
|
// implementation files.
|
|
|
|
func (d *AllocDir) MountSharedDir(task string) error {
|
|
|
|
taskDir, ok := d.TaskDirs[task]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("No task directory exists for %v", task)
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
taskLoc := filepath.Join(taskDir, SharedAllocName)
|
|
|
|
if err := d.mountSharedDir(taskLoc); err != nil {
|
|
|
|
return fmt.Errorf("Failed to mount shared directory for task %v: %v", task, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.mounted = append(d.mounted, taskLoc)
|
|
|
|
return nil
|
2015-09-23 04:56:29 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 00:47:38 +00:00
|
|
|
func fileCopy(src, dst string, perm os.FileMode) error {
|
2015-09-23 04:56:29 +00:00
|
|
|
// Do a simple copy.
|
|
|
|
srcFile, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Couldn't open src file %v: %v", src, err)
|
|
|
|
}
|
|
|
|
|
2015-09-25 00:47:38 +00:00
|
|
|
dstFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE, perm)
|
2015-09-23 04:56:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Couldn't create destination file %v: %v", dst, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := io.Copy(dstFile, srcFile); err != nil {
|
|
|
|
return fmt.Errorf("Couldn't copy %v to %v: %v", src, dst, err)
|
2015-09-21 21:13:17 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 04:56:29 +00:00
|
|
|
return nil
|
2015-09-21 21:13:17 +00:00
|
|
|
}
|