Skip to content

Commit

Permalink
Feat/update docker config (#195)
Browse files Browse the repository at this point in the history
* feat: add a retry when network interface is not ready

* feat: add max number of retries for socket binding

* feat: add a 5-second delay on EVCC start up

* feat: update Windows support

* fix: regex

* feat: add comment on tcp binding process
  • Loading branch information
santiagosalamandri authored Jan 27, 2023
1 parent c714f1a commit 9f2f02e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ build: .generate_v2_certs
@# This conversion is required, otherwise we wouldn't be able to spawn the evcc start script.
@# @ is used as a separator and allows us to escape '/', so we can substitute the '/' itself
@sed -i.bkp 's@/secc/g@/evcc/g@g' iso15118/evcc/Dockerfile
@# Add a delay on EVCC to give SECC time to start up
@sed -i'.bkp' -e 's@CMD /venv/bin/iso15118@CMD echo "Waiting for 5 seconds to start EVCC" \&\& sleep 5 \&\& /venv/bin/iso15118@g' iso15118/evcc/Dockerfile
docker-compose build

# Run using dev env vars
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ The primary dependencies to install the project are the following:
> - Linux
>
> - MacOS is not fully supported -- see "IPv6 Warning" below
> - Other non-Linux operating systems are not supported*
> - Windows is supported using [WSL](https://learn.microsoft.com/en-us/windows/wsl/install).
> - Other non-Linux operating systems are not supported
>
> - Poetry [^3]
> - Python >= 3.9
Expand Down
44 changes: 31 additions & 13 deletions iso15118/secc/transport/tcp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,38 @@ async def server_factory(self, ready_event: asyncio.Event, tls: bool) -> None:
port = self.port_tls
ssl_context = get_ssl_context(True)
server_type = "TLS"
# Initialise socket for IPv6 TCP packets
# Address family (determines network layer protocol, here IPv6)
# Socket type (stream, determines transport layer protocol TCP)
sock = socket.socket(family=socket.AF_INET6, type=socket.SOCK_STREAM)

# Allows address to be reused
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

self.full_ipv6_address = await get_link_local_full_addr(port, self.iface)
self.ipv6_address_host = self.full_ipv6_address[0]

# Bind the socket to the IP address and port for receiving
# TCP packets
sock.bind(self.full_ipv6_address)
MAX_RETRIES: int = 3
BACK_OFF_SECONDS: float = 0.5
# Note: When the socket is being created inside a container,
# sometimes the network interface is not ready yet and the binding
# process fails the first time.
# Therefore, a wait-and-retry block has been added.
for i in range(MAX_RETRIES):
# Initialise socket for IPv6 TCP packets
# Address family (determines network layer protocol, here IPv6)
# Socket type (stream, determines transport layer protocol TCP)
sock = socket.socket(family=socket.AF_INET6, type=socket.SOCK_STREAM)

# Allows address to be reused
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

self.full_ipv6_address = await get_link_local_full_addr(port, self.iface)
self.ipv6_address_host = self.full_ipv6_address[0]

# Bind the socket to the IP address and port for receiving
# TCP packets
try:
sock.bind(self.full_ipv6_address)
break
except OSError as e:
# Once the max amount of retries has been reached, reraise the exception
if i == MAX_RETRIES - 1:
raise e
else:
logger.info(f"{e} on {server_type} server. Retrying...")
await asyncio.sleep(BACK_OFF_SECONDS)
continue

server = await asyncio.start_server(
# The client_connected_cb callback, which is the __call__ method of
Expand Down
3 changes: 2 additions & 1 deletion template.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ENV PYTHONFAULTHANDLER=1 \
PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
PIP_DEFAULT_TIMEOUT=100 \
POETRY_VERSION=1.1.11 \
POETRY_VERSION=1.3.2 \
VIRTUALENV_PIP=21.2.1 \
MYPY_VERSION=0.930

Expand Down Expand Up @@ -46,6 +46,7 @@ RUN /venv/bin/pip install dist/*.whl

# Replace with in-container cert generation DevOps#2664
COPY --from=build /usr/src/app/iso15118/shared/pki/ /usr/src/app/iso15118/shared/pki/
COPY --from=build /usr/src/app/iso15118/shared/examples/evcc/iso15118_2/ /usr/src/app/iso15118/shared/examples/evcc/iso15118_2/

RUN /venv/bin/pip install aiofile
# This will run the entrypoint script defined in the pyproject.toml
Expand Down

0 comments on commit 9f2f02e

Please sign in to comment.