console.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package control
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "golang.org/x/net/context"
  7. log "github.com/Sirupsen/logrus"
  8. "github.com/codegangsta/cli"
  9. composeConfig "github.com/docker/libcompose/config"
  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. func consoleSubcommands() []cli.Command {
  16. return []cli.Command{
  17. {
  18. Name: "switch",
  19. Usage: "switch console without a reboot",
  20. Action: consoleSwitch,
  21. Flags: []cli.Flag{
  22. cli.BoolFlag{
  23. Name: "force, f",
  24. Usage: "do not prompt for input",
  25. },
  26. },
  27. },
  28. {
  29. Name: "enable",
  30. Usage: "set console to be switched on next reboot",
  31. Action: consoleEnable,
  32. },
  33. {
  34. Name: "list",
  35. Usage: "list available consoles",
  36. Action: consoleList,
  37. },
  38. }
  39. }
  40. func consoleSwitch(c *cli.Context) error {
  41. if len(c.Args()) != 1 {
  42. log.Fatal("Must specify exactly one console to switch to")
  43. }
  44. newConsole := c.Args()[0]
  45. if !c.Bool("force") {
  46. in := bufio.NewReader(os.Stdin)
  47. fmt.Println("Switching consoles will destroy the current console container and restart Docker.")
  48. fmt.Println("Note: You will also be logged out.")
  49. if !yes(in, "Continue") {
  50. return nil
  51. }
  52. }
  53. cfg := config.LoadConfig()
  54. if newConsole != "default" {
  55. if err := compose.StageServices(cfg, newConsole); err != nil {
  56. return err
  57. }
  58. }
  59. service, err := compose.CreateService(nil, "switch-console", &composeConfig.ServiceConfigV1{
  60. LogDriver: "json-file",
  61. Privileged: true,
  62. Net: "host",
  63. Pid: "host",
  64. Image: fmt.Sprintf("rancher/os-base:%s", config.VERSION),
  65. Labels: map[string]string{
  66. config.SCOPE: config.SYSTEM,
  67. },
  68. Command: []string{"/usr/bin/switch-console", newConsole},
  69. VolumesFrom: []string{"all-volumes"},
  70. })
  71. if err != nil {
  72. return err
  73. }
  74. if err = service.Delete(context.Background(), options.Delete{}); err != nil {
  75. return err
  76. }
  77. if err = service.Up(context.Background(), options.Up{}); err != nil {
  78. return err
  79. }
  80. return service.Log(context.Background(), true)
  81. }
  82. func consoleEnable(c *cli.Context) error {
  83. if len(c.Args()) != 1 {
  84. log.Fatal("Must specify exactly one console to enable")
  85. }
  86. newConsole := c.Args()[0]
  87. cfg := config.LoadConfig()
  88. if newConsole != "default" {
  89. if err := compose.StageServices(cfg, newConsole); err != nil {
  90. return err
  91. }
  92. }
  93. if err := config.Set("rancher.console", newConsole); err != nil {
  94. log.Errorf("Failed to update 'rancher.console': %v", err)
  95. }
  96. return nil
  97. }
  98. func consoleList(c *cli.Context) error {
  99. cfg := config.LoadConfig()
  100. consoles, err := network.GetConsoles(cfg.Rancher.Repositories.ToArray())
  101. if err != nil {
  102. return err
  103. }
  104. fmt.Println("default")
  105. for _, console := range consoles {
  106. fmt.Println(console)
  107. }
  108. return nil
  109. }