engine.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. _, exists := eng.handlers[name]
  42. if exists {
  43. return fmt.Errorf("Can't overwrite handler for command %s", name)
  44. }
  45. eng.handlers[name] = handler
  46. return nil
  47. }
  48. // New initializes a new engine managing the directory specified at `root`.
  49. // `root` is used to store containers and any other state private to the engine.
  50. // Changing the contents of the root without executing a job will cause unspecified
  51. // behavior.
  52. func New(root string) (*Engine, error) {
  53. // Check for unsupported architectures
  54. if runtime.GOARCH != "amd64" {
  55. return nil, fmt.Errorf("The docker runtime currently only supports amd64 (not %s). This will change in the future. Aborting.", runtime.GOARCH)
  56. }
  57. // Check for unsupported kernel versions
  58. // FIXME: it would be cleaner to not test for specific versions, but rather
  59. // test for specific functionalities.
  60. // Unfortunately we can't test for the feature "does not cause a kernel panic"
  61. // without actually causing a kernel panic, so we need this workaround until
  62. // the circumstances of pre-3.8 crashes are clearer.
  63. // For details see http://github.com/dotcloud/docker/issues/407
  64. if k, err := utils.GetKernelVersion(); err != nil {
  65. log.Printf("WARNING: %s\n", err)
  66. } else {
  67. if utils.CompareKernelVersion(k, &utils.KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0}) < 0 {
  68. if os.Getenv("DOCKER_NOWARN_KERNEL_VERSION") == "" {
  69. 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())
  70. }
  71. }
  72. }
  73. if err := os.MkdirAll(root, 0700); err != nil && !os.IsExist(err) {
  74. return nil, err
  75. }
  76. // Docker makes some assumptions about the "absoluteness" of root
  77. // ... so let's make sure it has no symlinks
  78. if p, err := filepath.Abs(root); err != nil {
  79. log.Fatalf("Unable to get absolute root (%s): %s", root, err)
  80. } else {
  81. root = p
  82. }
  83. if p, err := filepath.EvalSymlinks(root); err != nil {
  84. log.Fatalf("Unable to canonicalize root (%s): %s", root, err)
  85. } else {
  86. root = p
  87. }
  88. eng := &Engine{
  89. root: root,
  90. handlers: make(map[string]Handler),
  91. id: utils.RandomString(),
  92. Stdout: os.Stdout,
  93. Stderr: os.Stderr,
  94. Stdin: os.Stdin,
  95. }
  96. // Copy existing global handlers
  97. for k, v := range globalHandlers {
  98. eng.handlers[k] = v
  99. }
  100. return eng, nil
  101. }
  102. func (eng *Engine) String() string {
  103. return fmt.Sprintf("%s|%s", eng.Root(), eng.id[:8])
  104. }
  105. // Job creates a new job which can later be executed.
  106. // This function mimics `Command` from the standard os/exec package.
  107. func (eng *Engine) Job(name string, args ...string) *Job {
  108. job := &Job{
  109. Eng: eng,
  110. Name: name,
  111. Args: args,
  112. Stdin: NewInput(),
  113. Stdout: NewOutput(),
  114. Stderr: NewOutput(),
  115. env: &Env{},
  116. }
  117. job.Stderr.Add(utils.NopWriteCloser(eng.Stderr))
  118. handler, exists := eng.handlers[name]
  119. if exists {
  120. job.handler = handler
  121. }
  122. return job
  123. }
  124. func (eng *Engine) Logf(format string, args ...interface{}) (n int, err error) {
  125. if os.Getenv("TEST") == "" {
  126. prefixedFormat := fmt.Sprintf("[%s] %s\n", eng, strings.TrimRight(format, "\n"))
  127. return fmt.Fprintf(eng.Stderr, prefixedFormat, args...)
  128. }
  129. return 0, nil
  130. }