engine.go 2.4 KB

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