123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- package control
- import (
- "bufio"
- "fmt"
- "io/ioutil"
- "net/http"
- "net/url"
- "os"
- "strings"
- log "github.com/Sirupsen/logrus"
- "gopkg.in/yaml.v2"
- dockerClient "github.com/fsouza/go-dockerclient"
- "github.com/codegangsta/cli"
- "github.com/rancherio/os/cmd/power"
- "github.com/rancherio/os/config"
- "github.com/rancherio/os/docker"
- )
- 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{
- Name: "stage, s",
- Usage: "Only stage the new upgrade, don't apply it",
- },
- cli.StringFlag{
- Name: "image, i",
- Usage: "upgrade to a certain image",
- },
- cli.BoolFlag{
- Name: "force, f",
- Usage: "do not prompt for input",
- },
- cli.BoolFlag{
- Name: "no-reboot",
- Usage: "do not reboot after upgrade",
- },
- },
- },
- {
- Name: "list",
- Usage: "list the current available versions",
- Action: osMetaDataGet,
- },
- {
- Name: "version",
- Usage: "show the currently installed version",
- Action: osVersion,
- },
- }
- }
- func getImages() (*Images, error) {
- upgradeUrl, err := getUpgradeUrl()
- if err != nil {
- return nil, err
- }
- 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
- }
- }
- return parseBody(body)
- }
- func osMetaDataGet(c *cli.Context) {
- images, err := getImages()
- if err != nil {
- log.Fatal(err)
- }
- client, err := docker.NewSystemClient()
- if err != nil {
- log.Fatal(err)
- }
- for _, image := range images.Available {
- _, err := client.InspectImage(image)
- if err == dockerClient.ErrNoSuchImage {
- fmt.Println(image, "remote")
- } else {
- fmt.Println(image, "local")
- }
- }
- }
- func getLatestImage() (string, error) {
- images, err := getImages()
- if err != nil {
- return "", err
- }
- return images.Current, nil
- }
- func osUpgrade(c *cli.Context) {
- image := c.String("image")
- if image == "" {
- var err error
- image, err = getLatestImage()
- if err != nil {
- log.Fatal(err)
- }
- if image == "" {
- log.Fatal("Failed to find latest image")
- }
- }
- if c.Args().Present() {
- log.Fatalf("invalid arguments %v", c.Args())
- }
- startUpgradeContainer(image, c.Bool("stage"), c.Bool("force"), !c.Bool("no-reboot"))
- }
- func osVersion(c *cli.Context) {
- fmt.Println(config.VERSION)
- }
- 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"
- }
- func startUpgradeContainer(image string, stage, force, reboot bool) {
- in := bufio.NewReader(os.Stdin)
- container := docker.NewContainer(config.DOCKER_SYSTEM_HOST, &config.ContainerConfig{
- Cmd: "--name=os-upgrade " +
- "--log-driver=json-file " +
- "--rm " +
- "--privileged " +
- "--net=host " +
- image + " " +
- "-t rancher-upgrade " +
- "-r " + config.VERSION,
- }).Stage()
- if container.Err != nil {
- log.Fatal(container.Err)
- }
- if !stage {
- fmt.Printf("Upgrading to %s\n", image)
- if !force {
- if !yes(in, "Continue") {
- os.Exit(1)
- }
- }
- container.Start()
- if container.Err != nil {
- log.Fatal(container.Err)
- }
- client, err := docker.NewClient(config.DOCKER_SYSTEM_HOST)
- if err != nil {
- log.Fatal(err)
- }
- go func() {
- client.Logs(dockerClient.LogsOptions{
- Container: container.Container.ID,
- OutputStream: os.Stdout,
- ErrorStream: os.Stderr,
- Follow: true,
- Stdout: true,
- Stderr: true,
- })
- }()
- exit, err := client.WaitContainer(container.Container.ID)
- if err != nil {
- log.Fatal(err)
- }
- if container.Err != nil {
- log.Fatal(container.Err)
- }
- if exit == 0 {
- if reboot && (force || yes(in, "Continue with reboot")) {
- log.Info("Rebooting")
- power.Reboot()
- }
- } else {
- log.Error("Upgrade failed")
- os.Exit(exit)
- }
- }
- }
- func parseBody(body []byte) (*Images, error) {
- update := &Images{}
- err := yaml.Unmarshal(body, update)
- if err != nil {
- return nil, err
- }
- return update, nil
- }
- func getUpgradeUrl() (string, error) {
- cfg, err := config.LoadConfig()
- if err != nil {
- return "", err
- }
- return cfg.Upgrade.Url, nil
- }
|