def test_create_with_ipv6_address(self):
        net_name, net_id = self.create_network(
            ipam=create_ipam_config(
                driver='default',
                pool_configs=[create_ipam_pool(subnet="2001:389::1/64")],
            ),
        )
        container = self.client.create_container(
            image='busybox', command='top',
            host_config=self.client.create_host_config(network_mode=net_name),
            networking_config=self.client.create_networking_config({
                net_name: self.client.create_endpoint_config(
                    ipv6_address='2001:389::f00d'
                )
            })
        )
        self.tmp_containers.append(container)
        self.client.start(container)

        container_data = self.client.inspect_container(container)
        self.assertEqual(
            container_data[
                'NetworkSettings']['Networks'][net_name]['IPAMConfig'][
                'IPv6Address'
            ],
            '2001:389::f00d'
        )
Example #2
0
    def test_create_network_with_ipam_config(self):
        _, net_id = self.create_network(ipam=create_ipam_config(
            driver='default',
            pool_configs=[
                create_ipam_pool(
                    subnet="172.28.0.0/16",
                    iprange="172.28.5.0/24",
                    gateway="172.28.5.254",
                    aux_addresses={
                        "a": "172.28.1.5",
                        "b": "172.28.1.6",
                        "c": "172.28.1.7",
                    },
                ),
            ],
        ), )

        net = self.client.inspect_network(net_id)
        ipam = net['IPAM']

        assert ipam.pop('Options', None) is None

        assert ipam['Driver'] == 'default'

        assert ipam['Config'] == [{
            'Subnet': "172.28.0.0/16",
            'IPRange': "172.28.5.0/24",
            'Gateway': "172.28.5.254",
            'AuxiliaryAddresses': {
                "a": "172.28.1.5",
                "b": "172.28.1.6",
                "c": "172.28.1.7",
            },
        }]
    def test_connect_with_ipv6_address(self):
        net_name, net_id = self.create_network(
            ipam=create_ipam_config(
                driver='default',
                pool_configs=[
                    create_ipam_pool(
                        subnet="2001:389::1/64", iprange="2001:389::0/96",
                        gateway="2001:389::ffff"
                    )
                ]
            )
        )

        container = self.create_and_start(
            host_config=self.client.create_host_config(network_mode=net_name))

        self.client.disconnect_container_from_network(container, net_name)
        self.client.connect_container_to_network(
            container, net_name, ipv6_address='2001:389::f00d'
        )

        container_data = self.client.inspect_container(container)
        net_data = container_data['NetworkSettings']['Networks'][net_name]
        self.assertEqual(
            net_data['IPAMConfig']['IPv6Address'], '2001:389::f00d'
        )
    def test_connect_with_ipv4_address(self):
        net_name, net_id = self.create_network(
            ipam=create_ipam_config(
                driver='default',
                pool_configs=[
                    create_ipam_pool(
                        subnet="172.28.0.0/16", iprange="172.28.5.0/24",
                        gateway="172.28.5.254"
                    )
                ]
            )
        )

        container = self.create_and_start(
            host_config=self.client.create_host_config(network_mode=net_name))

        self.client.disconnect_container_from_network(container, net_name)
        self.client.connect_container_to_network(
            container, net_name, ipv4_address='172.28.5.24'
        )

        container_data = self.client.inspect_container(container)
        net_data = container_data['NetworkSettings']['Networks'][net_name]
        self.assertEqual(
            net_data['IPAMConfig']['IPv4Address'], '172.28.5.24'
        )
