switch_console.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package control
  2. import (
  3. "errors"
  4. "github.com/rancher/os/config"
  5. "github.com/rancher/os/pkg/compose"
  6. "github.com/rancher/os/pkg/log"
  7. "github.com/codegangsta/cli"
  8. "github.com/docker/libcompose/project/options"
  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. // stop docker and console to avoid zombie process
  22. if err = project.Stop(context.Background(), 10, "docker"); err != nil {
  23. log.Errorf("Failed to stop Docker: %v", err)
  24. }
  25. if err = project.Stop(context.Background(), 10, "console"); err != nil {
  26. log.Errorf("Failed to stop console: %v", err)
  27. }
  28. if newConsole != "default" {
  29. if err = compose.LoadSpecialService(project, cfg, "console", newConsole); err != nil {
  30. return err
  31. }
  32. }
  33. if err = config.Set("rancher.console", newConsole); err != nil {
  34. log.Errorf("Failed to update 'rancher.console': %v", err)
  35. }
  36. if err = project.Up(context.Background(), options.Up{
  37. Log: true,
  38. }, "console"); err != nil {
  39. return err
  40. }
  41. if err = project.Start(context.Background(), "docker"); err != nil {
  42. log.Errorf("Failed to start Docker: %v", err)
  43. }
  44. return nil
  45. }