Home | History | Annotate | Download | only in server2
      1 # Copyright 2014 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 os
      6 import environment
      7 
      8 from caching_file_system import CachingFileSystem
      9 from empty_dir_file_system import EmptyDirFileSystem
     10 from extensions_paths import LOCAL_GCS_DIR, LOCAL_GCS_DEBUG_CONF
     11 from local_file_system import LocalFileSystem
     12 from path_util import IsDirectory, ToDirectory
     13 
     14 class CloudStorageFileSystemProvider(object):
     15   '''Provides CloudStorageFileSystem bound to a GCS bucket.
     16   '''
     17   def __init__(self, object_store_creator):
     18     self._object_store_creator = object_store_creator
     19 
     20   def Create(self, bucket):
     21     '''Creates a CloudStorageFileSystemProvider.
     22 
     23     |bucket| is the name of GCS bucket, eg devtools-docs. It is expected
     24              that this bucket has Read permission for this app in its ACLs.
     25 
     26     Optional configuration can be set in a local_debug/gcs_debug.conf file:
     27       use_local_fs=True|False
     28       access_token=<token>
     29       remote_bucket_prefix=<prefix>
     30 
     31     If running in Preview mode or in Development mode with use_local_fs set to
     32     True, buckets and files are looked inside the local_debug folder instead
     33     of in the real GCS server. Preview server does not support direct GCS
     34     access, so it is always forced to use a LocalFileSystem.
     35 
     36     For real GCS access in the Development mode (dev_appserver.py),
     37     access_token and remote_bucket_prefix options can be
     38     used to change the way GCS files are accessed. Both are ignored in a real
     39     appengine instance.
     40 
     41     "access_token" is always REQUIRED on dev_appengine, otherwise you will
     42     get 404 (auth) errors. You can get one access_token valid for a few minutes
     43     by typing:
     44       gsutil -d ls 2>&1 | grep "Bearer" |
     45          sed "s/.*Bearer \(.*\).r.nUser-Agent.*/access_token=\1/" )"
     46 
     47     A sample output would be:
     48       access_token=ya29.1.AADtN_VW5ibbfLHV5cMIK5ss4bHtVzBXpa4byjd
     49 
     50     Now add this line to the local_debug/gcs_debug.conf file and restart the
     51     appengine development server.
     52 
     53     Remember that you will need a new access_token every ten minutes or
     54     so. If you get 404 errors on log, update it. Access token is not
     55     used for a deployed appengine app, only if you use dev_appengine.py.
     56 
     57     remote_bucket_prefix is useful if you want to test on your own GCS buckets
     58     before using the real GCS buckets.
     59 
     60     '''
     61     if not environment.IsReleaseServer() and not environment.IsDevServer():
     62       bucket_local_path = os.path.join(LOCAL_GCS_DIR, bucket)
     63       if IsDirectory(bucket_local_path):
     64         return LocalFileSystem(bucket_local_path)
     65       else:
     66         return EmptyDirFileSystem()
     67 
     68     debug_access_token = None
     69     debug_bucket_prefix = None
     70     use_local_fs = False
     71 
     72     if environment.IsDevServer() and os.path.exists(LOCAL_GCS_DEBUG_CONF):
     73       with open(LOCAL_GCS_DEBUG_CONF, "r") as token_file:
     74         properties = dict(line.strip().split('=', 1) for line in token_file)
     75       use_local_fs = properties.get('use_local_fs', 'False')=='True'
     76       debug_access_token = properties.get('access_token', None)
     77       debug_bucket_prefix = properties.get('remote_bucket_prefix', None)
     78 
     79     if environment.IsDevServer() and use_local_fs:
     80       return LocalFileSystem(ToDirectory(os.path.join(LOCAL_GCS_DIR, bucket)))
     81 
     82     # gcs_file_system has strong dependencies on runtime appengine APIs,
     83     # so we only import it when we are sure we are not on preview.py or tests.
     84     from gcs_file_system import CloudStorageFileSystem
     85     return CachingFileSystem(CloudStorageFileSystem(bucket,
     86         debug_access_token, debug_bucket_prefix),
     87         self._object_store_creator)
     88 
     89   @staticmethod
     90   def ForEmpty():
     91     class EmptyImpl(object):
     92       def Create(self, bucket):
     93         return EmptyDirFileSystem()
     94     return EmptyImpl()
     95