<s> import abc <EOL> import collections <EOL> import itertools <EOL> from oslo_log import log <EOL> import six <EOL> from sahara import context <EOL> from sahara . i18n import _LW <EOL> from sahara . utils . openstack import manila <EOL> LOG = log . getLogger ( __name__ ) <EOL> def mount_shares ( cluster ) : <EOL> """<STR_LIT>""" <EOL> node_groups = ( ng for ng in cluster . node_groups if ng . shares ) <EOL> ng_mounts = [ _mount ( ng , share_config ) for ng in node_groups <EOL> for share_config in ng . shares ] <EOL> c_mounts = [ _mount ( ng , share_config ) for ng in cluster . node_groups <EOL> for share_config in cluster . shares or [ ] ] <EOL> if not ( ng_mounts or c_mounts ) : <EOL> return <EOL> ng_mounts_by_share_id = _group_mounts_by_share_id ( ng_mounts ) <EOL> c_mounts_by_share_id = _group_mounts_by_share_id ( c_mounts ) <EOL> all_share_ids = ( set ( ng_mounts_by_share_id . keys ( ) ) | <EOL> set ( c_mounts_by_share_id . keys ( ) ) ) <EOL> mounts_by_share_id = { <EOL> share_id : c_mounts_by_share_id . get ( share_id ) or <EOL> ng_mounts_by_share_id [ share_id ] for share_id in all_share_ids } <EOL> all_mounts = itertools . chain ( * mounts_by_share_id . values ( ) ) <EOL> mounts_by_ng_id = _group_mounts_by_ng_id ( all_mounts ) <EOL> client = manila . client ( ) <EOL> handlers_by_share_id = { id : _ShareHandler . create_from_id ( id , client ) <EOL> for id in all_share_ids } <EOL> for mounts in mounts_by_ng_id . values ( ) : <EOL> node_group_shares = _NodeGroupShares ( mounts [ <NUM_LIT:0> ] . node_group ) <EOL> for mount in mounts : <EOL> share_id = mount . share_config [ '<STR_LIT:id>' ] <EOL> node_group_shares . add_share ( mount . share_config , <EOL> handlers_by_share_id [ share_id ] ) <EOL> node_group_shares . mount_shares_to_node_group ( ) <EOL> def unmount_shares ( cluster , unmount_share_list ) : <EOL> """<STR_LIT>""" <EOL> client = manila . client ( ) <EOL> unmount_share_ids = ( set ( s [ '<STR_LIT:id>' ] for s in unmount_share_list ) ) <EOL> handlers_by_share_id = { id : _ShareHandler . create_from_id ( id , client ) <EOL> for id in unmount_share_ids } <EOL> for share in unmount_share_list : <EOL> for ng in cluster . node_groups : <EOL> for instance in ng . instances : <EOL> handlers_by_share_id [ share [ '<STR_LIT:id>' ] ] . unmount_from_instance ( <EOL> instance . remote ( ) , share ) <EOL> _mount = collections . namedtuple ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def _group_mounts ( mounts , grouper ) : <EOL> result = collections . defaultdict ( list ) <EOL> for mount in mounts : <EOL> result [ grouper ( mount ) ] . append ( mount ) <EOL> return result <EOL> def _group_mounts_by_share_id ( mounts ) : <EOL> return _group_mounts ( mounts , lambda mount : mount . share_config [ '<STR_LIT:id>' ] ) <EOL> def _group_mounts_by_ng_id ( mounts ) : <EOL> return _group_mounts ( mounts , lambda mount : mount . node_group [ '<STR_LIT:id>' ] ) <EOL> class _NodeGroupShares ( object ) : <EOL> """<STR_LIT>""" <EOL> _share = collections . namedtuple ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def __init__ ( self , node_group ) : <EOL> self . node_group = node_group <EOL> self . shares = [ ] <EOL> def add_share ( self , share_config , handler ) : <EOL> """<STR_LIT>""" <EOL> self . shares . append ( self . _share ( share_config , handler ) ) <EOL> def mount_shares_to_node_group ( self ) : <EOL> """<STR_LIT>""" <EOL> for instance in self . node_group . instances : <EOL> with context . set_current_instance_id ( instance . instance_id ) : <EOL> self . _mount_shares_to_instance ( instance ) <EOL> def _mount_shares_to_instance ( self , instance ) : <EOL> for share in self . shares : <EOL> share . handler . allow_access_to_instance ( instance , <EOL> share . share_config ) <EOL> with instance . remote ( ) as remote : <EOL> share_types = set ( type ( share . handler ) for share in self . shares ) <EOL> for share_type in share_types : <EOL> share_type . setup_instance ( remote ) <EOL> for share in self . shares : <EOL> share . handler . mount_to_instance ( remote , share . share_config ) <EOL> @ six . add_metaclass ( abc . ABCMeta ) <EOL> class _ShareHandler ( object ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setup_instance ( cls , remote ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> @ classmethod <EOL> def create_from_id ( cls , share_id , client ) : <EOL> """<STR_LIT>""" <EOL> share = manila . get_share ( client , share_id , <EOL> raise_on_error = True ) <EOL> mounter_class = _share_types [ share . share_proto ] <EOL> return mounter_class ( share , client ) <EOL> def __init__ ( self , share , client ) : <EOL> self . share = share <EOL> self . client = client <EOL> def allow_access_to_instance ( self , instance , share_config ) : <EOL> """<STR_LIT>""" <EOL> access_level = self . _get_access_level ( share_config ) <EOL> ip_filter = lambda x : ( x . access_type == '<STR_LIT>' and <EOL> x . access_to == instance . internal_ip ) <EOL> accesses = list ( filter ( ip_filter , self . share . access_list ( ) ) ) <EOL> if accesses : <EOL> access = accesses [ <NUM_LIT:0> ] <EOL> if access . access_level not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> LOG . warning ( <EOL> _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) . format ( <EOL> access_level = access . access_level , <EOL> share_id = self . share . id , <EOL> ip = instance . internal_ip ) ) <EOL> elif access . access_level == '<STR_LIT>' and access_level == '<STR_LIT>' : <EOL> self . share . deny ( access . id ) <EOL> self . share . allow ( '<STR_LIT>' , instance . internal_ip , access_level ) <EOL> else : <EOL> self . share . allow ( '<STR_LIT>' , instance . internal_ip , access_level ) <EOL> @ abc . abstractmethod <EOL> def mount_to_instance ( self , remote , share_info ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> @ abc . abstractmethod <EOL> def unmount_from_instance ( self , remote , share_info ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _get_access_level ( self , share_config ) : <EOL> return share_config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def _default_mount ( self ) : <EOL> return '<STR_LIT>' . format ( self . share . id ) <EOL> def _get_path ( self , share_info ) : <EOL> return share_info . get ( '<STR_LIT:path>' , self . _default_mount ( ) ) <EOL> class _NFSMounter ( _ShareHandler ) : <EOL> """<STR_LIT>""" <EOL> _DEBIAN_INSTALL = "<STR_LIT>" <EOL> _REDHAT_INSTALL = "<STR_LIT>" <EOL> _NFS_CHECKS = { <EOL> "<STR_LIT>" : _REDHAT_INSTALL , <EOL> "<STR_LIT>" : _REDHAT_INSTALL , <EOL> "<STR_LIT>" : _REDHAT_INSTALL , <EOL> "<STR_LIT>" : _DEBIAN_INSTALL <EOL> } <EOL> _MKDIR_COMMAND = '<STR_LIT>' <EOL> _MOUNT_COMMAND = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> _UNMOUNT_COMMAND = ( "<STR_LIT>" ) <EOL> _RMDIR_COMMAND = '<STR_LIT>' <EOL> @ classmethod <EOL> def setup_instance ( cls , remote ) : <EOL> """<STR_LIT>""" <EOL> response = remote . execute_command ( '<STR_LIT>' ) <EOL> distro = response [ <NUM_LIT:1> ] . strip ( ) . lower ( ) <EOL> if distro in cls . _NFS_CHECKS : <EOL> command = cls . _NFS_CHECKS [ distro ] <EOL> remote . execute_command ( command , run_as_root = True ) <EOL> else : <EOL> LOG . warning ( <EOL> _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) . format ( distro = distro ) ) <EOL> def mount_to_instance ( self , remote , share_info ) : <EOL> """<STR_LIT>""" <EOL> local_path = self . _get_path ( share_info ) <EOL> access_level = self . _get_access_level ( share_info ) <EOL> access_arg = '<STR_LIT>' if access_level == '<STR_LIT>' else '<STR_LIT>' <EOL> remote . execute_command ( self . _MKDIR_COMMAND % local_path , <EOL> run_as_root = True ) <EOL> mount_command = self . _MOUNT_COMMAND % { <EOL> "<STR_LIT>" : self . share . export_location , <EOL> "<STR_LIT>" : local_path , <EOL> "<STR_LIT>" : access_arg } <EOL> remote . execute_command ( mount_command , run_as_root = True ) <EOL> def unmount_from_instance ( self , remote , share_info ) : <EOL> """<STR_LIT>""" <EOL> local_path = self . _get_path ( share_info ) <EOL> unmount_command = self . _UNMOUNT_COMMAND % local_path <EOL> rmdir_command = self . _RMDIR_COMMAND % local_path <EOL> remote . execute_command ( unmount_command , run_as_root = True ) <EOL> remote . execute_command ( rmdir_command , run_as_root = True ) <EOL> _share_types = { "<STR_LIT>" : _NFSMounter } <EOL> SUPPORTED_SHARE_TYPES = _share_types . keys ( ) <EOL> def _make_share_path ( mount_point , path ) : <EOL> return "<STR_LIT>" . format ( mount_point , path ) <EOL> def default_mount ( share_id ) : <EOL> client = manila . client ( ) <EOL> return _ShareHandler . create_from_id ( share_id , client ) . _default_mount ( ) <EOL> def get_share_path ( url , shares ) : <EOL> url = six . moves . urllib . parse . urlparse ( url ) <EOL> share_list = list ( filter ( lambda s : s [ '<STR_LIT:id>' ] == url . netloc , shares ) ) <EOL> if not share_list : <EOL> path = None <EOL> else : <EOL> mount_point = share_list [ <NUM_LIT:0> ] . get ( '<STR_LIT:path>' , None ) <EOL> if mount_point is None : <EOL> mount_point = default_mount ( share_list [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] ) <EOL> path = _make_share_path ( mount_point , url . path ) <EOL> return path </s>
