Skip to content

Commit

Permalink
container: added 'ip_address' property to container (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisTimperley authored Feb 19, 2018
1 parent 47e9b79 commit be687d2
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion bugzoo/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import time

from tempfile import NamedTemporaryFile
from typing import List, Iterator, Dict, Optional
from typing import List, Iterator, Dict, Optional, Union
from timeit import default_timer as timer
from ipaddress import IPv4Address, IPv6Address

from bugzoo.core import Language
from bugzoo.cmd import ExecResponse, PendingExecResponse
Expand Down Expand Up @@ -118,6 +119,27 @@ def container(self):
"""
return self.__container

@property
def ip_address(self,
raise_error: bool = False
) -> Optional[Union[IPv4Address, IPv6Address]]:
"""
The IP address used by this container, or None if no IP address.
"""
# TODO: refactor!
api_client = docker.APIClient(base_url='unix://var/run/docker.sock')
container_info = api_client.inspect_container(container.id)
address = container_info['NetworkSettings']['IPAddress']
try:
return IPv4Address(address)
except ipaddress.AddressValueError:
try:
return IPv6Address(address)
except ipaddress.AddressValueError:
if raise_error:
raise
return None

@property
def alive(self) -> bool:
"""
Expand Down

0 comments on commit be687d2

Please sign in to comment.