Example #5
0
    def create_network(self):
        if not self.existing_network:
            ipam_pools = []
            if self.parameters.ipam_options:
                if HAS_DOCKER_PY_2:
                    ipam_pools.append(IPAMPool(**self.parameters.ipam_options))
                else:
                    ipam_pools.append(utils.create_ipam_pool(**self.parameters.ipam_options))

            if HAS_DOCKER_PY_2:
                ipam_config = IPAMConfig(driver=self.parameters.ipam_driver,
                                         pool_configs=ipam_pools)
            else:
                ipam_config = utils.create_ipam_config(driver=self.parameters.ipam_driver,
                                                       pool_configs=ipam_pools)

            if not self.check_mode:
                resp = self.client.create_network(self.parameters.network_name,
                                                  driver=self.parameters.driver,
                                                  options=self.parameters.driver_options,
                                                  ipam=ipam_config)

                self.existing_network = self.client.inspect_network(resp['Id'])
            self.results['actions'].append("Created network %s with driver %s" % (self.parameters.network_name, self.parameters.driver))
            self.results['changed'] = True
    def create_network(self):
        if not self.existing_network:
            params = dict(
                driver=self.parameters.driver,
                options=self.parameters.driver_options,
            )

            ipam_pools = []
            if self.parameters.ipam_config:
                for ipam_pool in self.parameters.ipam_config:
                    if HAS_DOCKER_PY_2 or HAS_DOCKER_PY_3:
                        ipam_pools.append(IPAMPool(**ipam_pool))
                    else:
                        ipam_pools.append(utils.create_ipam_pool(**ipam_pool))

            if HAS_DOCKER_PY_2 or HAS_DOCKER_PY_3:
                params['ipam'] = IPAMConfig(driver=self.parameters.ipam_driver,
                                            pool_configs=ipam_pools)
            else:
                params['ipam'] = utils.create_ipam_config(driver=self.parameters.ipam_driver,
                                                          pool_configs=ipam_pools)

            if self.parameters.enable_ipv6 is not None:
                params['enable_ipv6'] = self.parameters.enable_ipv6
            if self.parameters.internal is not None:
                params['internal'] = self.parameters.internal

            if not self.check_mode:
                resp = self.client.create_network(self.parameters.network_name, **params)

                self.existing_network = self.client.inspect_network(resp['Id'])
            self.results['actions'].append("Created network %s with driver %s" % (self.parameters.network_name, self.parameters.driver))
            self.results['changed'] = True
Example #7
0
    def create_network(self):
        if not self.existing_network:
            ipam_pools = []
            if self.parameters.ipam_options:
                if HAS_DOCKER_PY_2:
                    ipam_pools.append(IPAMPool(**self.parameters.ipam_options))
                else:
                    ipam_pools.append(
                        utils.create_ipam_pool(**self.parameters.ipam_options))

            if HAS_DOCKER_PY_2:
                ipam_config = IPAMConfig(driver=self.parameters.ipam_driver,
                                         pool_configs=ipam_pools)
            else:
                ipam_config = utils.create_ipam_config(
                    driver=self.parameters.ipam_driver,
                    pool_configs=ipam_pools)

            if not self.check_mode:
                resp = self.client.create_network(
                    self.parameters.network_name,
                    driver=self.parameters.driver,
                    options=self.parameters.driver_options,
                    ipam=ipam_config)

                self.existing_network = self.client.inspect_network(resp['Id'])
            self.results['actions'].append(
                "Created network %s with driver %s" %
                (self.parameters.network_name, self.parameters.driver))
            self.results['changed'] = True
Example #8
0
    def test_create_network(self):
        network_data = {
            "id": 'abc12345',
            "warning": "",
        }

        network_response = response(status_code=200, content=network_data)
        post = mock.Mock(return_value=network_response)

        with mock.patch('docker.Client.post', post):
            result = self.client.create_network('foo')
            self.assertEqual(result, network_data)

            self.assertEqual(post.call_args[0][0],
                             url_prefix + 'networks/create')

            self.assertEqual(json.loads(post.call_args[1]['data']),
                             {"Name": "foo"})

            opts = {
                'com.docker.network.bridge.enable_icc': False,
                'com.docker.network.bridge.enable_ip_masquerade': False,
            }
            self.client.create_network('foo', 'bridge', opts)

            self.assertEqual(json.loads(post.call_args[1]['data']), {
                "Name": "foo",
                "Driver": "bridge",
                "Options": opts
            })

            ipam_pool_config = create_ipam_pool(subnet="192.168.52.0/24",
                                                gateway="192.168.52.254")
            ipam_config = create_ipam_config(pool_configs=[ipam_pool_config])

            self.client.create_network("bar",
                                       driver="bridge",
                                       ipam=ipam_config)

            self.assertEqual(
                json.loads(post.call_args[1]['data']), {
                    "Name": "bar",
                    "Driver": "bridge",
                    "IPAM": {
                        "Driver":
                        "default",
                        "Config": [{
                            "IPRange": None,
                            "Gateway": "192.168.52.254",
                            "Subnet": "192.168.52.0/24",
                            "AuxiliaryAddresses": None,
                        }]
                    }
                })
