os.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. return parseBody(body)
  109. }
  110. func osMetaDataGet(c *cli.Context) error {
  111. images, err := getImages()
  112. if err != nil {
  113. log.Fatal(err)
  114. }
  115. client, err := docker.NewSystemClient()
  116. if err != nil {
  117. log.Fatal(err)
  118. }
  119. cfg := config.LoadConfig()
  120. runningName := cfg.Rancher.Upgrade.Image + ":" + config.Version
  121. foundRunning := false
  122. for i := len(images.Available) - 1; i >= 0; i-- {
  123. image := images.Available[i]
  124. _, _, err := client.ImageInspectWithRaw(context.Background(), image, false)
  125. local := "local"
  126. if dockerClient.IsErrImageNotFound(err) {
  127. local = "remote"
  128. }
  129. available := "available"
  130. if image == images.Current {
  131. available = "latest"
  132. }
  133. var running string
  134. if image == runningName {
  135. foundRunning = true
  136. running = "running"
  137. }
  138. fmt.Println(image, local, available, running)
  139. }
  140. if !foundRunning {
  141. fmt.Println(config.Version, "running")
  142. }
  143. return nil
  144. }
  145. func getLatestImage() (string, error) {
  146. images, err := getImages()
  147. if err != nil {
  148. return "", err
  149. }
  150. return images.Current, nil
  151. }
  152. func osUpgrade(c *cli.Context) error {
  153. if runtime.GOARCH != "amd64" {
  154. log.Fatalf("ros install / upgrade only supported on 'amd64', not '%s'", runtime.GOARCH)
  155. }
  156. image := c.String("image")
  157. if image == "" {
  158. var err error
  159. image, err = getLatestImage()
  160. if err != nil {
  161. log.Fatal(err)
  162. }
  163. if image == "" {
  164. log.Fatal("Failed to find latest image")
  165. }
  166. }
  167. if c.Args().Present() {
  168. log.Fatalf("invalid arguments %v", c.Args())
  169. }
  170. if err := startUpgradeContainer(
  171. image,
  172. c.Bool("stage"),
  173. c.Bool("force"),
  174. !c.Bool("no-reboot"),
  175. c.Bool("kexec"),
  176. c.Bool("upgrade-console"),
  177. c.Bool("debug"),
  178. c.String("append"),
  179. ); err != nil {
  180. log.Fatal(err)
  181. }
  182. return nil
  183. }
  184. func osVersion(c *cli.Context) error {
  185. fmt.Println(config.Version)
  186. return nil
  187. }
  188. func startUpgradeContainer(image string, stage, force, reboot, kexec, upgradeConsole, debug bool, kernelArgs string) error {
  189. command := []string{
  190. "-t", "rancher-upgrade",
  191. "-r", config.Version,
  192. }
  193. if kexec {
  194. command = append(command, "--kexec")
  195. }
  196. if debug {
  197. command = append(command, "--debug")
  198. }
  199. kernelArgs = strings.TrimSpace(kernelArgs)
  200. if kernelArgs != "" {
  201. command = append(command, "-a", kernelArgs)
  202. }
  203. if upgradeConsole {
  204. if err := config.Set("rancher.force_console_rebuild", true); err != nil {
  205. log.Fatal(err)
  206. }
  207. }
  208. fmt.Printf("Upgrading to %s\n", image)
  209. confirmation := "Continue"
  210. imageSplit := strings.Split(image, ":")
  211. if len(imageSplit) > 1 && imageSplit[1] == config.Version+config.Suffix {
  212. confirmation = fmt.Sprintf("Already at version %s. Continue anyway", imageSplit[1])
  213. }
  214. if !force && !yes(confirmation) {
  215. os.Exit(1)
  216. }
  217. container, err := compose.CreateService(nil, "os-upgrade", &composeConfig.ServiceConfigV1{
  218. LogDriver: "json-file",
  219. Privileged: true,
  220. Net: "host",
  221. Pid: "host",
  222. Image: image,
  223. Labels: map[string]string{
  224. config.ScopeLabel: config.System,
  225. },
  226. Command: command,
  227. })
  228. if err != nil {
  229. return err
  230. }
  231. client, err := docker.NewSystemClient()
  232. if err != nil {
  233. return err
  234. }
  235. // Only pull image if not found locally
  236. if _, _, err := client.ImageInspectWithRaw(context.Background(), image, false); err != nil {
  237. if err := container.Pull(context.Background()); err != nil {
  238. return err
  239. }
  240. }
  241. if !stage {
  242. // If there is already an upgrade container, delete it
  243. // Up() should to this, but currently does not due to a bug
  244. if err := container.Delete(context.Background(), options.Delete{}); err != nil {
  245. return err
  246. }
  247. if err := container.Up(context.Background(), options.Up{}); err != nil {
  248. return err
  249. }
  250. if err := container.Log(context.Background(), true); err != nil {
  251. return err
  252. }
  253. if err := container.Delete(context.Background(), options.Delete{}); err != nil {
  254. return err
  255. }
  256. if reboot && (force || yes("Continue with reboot")) {
  257. log.Info("Rebooting")
  258. power.Reboot()
  259. }
  260. }
  261. return nil
  262. }
  263. func parseBody(body []byte) (*Images, error) {
  264. update := &Images{}
  265. err := yaml.Unmarshal(body, update)
  266. if err != nil {
  267. return nil, err
  268. }
  269. return update, nil
  270. }
  271. func getUpgradeURL() (string, error) {
  272. cfg := config.LoadConfig()
  273. return cfg.Rancher.Upgrade.URL, nil
  274. }