switch_console.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package control
  2. import (
  3. "errors"
  4. "github.com/codegangsta/cli"
  5. "github.com/docker/libcompose/project/options"
  6. "github.com/rancher/os/compose"
  7. "github.com/rancher/os/config"
  8. "github.com/rancher/os/log"
  9. "golang.org/x/net/context"
  10. )
  11. func switchConsoleAction(c *cli.Context) error {
  12. if len(c.Args()) != 1 {
  13. return errors.New("Must specify exactly one existing container")
  14. }
  15. newConsole := c.Args()[0]
  16. cfg := config.LoadConfig()
  17. project, err := compose.GetProject(cfg, true, false)
  18. if err != nil {
  19. return err
  20. }
  21. if newConsole != "default" {
  22. if err = compose.LoadSpecialService(project, cfg, "console", newConsole); err != nil {
  23. return err
  24. }
  25. }
  26. if err = config.Set("rancher.console", newConsole); err != nil {
  27. log.Errorf("Failed to update 'rancher.console': %v", err)
  28. }
  29. if err = project.Up(context.Background(), options.Up{
  30. Log: true,
  31. }, "console"); err != nil {
  32. return err
  33. }
  34. if err = project.Restart(context.Background(), 10, "docker"); err != nil {
  35. log.Errorf("Failed to restart Docker: %v", err)
  36. }
  37. return nil
  38. }