upgrade.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. Copyright 2015 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package command
  14. import (
  15. "fmt"
  16. "log"
  17. "net/http"
  18. "os"
  19. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
  20. )
  21. func UpgradeCommand() cli.Command {
  22. return cli.Command{
  23. Name: "upgrade",
  24. Usage: "upgrade an old version etcd cluster to a new version",
  25. Flags: []cli.Flag{
  26. cli.StringFlag{Name: "old-version", Value: "1", Usage: "Old internal version"},
  27. cli.StringFlag{Name: "new-version", Value: "2", Usage: "New internal version"},
  28. cli.StringFlag{Name: "peer-url", Value: "", Usage: "An etcd peer url string"},
  29. },
  30. Action: handleUpgrade,
  31. }
  32. }
  33. func handleUpgrade(c *cli.Context) {
  34. if c.String("old-version") != "1" {
  35. fmt.Printf("Do not support upgrade from version %s\n", c.String("old-version"))
  36. os.Exit(1)
  37. }
  38. if c.String("new-version") != "2" {
  39. fmt.Printf("Do not support upgrade to version %s\n", c.String("new-version"))
  40. os.Exit(1)
  41. }
  42. t, err := getTransport(c)
  43. if err != nil {
  44. log.Fatal(err)
  45. }
  46. client := http.Client{Transport: t}
  47. resp, err := client.Get(c.String("peer-url") + "/v2/admin/next-internal-version")
  48. if err != nil {
  49. fmt.Printf("Failed to send upgrade request to %s: %v\n", c.String("peer-url"), err)
  50. return
  51. }
  52. if resp.StatusCode == http.StatusOK {
  53. fmt.Println("Cluster will start upgrading from internal version 1 to 2 in 10 seconds.")
  54. return
  55. }
  56. if resp.StatusCode == http.StatusNotFound {
  57. fmt.Println("Cluster cannot upgrade to 2: version is not 0.4.7")
  58. return
  59. }
  60. fmt.Printf("Faild to send upgrade request to %s: bad status code %d\n", c.String("cluster-url"), resp.StatusCode)
  61. }