github.com/osrg/gobgp@v2.0.0+incompatible/test/lib/bagpipe.py (about)

     1  # Copyright (C) 2015 Nippon Telegraph and Telephone Corporation.
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #    http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  # implied.
    13  # See the License for the specific language governing permissions and
    14  # limitations under the License.
    15  
    16  from __future__ import absolute_import
    17  
    18  from fabric import colors
    19  from fabric.api import local
    20  
    21  from lib.base import (
    22      BGPContainer,
    23      CmdBuffer,
    24  )
    25  
    26  
    27  class BagpipeContainer(BGPContainer):
    28  
    29      SHARED_VOLUME = '/root/shared_volume'
    30  
    31      def __init__(self, name, asn, router_id,
    32                   ctn_image_name='yoshima/bagpipe-bgp'):
    33          super(BagpipeContainer, self).__init__(name, asn, router_id,
    34                                                 ctn_image_name)
    35          self.shared_volumes.append((self.config_dir, self.SHARED_VOLUME))
    36  
    37      def run(self):
    38          super(BagpipeContainer, self).run()
    39          cmd = CmdBuffer(' ')
    40          cmd << 'docker exec'
    41          cmd << '{0} cp {1}/bgp.conf'.format(self.name, self.SHARED_VOLUME)
    42          cmd << '/etc/bagpipe-bgp/'
    43          local(str(cmd), capture=True)
    44          cmd = 'docker exec {0} service bagpipe-bgp start'.format(self.name)
    45          local(cmd, capture=True)
    46  
    47      def create_config(self):
    48          c = CmdBuffer()
    49          c << '[BGP]'
    50          if len(self.ip_addrs) > 0:
    51              c << 'local_address={0}'.format(self.ip_addrs[0][1].split('/')[0])
    52          for info in self.peers.values():
    53              c << 'peers={0}'.format(info['neigh_addr'].split('/')[0])
    54          c << 'my_as={0}'.format(self.asn)
    55          c << 'enable_rtc=True'
    56          c << '[API]'
    57          c << 'api_host=localhost'
    58          c << 'api_port=8082'
    59          c << '[DATAPLANE_DRIVER_IPVPN]'
    60          c << 'dataplane_driver = DummyDataplaneDriver'
    61          c << '[DATAPLANE_DRIVER_EVPN]'
    62          c << 'dataplane_driver = DummyDataplaneDriver'
    63  
    64          with open('{0}/bgp.conf'.format(self.config_dir), 'w') as f:
    65              print colors.yellow(str(c))
    66              f.writelines(str(c))
    67  
    68      def reload_config(self):
    69          cmd = CmdBuffer(' ')
    70          cmd << 'docker exec'
    71          cmd << '{0} cp {1}/bgp.conf'.format(self.name, self.SHARED_VOLUME)
    72          cmd << '/etc/bagpipe-bgp/'
    73          local(str(cmd), capture=True)
    74          cmd = 'docker exec {0} service bagpipe-bgp restart'.format(self.name)
    75          local(cmd, capture=True)
    76  
    77      def pipework(self, bridge, ip_addr, intf_name=""):
    78          super(BagpipeContainer, self).pipework(bridge, ip_addr, intf_name)
    79          self.create_config()
    80          if self.is_running:
    81              self.reload_config()