Example #9
0
    def test_create_network(self):
        network_data = {
            "id": 'abc12345',
            "warning": "",
        }

        network_response = response(status_code=200, content=network_data)
        post = mock.Mock(return_value=network_response)

        with mock.patch('docker.Client.post', post):
            result = self.client.create_network('foo')
            self.assertEqual(result, network_data)

            self.assertEqual(
                post.call_args[0][0],
                url_prefix + 'networks/create')

            self.assertEqual(
                json.loads(post.call_args[1]['data']),
                {"name": "foo"})

            opts = {
                'com.docker.network.bridge.enable_icc': False,
                'com.docker.network.bridge.enable_ip_masquerade': False,
            }
            self.client.create_network('foo', 'bridge', opts)

            self.assertEqual(
                json.loads(post.call_args[1]['data']),
                {"name": "foo", "driver": "bridge", "options": opts})

            ipam_pool_config = create_ipam_pool(subnet="192.168.52.0/24",
                                                gateway="192.168.52.254")
            ipam_config = create_ipam_config(pool_configs=[ipam_pool_config])

            self.client.create_network("bar", driver="bridge",
                                       ipam=ipam_config)

            self.assertEqual(
                json.loads(post.call_args[1]['data']),
                {
                    "name": "bar",
                    "driver": "bridge",
                    "ipam": {
                        "driver": "default",
                        "config": [{
                            "iprange": None,
                            "gateway": "192.168.52.254",
                            "subnet": "192.168.52.0/24",
                            "auxaddresses": None
                        }]
                    }
                })
Example #10
0
def create_network(client, name):
    cidr = get_next_cidr(client)
    print("Creating network {0} with subnet {1}".format(name, cidr.exploded))
    
    networks = client.networks(names=(name,))
    if len(networks) > 0:
        for network in networks:
            client.remove_network(name)
    
    ipam_pool = create_ipam_pool(
            subnet=cidr.exploded, gateway=(cidr.network_address + 1).exploded)
    ipam_config = create_ipam_config(pool_configs=[ipam_pool])
    client.create_network(name, ipam=ipam_config)
Example #11
0
    def create_network(self):
        if not self.existing_network:
            params = dict(
                driver=self.parameters.driver,
                options=self.parameters.driver_options,
            )

            ipam_pools = []
            if self.parameters.ipam_config:
                for ipam_pool in self.parameters.ipam_config:
                    if LooseVersion(docker_version) >= LooseVersion('2.0.0'):
                        ipam_pools.append(IPAMPool(**ipam_pool))
                    else:
                        ipam_pools.append(utils.create_ipam_pool(**ipam_pool))

            if self.parameters.ipam_driver or self.parameters.ipam_driver_options or ipam_pools:
                # Only add ipam parameter if a driver was specified or if IPAM parameters
                # were specified. Leaving this parameter away can significantly speed up
                # creation; on my machine creation with this option needs ~15 seconds,
                # and without just a few seconds.
                if LooseVersion(docker_version) >= LooseVersion('2.0.0'):
                    params['ipam'] = IPAMConfig(
                        driver=self.parameters.ipam_driver,
                        pool_configs=ipam_pools,
                        options=self.parameters.ipam_driver_options)
                else:
                    params['ipam'] = utils.create_ipam_config(
                        driver=self.parameters.ipam_driver,
                        pool_configs=ipam_pools)

            if self.parameters.enable_ipv6 is not None:
                params['enable_ipv6'] = self.parameters.enable_ipv6
            if self.parameters.internal is not None:
                params['internal'] = self.parameters.internal
            if self.parameters.scope is not None:
                params['scope'] = self.parameters.scope
            if self.parameters.attachable is not None:
                params['attachable'] = self.parameters.attachable
            if self.parameters.labels:
                params['labels'] = self.parameters.labels

            if not self.check_mode:
                resp = self.client.create_network(self.parameters.name,
                                                  **params)
                self.client.report_warnings(resp, ['Warning'])
                self.existing_network = self.client.get_network(
                    network_id=resp['Id'])
            self.results['actions'].append(
                "Created network %s with driver %s" %
                (self.parameters.name, self.parameters.driver))
            self.results['changed'] = True
Example #12
0
    def test_create_ipam_config(self):
        ipam_pool = create_ipam_pool(subnet='192.168.52.0/24',
                                     gateway='192.168.52.254')

        ipam_config = create_ipam_config(pool_configs=[ipam_pool])
        self.assertEqual(ipam_config, {
            'Driver': 'default',
            'Config': [{
                'Subnet': '192.168.52.0/24',
                'Gateway': '192.168.52.254',
                'AuxiliaryAddresses': None,
                'IPRange': None,
            }]
        })
Example #13
0
    def test_create_ipam_config(self):
        ipam_pool = create_ipam_pool(subnet='192.168.52.0/24',
                                     gateway='192.168.52.254')

        ipam_config = create_ipam_config(pool_configs=[ipam_pool])
        self.assertEqual(ipam_config, {
            'driver': 'default',
            'config': [{
                'subnet': '192.168.52.0/24',
                'gateway': '192.168.52.254',
                'auxaddresses': None,
                'iprange': None
            }]
        })
