1
0
mirror of https://github.com/rancher/os.git synced 2025-06-10 23:45:36 +00:00
os/cmd/control/os.go

254 lines
4.6 KiB
Go
Raw Normal View History

package control
import (
2015-03-20 18:34:34 +00:00
"bufio"
"fmt"
"io/ioutil"
"net/http"
2015-03-18 12:34:23 +00:00
"net/url"
2015-03-19 21:56:41 +00:00
"os"
2015-03-18 12:34:23 +00:00
"strings"
log "github.com/Sirupsen/logrus"
yaml "github.com/cloudfoundry-incubator/candiedyaml"
2015-03-18 12:34:23 +00:00
dockerClient "github.com/fsouza/go-dockerclient"
"github.com/codegangsta/cli"
2015-08-04 21:45:38 +00:00
"github.com/docker/libcompose/project"
"github.com/rancher/os/cmd/power"
"github.com/rancher/os/compose"
"github.com/rancher/os/config"
"github.com/rancher/os/docker"
)
2015-03-18 12:34:23 +00:00
type Images struct {
Current string `yaml:"current,omitempty"`
Available []string `yaml:"available,omitempty"`
}
func osSubcommands() []cli.Command {
return []cli.Command{
{
Name: "upgrade",
Usage: "upgrade to latest version",
Action: osUpgrade,
Flags: []cli.Flag{
cli.BoolFlag{
2015-02-23 19:00:33 +00:00
Name: "stage, s",
Usage: "Only stage the new upgrade, don't apply it",
},
cli.StringFlag{
2015-02-23 19:00:33 +00:00
Name: "image, i",
Usage: "upgrade to a certain image",
},
2015-03-19 21:56:41 +00:00
cli.BoolFlag{
Name: "force, f",
Usage: "do not prompt for input",
},
cli.BoolFlag{
Name: "no-reboot",
Usage: "do not reboot after upgrade",
},
2016-02-27 23:30:53 +00:00
cli.BoolFlag{
Name: "kexec",
Usage: "reboot using kexec",
},
},
},
{
2015-02-23 19:00:33 +00:00
Name: "list",
Usage: "list the current available versions",
Action: osMetaDataGet,
},
2015-04-11 15:32:15 +00:00
{
Name: "version",
Usage: "show the currently installed version",
Action: osVersion,
},
}
}
2015-03-18 12:34:23 +00:00
func getImages() (*Images, error) {
upgradeUrl, err := getUpgradeUrl()
if err != nil {
2015-03-18 12:34:23 +00:00
return nil, err
}
2015-03-18 12:34:23 +00:00
var body []byte
if strings.HasPrefix(upgradeUrl, "/") {
body, err = ioutil.ReadFile(upgradeUrl)
if err != nil {
return nil, err
}
} else {
u, err := url.Parse(upgradeUrl)
if err != nil {
return nil, err
}
q := u.Query()
q.Set("current", config.VERSION)
u.RawQuery = q.Encode()
upgradeUrl = u.String()
resp, err := http.Get(upgradeUrl)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
}
2015-03-18 12:34:23 +00:00
return parseBody(body)
}
2015-02-23 19:00:33 +00:00
func osMetaDataGet(c *cli.Context) {
2015-03-18 12:34:23 +00:00
images, err := getImages()
if err != nil {
log.Fatal(err)
}
2015-03-18 12:34:23 +00:00
client, err := docker.NewSystemClient()
if err != nil {
log.Fatal(err)
}
2015-03-18 12:34:23 +00:00
for _, image := range images.Available {
_, err := client.InspectImage(image)
if err == dockerClient.ErrNoSuchImage {
2015-03-19 21:56:41 +00:00
fmt.Println(image, "remote")
2015-03-18 12:34:23 +00:00
} else {
2015-03-19 21:56:41 +00:00
fmt.Println(image, "local")
2015-03-18 12:34:23 +00:00
}
}
}
2015-03-18 12:34:23 +00:00
func getLatestImage() (string, error) {
images, err := getImages()
if err != nil {
return "", err
}
2015-03-18 12:34:23 +00:00
return images.Current, nil
}
func osUpgrade(c *cli.Context) {
image := c.String("image")
if image == "" {
var err error
2015-03-18 12:34:23 +00:00
image, err = getLatestImage()
if err != nil {
log.Fatal(err)
}
2015-03-18 12:34:23 +00:00
if image == "" {
log.Fatal("Failed to find latest image")
}
}
if c.Args().Present() {
log.Fatalf("invalid arguments %v", c.Args())
}
2016-02-27 23:30:53 +00:00
if err := startUpgradeContainer(image, c.Bool("stage"), c.Bool("force"), !c.Bool("no-reboot"), c.Bool("kexec")); err != nil {
2015-08-04 21:45:38 +00:00
log.Fatal(err)
}
}
2015-04-11 15:32:15 +00:00
func osVersion(c *cli.Context) {
fmt.Println(config.VERSION)
}
2015-03-20 18:34:34 +00:00
func yes(in *bufio.Reader, question string) bool {
fmt.Printf("%s [y/N]: ", question)
line, err := in.ReadString('\n')
if err != nil {
log.Fatal(err)
}
return strings.ToLower(line[0:1]) == "y"
}
2016-02-27 23:30:53 +00:00
func startUpgradeContainer(image string, stage, force, reboot, kexec bool) error {
2015-03-20 18:34:34 +00:00
in := bufio.NewReader(os.Stdin)
2016-02-27 23:30:53 +00:00
command := []string{
"-t", "rancher-upgrade",
"-r", config.VERSION,
}
if kexec {
command = append(command, "-k")
}
2015-08-04 21:45:38 +00:00
container, err := compose.CreateService(nil, "os-upgrade", &project.ServiceConfig{
LogDriver: "json-file",
Privileged: true,
Net: "host",
2016-02-27 23:30:53 +00:00
Pid: "host",
2015-08-04 21:45:38 +00:00
Image: image,
Labels: project.NewSliceorMap(map[string]string{
config.SCOPE: config.SYSTEM,
}),
2016-02-27 23:30:53 +00:00
Command: project.NewCommand(command...),
2015-08-04 21:45:38 +00:00
})
if err != nil {
return err
}
2015-08-04 21:45:38 +00:00
if err := container.Pull(); err != nil {
return err
}
if !stage {
2015-04-13 18:46:38 +00:00
fmt.Printf("Upgrading to %s\n", image)
2015-03-19 21:56:41 +00:00
if !force {
2015-03-20 18:34:34 +00:00
if !yes(in, "Continue") {
2015-03-19 21:56:41 +00:00
os.Exit(1)
}
}
2016-02-26 20:04:32 +00:00
if err := container.Up(); err != nil {
2015-08-04 21:45:38 +00:00
return err
}
2015-03-19 21:56:41 +00:00
2015-08-04 21:45:38 +00:00
if err := container.Log(); err != nil {
return err
2015-03-19 21:56:41 +00:00
}
2016-02-25 22:02:58 +00:00
if err := container.Delete(); err != nil {
return err
}
2015-08-04 21:45:38 +00:00
if reboot && (force || yes(in, "Continue with reboot")) {
log.Info("Rebooting")
power.Reboot()
2015-03-19 21:56:41 +00:00
}
}
2015-08-04 21:45:38 +00:00
return nil
}
2015-03-18 12:34:23 +00:00
func parseBody(body []byte) (*Images, error) {
update := &Images{}
err := yaml.Unmarshal(body, update)
if err != nil {
2015-03-18 12:34:23 +00:00
return nil, err
}
2015-03-18 12:34:23 +00:00
return update, nil
}
2015-03-18 12:34:23 +00:00
func getUpgradeUrl() (string, error) {
cfg, err := config.LoadConfig()
if err != nil {
return "", err
}
return cfg.Rancher.Upgrade.Url, nil
}