Skip to content

Commit 27e0ad7

Browse files
authored
Fix dependencies update (#6)
* Previous code would still process the incoming mqtt message if the schema was rejected, so that was fixed. Also, the slac_timeout was included in the wrong field of the read function * Due to a bug in poetry, the username and password of the private repo needs to be specified in the url * adapted the time sleep for the ev simulator * removed timeout incorrect config * changed sleep time in ev simu * reverted username/pass change to allow CI to pass (test) * added CWD to the path to the .env file to avoid issues when starting the lib as if the module is installed in the env, environs tries to fetch the .env from the directory where it was installed and not from the one where the application was started * added option to set the path to the .env file of the system * fixed README, removed unused env settings and removed network_mode host from prod yml * Added a recipe to start the code with elevated privileges and adapted the README * added recipe to start the ev simulation of slac * reformatted code * reformatted code * removed sed from make poetry-update and refactored install-local
1 parent 5068b4f commit 27e0ad7

14 files changed

+198
-92
lines changed

.env.dev renamed to .env.dev.docker

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
MQTT_HOST=mqtt
33
MQTT_PORT=9001
44

5-
# Redis Settings
6-
REDIS_HOST=redis
7-
REDIS_PORT=6379
8-
95
# SLAC Settings
106
NETWORK_INTERFACE=eth0
117
SLAC_INIT_TIMEOUT=1000

.env.local-run renamed to .env.dev.local

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
MQTT_HOST=localhost
33
MQTT_PORT=9001
44

5-
# Redis Settings
6-
REDIS_HOST=localhost
7-
REDIS_PORT=6379
85

96
# SLAC Settings
107
NETWORK_INTERFACE=eth0

.github/workflows/pull-request.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020

2121
- name: Create env file
2222
run: |
23-
cp .env.dev .env
23+
cp .env.dev.local .env
2424
2525
- name: Code Quality Check
2626
shell: bash

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ RUN poetry update
3434
# Copy the project to the system and install all dependencies
3535
COPY . ./
3636
# The Env file must be copied to run the tests
37-
RUN mv .env.dev .env
37+
RUN mv .env.dev.docker .env
3838
RUN poetry install --no-interaction --no-ansi
3939
# Run the tests and linting for slac (as we are not using poetry venv, we are
4040
# not running it with RUN poetry run pytest...)

Makefile

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# all the recipes are phony (no files to check).
2-
.PHONY: .check-env-vars .deps docs tests build dev run update install-local run-local deploy help
2+
.PHONY: .check-env-vars .deps .pip-install docs tests build dev run update install-local run-local deploy help configure-credentials
33
.DEFAULT_GOAL := help
44

55
IS_LINUX_OS := $(shell uname -s | grep -c Linux)
@@ -14,10 +14,13 @@ help:
1414
@echo "Please use 'make <target>' where <target> is one of"
1515
@echo ""
1616
@echo " build builds the app in Docker"
17-
@echo " dev runs the app with docker-compose.dev"
18-
@echo " run-local runs the app locally"
17+
@echo " run runs the app in Docker with prod settings"
18+
@echo " dev runs the app in Docker with dev settings"
19+
@echo " run-local runs the app locally with prod settings"
20+
@echo " run-local-sudo runs the app locally using prod settings and root privileges"
1921
@echo " poetry-update updates the dependencies in poetry.lock"
2022
@echo " install-local installs slac into the current environment"
23+
@echo " set-credentials sets the Switch PyPi credentials directly into pyroject.toml. Use this recipe with caution"
2124
@echo " tests run all the tests"
2225
@echo " reformat reformats the code, using Black"
2326
@echo " flake8 flakes8 the code"
@@ -27,7 +30,7 @@ help:
2730

2831

2932
.check-os:
30-
# The @ is to surpress the output of the evaluation
33+
# The @ is to surpress the output of the evaluation
3134
@if [ ${IS_LINUX_OS} -eq 0 ]; then echo "This Recipe is not available in non-Linux Systems"; exit 3; fi
3235

3336
.check-env-vars:
@@ -55,23 +58,32 @@ run: .check-env-vars
5558
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up
5659

5760
poetry-config: .check-env-vars
58-
# For external packages, poetry saves metadata
59-
# in it's cache which can raise versioning problems, if the package
60-
# suffered version support changes. Thus, we clean poetry cache
61+
@# For external packages, poetry saves metadata
62+
@# in it's cache which can raise versioning problems, if the package
63+
@# suffered version support changes. Thus, we clean poetry cache
6164
yes | poetry cache clear --all mqtt_api
6265
poetry config http-basic.pypi-switch ${PYPI_USER} ${PYPI_PASS}
6366

67+
set-credentials: .check-env-vars
68+
@# Due to a Keyring issue under Ubuntu systems, the password configuration does not work as expected: https://github.com/python-poetry/poetry/issues/4902
69+
@# As so, instead we use sed to substitute the credentials.
70+
sed -i.bkp 's@https://pypi.switch-ev.com/simple/@https://${PYPI_USER}:${PYPI_PASS}\@pypi.switch-ev.com/simple/@g' pyproject.toml
71+
6472
poetry-update: poetry-config
6573
poetry update
6674

67-
poetry-install: poetry-update
68-
poetry install
69-
70-
install-local: poetry-install
75+
install-local: .check-env-vars
76+
pip install . --extra-index-url https://$PYPI_USER:$PYPI_PASS@pypi.switch-ev.com/simple
7177

7278
run-local:
7379
python slac/main.py
7480

81+
run-local-sudo:
82+
sudo $(shell which python) slac/main.py
83+
84+
run-ev-slac:
85+
sudo $(shell which python) slac/examples/ev_slac_scapy.py
86+
7587
mypy:
7688
mypy --config-file mypy.ini slac tests
7789

README.md

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ There are two recommended ways of running the project:
3030
$ make run-local
3131
```
3232

33-
If you follow this path, then you will need to provide the PYPI credentials
34-
and export them as ENVs:
33+
For a successful dependencies installation, you will need to provide the
34+
credentials to the Switch PyPi private server:
3535

3636
```shell
3737
$ export PYPI_USER=****
3838
$ export PYPI_PASS=****
3939
```
40+
Contact André <andre@switch-ev.com> if you have questions about it.
4041

4142
The last required step to effectively start the SLAC mechanism, is to force the
4243
detection of state `B`of the Control Pilot circuit. This project relies on
@@ -56,21 +57,75 @@ async with Client("broker.hivemq.com") as client:
5657
For more information about the MQTT API used by Switch check the following link:
5758
https://app.gitbook.com/@switch-ev/s/josev-api/
5859

59-
Finally, the project includes a few configuration variables whose default
60-
values can be modified by setting them as an environmental variable:
60+
Finally, the project includes a few configuration variables, whose default
61+
values can be modified by setting them as environmental variables.
62+
The following table provides a few of the available variables:
6163

6264
| ENV | Default Value | Description |
6365
| ----------------- | --------------------- | ------------------------------------------------------------------------------ |
6466
| NETWORK_INTERFACE | `"eth0"` | HomePlug Network Interface |
6567
| SLAC_INIT_TIMEOUT | `50` | Timeout[s] for the reception of the first slac message after state B detection |
66-
| MQTT_URL | `"broker.hivemq.com"` | MQTT Broker URL |
67-
| MQTT_USER | `None` | Username for Client Authorization |
68-
| MQTT_PASS | `None` | Password for Client Authorization |
68+
| MQTT_HOST | No Default | MQTT Broker URL |
69+
| MQTT_PORT | No Default | MQTT Broker Port |
70+
| LOG_LEVEL | `INFO` | Level of the Python log service |
6971

70-
Any of those variables can be set by exporting their value in the env:
72+
73+
The project includes a few environmental files, in the root directory, for
74+
different purposes:
75+
76+
* `.env.dev.docker` - ENV file with development settings, tailored to be used with docker
77+
* `.env.dev.local` - ENV file with development settings, tailored to be used with
78+
the local host
79+
80+
If the user runs the project locally, e.g. using `$ make install-local && make run-local`,
81+
it is required to create a `.env` file, containing the required settings.
82+
83+
This means, if development settings are desired, one can simply copy the contents
84+
of `.env.dev.local` to `.env`.
85+
86+
If Docker is used, the command `make run` will try to get the `.env` file;
87+
The command `make dev` will fetch the contents of `.env.dev.docker` - thus,
88+
in this case, the user does not need to create a `.env` file, as Docker will
89+
automatically fetch the `.env.dev.docker` one.
90+
91+
The key-value pairs defined in the `.env` file directly affect the settings
92+
present in `slac/environment.py`. In this script, the user will find all the settings that can be configured.
93+
94+
Any of those variables can also be set by exporting their value in the env:
7195

7296
`$ export NETWORK_INTERFACE=eth1`
7397

98+
99+
100+
## Known Issues and Limitation
101+
102+
1. `make install-local`or `make poetry-update` may fail, depending on your system.
103+
Poetry relies on the `Keyring` of your system and, unfortunately, this can create
104+
problems. The common outcome is that Poetry won't authenticate against the
105+
Switch private PyPi server:
106+
```shell
107+
RepositoryError
108+
403 Client Error: Forbidden for url: https://pypi.switch-ev.com/simple/flake8/
109+
```
110+
There are two possible solutions:
111+
1. Explicitly inject the credentials into pyproject.toml
112+
For that you can use the following command:
113+
`$ make set-credentials`
114+
2. Disable your Keyring for Python, following the steps on this page:
115+
https://blog.frank-mich.com/python-poetry-1-0-0-private-repo-issue-fix/
116+
117+
If you follow one of the above steps, the installation shall run smoothly.
118+
119+
2. `make run-local` may not work in your system
120+
121+
SLAC requires the use of Level 2 frames, as so, the app requires low level access to
122+
the socket interface. Such level of access is only attained with root priviliges, so
123+
if the user group that your system is using does not have root priviliges, the app will
124+
fail to run.
125+
126+
In order to run the app with root priviliges, try the following command, instead:
127+
`$ make run-local-sudo`
128+
74129
## Integration Test with an EV SLAC Simulator
75130

76131
The EVSE SLAC code can be tested against the EV counterpart. For convenience,

docker-compose.dev.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
version: '3.8'
22
services:
33
slac:
4-
env_file: .env.dev
4+
env_file: .env.dev.docker

docker-compose.prod.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
version: '3.9'
22
services:
33
slac:
4-
network_mode: host
54
env_file: .env

docker-compose.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,3 @@ services:
88
- PYPI_USER=${PYPI_USER}
99
- PYPI_PASS=${PYPI_PASS}
1010
dockerfile: Dockerfile
11-
env_file: .env.dev
12-

0 commit comments

Comments
 (0)