<s> import logging <EOL> from oslo_config import cfg <EOL> from oslo_utils import encodeutils <EOL> import six <EOL> from stevedore import driver <EOL> from stevedore import extension <EOL> from glance_store import capabilities <EOL> from glance_store import exceptions <EOL> from glance_store import i18n <EOL> from glance_store import location <EOL> CONF = cfg . CONF <EOL> LOG = logging . getLogger ( __name__ ) <EOL> _ = i18n . _ <EOL> _STORE_OPTS = [ <EOL> cfg . ListOpt ( '<STR_LIT>' , default = [ '<STR_LIT:file>' , '<STR_LIT:http>' ] , <EOL> help = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , default = '<STR_LIT:file>' , <EOL> help = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , default = <NUM_LIT:0> , <EOL> help = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> ] <EOL> _STORE_CFG_GROUP = '<STR_LIT>' <EOL> def _list_opts ( ) : <EOL> driver_opts = [ ] <EOL> mgr = extension . ExtensionManager ( '<STR_LIT>' ) <EOL> drivers = [ ext . name for ext in mgr ] <EOL> handled_drivers = [ ] <EOL> for store_entry in drivers : <EOL> driver_cls = _load_store ( None , store_entry , False ) <EOL> if driver_cls and driver_cls not in handled_drivers : <EOL> if getattr ( driver_cls , '<STR_LIT>' , None ) is not None : <EOL> driver_opts += driver_cls . OPTIONS <EOL> handled_drivers . append ( driver_cls ) <EOL> return ( [ ( _STORE_CFG_GROUP , _STORE_OPTS ) ] + <EOL> [ ( _STORE_CFG_GROUP , driver_opts ) ] ) <EOL> def register_opts ( conf ) : <EOL> opts = _list_opts ( ) <EOL> for group , opt_list in opts : <EOL> LOG . debug ( "<STR_LIT>" % group ) <EOL> for opt in opt_list : <EOL> conf . register_opt ( opt , group = group ) <EOL> class Indexable ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , wrapped , size ) : <EOL> """<STR_LIT>""" <EOL> self . wrapped = wrapped <EOL> self . size = int ( size ) if size else ( wrapped . len <EOL> if hasattr ( wrapped , '<STR_LIT>' ) else <NUM_LIT:0> ) <EOL> self . cursor = <NUM_LIT:0> <EOL> self . chunk = None <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> for self . chunk in self . wrapped : <EOL> yield self . chunk <EOL> def __getitem__ ( self , i ) : <EOL> """<STR_LIT>""" <EOL> start = i . start if isinstance ( i , slice ) else i <EOL> if start < self . cursor : <EOL> return self . chunk [ ( start - self . cursor ) : ] <EOL> self . chunk = self . another ( ) <EOL> if self . chunk : <EOL> self . cursor += len ( self . chunk ) <EOL> return self . chunk <EOL> def another ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def getvalue ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . wrapped . getvalue ( ) <EOL> def __len__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . size <EOL> def _load_store ( conf , store_entry , invoke_load = True ) : <EOL> try : <EOL> LOG . debug ( "<STR_LIT>" , store_entry ) <EOL> mgr = driver . DriverManager ( '<STR_LIT>' , <EOL> store_entry , <EOL> invoke_args = [ conf ] , <EOL> invoke_on_load = invoke_load ) <EOL> return mgr . driver <EOL> except RuntimeError as e : <EOL> LOG . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" % dict ( driver = str ( [ driver , e ] ) ) ) <EOL> def _load_stores ( conf ) : <EOL> for store_entry in set ( conf . glance_store . stores ) : <EOL> try : <EOL> store_instance = _load_store ( conf , store_entry ) <EOL> if not store_instance : <EOL> continue <EOL> yield ( store_entry , store_instance ) <EOL> except exceptions . BadStoreConfiguration : <EOL> continue <EOL> def create_stores ( conf = CONF ) : <EOL> """<STR_LIT>""" <EOL> store_count = <NUM_LIT:0> <EOL> for ( store_entry , store_instance ) in _load_stores ( conf ) : <EOL> try : <EOL> schemes = store_instance . get_schemes ( ) <EOL> store_instance . configure ( re_raise_bsc = False ) <EOL> except NotImplementedError : <EOL> continue <EOL> if not schemes : <EOL> raise exceptions . BackendException ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % store_entry ) <EOL> else : <EOL> LOG . debug ( "<STR_LIT>" , <EOL> store_entry , schemes ) <EOL> scheme_map = { } <EOL> loc_cls = store_instance . get_store_location_class ( ) <EOL> for scheme in schemes : <EOL> scheme_map [ scheme ] = { <EOL> '<STR_LIT:store>' : store_instance , <EOL> '<STR_LIT>' : loc_cls , <EOL> '<STR_LIT>' : store_entry <EOL> } <EOL> location . register_scheme_map ( scheme_map ) <EOL> store_count += <NUM_LIT:1> <EOL> return store_count <EOL> def verify_default_store ( ) : <EOL> scheme = CONF . glance_store . default_store <EOL> try : <EOL> get_store_from_scheme ( scheme ) <EOL> except exceptions . UnknownScheme : <EOL> msg = _ ( "<STR_LIT>" ) % scheme <EOL> raise RuntimeError ( msg ) <EOL> def get_known_schemes ( ) : <EOL> """<STR_LIT>""" <EOL> return location . SCHEME_TO_CLS_MAP . keys ( ) <EOL> def get_store_from_scheme ( scheme ) : <EOL> """<STR_LIT>""" <EOL> if scheme not in location . SCHEME_TO_CLS_MAP : <EOL> raise exceptions . UnknownScheme ( scheme = scheme ) <EOL> scheme_info = location . SCHEME_TO_CLS_MAP [ scheme ] <EOL> store = scheme_info [ '<STR_LIT:store>' ] <EOL> if not store . is_capable ( capabilities . BitMasks . DRIVER_REUSABLE ) : <EOL> store_entry = scheme_info [ '<STR_LIT>' ] <EOL> store = _load_store ( store . conf , store_entry , invoke_load = True ) <EOL> store . configure ( ) <EOL> try : <EOL> scheme_map = { } <EOL> loc_cls = store . get_store_location_class ( ) <EOL> for scheme in store . get_schemes ( ) : <EOL> scheme_map [ scheme ] = { <EOL> '<STR_LIT:store>' : store , <EOL> '<STR_LIT>' : loc_cls , <EOL> '<STR_LIT>' : store_entry <EOL> } <EOL> location . register_scheme_map ( scheme_map ) <EOL> except NotImplementedError : <EOL> scheme_info [ '<STR_LIT:store>' ] = store <EOL> return store <EOL> def get_store_from_uri ( uri ) : <EOL> """<STR_LIT>""" <EOL> scheme = uri [ <NUM_LIT:0> : uri . find ( '<STR_LIT:/>' ) - <NUM_LIT:1> ] <EOL> return get_store_from_scheme ( scheme ) <EOL> def get_from_backend ( uri , offset = <NUM_LIT:0> , chunk_size = None , context = None ) : <EOL> """<STR_LIT>""" <EOL> loc = location . get_location_from_uri ( uri , conf = CONF ) <EOL> store = get_store_from_uri ( uri ) <EOL> return store . get ( loc , offset = offset , <EOL> chunk_size = chunk_size , <EOL> context = context ) <EOL> def get_size_from_backend ( uri , context = None ) : <EOL> """<STR_LIT>""" <EOL> loc = location . get_location_from_uri ( uri , conf = CONF ) <EOL> store = get_store_from_uri ( uri ) <EOL> return store . get_size ( loc , context = context ) <EOL> def delete_from_backend ( uri , context = None ) : <EOL> """<STR_LIT>""" <EOL> loc = location . get_location_from_uri ( uri , conf = CONF ) <EOL> store = get_store_from_uri ( uri ) <EOL> return store . delete ( loc , context = context ) <EOL> def get_store_from_location ( uri ) : <EOL> """<STR_LIT>""" <EOL> loc = location . get_location_from_uri ( uri , conf = CONF ) <EOL> return loc . store_name <EOL> def check_location_metadata ( val , key = '<STR_LIT>' ) : <EOL> if isinstance ( val , dict ) : <EOL> for key in val : <EOL> check_location_metadata ( val [ key ] , key = key ) <EOL> elif isinstance ( val , list ) : <EOL> ndx = <NUM_LIT:0> <EOL> for v in val : <EOL> check_location_metadata ( v , key = '<STR_LIT>' % ( key , ndx ) ) <EOL> ndx = ndx + <NUM_LIT:1> <EOL> elif not isinstance ( val , six . text_type ) : <EOL> raise exceptions . BackendException ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> % dict ( key = key , type = type ( val ) ) ) <EOL> def store_add_to_backend ( image_id , data , size , store , context = None , <EOL> verifier = None ) : <EOL> """<STR_LIT>""" <EOL> ( location , size , checksum , metadata ) = store . add ( image_id , <EOL> data , <EOL> size , <EOL> context = context , <EOL> verifier = verifier ) <EOL> if metadata is not None : <EOL> if not isinstance ( metadata , dict ) : <EOL> msg = ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> % dict ( driver = str ( store ) , metadata = str ( metadata ) ) ) <EOL> LOG . error ( msg ) <EOL> raise exceptions . BackendException ( msg ) <EOL> try : <EOL> check_location_metadata ( metadata ) <EOL> except exceptions . BackendException as e : <EOL> e_msg = ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % <EOL> dict ( driver = encodeutils . exception_to_unicode ( store ) , <EOL> metadata = encodeutils . exception_to_unicode ( metadata ) , <EOL> e = encodeutils . exception_to_unicode ( e ) ) ) <EOL> LOG . error ( e_msg ) <EOL> raise exceptions . BackendException ( e_msg ) <EOL> return ( location , size , checksum , metadata ) <EOL> def add_to_backend ( conf , image_id , data , size , scheme = None , context = None , <EOL> verifier = None ) : <EOL> if scheme is None : <EOL> scheme = conf [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> store = get_store_from_scheme ( scheme ) <EOL> return store_add_to_backend ( image_id , data , size , store , context , <EOL> verifier ) <EOL> def set_acls ( location_uri , public = False , read_tenants = [ ] , <EOL> write_tenants = None , context = None ) : <EOL> if write_tenants is None : <EOL> write_tenants = [ ] <EOL> loc = location . get_location_from_uri ( location_uri , conf = CONF ) <EOL> scheme = get_store_from_location ( location_uri ) <EOL> store = get_store_from_scheme ( scheme ) <EOL> try : <EOL> store . set_acls ( loc , public = public , <EOL> read_tenants = read_tenants , <EOL> write_tenants = write_tenants , <EOL> context = context ) <EOL> except NotImplementedError : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) ) </s>
