forked from testcontainers/testcontainers-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
56 lines (41 loc) · 1.37 KB
/
Copy pathutils.py
File metadata and controls
56 lines (41 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
import sys
import subprocess
LINUX = "linux"
MAC = "mac"
WIN = "win"
def os_name():
pl = sys.platform
if pl == "linux" or pl == "linux2":
return LINUX
elif pl == "darwin":
return MAC
elif pl == "win32":
return WIN
def is_mac():
return MAC == os_name()
def is_linux():
return LINUX == os_name()
def is_windows():
return WIN == os_name()
def inside_container():
"""
Returns true if we are running inside a container.
https://github.com/docker/docker/blob/a9fa38b1edf30b23cae3eade0be48b3d4b1de14b/daemon/initlayer/setup_unix.go#L25
"""
return os.path.exists('/.dockerenv')
def default_gateway_ip():
"""
Returns gateway IP address of the host that testcontainer process is
running on
https://github.com/testcontainers/testcontainers-java/blob/3ad8d80e2484864e554744a4800a81f6b7982168/core/src/main/java/org/testcontainers/dockerclient/DockerClientConfigUtils.java#L27
"""
cmd = ["sh", "-c", "ip route|awk '/default/ { print $3 }'"]
try:
process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
ip_address = process.communicate()[0]
if ip_address and process.returncode == 0:
return ip_address.decode('utf-8').strip().strip('\n')
except subprocess.SubprocessError:
return None