You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.3 KiB

  1. // Copyright 2011 Google Inc. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. // +build !appengine
  5. package internal
  6. import (
  7. "log"
  8. "net/http"
  9. "os"
  10. "strings"
  11. netcontext "golang.org/x/net/context"
  12. )
  13. // These functions are implementations of the wrapper functions
  14. // in ../appengine/identity.go. See that file for commentary.
  15. const (
  16. hDefaultVersionHostname = "X-AppEngine-Default-Version-Hostname"
  17. hRequestLogId = "X-AppEngine-Request-Log-Id"
  18. hDatacenter = "X-AppEngine-Datacenter"
  19. )
  20. func ctxHeaders(ctx netcontext.Context) http.Header {
  21. c := fromContext(ctx)
  22. if c == nil {
  23. return nil
  24. }
  25. return c.Request().Header
  26. }
  27. func DefaultVersionHostname(ctx netcontext.Context) string {
  28. return ctxHeaders(ctx).Get(hDefaultVersionHostname)
  29. }
  30. func RequestID(ctx netcontext.Context) string {
  31. return ctxHeaders(ctx).Get(hRequestLogId)
  32. }
  33. func Datacenter(ctx netcontext.Context) string {
  34. if dc := ctxHeaders(ctx).Get(hDatacenter); dc != "" {
  35. return dc
  36. }
  37. // If the header isn't set, read zone from the metadata service.
  38. // It has the format projects/[NUMERIC_PROJECT_ID]/zones/[ZONE]
  39. zone, err := getMetadata("instance/zone")
  40. if err != nil {
  41. log.Printf("Datacenter: %v", err)
  42. return ""
  43. }
  44. parts := strings.Split(string(zone), "/")
  45. if len(parts) == 0 {
  46. return ""
  47. }
  48. return parts[len(parts)-1]
  49. }
  50. func ServerSoftware() string {
  51. // TODO(dsymonds): Remove fallback when we've verified this.
  52. if s := os.Getenv("SERVER_SOFTWARE"); s != "" {
  53. return s
  54. }
  55. if s := os.Getenv("GAE_ENV"); s != "" {
  56. return s
  57. }
  58. return "Google App Engine/1.x.x"
  59. }
  60. // TODO(dsymonds): Remove the metadata fetches.
  61. func ModuleName(_ netcontext.Context) string {
  62. if s := os.Getenv("GAE_MODULE_NAME"); s != "" {
  63. return s
  64. }
  65. if s := os.Getenv("GAE_SERVICE"); s != "" {
  66. return s
  67. }
  68. return string(mustGetMetadata("instance/attributes/gae_backend_name"))
  69. }
  70. func VersionID(_ netcontext.Context) string {
  71. if s1, s2 := os.Getenv("GAE_MODULE_VERSION"), os.Getenv("GAE_MINOR_VERSION"); s1 != "" && s2 != "" {
  72. return s1 + "." + s2
  73. }
  74. if s1, s2 := os.Getenv("GAE_VERSION"), os.Getenv("GAE_DEPLOYMENT_ID"); s1 != "" && s2 != "" {
  75. return s1 + "." + s2
  76. }
  77. return string(mustGetMetadata("instance/attributes/gae_backend_version")) + "." + string(mustGetMetadata("instance/attributes/gae_backend_minor_version"))
  78. }
  79. func InstanceID() string {
  80. if s := os.Getenv("GAE_MODULE_INSTANCE"); s != "" {
  81. return s
  82. }
  83. if s := os.Getenv("GAE_INSTANCE"); s != "" {
  84. return s
  85. }
  86. return string(mustGetMetadata("instance/attributes/gae_backend_instance"))
  87. }
  88. func partitionlessAppID() string {
  89. // gae_project has everything except the partition prefix.
  90. if appID := os.Getenv("GAE_LONG_APP_ID"); appID != "" {
  91. return appID
  92. }
  93. if project := os.Getenv("GOOGLE_CLOUD_PROJECT"); project != "" {
  94. return project
  95. }
  96. return string(mustGetMetadata("instance/attributes/gae_project"))
  97. }
  98. func fullyQualifiedAppID(_ netcontext.Context) string {
  99. if s := os.Getenv("GAE_APPLICATION"); s != "" {
  100. return s
  101. }
  102. appID := partitionlessAppID()
  103. part := os.Getenv("GAE_PARTITION")
  104. if part == "" {
  105. part = string(mustGetMetadata("instance/attributes/gae_partition"))
  106. }
  107. if part != "" {
  108. appID = part + "~" + appID
  109. }
  110. return appID
  111. }
  112. func IsDevAppServer() bool {
  113. return os.Getenv("RUN_WITH_DEVAPPSERVER") != ""
  114. }