identity_vm.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. return fromContext(ctx).Request().Header
  20. }
  21. func DefaultVersionHostname(ctx netcontext.Context) string {
  22. return ctxHeaders(ctx).Get(hDefaultVersionHostname)
  23. }
  24. func RequestID(ctx netcontext.Context) string {
  25. return ctxHeaders(ctx).Get(hRequestLogId)
  26. }
  27. func Datacenter(ctx netcontext.Context) string {
  28. return ctxHeaders(ctx).Get(hDatacenter)
  29. }
  30. func ServerSoftware() string {
  31. // TODO(dsymonds): Remove fallback when we've verified this.
  32. if s := os.Getenv("SERVER_SOFTWARE"); s != "" {
  33. return s
  34. }
  35. return "Google App Engine/1.x.x"
  36. }
  37. // TODO(dsymonds): Remove the metadata fetches.
  38. func ModuleName(_ netcontext.Context) string {
  39. if s := os.Getenv("GAE_MODULE_NAME"); s != "" {
  40. return s
  41. }
  42. return string(mustGetMetadata("instance/attributes/gae_backend_name"))
  43. }
  44. func VersionID(_ netcontext.Context) string {
  45. if s1, s2 := os.Getenv("GAE_MODULE_VERSION"), os.Getenv("GAE_MINOR_VERSION"); s1 != "" && s2 != "" {
  46. return s1 + "." + s2
  47. }
  48. return string(mustGetMetadata("instance/attributes/gae_backend_version")) + "." + string(mustGetMetadata("instance/attributes/gae_backend_minor_version"))
  49. }
  50. func InstanceID() string {
  51. if s := os.Getenv("GAE_MODULE_INSTANCE"); s != "" {
  52. return s
  53. }
  54. return string(mustGetMetadata("instance/attributes/gae_backend_instance"))
  55. }
  56. func partitionlessAppID() string {
  57. // gae_project has everything except the partition prefix.
  58. appID := os.Getenv("GAE_LONG_APP_ID")
  59. if appID == "" {
  60. appID = string(mustGetMetadata("instance/attributes/gae_project"))
  61. }
  62. return appID
  63. }
  64. func fullyQualifiedAppID(_ netcontext.Context) string {
  65. appID := partitionlessAppID()
  66. part := os.Getenv("GAE_PARTITION")
  67. if part == "" {
  68. part = string(mustGetMetadata("instance/attributes/gae_partition"))
  69. }
  70. if part != "" {
  71. appID = part + "~" + appID
  72. }
  73. return appID
  74. }
  75. func IsDevAppServer() bool {
  76. return os.Getenv("RUN_WITH_DEVAPPSERVER") != ""
  77. }