<s> import logging <EOL> import os <EOL> import shutil <EOL> from django . utils . translation import ugettext as _ <EOL> from desktop . lib . exceptions_renderable import PopupException <EOL> from desktop . lib . i18n import smart_str <EOL> from indexer . conf import CORE_INSTANCE_DIR <EOL> from indexer . utils import copy_configs <EOL> from libsolr . api import SolrApi <EOL> from libzookeeper . conf import ENSEMBLE <EOL> from libzookeeper . models import ZookeeperClient <EOL> from search . conf import SOLR_URL , SECURITY_ENABLED <EOL> LOG = logging . getLogger ( __name__ ) <EOL> MAX_UPLOAD_SIZE = <NUM_LIT:100> * <NUM_LIT> * <NUM_LIT> <EOL> ALLOWED_FIELD_ATTRIBUTES = set ( [ '<STR_LIT:name>' , '<STR_LIT:type>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> FLAGS = [ ( '<STR_LIT:I>' , '<STR_LIT>' ) , ( '<STR_LIT:T>' , '<STR_LIT>' ) , ( '<STR_LIT:S>' , '<STR_LIT>' ) , ( '<STR_LIT:M>' , '<STR_LIT>' ) ] <EOL> ZK_SOLR_CONFIG_NAMESPACE = '<STR_LIT>' <EOL> def get_solr_ensemble ( ) : <EOL> return '<STR_LIT>' % ENSEMBLE . get ( ) <EOL> class IndexControllerException ( Exception ) : <EOL> pass <EOL> class IndexController ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , user ) : <EOL> self . user = user <EOL> self . api = SolrApi ( SOLR_URL . get ( ) , self . user , SECURITY_ENABLED . get ( ) ) <EOL> def is_solr_cloud_mode ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> try : <EOL> self . api . collections ( ) <EOL> setattr ( self , '<STR_LIT>' , True ) <EOL> except Exception , e : <EOL> LOG . info ( '<STR_LIT>' % e ) <EOL> setattr ( self , '<STR_LIT>' , False ) <EOL> return getattr ( self , '<STR_LIT>' ) <EOL> def get_indexes ( self ) : <EOL> indexes = [ ] <EOL> try : <EOL> if self . is_solr_cloud_mode ( ) : <EOL> collections = self . api . collections2 ( ) <EOL> for name in collections : <EOL> indexes . append ( { '<STR_LIT:name>' : name , '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : [ ] } ) <EOL> solr_cores = self . api . cores ( ) <EOL> for name in solr_cores : <EOL> indexes . append ( { '<STR_LIT:name>' : name , '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : [ ] } ) <EOL> solr_aliases = self . api . aliases ( ) <EOL> for name in solr_aliases : <EOL> collections = solr_aliases [ name ] . split ( ) <EOL> indexes . append ( { '<STR_LIT:name>' : name , '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : collections } ) <EOL> except Exception , e : <EOL> msg = _ ( '<STR_LIT>' ) % e <EOL> LOG . warn ( msg ) <EOL> raise PopupException ( msg , detail = smart_str ( e ) ) <EOL> return indexes <EOL> def create_index ( self , name , fields , unique_key_field = '<STR_LIT:id>' , df = '<STR_LIT:text>' ) : <EOL> """<STR_LIT>""" <EOL> if self . is_solr_cloud_mode ( ) : <EOL> self . _create_solr_cloud_index ( name , fields , unique_key_field , df ) <EOL> else : <EOL> self . _create_non_solr_cloud_index ( name , fields , unique_key_field , df ) <EOL> return name <EOL> def _create_solr_cloud_index ( self , name , fields , unique_key_field , df ) : <EOL> with ZookeeperClient ( hosts = get_solr_ensemble ( ) , read_only = False ) as zc : <EOL> tmp_path , solr_config_path = copy_configs ( fields , unique_key_field , df , True ) <EOL> try : <EOL> root_node = '<STR_LIT>' % ( ZK_SOLR_CONFIG_NAMESPACE , name ) <EOL> config_root_path = '<STR_LIT>' % ( solr_config_path , '<STR_LIT>' ) <EOL> zc . copy_path ( root_node , config_root_path ) <EOL> if not self . api . create_collection ( name ) : <EOL> raise Exception ( '<STR_LIT>' % name ) <EOL> except Exception , e : <EOL> if zc . path_exists ( root_node ) : <EOL> zc . delete_path ( root_node ) <EOL> raise PopupException ( _ ( '<STR_LIT>' ) , detail = e ) <EOL> finally : <EOL> shutil . rmtree ( tmp_path ) <EOL> def _create_non_solr_cloud_index ( self , name , fields , unique_key_field , df ) : <EOL> instancedir = os . path . join ( CORE_INSTANCE_DIR . get ( ) , name ) <EOL> if os . path . exists ( instancedir ) : <EOL> raise PopupException ( _ ( "<STR_LIT>" ) % instancedir ) <EOL> try : <EOL> tmp_path , solr_config_path = copy_configs ( fields , unique_key_field , df , False ) <EOL> try : <EOL> shutil . move ( solr_config_path , instancedir ) <EOL> finally : <EOL> shutil . rmtree ( tmp_path ) <EOL> if not self . api . create_core ( name , instancedir ) : <EOL> raise Exception ( '<STR_LIT>' % name ) <EOL> except Exception , e : <EOL> raise PopupException ( _ ( '<STR_LIT>' ) , detail = e ) <EOL> finally : <EOL> shutil . rmtree ( instancedir ) <EOL> def delete_index ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_solr_cloud_mode ( ) : <EOL> raise PopupException ( _ ( '<STR_LIT>' ) ) <EOL> if self . api . remove_collection ( name ) : <EOL> try : <EOL> root_node = '<STR_LIT>' % ( ZK_SOLR_CONFIG_NAMESPACE , name ) <EOL> with ZookeeperClient ( hosts = get_solr_ensemble ( ) , read_only = False ) as zc : <EOL> zc . delete_path ( root_node ) <EOL> except Exception , e : <EOL> self . api . add_collection ( name ) <EOL> raise PopupException ( _ ( '<STR_LIT>' ) , detail = e ) <EOL> else : <EOL> raise PopupException ( _ ( '<STR_LIT>' ) ) <EOL> def get_index_schema ( self , index_name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> field_data = self . api . fields ( index_name ) <EOL> fields = self . _format_flags ( field_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> uniquekey = self . api . uniquekey ( index_name ) <EOL> return uniquekey , fields <EOL> except Exception , e : <EOL> LOG . exception ( e . message ) <EOL> raise IndexControllerException ( _ ( "<STR_LIT>" % index_name ) ) <EOL> def delete_alias ( self , name ) : <EOL> return self . api . delete_alias ( name ) <EOL> def _format_flags ( self , fields ) : <EOL> for name , properties in fields . items ( ) : <EOL> for ( code , value ) in FLAGS : <EOL> if code in properties [ '<STR_LIT>' ] : <EOL> properties [ value ] = True <EOL> return fields </s>
