os.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package control
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/url"
  6. "os"
  7. "runtime"
  8. "strings"
  9. "github.com/rancher/os/cmd/power"
  10. "github.com/rancher/os/config"
  11. "github.com/rancher/os/pkg/compose"
  12. "github.com/rancher/os/pkg/docker"
  13. "github.com/rancher/os/pkg/log"
  14. "github.com/rancher/os/pkg/util"
  15. "github.com/rancher/os/pkg/util/network"
  16. yaml "github.com/cloudfoundry-incubator/candiedyaml"
  17. "github.com/codegangsta/cli"
  18. dockerClient "github.com/docker/engine-api/client"
  19. composeConfig "github.com/docker/libcompose/config"
  20. "github.com/docker/libcompose/project/options"
  21. "golang.org/x/net/context"
  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. if isExist := checkGlobalCfg(); !isExist {
  167. log.Fatalf("ros upgrade cannot be supported")
  168. }
  169. image := c.String("image")
  170. if image == "" {
  171. var err error
  172. image, err = getLatestImage()
  173. if err != nil {
  174. log.Fatal(err)
  175. }
  176. if image == "" {
  177. log.Fatal("Failed to find latest image")
  178. }
  179. }
  180. if c.Args().Present() {
  181. log.Fatalf("invalid arguments %v", c.Args())
  182. }
  183. if err := startUpgradeContainer(
  184. image,
  185. c.Bool("stage"),
  186. c.Bool("force"),
  187. !c.Bool("no-reboot"),
  188. c.Bool("kexec"),
  189. c.Bool("upgrade-console"),
  190. c.Bool("debug"),
  191. c.String("append"),
  192. ); err != nil {
  193. log.Fatal(err)
  194. }
  195. return nil
  196. }
  197. func osVersion(c *cli.Context) error {
  198. fmt.Println(config.Version)
  199. return nil
  200. }
  201. func startUpgradeContainer(image string, stage, force, reboot, kexec, upgradeConsole, debug bool, kernelArgs string) error {
  202. command := []string{
  203. "-t", "rancher-upgrade",
  204. "-r", config.Version,
  205. }
  206. if kexec {
  207. command = append(command, "--kexec")
  208. }
  209. if debug {
  210. command = append(command, "--debug")
  211. }
  212. kernelArgs = strings.TrimSpace(kernelArgs)
  213. if kernelArgs != "" {
  214. command = append(command, "-a", kernelArgs)
  215. }
  216. if upgradeConsole {
  217. if err := config.Set("rancher.force_console_rebuild", true); err != nil {
  218. log.Fatal(err)
  219. }
  220. }
  221. fmt.Printf("Upgrading to %s\n", image)
  222. confirmation := "Continue"
  223. imageSplit := strings.Split(image, ":")
  224. if len(imageSplit) > 1 && imageSplit[1] == config.Version+config.Suffix {
  225. confirmation = fmt.Sprintf("Already at version %s. Continue anyway", imageSplit[1])
  226. }
  227. if !force && !yes(confirmation) {
  228. os.Exit(1)
  229. }
  230. container, err := compose.CreateService(nil, "os-upgrade", &composeConfig.ServiceConfigV1{
  231. LogDriver: "json-file",
  232. Privileged: true,
  233. Net: "host",
  234. Pid: "host",
  235. Image: image,
  236. Labels: map[string]string{
  237. config.ScopeLabel: config.System,
  238. },
  239. Command: command,
  240. })
  241. if err != nil {
  242. return err
  243. }
  244. client, err := docker.NewSystemClient()
  245. if err != nil {
  246. return err
  247. }
  248. // Only pull image if not found locally
  249. if _, _, err := client.ImageInspectWithRaw(context.Background(), image, false); err != nil {
  250. if err := container.Pull(context.Background()); err != nil {
  251. return err
  252. }
  253. }
  254. if !stage {
  255. // If there is already an upgrade container, delete it
  256. // Up() should to this, but currently does not due to a bug
  257. if err := container.Delete(context.Background(), options.Delete{}); err != nil {
  258. return err
  259. }
  260. if err := container.Up(context.Background(), options.Up{}); err != nil {
  261. return err
  262. }
  263. if err := container.Log(context.Background(), true); err != nil {
  264. return err
  265. }
  266. if err := container.Delete(context.Background(), options.Delete{}); err != nil {
  267. return err
  268. }
  269. if reboot && (force || yes("Continue with reboot")) {
  270. log.Info("Rebooting")
  271. power.Reboot()
  272. }
  273. }
  274. return nil
  275. }
  276. func parseBody(body []byte) (*Images, error) {
  277. update := &Images{}
  278. err := yaml.Unmarshal(body, update)
  279. if err != nil {
  280. return nil, err
  281. }
  282. return update, nil
  283. }
  284. func getUpgradeURL() (string, error) {
  285. cfg := config.LoadConfig()
  286. return cfg.Rancher.Upgrade.URL, nil
  287. }