engine.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package engine
  2. import (
  3. "fmt"
  4. "github.com/dotcloud/docker/utils"
  5. "io"
  6. "log"
  7. "os"
  8. "path/filepath"
  9. "runtime"
  10. "strings"
  11. )
  12. type Handler func(*Job) Status
  13. var globalHandlers map[string]Handler
  14. func init() {
  15. globalHandlers = make(map[string]Handler)
  16. }
  17. func Register(name string, handler Handler) error {
  18. _, exists := globalHandlers[name]
  19. if exists {
  20. return fmt.Errorf("Can't overwrite global handler for command %s", name)
  21. }
  22. globalHandlers[name] = handler
  23. return nil
  24. }
  25. // The Engine is the core of Docker.
  26. // It acts as a store for *containers*, and allows manipulation of these
  27. // containers by executing *jobs*.
  28. type Engine struct {
  29. root string
  30. handlers map[string]Handler
  31. hack Hack // data for temporary hackery (see hack.go)
  32. id string
  33. Stdout io.Writer
  34. Stderr io.Writer
  35. Stdin io.Reader
  36. }
  37. func (eng *Engine) Root() string {
  38. return eng.root
  39. }
  40. func (eng *Engine) Register(name string, handler Handler) error {
  41. eng.Logf("Register(%s) (handlers=%v)", name, eng.handlers)
  42. _, exists := eng.handlers[name]
  43. if exists {
  44. return fmt.Errorf("Can't overwrite handler for command %s", name)
  45. }
  46. eng.handlers[name] = handler
  47. return nil
  48. }
  49. // New initializes a new engine managing the directory specified at `root`.
  50. // `root` is used to store containers and any other state private to the engine.
  51. // Changing the contents of the root without executing a job will cause unspecified
  52. // behavior.
  53. func New(root string) (*Engine, error) {
  54. // Check for unsupported architectures
  55. if runtime.GOARCH != "amd64" {
  56. return nil, fmt.Errorf("The docker runtime currently only supports amd64 (not %s). This will change in the future. Aborting.", runtime.GOARCH)
  57. }
  58. // Check for unsupported kernel versions
  59. // FIXME: it would be cleaner to not test for specific versions, but rather
  60. // test for specific functionalities.
  61. // Unfortunately we can't test for the feature "does not cause a kernel panic"
  62. // without actually causing a kernel panic, so we need this workaround until
  63. // the circumstances of pre-3.8 crashes are clearer.
  64. // For details see http://github.com/dotcloud/docker/issues/407
  65. if k, err := utils.GetKernelVersion(); err != nil {
  66. log.Printf("WARNING: %s\n", err)
  67. } else {
  68. if utils.CompareKernelVersion(k, &utils.KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0}) < 0 {
  69. if os.Getenv("DOCKER_NOWARN_KERNEL_VERSION") == "" {
  70. log.Printf("WARNING: You are running linux kernel version %s, which might be unstable running docker. Please upgrade your kernel to 3.8.0.", k.String())
  71. }
  72. }
  73. }
  74. if err := os.MkdirAll(root, 0700); err != nil && !os.IsExist(err) {
  75. return nil, err
  76. }
  77. // Docker makes some assumptions about the "absoluteness" of root
  78. // ... so let's make sure it has no symlinks
  79. if p, err := filepath.Abs(root); err != nil {
  80. log.Fatalf("Unable to get absolute root (%s): %s", root, err)
  81. } else {
  82. root = p
  83. }
  84. if p, err := filepath.EvalSymlinks(root); err != nil {
  85. log.Fatalf("Unable to canonicalize root (%s): %s", root, err)
  86. } else {
  87. root = p
  88. }
  89. eng := &Engine{
  90. root: root,
  91. handlers: make(map[string]Handler),
  92. id: utils.RandomString(),
  93. Stdout: os.Stdout,
  94. Stderr: os.Stderr,
  95. Stdin: os.Stdin,
  96. }
  97. // Copy existing global handlers
  98. for k, v := range globalHandlers {
  99. eng.handlers[k] = v
  100. }
  101. return eng, nil
  102. }
  103. func (eng *Engine) String() string {
  104. return fmt.Sprintf("%s|%s", eng.Root(), eng.id[:8])
  105. }
  106. // Job creates a new job which can later be executed.
  107. // This function mimics `Command` from the standard os/exec package.
  108. func (eng *Engine) Job(name string, args ...string) *Job {
  109. job := &Job{
  110. Eng: eng,
  111. Name: name,
  112. Args: args,
  113. Stdin: NewInput(),
  114. Stdout: NewOutput(),
  115. Stderr: NewOutput(),
  116. env: &Env{},
  117. }
  118. job.Stderr.Add(utils.NopWriteCloser(eng.Stderr))
  119. handler, exists := eng.handlers[name]
  120. if exists {
  121. job.handler = handler
  122. }
  123. return job
  124. }
  125. func (eng *Engine) Logf(format string, args ...interface{}) (n int, err error) {
  126. prefixedFormat := fmt.Sprintf("[%s] %s\n", eng, strings.TrimRight(format, "\n"))
  127. return fmt.Fprintf(eng.Stderr, prefixedFormat, args...)
  128. }