Esempio n. 1
0
def sanitize_container_endpoint(url):
    if not container.is_container():
        return url
    parsed = urlparse.urlparse(url)

    if parsed.hostname in ['127.0.0.1', 'localhost']:
        url = _set_to_container_ip(parsed)

    return url
Esempio n. 2
0
def connect(self, address):
    if is_container():
        if isinstance(address, tuple) and is_localhost(address[0]):
            # handle AF_INET and AF_INET6
            address = list(address)
            address[0] = get_container_ip()
            address = tuple(address)
        elif isinstance(address, basestring) and is_localhost(address):
            # handle AF_UNIX
            address = get_container_ip()
    getattr(_socketobject, '_connect')(self, address)
Esempio n. 3
0
def mongo_init(self,
               host=None,
               port=None,
               max_pool_size=100,
               document_class=dict,
               tz_aware=False,
               _connect=True,
               **kwargs):
    if container.is_container():
        if host in ('localhost', '127.0.0.1', '::1'):
            host = container.get_container_ip()
    return MongoClient.__old_init__(self, host, port, max_pool_size,
                                    document_class, tz_aware, _connect,
                                    **kwargs)
Esempio n. 4
0
def postgres_connect(dsn=None,
                     connection_factory=None,
                     cursor_factory=None,
                     **kwargs):
    container_ip = container.get_container_ip()
    if dsn is not None and container.is_container(
    ) and container_ip is not None:
        match = re.search(POSSIBLE_HOSTS_REGEX, dsn)
        if match is not None:
            to_replace = match.group(0)
            new_host = "host=%s" % container_ip
            dsn = dsn.replace(to_replace, new_host)
    return psycopg2._old_connect(dsn, connection_factory, cursor_factory,
                                 **kwargs)
Esempio n. 5
0
def check_output(cmd):
    if is_container():
        cid = get_container_id()
        client = docker.from_env(
            assert_hostname=False,
            version="auto",
            timeout=5,
        )
        target = client.containers.get(cid)
        return target.exec_run(cmd)

    elif is_patched():
        return getattr(subprocess, '_check_output')(cmd)

    else:
        return subprocess.check_output(cmd)
Esempio n. 6
0
    tar_info.mtime = time.time()
    tar_info.mode = 0755

    tar.addfile(tar_info, jar_bytes)
    tar.close()
    tar_bytes.seek(0)

    return tar_bytes


jar_file = '/opt/dataloop/embedded/lib/jmxquery.jar'
is_docker_env = False

try:
    from outlyer.plugin_helper.container import is_container, get_container_id
    if is_container():
        is_docker_env = True
        import docker
        client = docker.from_env(assert_hostname=False,
                                 version="auto",
                                 timeout=5)
        target = client.containers.get(get_container_id())
        target.put_archive('/tmp', build_tar(*read_jar()))
        jar_file = '/tmp/jmxquery.jar'
except ImportError:
    pass

p1 = get_metrics()
time.sleep(SAMPLE_TIME)
p2 = get_metrics()
Esempio n. 7
0
 def test_not_container_when_no_container_id(self):
     self.env.unset('CONTAINER_ID')
     self.assertFalse(container.is_container())
Esempio n. 8
0
 def test_is_container_when_container_id(self):
     self.env.set('CONTAINER_ID', CONTAINER_ID)
     self.assertTrue(container.is_container())