install.go 3.1 KB

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