install.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package control
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "strings"
  8. log "github.com/Sirupsen/logrus"
  9. "github.com/codegangsta/cli"
  10. "github.com/rancher/os/cmd/power"
  11. "github.com/rancher/os/config"
  12. "github.com/rancher/os/util"
  13. )
  14. var installCommand = cli.Command{
  15. Name: "install",
  16. Usage: "install RancherOS to disk",
  17. HideHelp: true,
  18. Action: installAction,
  19. Flags: []cli.Flag{
  20. cli.StringFlag{
  21. Name: "image, i",
  22. Usage: "install from a certain image",
  23. },
  24. cli.StringFlag{
  25. Name: "install-type, t",
  26. Usage: `generic: (Default) Creates 1 ext4 partition and installs RancherOS
  27. amazon-ebs: Installs RancherOS and sets up PV-GRUB`,
  28. },
  29. cli.StringFlag{
  30. Name: "cloud-config, c",
  31. Usage: "cloud-config yml file - needed for SSH authorized keys",
  32. },
  33. cli.StringFlag{
  34. Name: "device, d",
  35. Usage: "storage device",
  36. },
  37. cli.BoolFlag{
  38. Name: "force, f",
  39. Usage: "[ DANGEROUS! Data loss can happen ] partition/format without prompting",
  40. },
  41. cli.BoolFlag{
  42. Name: "no-reboot",
  43. Usage: "do not reboot after install",
  44. },
  45. cli.StringFlag{
  46. Name: "append, a",
  47. Usage: "append additional kernel parameters",
  48. },
  49. },
  50. }
  51. func installAction(c *cli.Context) error {
  52. if c.Args().Present() {
  53. log.Fatalf("invalid arguments %v", c.Args())
  54. }
  55. device := c.String("device")
  56. if device == "" {
  57. log.Fatal("Can not proceed without -d <dev> specified")
  58. }
  59. image := c.String("image")
  60. cfg := config.LoadConfig()
  61. if image == "" {
  62. image = cfg.Rancher.Upgrade.Image + ":" + config.VERSION + config.SUFFIX
  63. }
  64. installType := c.String("install-type")
  65. if installType == "" {
  66. log.Info("No install type specified...defaulting to generic")
  67. installType = "generic"
  68. }
  69. cloudConfig := c.String("cloud-config")
  70. if cloudConfig == "" {
  71. log.Warn("Cloud-config not provided: you might need to provide cloud-config on boot with ssh_authorized_keys")
  72. } else {
  73. uc := "/opt/user_config.yml"
  74. if err := util.FileCopy(cloudConfig, uc); err != nil {
  75. log.WithFields(log.Fields{"cloudConfig": cloudConfig}).Fatal("Failed to copy cloud-config")
  76. }
  77. cloudConfig = uc
  78. }
  79. append := strings.TrimSpace(c.String("append"))
  80. force := c.Bool("force")
  81. reboot := !c.Bool("no-reboot")
  82. if err := runInstall(image, installType, cloudConfig, device, append, force, reboot); err != nil {
  83. log.WithFields(log.Fields{"err": err}).Fatal("Failed to run install")
  84. }
  85. return nil
  86. }
  87. func runInstall(image, installType, cloudConfig, device, append string, force, reboot bool) error {
  88. in := bufio.NewReader(os.Stdin)
  89. fmt.Printf("Installing from %s\n", image)
  90. if !force {
  91. if !yes(in, "Continue") {
  92. os.Exit(1)
  93. }
  94. }
  95. if installType == "generic" {
  96. cmd := exec.Command("system-docker", "run", "--net=host", "--privileged", "--volumes-from=all-volumes",
  97. "--entrypoint=/scripts/set-disk-partitions", image, device)
  98. cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
  99. if err := cmd.Run(); err != nil {
  100. return err
  101. }
  102. }
  103. cmd := exec.Command("system-docker", "run", "--net=host", "--privileged", "--volumes-from=user-volumes",
  104. "--volumes-from=command-volumes", image, "-d", device, "-t", installType, "-c", cloudConfig, "-a", append)
  105. cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
  106. if err := cmd.Run(); err != nil {
  107. return err
  108. }
  109. if reboot && (force || yes(in, "Continue with reboot")) {
  110. log.Info("Rebooting")
  111. power.Reboot()
  112. }
  113. return nil
  114. }