engine.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package control
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "sort"
  6. "strings"
  7. "golang.org/x/net/context"
  8. log "github.com/Sirupsen/logrus"
  9. "github.com/codegangsta/cli"
  10. "github.com/docker/libcompose/project/options"
  11. "github.com/rancher/os/compose"
  12. "github.com/rancher/os/config"
  13. "github.com/rancher/os/util/network"
  14. )
  15. const (
  16. dockerDone = "/run/docker-done"
  17. )
  18. func engineSubcommands() []cli.Command {
  19. return []cli.Command{
  20. {
  21. Name: "switch",
  22. Usage: "switch Docker engine without a reboot",
  23. Action: engineSwitch,
  24. Flags: []cli.Flag{
  25. cli.BoolFlag{
  26. Name: "force, f",
  27. Usage: "do not prompt for input",
  28. },
  29. cli.BoolFlag{
  30. Name: "no-pull",
  31. Usage: "don't pull console image",
  32. },
  33. },
  34. },
  35. {
  36. Name: "enable",
  37. Usage: "set Docker engine to be switched on next reboot",
  38. Action: engineEnable,
  39. },
  40. {
  41. Name: "list",
  42. Usage: "list available Docker engines",
  43. Action: engineList,
  44. },
  45. }
  46. }
  47. func engineSwitch(c *cli.Context) error {
  48. if len(c.Args()) != 1 {
  49. log.Fatal("Must specify exactly one Docker engine to switch to")
  50. }
  51. newEngine := c.Args()[0]
  52. cfg := config.LoadConfig()
  53. project, err := compose.GetProject(cfg, true, false)
  54. if err != nil {
  55. log.Fatal(err)
  56. }
  57. if err = project.Stop(context.Background(), 10, "docker"); err != nil {
  58. log.Fatal(err)
  59. }
  60. if err = compose.LoadSpecialService(project, cfg, "docker", newEngine); err != nil {
  61. log.Fatal(err)
  62. }
  63. if err = project.Up(context.Background(), options.Up{}, "docker"); err != nil {
  64. log.Fatal(err)
  65. }
  66. if err := config.Set("rancher.docker.engine", newEngine); err != nil {
  67. log.Errorf("Failed to update rancher.docker.engine: %v", err)
  68. }
  69. return nil
  70. }
  71. func engineEnable(c *cli.Context) error {
  72. if len(c.Args()) != 1 {
  73. log.Fatal("Must specify exactly one Docker engine to enable")
  74. }
  75. newEngine := c.Args()[0]
  76. cfg := config.LoadConfig()
  77. if err := compose.StageServices(cfg, newEngine); err != nil {
  78. return err
  79. }
  80. if err := config.Set("rancher.docker.engine", newEngine); err != nil {
  81. log.Errorf("Failed to update 'rancher.docker.engine': %v", err)
  82. }
  83. return nil
  84. }
  85. func engineList(c *cli.Context) error {
  86. cfg := config.LoadConfig()
  87. engines, err := network.GetEngines(cfg.Rancher.Repositories.ToArray())
  88. if err != nil {
  89. return err
  90. }
  91. sort.Strings(engines)
  92. currentEngine := currentEngine()
  93. for _, engine := range engines {
  94. if engine == currentEngine {
  95. fmt.Printf("current %s\n", engine)
  96. } else if engine == cfg.Rancher.Docker.Engine {
  97. fmt.Printf("enabled %s\n", engine)
  98. } else {
  99. fmt.Printf("disabled %s\n", engine)
  100. }
  101. }
  102. return nil
  103. }
  104. func currentEngine() (engine string) {
  105. engineBytes, err := ioutil.ReadFile(dockerDone)
  106. if err == nil {
  107. engine = strings.TrimSpace(string(engineBytes))
  108. } else {
  109. log.Warnf("Failed to detect current Docker engine: %v", err)
  110. }
  111. return
  112. }