Example #14
0
    def test_create_ipam_config(self):
        ipam_pool = create_ipam_pool(subnet='192.168.52.0/24',
                                     gateway='192.168.52.254')

        ipam_config = create_ipam_config(pool_configs=[ipam_pool])
        self.assertEqual(ipam_config, {
            'Driver': 'default',
            'Config': [{
                'Subnet': '192.168.52.0/24',
                'Gateway': '192.168.52.254',
                'AuxiliaryAddresses': None,
                'IPRange': None,
            }]
        })
Example #15
0
    def test_create_ipam_config(self):
        ipam_pool = create_ipam_pool(subnet='192.168.52.0/24',
                                     gateway='192.168.52.254')

        ipam_config = create_ipam_config(pool_configs=[ipam_pool])
        self.assertEqual(ipam_config, {
            'driver': 'default',
            'config': [{
                'subnet': '192.168.52.0/24',
                'gateway': '192.168.52.254',
                'auxaddresses': None,
                'iprange': None
            }]
        })
Example #16
0
def create_ipam_config_from_dict(ipam_dict):
    if not ipam_dict:
        return None

    return create_ipam_config(
        driver=ipam_dict.get('driver'),
        pool_configs=[
            create_ipam_pool(
                subnet=config.get('subnet'),
                iprange=config.get('ip_range'),
                gateway=config.get('gateway'),
                aux_addresses=config.get('aux_addresses'),
            ) for config in ipam_dict.get('config', [])
        ],
    )
Example #17
0
def create_network():
    ipam_config = docker_utils.create_ipam_config(
        pool_configs=[docker_utils.create_ipam_pool(
            subnet=NETWORKING_CONF['ipam']['subnet'],
            iprange=NETWORKING_CONF['ipam']['iprange']
        )]
    )

    network = docker_cli.create_network(
        name=network_name,
        driver=NETWORKING_CONF['network_driver'],
        ipam=ipam_config,
        internal=NETWORKING_CONF['internal']
    )

    return network.get('Id')
Example #18
0
def create_ipam_config_from_dict(ipam_dict):
    if not ipam_dict:
        return None

    return create_ipam_config(
        driver=ipam_dict.get('driver'),
        pool_configs=[
            create_ipam_pool(
                subnet=config.get('subnet'),
                iprange=config.get('ip_range'),
                gateway=config.get('gateway'),
                aux_addresses=config.get('aux_addresses'),
            )
            for config in ipam_dict.get('config', [])
        ],
    )
Example #19
0
def create_ipam_config_from_dict(ipam_dict):
    if not ipam_dict:
        return None

    return create_ipam_config(
        driver=ipam_dict.get("driver"),
        pool_configs=[
            create_ipam_pool(
                subnet=config.get("subnet"),
                iprange=config.get("ip_range"),
                gateway=config.get("gateway"),
                aux_addresses=config.get("aux_addresses"),
            )
            for config in ipam_dict.get("config", [])
        ],
    )
Example #20
0
    def test_create_ipam_config(self):
        ipam_pool = create_ipam_pool(subnet="192.168.52.0/24", gateway="192.168.52.254")

        ipam_config = create_ipam_config(pool_configs=[ipam_pool])
        self.assertEqual(
            ipam_config,
            {
                "Driver": "default",
                "Config": [
                    {
                        "Subnet": "192.168.52.0/24",
                        "Gateway": "192.168.52.254",
                        "AuxiliaryAddresses": None,
                        "IPRange": None,
                    }
                ],
            },
        )
Example #21
0
    def create_network(self):
        if not self.existing_network:
            params = dict(
                driver=self.parameters.driver,
                options=self.parameters.driver_options,
            )

            ipam_pools = []
            if self.parameters.ipam_config:
                for ipam_pool in self.parameters.ipam_config:
                    if HAS_DOCKER_PY_2 or HAS_DOCKER_PY_3:
                        ipam_pools.append(IPAMPool(**ipam_pool))
                    else:
                        ipam_pools.append(utils.create_ipam_pool(**ipam_pool))

            if self.parameters.ipam_driver or ipam_pools:
                # Only add ipam parameter if a driver was specified or if IPAM parameters
                # were specified. Leaving this parameter away can significantly speed up
                # creation; on my machine creation with this option needs ~15 seconds,
                # and without just a few seconds.
                if HAS_DOCKER_PY_2 or HAS_DOCKER_PY_3:
                    params['ipam'] = IPAMConfig(
                        driver=self.parameters.ipam_driver,
                        pool_configs=ipam_pools)
                else:
                    params['ipam'] = utils.create_ipam_config(
                        driver=self.parameters.ipam_driver,
                        pool_configs=ipam_pools)

            if self.parameters.enable_ipv6 is not None:
                params['enable_ipv6'] = self.parameters.enable_ipv6
            if self.parameters.internal is not None:
                params['internal'] = self.parameters.internal

            if not self.check_mode:
                resp = self.client.create_network(self.parameters.network_name,
                                                  **params)

                self.existing_network = self.client.inspect_network(resp['Id'])
            self.results['actions'].append(
                "Created network %s with driver %s" %
                (self.parameters.network_name, self.parameters.driver))
            self.results['changed'] = True
