2016-12-03 01:04:07 +00:00
|
|
|
package allocdir
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
2016-12-03 01:04:07 +00:00
|
|
|
)
|
|
|
|
|
2017-01-05 21:53:11 +00:00
|
|
|
// TaskDir contains all of the paths relevant to a task. All paths are on the
|
|
|
|
// host system so drivers should mount/link into task containers as necessary.
|
2016-12-03 01:04:07 +00:00
|
|
|
type TaskDir struct {
|
2018-10-04 19:08:20 +00:00
|
|
|
// AllocDir is the path to the alloc directory on the host
|
|
|
|
AllocDir string
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
// Dir is the path to Task directory on the host
|
|
|
|
Dir string
|
|
|
|
|
|
|
|
// SharedAllocDir is the path to shared alloc directory on the host
|
|
|
|
// <alloc_dir>/alloc/
|
|
|
|
SharedAllocDir string
|
|
|
|
|
|
|
|
// SharedTaskDir is the path to the shared alloc directory linked into
|
|
|
|
// the task directory on the host.
|
|
|
|
// <task_dir>/alloc/
|
|
|
|
SharedTaskDir string
|
|
|
|
|
|
|
|
// LocalDir is the path to the task's local directory on the host
|
|
|
|
// <task_dir>/local/
|
|
|
|
LocalDir string
|
|
|
|
|
|
|
|
// LogDir is the path to the task's log directory on the host
|
|
|
|
// <alloc_dir>/alloc/logs/
|
|
|
|
LogDir string
|
|
|
|
|
|
|
|
// SecretsDir is the path to secrets/ directory on the host
|
|
|
|
// <task_dir>/secrets/
|
|
|
|
SecretsDir string
|
2017-01-05 23:57:58 +00:00
|
|
|
|
2021-10-15 23:56:14 +00:00
|
|
|
// skip embedding these paths in chroots. Used for avoiding embedding
|
|
|
|
// client.alloc_dir recursively.
|
|
|
|
skip map[string]struct{}
|
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
logger hclog.Logger
|
2016-12-03 01:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newTaskDir creates a TaskDir struct with paths set. Call Build() to
|
|
|
|
// create paths on disk.
|
|
|
|
//
|
|
|
|
// Call AllocDir.NewTaskDir to create new TaskDirs
|
2021-10-15 23:56:14 +00:00
|
|
|
func newTaskDir(logger hclog.Logger, clientAllocDir, allocDir, taskName string) *TaskDir {
|
2016-12-03 01:04:07 +00:00
|
|
|
taskDir := filepath.Join(allocDir, taskName)
|
2018-08-29 22:05:03 +00:00
|
|
|
|
|
|
|
logger = logger.Named("task_dir").With("task_name", taskName)
|
|
|
|
|
2021-10-15 23:56:14 +00:00
|
|
|
// skip embedding client.alloc_dir in chroots
|
|
|
|
skip := map[string]struct{}{clientAllocDir: {}}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
return &TaskDir{
|
2018-10-04 19:08:20 +00:00
|
|
|
AllocDir: allocDir,
|
2016-12-03 01:04:07 +00:00
|
|
|
Dir: taskDir,
|
|
|
|
SharedAllocDir: filepath.Join(allocDir, SharedAllocName),
|
|
|
|
LogDir: filepath.Join(allocDir, SharedAllocName, LogDirName),
|
|
|
|
SharedTaskDir: filepath.Join(taskDir, SharedAllocName),
|
|
|
|
LocalDir: filepath.Join(taskDir, TaskLocal),
|
|
|
|
SecretsDir: filepath.Join(taskDir, TaskSecrets),
|
2021-10-15 23:56:14 +00:00
|
|
|
skip: skip,
|
2017-01-05 23:57:58 +00:00
|
|
|
logger: logger,
|
2016-12-03 01:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 23:44:52 +00:00
|
|
|
// Build default directories and permissions in a task directory. chrootCreated
|
|
|
|
// allows skipping chroot creation if the caller knows it has already been
|
2021-10-15 23:56:14 +00:00
|
|
|
// done. client.alloc_dir will be skipped.
|
2019-01-04 21:05:40 +00:00
|
|
|
func (t *TaskDir) Build(createChroot bool, chroot map[string]string) error {
|
2016-12-03 01:04:07 +00:00
|
|
|
if err := os.MkdirAll(t.Dir, 0777); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the task directory have non-root permissions.
|
2017-04-04 17:48:29 +00:00
|
|
|
if err := dropDirPermissions(t.Dir, os.ModePerm); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a local directory that each task can use.
|
2017-03-03 03:35:31 +00:00
|
|
|
if err := os.MkdirAll(t.LocalDir, 0777); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-04 17:48:29 +00:00
|
|
|
if err := dropDirPermissions(t.LocalDir, os.ModePerm); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the directories that should be in every task.
|
2017-04-04 17:48:29 +00:00
|
|
|
for dir, perms := range TaskDirs {
|
2016-12-03 01:04:07 +00:00
|
|
|
absdir := filepath.Join(t.Dir, dir)
|
2017-04-04 17:48:29 +00:00
|
|
|
if err := os.MkdirAll(absdir, perms); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-04 17:48:29 +00:00
|
|
|
if err := dropDirPermissions(absdir, perms); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 19:28:34 +00:00
|
|
|
// Only link alloc dir into task dir for chroot fs isolation.
|
2017-01-12 19:15:31 +00:00
|
|
|
// Image based isolation will bind the shared alloc dir in the driver.
|
2017-01-18 19:28:34 +00:00
|
|
|
// If there's no isolation the task will use the host path to the
|
|
|
|
// shared alloc dir.
|
2019-01-04 21:05:40 +00:00
|
|
|
if createChroot {
|
2017-03-02 21:21:34 +00:00
|
|
|
// If the path doesn't exist OR it exists and is empty, link it
|
|
|
|
empty, _ := pathEmpty(t.SharedTaskDir)
|
|
|
|
if !pathExists(t.SharedTaskDir) || empty {
|
|
|
|
if err := linkDir(t.SharedAllocDir, t.SharedTaskDir); err != nil {
|
|
|
|
return fmt.Errorf("Failed to mount shared directory for task: %v", err)
|
|
|
|
}
|
2017-01-12 19:15:31 +00:00
|
|
|
}
|
2016-12-03 01:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the secret directory
|
|
|
|
if err := createSecretDir(t.SecretsDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-04 17:48:29 +00:00
|
|
|
if err := dropDirPermissions(t.SecretsDir, os.ModePerm); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build chroot if chroot filesystem isolation is going to be used
|
2019-01-04 21:05:40 +00:00
|
|
|
if createChroot {
|
|
|
|
if err := t.buildChroot(chroot); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// buildChroot takes a mapping of absolute directory or file paths on the host
|
|
|
|
// to their intended, relative location within the task directory. This
|
|
|
|
// attempts hardlink and then defaults to copying. If the path exists on the
|
2019-01-04 21:05:40 +00:00
|
|
|
// host and can't be embedded an error is returned.
|
|
|
|
func (t *TaskDir) buildChroot(entries map[string]string) error {
|
|
|
|
return t.embedDirs(entries)
|
2016-12-03 01:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskDir) embedDirs(entries map[string]string) error {
|
|
|
|
subdirs := make(map[string]string)
|
|
|
|
for source, dest := range entries {
|
2021-10-15 23:56:14 +00:00
|
|
|
if _, ok := t.skip[source]; ok {
|
|
|
|
// source in skip list
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
// Check to see if directory exists on host.
|
|
|
|
s, err := os.Stat(source)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Embedding a single file
|
|
|
|
if !s.IsDir() {
|
|
|
|
if err := createDir(t.Dir, filepath.Dir(dest)); err != nil {
|
|
|
|
return fmt.Errorf("Couldn't create destination directory %v: %v", dest, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the file.
|
|
|
|
taskEntry := filepath.Join(t.Dir, dest)
|
2017-04-12 19:31:00 +00:00
|
|
|
uid, gid := getOwner(s)
|
|
|
|
if err := linkOrCopy(source, taskEntry, uid, gid, s.Mode().Perm()); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create destination directory.
|
|
|
|
destDir := filepath.Join(t.Dir, dest)
|
|
|
|
|
|
|
|
if err := createDir(t.Dir, dest); err != nil {
|
|
|
|
return fmt.Errorf("Couldn't create destination directory %v: %v", destDir, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enumerate the files in source.
|
|
|
|
dirEntries, err := ioutil.ReadDir(source)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Couldn't read directory %v: %v", source, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range dirEntries {
|
|
|
|
hostEntry := filepath.Join(source, entry.Name())
|
|
|
|
taskEntry := filepath.Join(destDir, filepath.Base(hostEntry))
|
|
|
|
if entry.IsDir() {
|
|
|
|
subdirs[hostEntry] = filepath.Join(dest, filepath.Base(hostEntry))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if entry exists. This can happen if restarting a failed
|
|
|
|
// task.
|
|
|
|
if _, err := os.Lstat(taskEntry); err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !entry.Mode().IsRegular() {
|
|
|
|
// If it is a symlink we can create it, otherwise we skip it.
|
|
|
|
if entry.Mode()&os.ModeSymlink == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
// Symlinking twice
|
|
|
|
if err.(*os.LinkError).Err.Error() != "file exists" {
|
|
|
|
return fmt.Errorf("Couldn't create symlink: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-04-12 19:31:00 +00:00
|
|
|
uid, gid := getOwner(entry)
|
|
|
|
if err := linkOrCopy(hostEntry, taskEntry, uid, gid, entry.Mode().Perm()); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recurse on self to copy subdirectories.
|
|
|
|
if len(subdirs) != 0 {
|
|
|
|
return t.embedDirs(subdirs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|