os.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package control
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "net/url"
  8. "os"
  9. "strings"
  10. log "github.com/Sirupsen/logrus"
  11. "gopkg.in/yaml.v2"
  12. dockerClient "github.com/fsouza/go-dockerclient"
  13. "github.com/codegangsta/cli"
  14. "github.com/rancherio/os/cmd/power"
  15. "github.com/rancherio/os/config"
  16. "github.com/rancherio/os/docker"
  17. )
  18. type Images struct {
  19. Current string `yaml:"current,omitempty"`
  20. Available []string `yaml:"available,omitempty"`
  21. }
  22. func osSubcommands() []cli.Command {
  23. return []cli.Command{
  24. {
  25. Name: "upgrade",
  26. Usage: "upgrade to latest version",
  27. Action: osUpgrade,
  28. Flags: []cli.Flag{
  29. cli.BoolFlag{
  30. Name: "stage, s",
  31. Usage: "Only stage the new upgrade, don't apply it",
  32. },
  33. cli.StringFlag{
  34. Name: "image, i",
  35. Usage: "upgrade to a certain image",
  36. },
  37. cli.BoolFlag{
  38. Name: "force, f",
  39. Usage: "do not prompt for input",
  40. },
  41. cli.BoolFlag{
  42. Name: "no-reboot",
  43. Usage: "do not reboot after upgrade",
  44. },
  45. },
  46. },
  47. {
  48. Name: "list",
  49. Usage: "list the current available versions",
  50. Action: osMetaDataGet,
  51. },
  52. {
  53. Name: "version",
  54. Usage: "show the currently installed version",
  55. Action: osVersion,
  56. },
  57. }
  58. }
  59. func getImages() (*Images, error) {
  60. upgradeUrl, err := getUpgradeUrl()
  61. if err != nil {
  62. return nil, err
  63. }
  64. var body []byte
  65. if strings.HasPrefix(upgradeUrl, "/") {
  66. body, err = ioutil.ReadFile(upgradeUrl)
  67. if err != nil {
  68. return nil, err
  69. }
  70. } else {
  71. u, err := url.Parse(upgradeUrl)
  72. if err != nil {
  73. return nil, err
  74. }
  75. q := u.Query()
  76. q.Set("current", config.VERSION)
  77. u.RawQuery = q.Encode()
  78. upgradeUrl = u.String()
  79. resp, err := http.Get(upgradeUrl)
  80. if err != nil {
  81. return nil, err
  82. }
  83. defer resp.Body.Close()
  84. body, err = ioutil.ReadAll(resp.Body)
  85. if err != nil {
  86. return nil, err
  87. }
  88. }
  89. return parseBody(body)
  90. }
  91. func osMetaDataGet(c *cli.Context) {
  92. images, err := getImages()
  93. if err != nil {
  94. log.Fatal(err)
  95. }
  96. client, err := docker.NewSystemClient()
  97. if err != nil {
  98. log.Fatal(err)
  99. }
  100. for _, image := range images.Available {
  101. _, err := client.InspectImage(image)
  102. if err == dockerClient.ErrNoSuchImage {
  103. fmt.Println(image, "remote")
  104. } else {
  105. fmt.Println(image, "local")
  106. }
  107. }
  108. }
  109. func getLatestImage() (string, error) {
  110. images, err := getImages()
  111. if err != nil {
  112. return "", err
  113. }
  114. return images.Current, nil
  115. }
  116. func osUpgrade(c *cli.Context) {
  117. image := c.String("image")
  118. if image == "" {
  119. var err error
  120. image, err = getLatestImage()
  121. if err != nil {
  122. log.Fatal(err)
  123. }
  124. if image == "" {
  125. log.Fatal("Failed to find latest image")
  126. }
  127. }
  128. if c.Args().Present() {
  129. log.Fatalf("invalid arguments %v", c.Args())
  130. }
  131. startUpgradeContainer(image, c.Bool("stage"), c.Bool("force"), !c.Bool("no-reboot"))
  132. }
  133. func osVersion(c *cli.Context) {
  134. fmt.Println(config.VERSION)
  135. }
  136. func yes(in *bufio.Reader, question string) bool {
  137. fmt.Printf("%s [y/N]: ", question)
  138. line, err := in.ReadString('\n')
  139. if err != nil {
  140. log.Fatal(err)
  141. }
  142. return strings.ToLower(line[0:1]) == "y"
  143. }
  144. func startUpgradeContainer(image string, stage, force, reboot bool) {
  145. in := bufio.NewReader(os.Stdin)
  146. container := docker.NewContainer(config.DOCKER_SYSTEM_HOST, &config.ContainerConfig{
  147. Cmd: "--name=os-upgrade " +
  148. "--log-driver=json-file " +
  149. "--rm " +
  150. "--privileged " +
  151. "--net=host " +
  152. image + " " +
  153. "-t rancher-upgrade " +
  154. "-r " + config.VERSION,
  155. }).Stage()
  156. if container.Err != nil {
  157. log.Fatal(container.Err)
  158. }
  159. if !stage {
  160. fmt.Printf("Upgrading to %s\n", image)
  161. if !force {
  162. if !yes(in, "Continue") {
  163. os.Exit(1)
  164. }
  165. }
  166. container.Start()
  167. if container.Err != nil {
  168. log.Fatal(container.Err)
  169. }
  170. client, err := docker.NewClient(config.DOCKER_SYSTEM_HOST)
  171. if err != nil {
  172. log.Fatal(err)
  173. }
  174. go func() {
  175. client.Logs(dockerClient.LogsOptions{
  176. Container: container.Container.ID,
  177. OutputStream: os.Stdout,
  178. ErrorStream: os.Stderr,
  179. Follow: true,
  180. Stdout: true,
  181. Stderr: true,
  182. })
  183. }()
  184. exit, err := client.WaitContainer(container.Container.ID)
  185. if err != nil {
  186. log.Fatal(err)
  187. }
  188. if container.Err != nil {
  189. log.Fatal(container.Err)
  190. }
  191. if exit == 0 {
  192. if reboot && (force || yes(in, "Continue with reboot")) {
  193. log.Info("Rebooting")
  194. power.Reboot()
  195. }
  196. } else {
  197. log.Error("Upgrade failed")
  198. os.Exit(exit)
  199. }
  200. }
  201. }
  202. func parseBody(body []byte) (*Images, error) {
  203. update := &Images{}
  204. err := yaml.Unmarshal(body, update)
  205. if err != nil {
  206. return nil, err
  207. }
  208. return update, nil
  209. }
  210. func getUpgradeUrl() (string, error) {
  211. cfg, err := config.LoadConfig()
  212. if err != nil {
  213. return "", err
  214. }
  215. return cfg.Upgrade.Url, nil
  216. }