Example #22
0
    def test_create_with_ipv4_address(self):
        net_name, net_id = self.create_network(ipam=create_ipam_config(
            driver='default',
            pool_configs=[create_ipam_pool(subnet="132.124.0.0/16")],
        ), )
        container = self.client.create_container(
            image='busybox',
            command='top',
            host_config=self.client.create_host_config(network_mode=net_name),
            networking_config=self.client.create_networking_config({
                net_name:
                self.client.create_endpoint_config(ipv4_address='132.124.0.23')
            }))
        self.tmp_containers.append(container)
        self.client.start(container)

        container_data = self.client.inspect_container(container)
        self.assertEqual(
            container_data['NetworkSettings']['Networks'][net_name]
            ['IPAMConfig']['IPv4Address'], '132.124.0.23')
Example #23
0
    def test_connect_with_ipv4_address(self):
        net_name, net_id = self.create_network(ipam=create_ipam_config(
            driver='default',
            pool_configs=[
                create_ipam_pool(subnet="172.28.0.0/16",
                                 iprange="172.28.5.0/24",
                                 gateway="172.28.5.254")
            ]))

        container = self.create_and_start(
            host_config=self.client.create_host_config(network_mode=net_name))

        self.client.disconnect_container_from_network(container, net_name)
        self.client.connect_container_to_network(container,
                                                 net_name,
                                                 ipv4_address='172.28.5.24')

        container_data = self.client.inspect_container(container)
        net_data = container_data['NetworkSettings']['Networks'][net_name]
        self.assertEqual(net_data['IPAMConfig']['IPv4Address'], '172.28.5.24')
Example #24
0
    def test_connect_with_ipv6_address(self):
        net_name, net_id = self.create_network(ipam=create_ipam_config(
            driver='default',
            pool_configs=[
                create_ipam_pool(subnet="2001:389::1/64",
                                 iprange="2001:389::0/96",
                                 gateway="2001:389::ffff")
            ]))

        container = self.create_and_start(
            host_config=self.client.create_host_config(network_mode=net_name))

        self.client.disconnect_container_from_network(container, net_name)
        self.client.connect_container_to_network(container,
                                                 net_name,
                                                 ipv6_address='2001:389::f00d')

        container_data = self.client.inspect_container(container)
        net_data = container_data['NetworkSettings']['Networks'][net_name]
        self.assertEqual(net_data['IPAMConfig']['IPv6Address'],
                         '2001:389::f00d')
    def test_create_network_with_ipam_config(self):
        _, net_id = self.create_network(
            ipam=create_ipam_config(
                driver='default',
                pool_configs=[
                    create_ipam_pool(
                        subnet="172.28.0.0/16",
                        iprange="172.28.5.0/24",
                        gateway="172.28.5.254",
                        aux_addresses={
                            "a": "172.28.1.5",
                            "b": "172.28.1.6",
                            "c": "172.28.1.7",
                        },
                    ),
                ],
            ),
        )

        net = self.client.inspect_network(net_id)
        ipam = net['IPAM']

        assert ipam.pop('Options', None) is None

        assert ipam == {
            'Driver': 'default',
            'Config': [{
                'Subnet': "172.28.0.0/16",
                'IPRange': "172.28.5.0/24",
                'Gateway': "172.28.5.254",
                'AuxiliaryAddresses': {
                    "a": "172.28.1.5",
                    "b": "172.28.1.6",
                    "c": "172.28.1.7",
                },
            }],
        }
