upgrade.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. "github.com/coreos/etcd/pkg/transport"
  21. )
  22. func UpgradeCommand() cli.Command {
  23. return cli.Command{
  24. Name: "upgrade",
  25. Usage: "upgrade an old version etcd cluster to a new version",
  26. Flags: []cli.Flag{
  27. cli.StringFlag{Name: "old-version", Value: "1", Usage: "Old internal version"},
  28. cli.StringFlag{Name: "new-version", Value: "2", Usage: "New internal version"},
  29. cli.StringFlag{Name: "peer-url", Value: "http://localhost:7001", Usage: "An etcd peer url string"},
  30. cli.StringFlag{Name: "peer-cert-file", Value: "", Usage: "identify HTTPS peer using this SSL certificate file"},
  31. cli.StringFlag{Name: "peer-key-file", Value: "", Usage: "identify HTTPS peer using this SSL key file"},
  32. cli.StringFlag{Name: "peer-ca-file", Value: "", Usage: "verify certificates of HTTPS-enabled peers using this CA bundle"},
  33. },
  34. Action: handleUpgrade,
  35. }
  36. }
  37. func handleUpgrade(c *cli.Context) {
  38. if c.String("old-version") != "1" {
  39. fmt.Printf("Do not support upgrade from version %s\n", c.String("old-version"))
  40. os.Exit(1)
  41. }
  42. if c.String("new-version") != "2" {
  43. fmt.Printf("Do not support upgrade to version %s\n", c.String("new-version"))
  44. os.Exit(1)
  45. }
  46. tls := transport.TLSInfo{
  47. CAFile: c.String("peer-ca-file"),
  48. CertFile: c.String("peer-cert-file"),
  49. KeyFile: c.String("peer-key-file"),
  50. }
  51. t, err := transport.NewTransport(tls)
  52. if err != nil {
  53. log.Fatal(err)
  54. }
  55. client := http.Client{Transport: t}
  56. resp, err := client.Get(c.String("peer-url") + "/v2/admin/next-internal-version")
  57. if err != nil {
  58. fmt.Printf("Failed to send upgrade request to %s: %v\n", c.String("peer-url"), err)
  59. return
  60. }
  61. if resp.StatusCode == http.StatusOK {
  62. fmt.Println("Cluster will start upgrading from internal version 1 to 2 in 10 seconds.")
  63. return
  64. }
  65. if resp.StatusCode == http.StatusNotFound {
  66. fmt.Println("Cluster cannot upgrade to 2: version is not 0.4.7")
  67. return
  68. }
  69. fmt.Printf("Faild to send upgrade request to %s: bad status code %d\n", c.String("cluster-url"), resp.StatusCode)
  70. }