Beispiel #1
0
    def run(self, brname='', rm=True):

        if rm and ctn_exists(self.name):
            print 'remove container:', self.name
            dckr.remove_container(self.name, force=True)

        config = dckr.create_host_config(binds=['{0}:{1}'.format(os.path.abspath(self.host_dir), self.guest_dir)],
                                         privileged=True)
        ctn = dckr.create_container(image=self.image, command='bash', detach=True, name=self.name,
                                    stdin_open=True, volumes=[self.guest_dir], host_config=config)
        dckr.start(container=self.name)
        if brname != '':
            connect_ctn_to_br(self.name, brname)
        self.ctn_id = ctn['Id']

        return ctn
    def run(self, dckr_net_name='', rm=True):

        if rm and ctn_exists(self.name):
            print('remove container:', self.name)
            dckr.remove_container(self.name, force=True)

        host_config = dckr.create_host_config(binds=[
            '{0}:{1}'.format(os.path.abspath(self.host_dir), self.guest_dir)
        ],
                                              privileged=True,
                                              network_mode='bridge',
                                              cap_add=['NET_ADMIN'])

        ctn = dckr.create_container(image=self.image,
                                    entrypoint='bash',
                                    detach=True,
                                    name=self.name,
                                    stdin_open=True,
                                    volumes=[self.guest_dir],
                                    host_config=host_config)
        self.ctn_id = ctn['Id']

        ipv4_addresses = self.get_ipv4_addresses()

        net_id = None
        for network in dckr.networks(names=[dckr_net_name]):
            if network['Name'] != dckr_net_name:
                continue

            net_id = network['Id']
            if not 'IPAM' in network:
                print('can\'t verify if container\'s IP addresses '
                      'are valid for Docker network {}: missing IPAM'.format(
                          dckr_net_name))
                break
            ipam = network['IPAM']

            if not 'Config' in ipam:
                print('can\'t verify if container\'s IP addresses '
                      'are valid for Docker network {}: missing IPAM.Config'.
                      format(dckr_net_name))
                break

            ip_ok = False
            network_subnets = [
                item['Subnet'] for item in ipam['Config'] if 'Subnet' in item
            ]
            for ip in ipv4_addresses:
                for subnet in network_subnets:
                    ip_ok = netaddr.IPAddress(ip) in netaddr.IPNetwork(subnet)

                if not ip_ok:
                    print(
                        'the container\'s IP address {} is not valid for Docker network {} '
                        'since it\'s not part of any of its subnets ({})'.
                        format(ip, dckr_net_name, ', '.join(network_subnets)))
                    print(
                        'Please consider removing the Docket network {net} '
                        'to allow bgperf to create it again using the '
                        'expected subnet:\n'
                        '  docker network rm {net}'.format(net=dckr_net_name))
                    sys.exit(1)
            break

        if net_id is None:
            print('Docker network "{}" not found!'.format(dckr_net_name))
            return

        dckr.connect_container_to_network(self.ctn_id,
                                          net_id,
                                          ipv4_address=ipv4_addresses[0])
        dckr.start(container=self.name)

        if len(ipv4_addresses) > 1:

            # get the interface used by the first IP address already added by Docker
            dev = None
            res = self.local('ip addr')
            for line in res.split(b'\n'):
                if ipv4_addresses[0].encode('utf-8') in line:
                    dev = line.split(b' ')[-1].strip()
            if not dev:
                dev = "eth0"

            for ip in ipv4_addresses[1:]:
                self.local('ip addr add {} dev {}'.format(ip, dev))

        return ctn
Beispiel #3
0
    def run_relay(name, ip, listen, talk, dckr_net_name='', rm=True):
        if rm and ctn_exists(name):
            print 'remove relay containers:', name
            dckr.remove_container(name, force=True)

        ctn = dckr.create_container(
            image='sproxy',
            detach=True,
            name=name,
            environment=['LISTEN={0}'.format(listen), 'TALK={0}'.format(talk)])
        ctn_id = ctn['Id']

        ipv4_addresses = [ip]

        net_id = None
        for network in dckr.networks(names=[dckr_net_name]):
            if network['Name'] != dckr_net_name:
                continue

            net_id = network['Id']
            if not 'IPAM' in network:
                print(
                    'can\'t verify if container\'s IP addresses '
                    'are valid for Docker network {}: missing IPAM'.format(
                        dckr_net_name))
                break
            ipam = network['IPAM']

            if not 'Config' in ipam:
                print(
                    'can\'t verify if container\'s IP addresses '
                    'are valid for Docker network {}: missing IPAM.Config'.
                    format(dckr_net_name))
                break

            ip_ok = False
            network_subnets = [
                item['Subnet'] for item in ipam['Config'] if 'Subnet' in item
            ]

            for ip in ipv4_addresses:
                for subnet in network_subnets:
                    ip_ok = netaddr.IPAddress(ip) in netaddr.IPNetwork(subnet)

                if not ip_ok:
                    print(
                        'the container\'s IP address {} is not valid for Docker network {} '
                        'since it\'s not part of any of its subnets ({})'.
                        format(ip, dckr_net_name, ', '.join(network_subnets)))
                    print(
                        'Please consider removing the Docket network {net} '
                        'to allow bgperf to create it again using the '
                        'expected subnet:\n'
                        '  docker network rm {net}'.format(net=dckr_net_name))
                    sys.exit(1)
            break

        if net_id is None:
            print 'Docker network "{}" not found!'.format(dckr_net_name)
            return

        dckr.connect_container_to_network(ctn_id,
                                          net_id,
                                          ipv4_address=ipv4_addresses[0])
        dckr.start(container=name)

        return ctn