Example #26
0
    def run(self, time_add, time_to_run, peer_number, runs=1):
        """
        Run the benchmark

        :param peer_number: How many starting peers
        :param time_add: How much time before starting the benchmark
        :param time_to_run: How much time to run the benchmark for
        :param runs: RUn the benchmark how many time
        """
        time_add *= 1000
        time_to_run *= 1000
        if self.local:
            service_image = self.app_config['service']['name']
            if self.use_tracker:
                tracker_image = self.app_config['tracker']['name']
            with subprocess.Popen(['../gradlew', '-p', '..', 'docker'],
                                  stdin=subprocess.PIPE,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE,
                                  universal_newlines=True) as p:
                for line in p.stdout:
                    print(line, end='')
        else:
            service_image = (self.app_config['repository']['name'] +
                             self.app_config['service']['name'])
            self.logger.info(self.client.images.pull(service_image))

            if self.use_tracker:
                tracker_image = (self.app_config['repository']['name'] +
                                 self.app_config['tracker']['name'])
                self.logger.info(self.client.images.pull(tracker_image))

            try:
                self.client.swarm.init()
                if not self.local:
                    self.logger.info('Joining Swarm on every hosts:')
                    token = self.client.swarm.attrs['JoinTokens']['Worker']
                    subprocess.call([
                        'parallel-ssh', '-t', '0', '-h', 'config/hosts',
                        'docker', 'swarm', 'join', '--token', token,
                        '{:s}:2377'.format(self.cluster_config['manager_ip'])
                    ])
                ipam_pool = utils.create_ipam_pool(
                    subnet=self.app_config['service']['network']['subnet'])
                ipam_config = utils.create_ipam_config(
                    pool_configs=[ipam_pool])
                self.client.networks.create(
                    self.app_config['service']['network']['name'],
                    driver='overlay',
                    ipam=ipam_config)
            except errors.APIError:
                self.logger.info('Host is already part of a swarm')
                if not self.client.networks.list(
                        names=[self.app_config['service']['network']['name']]):
                    self.logger.error('Network  doesn\'t exist!')
                    exit(1)

        for run_nb, _ in enumerate(range(runs), 1):
            if self.use_tracker:
                self._create_service(
                    self.app_config['tracker']['name'],
                    tracker_image,
                    placement=['node.role == manager'],
                    mem_limit=self.app_config['service']['mem_limit'])
                self._wait_on_service(self.app_config['tracker']['name'], 1)
            time_to_start = int((time.time() * 1000) + time_add)
            self.logger.debug(
                datetime.utcfromtimestamp(time_to_start / 1000).isoformat())

            environment_vars = {
                'PEER_NUMBER': peer_number,
                'TIME': time_to_start,
                'TIME_TO_RUN': time_to_run
            }
            if 'parameters' in self.app_config['service']:
                environment_vars.update(
                    self.app_config['service']['parameters'])
            environment_vars = [
                '{:s}={}'.format(k, v) for k, v in environment_vars.items()
            ]
            self.logger.debug(environment_vars)

            service_replicas = 0 if self.churn else peer_number
            log_storage = (self.cluster_config['local_data'] if self.local else
                           self.cluster_config['cluster_data'])

            if 'mem_limit' in self.app_config['service']:
                self._create_service(
                    self.app_config['service']['name'],
                    service_image,
                    env=environment_vars,
                    mounts=[
                        types.Mount(target='/data',
                                    source=log_storage,
                                    type='bind')
                    ],
                    replicas=service_replicas,
                    mem_limit=self.app_config['service']['mem_limit'])
            else:
                self._create_service(self.app_config['service']['name'],
                                     service_image,
                                     env=environment_vars,
                                     mounts=[
                                         types.Mount(target='/data',
                                                     source=log_storage,
                                                     type='bind')
                                     ],
                                     replicas=service_replicas)

            self.logger.info(
                'Running Benchmark -> Experiment: {:d}/{:d}'.format(
                    run_nb, runs))
            if self.churn:
                thread = threading.Thread(
                    target=self._run_churn,
                    args=[time_to_start + self.churn.delay],
                    daemon=True)
                thread.start()
                self._wait_on_service(self.app_config['service']['name'],
                                      0,
                                      inverse=True)
                self.logger.info('Running with churn')
                if self.churn.synthetic:
                    # Wait for some peers to at least start
                    time.sleep(120)
                    total = [
                        sum(x)
                        for x in zip(*self.churn.churn_params['synthetic'])
                    ]
                    # Wait until only stopped containers are still alive
                    self._wait_on_service(self.app_config['service']['name'],
                                          containers_nb=total[0],
                                          total_nb=total[1])
                else:
                    # TODO not the most elegant solution
                    thread.join()  # Wait for churn to finish
                    time.sleep(300)  # Wait 5 more minutes

            else:
                self._wait_on_service(self.app_config['service']['name'],
                                      0,
                                      inverse=True)
                self.logger.info('Running without churn')
                self._wait_on_service(self.app_config['service']['name'], 0)
            self.stop()

            self.logger.info('Services removed')
            time.sleep(30)

            if not self.local:
                subprocess.call(
                    'parallel-ssh -t 0 -h config/hosts'
                    ' "mkdir -p {path}/test-{nb}/capture &&'
                    ' mv {path}/*.txt {path}/test-{nb}/ &&'
                    ' mv {path}/capture/*.csv {path}/test-{nb}/capture/"'.
                    format(path=self.cluster_config['cluster_data'],
                           nb=run_nb),
                    shell=True)

            subprocess.call('mkdir -p {path}/test-{nb}/capture'.format(
                path=log_storage, nb=run_nb),
                            shell=True)
            subprocess.call('mv {path}/*.txt {path}/test-{nb}/'.format(
                path=log_storage, nb=run_nb),
                            shell=True)
            subprocess.call(
                'mv {path}/capture/*.csv {path}/test-{nb}/capture/'.format(
                    path=log_storage, nb=run_nb),
                shell=True)

        self.logger.info('Benchmark done!')
