1
0
mirror of https://github.com/rancher/os.git synced 2025-05-03 13:46:20 +00:00
os/cmd/control/install.go

143 lines
3.8 KiB
Go
Raw Normal View History

2015-08-18 14:07:00 +00:00
package control
import (
"fmt"
"os"
"os/exec"
"strings"
2015-08-18 14:07:00 +00:00
"github.com/rancher/os/log"
2015-08-18 14:07:00 +00:00
"github.com/codegangsta/cli"
"github.com/rancher/os/cmd/power"
"github.com/rancher/os/config"
"github.com/rancher/os/util"
2015-08-18 14:07:00 +00:00
)
var installCommand = cli.Command{
Name: "install",
Usage: "install RancherOS to disk",
HideHelp: true,
Action: installAction,
Flags: []cli.Flag{
cli.StringFlag{
// TODO: need to validate ? -i rancher/os:v0.3.1 just sat there.
Name: "image, i",
Usage: `install from a certain image (e.g., 'rancher/os:v0.7.0')
use 'ros os list' to see what versions are available.`,
2015-08-18 14:07:00 +00:00
},
cli.StringFlag{
Name: "install-type, t",
Usage: `generic: (Default) Creates 1 ext4 partition and installs RancherOS
amazon-ebs: Installs RancherOS and sets up PV-GRUB
2017-01-05 11:29:09 +00:00
syslinux: partition and format disk (mbr), then install RancherOS and setup Syslinux
gptsyslinux: partition and format disk (gpt), then install RancherOS and setup Syslinux
`,
2015-08-18 14:07:00 +00:00
},
cli.StringFlag{
Name: "cloud-config, c",
Usage: "cloud-config yml file - needed for SSH authorized keys",
},
cli.StringFlag{
Name: "device, d",
Usage: "storage device",
},
cli.BoolFlag{
Name: "force, f",
Usage: "[ DANGEROUS! Data loss can happen ] partition/format without prompting",
},
cli.BoolFlag{
Name: "no-reboot",
Usage: "do not reboot after install",
},
cli.StringFlag{
Name: "append, a",
Usage: "append additional kernel parameters",
},
2015-08-18 14:07:00 +00:00
},
}
func installAction(c *cli.Context) error {
2015-08-18 14:07:00 +00:00
if c.Args().Present() {
log.Fatalf("invalid arguments %v", c.Args())
}
device := c.String("device")
if device == "" {
log.Fatal("Can not proceed without -d <dev> specified")
}
image := c.String("image")
2016-06-02 01:41:55 +00:00
cfg := config.LoadConfig()
2015-08-18 14:07:00 +00:00
if image == "" {
2016-11-28 08:06:00 +00:00
image = cfg.Rancher.Upgrade.Image + ":" + config.Version + config.Suffix
2015-08-18 14:07:00 +00:00
}
installType := c.String("install-type")
if installType == "" {
log.Info("No install type specified...defaulting to generic")
installType = "generic"
}
cloudConfig := c.String("cloud-config")
if cloudConfig == "" {
log.Warn("Cloud-config not provided: you might need to provide cloud-config on boot with ssh_authorized_keys")
} else {
uc := "/opt/user_config.yml"
if err := util.FileCopy(cloudConfig, uc); err != nil {
log.WithFields(log.Fields{"cloudConfig": cloudConfig}).Fatal("Failed to copy cloud-config")
}
cloudConfig = uc
}
append := strings.TrimSpace(c.String("append"))
2015-08-18 14:07:00 +00:00
force := c.Bool("force")
reboot := !c.Bool("no-reboot")
if err := runInstall(image, installType, cloudConfig, device, append, force, reboot); err != nil {
2015-08-18 14:07:00 +00:00
log.WithFields(log.Fields{"err": err}).Fatal("Failed to run install")
}
return nil
2015-08-18 14:07:00 +00:00
}
func runInstall(image, installType, cloudConfig, device, append string, force, reboot bool) error {
2015-08-18 14:07:00 +00:00
fmt.Printf("Installing from %s\n", image)
if !force {
if !yes("Continue") {
2015-08-18 14:07:00 +00:00
os.Exit(1)
}
}
diskType := "msdos"
if installType == "gptsyslinux" {
diskType = "gpt"
}
if installType == "generic" ||
installType == "syslinux" ||
installType == "gptsyslinux" {
2015-08-18 14:07:00 +00:00
cmd := exec.Command("system-docker", "run",
"--net=host", "--privileged", "--volumes-from=all-volumes",
"--entrypoint=/scripts/set-disk-partitions",
image, device, diskType)
2015-08-18 14:07:00 +00:00
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
if err := cmd.Run(); err != nil {
return err
}
}
2016-09-30 04:55:19 +00:00
cmd := exec.Command("system-docker", "run", "--net=host", "--privileged", "--volumes-from=user-volumes",
"--volumes-from=command-volumes", image, "-d", device, "-t", installType, "-c", cloudConfig, "-a", append)
2015-08-18 14:07:00 +00:00
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
if err := cmd.Run(); err != nil {
return err
}
if reboot && (force || yes("Continue with reboot")) {
2015-08-18 14:07:00 +00:00
log.Info("Rebooting")
power.Reboot()
}
return nil
}