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

Side by Side 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: Remove inform_users template to fix presubmit failure (it's now a redirect) Created 5 years, 6 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 unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import json
6 import logging
7 import os
8
9 from environment import IsAppEngine, IsComputeEngine, IsTest
10
11
12 _METADATA_SERVER = 'http://metadata/computeMetadata/v1/instance/service-accounts '
13 _SERVICE_ACCOUNT = 'default'
14
15 # The environment variable to use as a fallback token source.
16 _ACCESS_TOKEN_ENV = 'DOCSERVER_ACCESS_TOKEN'
17
18
19 def CreateUrlFetcher(base_path=None):
20 if IsAppEngine():
21 from url_fetcher_appengine import UrlFetcherAppengine
22 fetcher = UrlFetcherAppengine()
23 elif not IsTest():
24 from url_fetcher_urllib2 import UrlFetcherUrllib2
25 fetcher = UrlFetcherUrllib2()
26 else:
27 from url_fetcher_fake import UrlFetcherFake
28 fetcher = UrlFetcherFake()
29 fetcher.SetBasePath(base_path)
30 return fetcher
31
32
33 def CreatePersistentObjectStore(namespace):
34 if IsAppEngine():
35 from persistent_object_store_appengine import PersistentObjectStoreAppengine
36 object_store = PersistentObjectStoreAppengine(namespace)
37 else:
38 from persistent_object_store_fake import PersistentObjectStoreFake
39 object_store = PersistentObjectStoreFake(namespace)
40 return object_store
41
42
43 def GetAccessToken():
44 '''Acquires an access token either from the metadata service (if running on
45 a real CE VM) or the DOCSERVER_ACCESS_TOKEN environment variable.
46
47 This may return None if no token is available. That should never happen while
48 running on a VM.
49 '''
50 # Attempt to grab an access token from the VM instance's metadata.
51 if IsComputeEngine():
52 fetcher = CreateUrlFetcher()
53 token_uri = '%s/%s/token' % (_METADATA_SERVER, _SERVICE_ACCOUNT)
54 token_response = None
55 try:
56 token_response = fetcher.Fetch(token_uri,
57 headers={'Metadata-Flavor': 'Google'})
58 if token_response.status_code == 200:
59 return json.loads(token_response.content)['access_token']
60 except Exception as e:
61 logging.warn('Failed to fetch access token from VM metadata.')
62 pass
63
64 logging.warn('Using access token from local environment.')
65 access_token = os.getenv(_ACCESS_TOKEN_ENV)
66 if access_token is None:
67 logging.error('No access token available. '
68 'Please set %s if you want things to work.' %
69 _ACCESS_TOKEN_ENV)
70 return access_token
OLDNEW
« no previous file with comments | « chrome/common/extensions/docs/server2/environment.py ('k') | chrome/common/extensions/docs/server2/fake_fetchers.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698