Example #27
0
def docker_network_add(name):
    cli = get_docker_client(DOCKER_BASE_URL)
    ipam_pool =  create_ipam_pool(subnet=CALICO_NETWORK)
    ipam_config = create_ipam_config(driver="calico", pool_configs=[ipam_pool])
    result = cli.create_network(name, driver="calico", ipam=ipam_config)
    logger.info("create docker network for app %s : %s" % (name, result))
Example #28
0
def docker_network_add(name):
    cli = get_docker_client(DOCKER_BASE_URL)
    ipam_pool = create_ipam_pool(subnet=CALICO_NETWORK)
    ipam_config = create_ipam_config(driver="calico", pool_configs=[ipam_pool])
    cli.create_network(name, driver="calico", ipam=ipam_config)
Example #29
0
    def run(self, time_add, time_to_run, peer_number, runs=1):
        """
        Run the benchmark

        :param peer_number: How many starting peers
        :param time_add: How much time before starting the benchmark
        :param time_to_run: How much time to run the benchmark for
        :param runs: RUn the benchmark how many time
        """
        time_add *= 1000
        time_to_run *= 1000
        if self.local:
            service_image = self.app_config['service']['name']
            if self.use_tracker:
                tracker_image = self.app_config['tracker']['name']
            with subprocess.Popen(['../gradlew', '-p', '..', 'docker'],
                                  stdin=subprocess.PIPE,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE,
                                  universal_newlines=True) as p:
                for line in p.stdout:
                    print(line, end='')
        else:
            service_image = (self.app_config['repository']['name']
                             + self.app_config['service']['name'])
            self.logger.info(self.client.images.pull(service_image))

            if self.use_tracker:
                tracker_image = (self.app_config['repository']['name']
                                 + self.app_config['tracker']['name'])
                self.logger.info(self.client.images.pull(tracker_image))

            try:
                self.client.swarm.init()
                if not self.local:
                    self.logger.info('Joining Swarm on every hosts:')
                    token = self.client.swarm.attrs['JoinTokens']['Worker']
                    subprocess.call(
                        [
                            'parallel-ssh',
                            '-t',
                            '0',
                            '-h',
                            'config/hosts',
                            'docker',
                            'swarm',
                            'join',
                            '--token',
                            token,
                            '{:s}:2377'.format(
                                self.cluster_config['manager_ip'])])
                ipam_pool = utils.create_ipam_pool(
                    subnet=self.app_config['service']['network']['subnet'])
                ipam_config = utils.create_ipam_config(
                    pool_configs=[ipam_pool])
                self.client.networks.create(
                    self.app_config['service']['network']['name'],
                    driver='overlay', ipam=ipam_config)
            except errors.APIError:
                self.logger.info('Host is already part of a swarm')
                if not self.client.networks.list(
                        names=[self.app_config['service']['network']['name']]):
                    self.logger.error('Network  doesn\'t exist!')
                    exit(1)

        for run_nb, _ in enumerate(range(runs), 1):
            if self.use_tracker:
                self._create_service(
                    self.app_config['tracker']['name'],
                    tracker_image,
                    placement=['node.role == manager'],
                    mem_limit=self.app_config['service']['mem_limit'])
                self._wait_on_service(self.app_config['tracker']['name'], 1)
            time_to_start = int((time.time() * 1000) + time_add)
            self.logger.debug(
                datetime.utcfromtimestamp(
                    time_to_start /
                    1000).isoformat())

            environment_vars = {'PEER_NUMBER': peer_number,
                                'TIME': time_to_start,
                                'TIME_TO_RUN': time_to_run}
            if 'parameters' in self.app_config['service']:
                environment_vars.update(
                    self.app_config['service']['parameters'])
            environment_vars = [
                '{:s}={}'.format(k, v) for k, v in environment_vars.items()]
            self.logger.debug(environment_vars)

            service_replicas = 0 if self.churn else peer_number
            log_storage = (self.cluster_config['local_data']
                           if self.local
                           else self.cluster_config['cluster_data'])

            if 'mem_limit' in self.app_config['service']:
                self._create_service(
                    self.app_config['service']['name'],
                    service_image,
                    env=environment_vars,
                    mounts=[
                        types.Mount(
                            target='/data',
                            source=log_storage,
                            type='bind')],
                    replicas=service_replicas,
                    mem_limit=self.app_config['service']['mem_limit'])
            else:
                self._create_service(
                    self.app_config['service']['name'],
                    service_image,
                    env=environment_vars,
                    mounts=[
                        types.Mount(
                            target='/data',
                            source=log_storage,
                            type='bind')],
                    replicas=service_replicas)

            self.logger.info(
                'Running Benchmark -> Experiment: {:d}/{:d}'.format(
                    run_nb, runs))
            if self.churn:
                thread = threading.Thread(
                    target=self._run_churn, args=[
                        time_to_start + self.churn.delay], daemon=True)
                thread.start()
                self._wait_on_service(
                    self.app_config['service']['name'], 0, inverse=True)
                self.logger.info('Running with churn')
                if self.churn.synthetic:
                    # Wait for some peers to at least start
                    time.sleep(120)
                    total = [sum(x) for x
                             in zip(*self.churn.churn_params['synthetic'])]
                    # Wait until only stopped containers are still alive
                    self._wait_on_service(
                        self.app_config['service']['name'],
                        containers_nb=total[0],
                        total_nb=total[1])
                else:
                    # TODO not the most elegant solution
                    thread.join()  # Wait for churn to finish
                    time.sleep(300)  # Wait 5 more minutes

            else:
                self._wait_on_service(
                    self.app_config['service']['name'], 0, inverse=True)
                self.logger.info('Running without churn')
                self._wait_on_service(self.app_config['service']['name'], 0)
            self.stop()

            self.logger.info('Services removed')
            time.sleep(30)

            if not self.local:
                subprocess.call(
                    'parallel-ssh -t 0 -h config/hosts'
                    ' "mkdir -p {path}/test-{nb}/capture &&'
                    ' mv {path}/*.txt {path}/test-{nb}/ &&'
                    ' mv {path}/capture/*.csv {path}/test-{nb}/capture/"'
                    .format(path=self.cluster_config['cluster_data'],
                            nb=run_nb),
                    shell=True)

            subprocess.call(
                'mkdir -p {path}/test-{nb}/capture'.format(path=log_storage,
                                                           nb=run_nb),
                shell=True)
            subprocess.call(
                'mv {path}/*.txt {path}/test-{nb}/'.format(path=log_storage,
                                                           nb=run_nb),
                shell=True)
            subprocess.call(
                'mv {path}/capture/*.csv {path}/test-{nb}/capture/'.format(
                    path=log_storage, nb=run_nb), shell=True)

        self.logger.info('Benchmark done!')
    else:
        service_image = DISTANT_REPOSITORY + SERVICE_NAME
        tracker_image = DISTANT_REPOSITORY + TRACKER_NAME
        for line in cli.pull(service_image, stream=True, decode=True):
            print(line)
        for line in cli.pull(tracker_image, stream=True):
            print(line)
    try:
        cli.init_swarm()
        if not args.local:
            logger.info('Joining Swarm on every hosts:')
            token = cli.inspect_swarm()['JoinTokens']['Worker']
            subprocess.call(['parallel-ssh', '-t', '0', '-h', 'hosts', 'docker', 'swarm',
                             'join', '--token', token, '{:s}:2377'.format(MANAGER_IP)])
        ipam_pool = utils.create_ipam_pool(subnet=SUBNET)
        ipam_config = utils.create_ipam_config(pool_configs=[ipam_pool])
        cli.create_network(NETWORK_NAME, 'overlay', ipam=ipam_config)
    except errors.APIError:
        logger.info('Host is already part of a swarm')
        if not cli.networks(names=[NETWORK_NAME]):
            logger.error('Network  doesn\'t exist!')
            exit(1)

    for run_nb, _ in enumerate(range(args.runs), 1):
        create_service(TRACKER_NAME, tracker_image, placement={'Constraints': ['node.role == manager']})
        wait_on_service(TRACKER_NAME, 1)
        time_to_start = int((time.time() * 1000) + args.time_add)
        logger.debug(datetime.utcfromtimestamp(time_to_start / 1000).isoformat())
        environment_vars = {'PEER_NUMBER': args.peer_number, 'TIME': time_to_start,
                            'TIME_TO_RUN': args.time_to_run, 'RATE': args.rate,
                            'FIXED_RATE': args.fixed_rate}