switch_console.go 977 B

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