<s> import os <EOL> from neutron . agent . common import config <EOL> from neutron . agent . l3 import ha_router <EOL> from neutron . agent . l3 import namespaces <EOL> from neutron . agent . linux import external_process <EOL> from neutron . agent . linux import utils <EOL> from neutron . callbacks import events <EOL> from neutron . callbacks import registry <EOL> from neutron . callbacks import resources <EOL> from neutron . common import constants <EOL> from neutron . common import exceptions <EOL> METADATA_SERVICE_NAME = '<STR_LIT>' <EOL> class MetadataDriver ( object ) : <EOL> def __init__ ( self , l3_agent ) : <EOL> self . metadata_port = l3_agent . conf . metadata_port <EOL> self . metadata_access_mark = l3_agent . conf . metadata_access_mark <EOL> registry . subscribe ( <EOL> after_router_added , resources . ROUTER , events . AFTER_CREATE ) <EOL> registry . subscribe ( <EOL> before_router_removed , resources . ROUTER , events . BEFORE_DELETE ) <EOL> @ classmethod <EOL> def metadata_filter_rules ( cls , port , mark ) : <EOL> return [ ( '<STR_LIT>' , '<STR_LIT>' % <EOL> ( mark , constants . ROUTER_MARK_MASK ) ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' <EOL> '<STR_LIT>' % port ) ] <EOL> @ classmethod <EOL> def metadata_mangle_rules ( cls , mark ) : <EOL> return [ ( '<STR_LIT>' , '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> { '<STR_LIT>' : namespaces . INTERNAL_DEV_PREFIX + '<STR_LIT:+>' , <EOL> '<STR_LIT:value>' : mark , <EOL> '<STR_LIT>' : constants . ROUTER_MARK_MASK } ) ] <EOL> @ classmethod <EOL> def metadata_nat_rules ( cls , port ) : <EOL> return [ ( '<STR_LIT>' , '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> { '<STR_LIT>' : namespaces . INTERNAL_DEV_PREFIX + '<STR_LIT:+>' , <EOL> '<STR_LIT:port>' : port } ) ] <EOL> @ classmethod <EOL> def _get_metadata_proxy_user_group_watchlog ( cls , conf ) : <EOL> user = conf . metadata_proxy_user or str ( os . geteuid ( ) ) <EOL> group = conf . metadata_proxy_group or str ( os . getegid ( ) ) <EOL> watch_log = conf . metadata_proxy_watch_log <EOL> if watch_log is None : <EOL> watch_log = utils . is_effective_user ( user ) <EOL> return user , group , watch_log <EOL> @ classmethod <EOL> def _get_metadata_proxy_callback ( cls , port , conf , network_id = None , <EOL> router_id = None ) : <EOL> uuid = network_id or router_id <EOL> if uuid is None : <EOL> raise exceptions . NetworkIdOrRouterIdRequiredError ( ) <EOL> if network_id : <EOL> lookup_param = '<STR_LIT>' % network_id <EOL> else : <EOL> lookup_param = '<STR_LIT>' % router_id <EOL> def callback ( pid_file ) : <EOL> metadata_proxy_socket = conf . metadata_proxy_socket <EOL> user , group , watch_log = ( <EOL> cls . _get_metadata_proxy_user_group_watchlog ( conf ) ) <EOL> proxy_cmd = [ '<STR_LIT>' , <EOL> '<STR_LIT>' % pid_file , <EOL> '<STR_LIT>' % metadata_proxy_socket , <EOL> lookup_param , <EOL> '<STR_LIT>' % conf . state_path , <EOL> '<STR_LIT>' % port , <EOL> '<STR_LIT>' % user , <EOL> '<STR_LIT>' % group ] <EOL> proxy_cmd . extend ( config . get_log_args ( <EOL> conf , '<STR_LIT>' % uuid , <EOL> metadata_proxy_watch_log = watch_log ) ) <EOL> return proxy_cmd <EOL> return callback <EOL> @ classmethod <EOL> def spawn_monitored_metadata_proxy ( cls , monitor , ns_name , port , conf , <EOL> network_id = None , router_id = None ) : <EOL> uuid = network_id or router_id <EOL> callback = cls . _get_metadata_proxy_callback ( <EOL> port , conf , network_id = network_id , router_id = router_id ) <EOL> pm = cls . _get_metadata_proxy_process_manager ( uuid , conf , <EOL> ns_name = ns_name , <EOL> callback = callback ) <EOL> pm . enable ( ) <EOL> monitor . register ( uuid , METADATA_SERVICE_NAME , pm ) <EOL> @ classmethod <EOL> def destroy_monitored_metadata_proxy ( cls , monitor , uuid , conf ) : <EOL> monitor . unregister ( uuid , METADATA_SERVICE_NAME ) <EOL> pm = cls . _get_metadata_proxy_process_manager ( uuid , conf ) <EOL> pm . disable ( ) <EOL> @ classmethod <EOL> def _get_metadata_proxy_process_manager ( cls , router_id , conf , ns_name = None , <EOL> callback = None ) : <EOL> return external_process . ProcessManager ( <EOL> conf = conf , <EOL> uuid = router_id , <EOL> namespace = ns_name , <EOL> default_cmd_callback = callback ) <EOL> def after_router_added ( resource , event , l3_agent , ** kwargs ) : <EOL> router = kwargs [ '<STR_LIT>' ] <EOL> proxy = l3_agent . metadata_driver <EOL> for c , r in proxy . metadata_filter_rules ( proxy . metadata_port , <EOL> proxy . metadata_access_mark ) : <EOL> router . iptables_manager . ipv4 [ '<STR_LIT>' ] . add_rule ( c , r ) <EOL> for c , r in proxy . metadata_mangle_rules ( proxy . metadata_access_mark ) : <EOL> router . iptables_manager . ipv4 [ '<STR_LIT>' ] . add_rule ( c , r ) <EOL> for c , r in proxy . metadata_nat_rules ( proxy . metadata_port ) : <EOL> router . iptables_manager . ipv4 [ '<STR_LIT>' ] . add_rule ( c , r ) <EOL> router . iptables_manager . apply ( ) <EOL> if not isinstance ( router , ha_router . HaRouter ) : <EOL> proxy . spawn_monitored_metadata_proxy ( <EOL> l3_agent . process_monitor , <EOL> router . ns_name , <EOL> proxy . metadata_port , <EOL> l3_agent . conf , <EOL> router_id = router . router_id ) <EOL> def before_router_removed ( resource , event , l3_agent , ** kwargs ) : <EOL> router = kwargs [ '<STR_LIT>' ] <EOL> proxy = l3_agent . metadata_driver <EOL> proxy . destroy_monitored_metadata_proxy ( l3_agent . process_monitor , <EOL> router . router [ '<STR_LIT:id>' ] , <EOL> l3_agent . conf ) </s>
