os.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package control
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/url"
  6. "os"
  7. "runtime"
  8. "strings"
  9. "golang.org/x/net/context"
  10. yaml "github.com/cloudfoundry-incubator/candiedyaml"
  11. "github.com/rancher/os/log"
  12. "github.com/codegangsta/cli"
  13. dockerClient "github.com/docker/engine-api/client"
  14. composeConfig "github.com/docker/libcompose/config"
  15. "github.com/docker/libcompose/project/options"
  16. "github.com/rancher/os/cmd/power"
  17. "github.com/rancher/os/compose"
  18. "github.com/rancher/os/config"
  19. "github.com/rancher/os/docker"
  20. "github.com/rancher/os/util"
  21. "github.com/rancher/os/util/network"
  22. )
  23. type Images struct {
  24. Current string `yaml:"current,omitempty"`
  25. Available []string `yaml:"available,omitempty"`
  26. }
  27. func osSubcommands() []cli.Command {
  28. return []cli.Command{
  29. {
  30. Name: "upgrade",
  31. Usage: "upgrade to latest version",
  32. Action: osUpgrade,
  33. Flags: []cli.Flag{
  34. cli.BoolFlag{
  35. Name: "stage, s",
  36. Usage: "Only stage the new upgrade, don't apply it",
  37. },
  38. cli.StringFlag{
  39. Name: "image, i",
  40. Usage: "upgrade to a certain image",
  41. },
  42. cli.BoolFlag{
  43. Name: "force, f",
  44. Usage: "do not prompt for input",
  45. },
  46. cli.BoolFlag{
  47. Name: "no-reboot",
  48. Usage: "do not reboot after upgrade",
  49. },
  50. cli.BoolFlag{
  51. Name: "kexec, k",
  52. Usage: "reboot using kexec",
  53. },
  54. cli.StringFlag{
  55. Name: "append",
  56. Usage: "append additional kernel parameters",
  57. },
  58. cli.BoolFlag{
  59. Name: "upgrade-console",
  60. Usage: "upgrade console even if persistent",
  61. },
  62. cli.BoolFlag{
  63. Name: "debug",
  64. Usage: "Run installer with debug output",
  65. },
  66. },
  67. },
  68. {
  69. Name: "list",
  70. Usage: "list the current available versions",
  71. Action: osMetaDataGet,
  72. },
  73. {
  74. Name: "version",
  75. Usage: "show the currently installed version",
  76. Action: osVersion,
  77. },
  78. }
  79. }
  80. func getImages() (*Images, error) {
  81. upgradeURL, err := getUpgradeURL()
  82. if err != nil {
  83. return nil, err
  84. }
  85. var body []byte
  86. if strings.HasPrefix(upgradeURL, "/") {
  87. body, err = ioutil.ReadFile(upgradeURL)
  88. if err != nil {
  89. return nil, err
  90. }
  91. } else {
  92. u, err := url.Parse(upgradeURL)
  93. if err != nil {
  94. return nil, err
  95. }
  96. q := u.Query()
  97. q.Set("current", config.Version)
  98. if hypervisor := util.GetHypervisor(); hypervisor == "" {
  99. q.Set("hypervisor", hypervisor)
  100. }
  101. u.RawQuery = q.Encode()
  102. upgradeURL = u.String()
  103. body, err = network.LoadFromNetwork(upgradeURL)
  104. if err != nil {
  105. return nil, err
  106. }
  107. }
  108. images, err := parseBody(body)
  109. if err != nil {
  110. return nil, err
  111. }
  112. cfg := config.LoadConfig()
  113. images.Current = formatImage(images.Current, cfg)
  114. for i := len(images.Available) - 1; i >= 0; i-- {
  115. images.Available[i] = formatImage(images.Available[i], cfg)
  116. }
  117. return images, nil
  118. }
  119. func osMetaDataGet(c *cli.Context) error {
  120. images, err := getImages()
  121. if err != nil {
  122. log.Fatal(err)
  123. }
  124. client, err := docker.NewSystemClient()
  125. if err != nil {
  126. log.Fatal(err)
  127. }
  128. cfg := config.LoadConfig()
  129. runningName := cfg.Rancher.Upgrade.Image + ":" + config.Version
  130. runningName = formatImage(runningName, cfg)
  131. foundRunning := false
  132. for i := len(images.Available) - 1; i >= 0; i-- {
  133. image := images.Available[i]
  134. _, _, err := client.ImageInspectWithRaw(context.Background(), image, false)
  135. local := "local"
  136. if dockerClient.IsErrImageNotFound(err) {
  137. local = "remote"
  138. }
  139. available := "available"
  140. if image == images.Current {
  141. available = "latest"
  142. }
  143. var running string
  144. if image == runningName {
  145. foundRunning = true
  146. running = "running"
  147. }
  148. fmt.Println(image, local, available, running)
  149. }
  150. if !foundRunning {
  151. fmt.Println(config.Version, "running")
  152. }
  153. return nil
  154. }
  155. func getLatestImage() (string, error) {
  156. images, err := getImages()
  157. if err != nil {
  158. return "", err
  159. }
  160. return images.Current, nil
  161. }
  162. func osUpgrade(c *cli.Context) error {
  163. if runtime.GOARCH != "amd64" {
  164. log.Fatalf("ros install / upgrade only supported on 'amd64', not '%s'", runtime.GOARCH)
  165. }
  166. image := c.String("image")
  167. if image == "" {
  168. var err error
  169. image, err = getLatestImage()
  170. if err != nil {
  171. log.Fatal(err)
  172. }
  173. if image == "" {
  174. log.Fatal("Failed to find latest image")
  175. }
  176. }
  177. if c.Args().Present() {
  178. log.Fatalf("invalid arguments %v", c.Args())
  179. }
  180. if err := startUpgradeContainer(
  181. image,
  182. c.Bool("stage"),
  183. c.Bool("force"),
  184. !c.Bool("no-reboot"),
  185. c.Bool("kexec"),
  186. c.Bool("upgrade-console"),
  187. c.Bool("debug"),
  188. c.String("append"),
  189. ); err != nil {
  190. log.Fatal(err)
  191. }
  192. return nil
  193. }
  194. func osVersion(c *cli.Context) error {
  195. fmt.Println(config.Version)
  196. return nil
  197. }
  198. func startUpgradeContainer(image string, stage, force, reboot, kexec, upgradeConsole, debug bool, kernelArgs string) error {
  199. command := []string{
  200. "-t", "rancher-upgrade",
  201. "-r", config.Version,
  202. }
  203. if kexec {
  204. command = append(command, "--kexec")
  205. }
  206. if debug {
  207. command = append(command, "--debug")
  208. }
  209. kernelArgs = strings.TrimSpace(kernelArgs)
  210. if kernelArgs != "" {
  211. command = append(command, "-a", kernelArgs)
  212. }
  213. if upgradeConsole {
  214. if err := config.Set("rancher.force_console_rebuild", true); err != nil {
  215. log.Fatal(err)
  216. }
  217. }
  218. fmt.Printf("Upgrading to %s\n", image)
  219. confirmation := "Continue"
  220. imageSplit := strings.Split(image, ":")
  221. if len(imageSplit) > 1 && imageSplit[1] == config.Version+config.Suffix {
  222. confirmation = fmt.Sprintf("Already at version %s. Continue anyway", imageSplit[1])
  223. }
  224. if !force && !yes(confirmation) {
  225. os.Exit(1)
  226. }
  227. container, err := compose.CreateService(nil, "os-upgrade", &composeConfig.ServiceConfigV1{
  228. LogDriver: "json-file",
  229. Privileged: true,
  230. Net: "host",
  231. Pid: "host",
  232. Image: image,
  233. Labels: map[string]string{
  234. config.ScopeLabel: config.System,
  235. },
  236. Command: command,
  237. })
  238. if err != nil {
  239. return err
  240. }
  241. client, err := docker.NewSystemClient()
  242. if err != nil {
  243. return err
  244. }
  245. // Only pull image if not found locally
  246. if _, _, err := client.ImageInspectWithRaw(context.Background(), image, false); err != nil {
  247. if err := container.Pull(context.Background()); err != nil {
  248. return err
  249. }
  250. }
  251. if !stage {
  252. // If there is already an upgrade container, delete it
  253. // Up() should to this, but currently does not due to a bug
  254. if err := container.Delete(context.Background(), options.Delete{}); err != nil {
  255. return err
  256. }
  257. if err := container.Up(context.Background(), options.Up{}); err != nil {
  258. return err
  259. }
  260. if err := container.Log(context.Background(), true); err != nil {
  261. return err
  262. }
  263. if err := container.Delete(context.Background(), options.Delete{}); err != nil {
  264. return err
  265. }
  266. if reboot && (force || yes("Continue with reboot")) {
  267. log.Info("Rebooting")
  268. power.Reboot()
  269. }
  270. }
  271. return nil
  272. }
  273. func parseBody(body []byte) (*Images, error) {
  274. update := &Images{}
  275. err := yaml.Unmarshal(body, update)
  276. if err != nil {
  277. return nil, err
  278. }
  279. return update, nil
  280. }
  281. func getUpgradeURL() (string, error) {
  282. cfg := config.LoadConfig()
  283. return cfg.Rancher.Upgrade.URL, nil
  284. }