engine.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package engine
  2. import (
  3. "fmt"
  4. "github.com/dotcloud/docker/utils"
  5. "io"
  6. "log"
  7. "os"
  8. "runtime"
  9. "strings"
  10. )
  11. type Handler func(*Job) Status
  12. var globalHandlers map[string]Handler
  13. func init() {
  14. globalHandlers = make(map[string]Handler)
  15. }
  16. func Register(name string, handler Handler) error {
  17. _, exists := globalHandlers[name]
  18. if exists {
  19. return fmt.Errorf("Can't overwrite global handler for command %s", name)
  20. }
  21. globalHandlers[name] = handler
  22. return nil
  23. }
  24. // The Engine is the core of Docker.
  25. // It acts as a store for *containers*, and allows manipulation of these
  26. // containers by executing *jobs*.
  27. type Engine struct {
  28. root string
  29. handlers map[string]Handler
  30. hack Hack // data for temporary hackery (see hack.go)
  31. id string
  32. Stdout io.Writer
  33. Stderr io.Writer
  34. Stdin io.Reader
  35. }
  36. func (eng *Engine) Root() string {
  37. return eng.root
  38. }
  39. func (eng *Engine) Register(name string, handler Handler) error {
  40. eng.Logf("Register(%s) (handlers=%v)", name, eng.handlers)
  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. eng := &Engine{
  77. root: root,
  78. handlers: make(map[string]Handler),
  79. id: utils.RandomString(),
  80. Stdout: os.Stdout,
  81. Stderr: os.Stderr,
  82. Stdin: os.Stdin,
  83. }
  84. // Copy existing global handlers
  85. for k, v := range globalHandlers {
  86. eng.handlers[k] = v
  87. }
  88. return eng, nil
  89. }
  90. func (eng *Engine) String() string {
  91. return fmt.Sprintf("%s|%s", eng.Root(), eng.id[:8])
  92. }
  93. // Job creates a new job which can later be executed.
  94. // This function mimics `Command` from the standard os/exec package.
  95. func (eng *Engine) Job(name string, args ...string) *Job {
  96. job := &Job{
  97. Eng: eng,
  98. Name: name,
  99. Args: args,
  100. Stdin: NewInput(),
  101. Stdout: NewOutput(),
  102. Stderr: NewOutput(),
  103. env: &Env{},
  104. }
  105. job.Stdout.Add(utils.NopWriteCloser(eng.Stdout))
  106. job.Stderr.Add(utils.NopWriteCloser(eng.Stderr))
  107. handler, exists := eng.handlers[name]
  108. if exists {
  109. job.handler = handler
  110. }
  111. return job
  112. }
  113. func (eng *Engine) Logf(format string, args ...interface{}) (n int, err error) {
  114. prefixedFormat := fmt.Sprintf("[%s] %s\n", eng, strings.TrimRight(format, "\n"))
  115. return fmt.Fprintf(eng.Stderr, prefixedFormat, args...)
  116. }