identity_vm.go 3.3 KB

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