engine.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package control
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "sort"
  6. "strings"
  7. "golang.org/x/net/context"
  8. "github.com/codegangsta/cli"
  9. "github.com/docker/libcompose/project/options"
  10. "github.com/rancher/os/cmd/control/service"
  11. "github.com/rancher/os/compose"
  12. "github.com/rancher/os/config"
  13. "github.com/rancher/os/log"
  14. "github.com/rancher/os/util"
  15. "github.com/rancher/os/util/network"
  16. )
  17. func engineSubcommands() []cli.Command {
  18. return []cli.Command{
  19. {
  20. Name: "switch",
  21. Usage: "switch Docker engine without a reboot",
  22. Action: engineSwitch,
  23. Flags: []cli.Flag{
  24. cli.BoolFlag{
  25. Name: "force, f",
  26. Usage: "do not prompt for input",
  27. },
  28. cli.BoolFlag{
  29. Name: "no-pull",
  30. Usage: "don't pull console image",
  31. },
  32. },
  33. },
  34. {
  35. Name: "enable",
  36. Usage: "set Docker engine to be switched on next reboot",
  37. Action: engineEnable,
  38. },
  39. {
  40. Name: "list",
  41. Usage: "list available Docker engines",
  42. Action: engineList,
  43. },
  44. }
  45. }
  46. func engineSwitch(c *cli.Context) error {
  47. if len(c.Args()) != 1 {
  48. log.Fatal("Must specify exactly one Docker engine to switch to")
  49. }
  50. newEngine := c.Args()[0]
  51. cfg := config.LoadConfig()
  52. validateEngine(newEngine, cfg)
  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. validateEngine(newEngine, cfg)
  78. if err := compose.StageServices(cfg, newEngine); err != nil {
  79. return err
  80. }
  81. if err := config.Set("rancher.docker.engine", newEngine); err != nil {
  82. log.Errorf("Failed to update 'rancher.docker.engine': %v", err)
  83. }
  84. return nil
  85. }
  86. func engineList(c *cli.Context) error {
  87. cfg := config.LoadConfig()
  88. engines := availableEngines(cfg)
  89. currentEngine := currentEngine()
  90. for _, engine := range engines {
  91. if engine == currentEngine {
  92. fmt.Printf("current %s\n", engine)
  93. } else if engine == cfg.Rancher.Docker.Engine {
  94. fmt.Printf("enabled %s\n", engine)
  95. } else {
  96. fmt.Printf("disabled %s\n", engine)
  97. }
  98. }
  99. return nil
  100. }
  101. func validateEngine(engine string, cfg *config.CloudConfig) {
  102. engines := availableEngines(cfg)
  103. if !service.IsLocalOrURL(engine) && !util.Contains(engines, engine) {
  104. log.Fatalf("%s is not a valid engine", engine)
  105. }
  106. }
  107. func availableEngines(cfg *config.CloudConfig) []string {
  108. engines, err := network.GetEngines(cfg.Rancher.Repositories.ToArray())
  109. if err != nil {
  110. log.Fatal(err)
  111. }
  112. sort.Strings(engines)
  113. return engines
  114. }
  115. func currentEngine() (engine string) {
  116. engineBytes, err := ioutil.ReadFile(dockerDone)
  117. if err == nil {
  118. engine = strings.TrimSpace(string(engineBytes))
  119. } else {
  120. log.Warnf("Failed to detect current Docker engine: %v", err)
  121. }
  122. return
  123. }