Beispiel #1
0
 def _render_with_template(self, template, **kwargs):
     template = Template(template)
     return template.render(request=request,
                            timestamp=time.time(),
                            datetime=time.ctime(),
                            context=_ContextHelper(),
                            error=self.error,
                            replay=self.request_replay,
                            own_container_id=docker_helper.get_current_container_id(),
                            read_config=docker_helper.read_configuration,
                            **kwargs)
Beispiel #2
0
    def _send_signal_in_swarm(self):
        current_container_id = get_current_container_id()
        if not current_container_id:
            return

        current_container = self.client.containers.get(current_container_id)
        if not current_container:
            return

        image = current_container.attrs['Config'].get('Image')
        if not image:
            return

        command = [sys.executable, __file__, '--label', self.label_name]

        log_driver = current_container.attrs['HostConfig']['LogConfig']['Type']
        log_config = current_container.attrs['HostConfig']['LogConfig'][
            'Config']

        sender = self.client.services.create(
            image=image,
            command=command,
            name='domain-automation-signal-%d-%d' %
            (int(time.time() * 1000), random.randint(100, 999)),
            env=['PYTHONPATH=%s' % os.environ.get('PYTHONPATH', '.')],
            log_driver=log_driver,
            log_driver_options=log_config,
            mode=ServiceMode('global'),
            restart_policy=RestartPolicy(condition='none', max_attempts=0),
            mounts=['/var/run/docker.sock:/var/run/docker.sock:ro'])

        max_wait = 60
        start_time = time.time()

        while abs(time.time() - start_time) < max_wait:
            if all(task['DesiredState'] == 'shutdown'
                   for task in sender.tasks()):
                break

            time.sleep(1)

        states = list(task['Status']['State'] for task in sender.tasks())
        logs = ''.join(
            item.decode() if hasattr(item, 'decode') else item
            for item in sender.logs(stdout=True, stderr=True)).strip()

        sender.remove()

        logger.info('Signalled containers with label %s - result: %s' %
                    (self.label_name, ', '.join(map(str, states))))

        if logs:
            logger.info('Signal logs: %s' % logs)
Beispiel #3
0
    def test_log_with_docker_helper(self):
        actions = [{'log': {
            'message': 'Running in {{ own_container_id }} '
                       'with environment: {{ read_config("TESTING_ENV") }}'
        }}]

        os.environ['TESTING_ENV'] = 'webhook-proxy-testing'

        output = self._invoke(actions)

        expected = 'Running in %s with environment: webhook-proxy-testing' % docker_helper.get_current_container_id()

        self.assertEqual(output, expected)
    def test_log_with_docker_helper(self):
        actions = [{
            "log": {
                "message":
                "Running in {{ own_container_id }} "
                'with environment: {{ read_config("TESTING_ENV") }}'
            }
        }]

        os.environ["TESTING_ENV"] = "webhook-proxy-testing"

        output = self._invoke(actions)

        expected = ("Running in %s with environment: webhook-proxy-testing" %
                    docker_helper.get_current_container_id())

        self.assertEqual(output, expected)
