Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(382)

Unified Diff: chrome/common/extensions/docs/server2/environment_wrappers.py

Issue 1151283007: Docserver overhaul: Gitiles away from me. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/common/extensions/docs/server2/environment_wrappers.py
diff --git a/chrome/common/extensions/docs/server2/environment_wrappers.py b/chrome/common/extensions/docs/server2/environment_wrappers.py
new file mode 100644
index 0000000000000000000000000000000000000000..13e602092ed88d0be5a6c4919cad18072423e4e0
--- /dev/null
+++ b/chrome/common/extensions/docs/server2/environment_wrappers.py
@@ -0,0 +1,70 @@
+# Copyright 2013 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import json
+import logging
+import os
+
+from environment import IsAppEngine, IsComputeEngine, IsTest
+
+
+_METADATA_SERVER = 'http://metadata/computeMetadata/v1/instance/service-accounts'
+_SERVICE_ACCOUNT = 'default'
+
+# The environment variable to use as a fallback token source.
+_ACCESS_TOKEN_ENV = 'DOCSERVER_ACCESS_TOKEN'
+
+
+def CreateUrlFetcher(base_path=None):
+ if IsAppEngine():
+ from url_fetcher_appengine import UrlFetcherAppengine
+ fetcher = UrlFetcherAppengine()
+ elif not IsTest():
+ from url_fetcher_urllib2 import UrlFetcherUrllib2
+ fetcher = UrlFetcherUrllib2()
+ else:
+ from url_fetcher_fake import UrlFetcherFake
+ fetcher = UrlFetcherFake()
+ fetcher.SetBasePath(base_path)
+ return fetcher
+
+
+def CreatePersistentObjectStore(namespace):
+ if IsAppEngine():
+ from persistent_object_store_appengine import PersistentObjectStoreAppengine
+ object_store = PersistentObjectStoreAppengine(namespace)
+ else:
+ from persistent_object_store_fake import PersistentObjectStoreFake
+ object_store = PersistentObjectStoreFake(namespace)
+ return object_store
+
+
+def GetAccessToken():
+ '''Acquires an access token either from the metadata service (if running on
+ a real CE VM) or the DOCSERVER_ACCESS_TOKEN environment variable.
+
+ This may return None if no token is available. That should never happen while
+ running on a VM.
+ '''
+ # Attempt to grab an access token from the VM instance's metadata.
+ if IsComputeEngine():
+ fetcher = CreateUrlFetcher()
+ token_uri = '%s/%s/token' % (_METADATA_SERVER, _SERVICE_ACCOUNT)
+ token_response = None
+ try:
+ token_response = fetcher.Fetch(token_uri,
+ headers={'Metadata-Flavor': 'Google'})
+ if token_response.status_code == 200:
+ return json.loads(token_response.content)['access_token']
+ except Exception as e:
+ logging.warn('Failed to fetch access token from VM metadata.')
+ pass
+
+ logging.warn('Using access token from local environment.')
+ access_token = os.getenv(_ACCESS_TOKEN_ENV)
+ if access_token is None:
+ logging.error('No access token available. '
+ 'Please set %s if you want things to work.' %
+ _ACCESS_TOKEN_ENV)
+ return access_token

Powered by Google App Engine
This is Rietveld 408576698