google_compute.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  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 framework
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "io/ioutil"
  18. "os/exec"
  19. "path/filepath"
  20. "strings"
  21. )
  22. // TODO: These should really just use the GCE API client library or at least use
  23. // better formatted output from the --format flag.
  24. // Returns master & node image string, or error
  25. func lookupClusterImageSources() (string, string, error) {
  26. // Given args for a gcloud compute command, run it with other args, and return the values,
  27. // whether separated by newlines, commas or semicolons.
  28. gcloudf := func(argv ...string) ([]string, error) {
  29. args := []string{"compute"}
  30. args = append(args, argv...)
  31. args = append(args, "--project", TestContext.CloudConfig.ProjectID)
  32. if TestContext.CloudConfig.MultiMaster {
  33. args = append(args, "--region", TestContext.CloudConfig.Region)
  34. } else {
  35. args = append(args, "--zone", TestContext.CloudConfig.Zone)
  36. }
  37. outputBytes, err := exec.Command("gcloud", args...).CombinedOutput()
  38. str := strings.Replace(string(outputBytes), ",", "\n", -1)
  39. str = strings.Replace(str, ";", "\n", -1)
  40. lines := strings.Split(str, "\n")
  41. if err != nil {
  42. Logf("lookupDiskImageSources: gcloud error with [%#v]; err:%v", argv, err)
  43. for _, l := range lines {
  44. Logf(" > %s", l)
  45. }
  46. }
  47. return lines, err
  48. }
  49. // Given a GCE instance, look through its disks, finding one that has a sourceImage
  50. host2image := func(instance string) (string, error) {
  51. // gcloud compute instances describe {INSTANCE} --format="get(disks[].source)"
  52. // gcloud compute disks describe {DISKURL} --format="get(sourceImage)"
  53. disks, err := gcloudf("instances", "describe", instance, "--format=get(disks[].source)")
  54. if err != nil {
  55. return "", err
  56. } else if len(disks) == 0 {
  57. return "", fmt.Errorf("instance %q had no findable disks", instance)
  58. }
  59. // Loop over disks, looking for the boot disk
  60. for _, disk := range disks {
  61. lines, err := gcloudf("disks", "describe", disk, "--format=get(sourceImage)")
  62. if err != nil {
  63. return "", err
  64. } else if len(lines) > 0 && lines[0] != "" {
  65. return lines[0], nil // break, we're done
  66. }
  67. }
  68. return "", fmt.Errorf("instance %q had no disk with a sourceImage", instance)
  69. }
  70. // gcloud compute instance-groups list-instances {GROUPNAME} --format="get(instance)"
  71. nodeName := ""
  72. instGroupName := strings.Split(TestContext.CloudConfig.NodeInstanceGroup, ",")[0]
  73. if lines, err := gcloudf("instance-groups", "list-instances", instGroupName, "--format=get(instance)"); err != nil {
  74. return "", "", err
  75. } else if len(lines) == 0 {
  76. return "", "", fmt.Errorf("no instances inside instance-group %q", instGroupName)
  77. } else {
  78. nodeName = lines[0]
  79. }
  80. nodeImg, err := host2image(nodeName)
  81. if err != nil {
  82. return "", "", err
  83. }
  84. frags := strings.Split(nodeImg, "/")
  85. nodeImg = frags[len(frags)-1]
  86. // For GKE clusters, MasterName will not be defined; we just leave masterImg blank.
  87. masterImg := ""
  88. if masterName := TestContext.CloudConfig.MasterName; masterName != "" {
  89. img, err := host2image(masterName)
  90. if err != nil {
  91. return "", "", err
  92. }
  93. frags = strings.Split(img, "/")
  94. masterImg = frags[len(frags)-1]
  95. }
  96. return masterImg, nodeImg, nil
  97. }
  98. // LogClusterImageSources writes out cluster image sources.
  99. func LogClusterImageSources() {
  100. masterImg, nodeImg, err := lookupClusterImageSources()
  101. if err != nil {
  102. Logf("Cluster image sources lookup failed: %v\n", err)
  103. return
  104. }
  105. Logf("cluster-master-image: %s", masterImg)
  106. Logf("cluster-node-image: %s", nodeImg)
  107. images := map[string]string{
  108. "master_os_image": masterImg,
  109. "node_os_image": nodeImg,
  110. }
  111. outputBytes, _ := json.MarshalIndent(images, "", " ")
  112. filePath := filepath.Join(TestContext.ReportDir, "images.json")
  113. if err := ioutil.WriteFile(filePath, outputBytes, 0644); err != nil {
  114. Logf("cluster images sources, could not write to %q: %v", filePath, err)
  115. }
  116. }