engine.go 3.4 KB

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