Beispiel #5
0
    def test_mock_container_id_with_cgroup_parent(self):
        with tempfile.NamedTemporaryFile(mode='w') as tmp:
            tmp.write("""
10:perf_event:/custom-cgroup/48b20f6cb5ea9d19407984994b312e85cfa4fce2fe5aac7d0b9e38ce86d9bd53
9:cpuset:/custom-cgroup/48b20f6cb5ea9d19407984994b312e85cfa4fce2fe5aac7d0b9e38ce86d9bd53
8:pids:/system.slice/docker.service/custom-cgroup/48b20f6cb5ea9d19407984994b312e85cfa4fce2fe5aac7d0b9e38ce86d9bd53
7:devices:/system.slice/docker.service/custom-cgroup/48b20f6cb5ea9d19407984994b312e85cfa4fce2fe5aac7d0b9e38ce86d9bd53
6:net_cls,net_prio:/custom-cgroup/48b20f6cb5ea9d19407984994b312e85cfa4fce2fe5aac7d0b9e38ce86d9bd53
5:blkio:/system.slice/docker.service/custom-cgroup/48b20f6cb5ea9d19407984994b312e85cfa4fce2fe5aac7d0b9e38ce86d9bd53
4:freezer:/custom-cgroup/48b20f6cb5ea9d19407984994b312e85cfa4fce2fe5aac7d0b9e38ce86d9bd53
3:memory:/system.slice/docker.service/custom-cgroup/48b20f6cb5ea9d19407984994b312e85cfa4fce2fe5aac7d0b9e38ce86d9bd53
2:cpu,cpuacct:/system.slice/docker.service/custom-cgroup/48b20f6cb5ea9d19407984994b312e85cfa4fce2fe5aac7d0b9e38ce86d9bd53
1:name=systemd:/system.slice/docker.service/custom-cgroup/48b20f6cb5ea9d19407984994b312e85cfa4fce2fe5aac7d0b9e38ce86d9bd53
            """)
            tmp.flush()

            self.assertEqual(
                helper.get_current_container_id(read_from=tmp.name),
                '48b20f6cb5ea9d19407984994b312e85cfa4fce2fe5aac7d0b9e38ce86d9bd53'
            )
Beispiel #6
0
    def test_mock_container_id(self):
        with tempfile.NamedTemporaryFile(mode='w') as tmp:
            tmp.write("""
10:perf_event:/docker/0aefd0c98f1af9b7fd5c09d576e5840ee9e798136b4a953b001587b1f0496adf
9:cpuset:/docker/0aefd0c98f1af9b7fd5c09d576e5840ee9e798136b4a953b001587b1f0496adf
8:pids:/docker/0aefd0c98f1af9b7fd5c09d576e5840ee9e798136b4a953b001587b1f0496adf
7:devices:/docker/0aefd0c98f1af9b7fd5c09d576e5840ee9e798136b4a953b001587b1f0496adf
6:net_cls,net_prio:/docker/0aefd0c98f1af9b7fd5c09d576e5840ee9e798136b4a953b001587b1f0496adf
5:blkio:/docker/0aefd0c98f1af9b7fd5c09d576e5840ee9e798136b4a953b001587b1f0496adf
4:freezer:/docker/0aefd0c98f1af9b7fd5c09d576e5840ee9e798136b4a953b001587b1f0496adf
3:memory:/docker/0aefd0c98f1af9b7fd5c09d576e5840ee9e798136b4a953b001587b1f0496adf
2:cpu,cpuacct:/docker/0aefd0c98f1af9b7fd5c09d576e5840ee9e798136b4a953b001587b1f0496adf
1:name=systemd:/docker/0aefd0c98f1af9b7fd5c09d576e5840ee9e798136b4a953b001587b1f0496adf
            """)
            tmp.flush()

            self.assertEqual(
                helper.get_current_container_id(read_from=tmp.name),
                '0aefd0c98f1af9b7fd5c09d576e5840ee9e798136b4a953b001587b1f0496adf'
            )
Beispiel #7
0
    def self(self):
        self_id = get_current_container_id()

        for service in self:
            if service.tasks.matching(self_id):
                return service
Beispiel #8
0
    def self(self):
        self_id = get_current_container_id()

        if self_id:
            return self.matching(self_id).first_value
Beispiel #9
0
    def test_own_container_id(self):
        app = pygen.PyGen(template='#cid={{ own_container_id }}')
        container_id = docker_helper.get_current_container_id()

        self.assertEqual('cid=%s' % container_id, app.generate())
Beispiel #10
0
def get_template_variables():
    return {
        'own_container_id': docker_helper.get_current_container_id(),
        'read_config': docker_helper.read_configuration
    }
Beispiel #11
0
    def test_container_id(self):
        self.assertIsNotNone(helper.get_current_container_id())

        for digit in helper.get_current_container_id():
            self.assertIn(digit, 'abcdef0123456789')