identity_vm.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "net/http"
  8. "os"
  9. netcontext "golang.org/x/net/context"
  10. )
  11. // These functions are implementations of the wrapper functions
  12. // in ../appengine/identity.go. See that file for commentary.
  13. const (
  14. hDefaultVersionHostname = "X-AppEngine-Default-Version-Hostname"
  15. hRequestLogId = "X-AppEngine-Request-Log-Id"
  16. hDatacenter = "X-AppEngine-Datacenter"
  17. )
  18. func ctxHeaders(ctx netcontext.Context) http.Header {
  19. c := fromContext(ctx)
  20. if c == nil {
  21. return nil
  22. }
  23. return c.Request().Header
  24. }
  25. func DefaultVersionHostname(ctx netcontext.Context) string {
  26. return ctxHeaders(ctx).Get(hDefaultVersionHostname)
  27. }
  28. func RequestID(ctx netcontext.Context) string {
  29. return ctxHeaders(ctx).Get(hRequestLogId)
  30. }
  31. func Datacenter(ctx netcontext.Context) string {
  32. return ctxHeaders(ctx).Get(hDatacenter)
  33. }
  34. func ServerSoftware() string {
  35. // TODO(dsymonds): Remove fallback when we've verified this.
  36. if s := os.Getenv("SERVER_SOFTWARE"); s != "" {
  37. return s
  38. }
  39. return "Google App Engine/1.x.x"
  40. }
  41. // TODO(dsymonds): Remove the metadata fetches.
  42. func ModuleName(_ netcontext.Context) string {
  43. if s := os.Getenv("GAE_MODULE_NAME"); s != "" {
  44. return s
  45. }
  46. return string(mustGetMetadata("instance/attributes/gae_backend_name"))
  47. }
  48. func VersionID(_ netcontext.Context) string {
  49. if s1, s2 := os.Getenv("GAE_MODULE_VERSION"), os.Getenv("GAE_MINOR_VERSION"); s1 != "" && s2 != "" {
  50. return s1 + "." + s2
  51. }
  52. return string(mustGetMetadata("instance/attributes/gae_backend_version")) + "." + string(mustGetMetadata("instance/attributes/gae_backend_minor_version"))
  53. }
  54. func InstanceID() string {
  55. if s := os.Getenv("GAE_MODULE_INSTANCE"); s != "" {
  56. return s
  57. }
  58. return string(mustGetMetadata("instance/attributes/gae_backend_instance"))
  59. }
  60. func partitionlessAppID() string {
  61. // gae_project has everything except the partition prefix.
  62. appID := os.Getenv("GAE_LONG_APP_ID")
  63. if appID == "" {
  64. appID = string(mustGetMetadata("instance/attributes/gae_project"))
  65. }
  66. return appID
  67. }
  68. func fullyQualifiedAppID(_ netcontext.Context) string {
  69. appID := partitionlessAppID()
  70. part := os.Getenv("GAE_PARTITION")
  71. if part == "" {
  72. part = string(mustGetMetadata("instance/attributes/gae_partition"))
  73. }
  74. if part != "" {
  75. appID = part + "~" + appID
  76. }
  77. return appID
  78. }
  79. func IsDevAppServer() bool {
  80. return os.Getenv("RUN_WITH_DEVAPPSERVER") != ""
  81. }