Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor build process #59

Merged
merged 16 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 53 additions & 91 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,114 +1,76 @@
# Packet Forwarder Docker File
# (C) Nebra Ltd 2019
# (C) Nebra Ltd 2021
# Licensed under the MIT License.

####################################################################################################
################################## Stage: builder ##################################################
########################### Stage: PktFwd Python App Builder #######################################
FROM balenalib/raspberry-pi-debian-python:buster-build-20210705 as pktfwd-builder

FROM balenalib/raspberry-pi-debian:buster-build as builder
# Variables used internally to this stage
ENV INPUT_DIR=/opt/input

# Move to correct working directory
WORKDIR /opt/iotloragateway/dev
# Variables to be referenced pktfwd-runner stage
ENV OUTPUT_DIR=/opt/output/pktfwd-dependencies

# Copy python dependencies for `pip install` later
WORKDIR "$INPUT_DIR"

# Copy python dependencies for `pip3 install` later
COPY requirements.txt requirements.txt

# This will be the path that venv uses for installation below
ENV PATH="/opt/iotloragateway/dev/venv/bin:$PATH"

# Install build tools
# hadolint ignore=DL3008
RUN apt-get update && \
apt-get -y install --no-install-recommends \
automake \
libtool \
autoconf \
git \
ca-certificates \
pkg-config \
build-essential \
python3 \
python3-pip \
python3-venv && \
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

During a recent engineering call, we decided not to use venv moving forward. The overall feeling is that Docker is already portable so venv is an unnecessary level of encapsulation. One advantage of the venv is that the python version is also copied.

# Because the PATH is already updated above, this command creates a new venv AND activates it
python3 -m venv /opt/iotloragateway/dev/venv && \
# Given venv is active, this `pip` refers to the python3 variant
pip install --no-cache-dir -r requirements.txt

# Copy the buildfiles and sx1302 concentrator fixes
COPY buildfiles buildfiles
COPY sx1302fixes sx1302fixes

# Clone the lora gateway and packet forwarder repos
RUN git clone https://github.com/NebraLtd/lora_gateway.git
RUN git clone https://github.com/NebraLtd/packet_forwarder.git

# Create folder needed by packetforwarder compiler
RUN mkdir -p /opt/iotloragateway/packetforwarder

# Compile for sx1301 concentrator on all the necessary SPI buses
RUN ./buildfiles/compileSX1301.sh spidev0.0
RUN ./buildfiles/compileSX1301.sh spidev0.1
RUN ./buildfiles/compileSX1301.sh spidev1.0
RUN ./buildfiles/compileSX1301.sh spidev1.1
RUN ./buildfiles/compileSX1301.sh spidev1.2
RUN ./buildfiles/compileSX1301.sh spidev2.0
RUN ./buildfiles/compileSX1301.sh spidev2.1
RUN ./buildfiles/compileSX1301.sh spidev32766.0

# Compile for sx1302 concentrator
RUN ./buildfiles/compileSX1302.sh

# No need to cleanup the builder
RUN pip3 install --target="$OUTPUT_DIR" --no-cache-dir -r requirements.txt

####################################################################################################
################################### Stage: runner ##################################################
###################################################################################################
################################## Stage: runner ##################################################
FROM balenalib/raspberry-pi-debian-python:buster-run-20210705 as pktfwd-runner

FROM balenalib/raspberry-pi-debian:buster-run as runner
ENV ROOT_DIR=/opt

# Start in sx1301 directory
WORKDIR /opt/iotloragateway/packet_forwarder/sx1301
# Copy from: Locations of build assets within images of earlier stages
ENV SX1301_PACKET_FORWARDER_OUTPUT_DIR=/opt/output
ENV SX1302_HAL_OUTPUT_DIR=/opt/output
ENV PKTFWD_BUILDER_OUTPUT_DIR=/opt/output/pktfwd-dependencies

# Install python3-venv and python3-rpi.gpio
# hadolint ignore=DL3008
RUN apt-get update && \
apt-get -y install \
python3-venv \
python3-rpi.gpio && \
apt-get autoremove -y && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Copy to: Locations build assets from earlier stages/source are copied into
ENV SX1301_DIR="$ROOT_DIR/sx1301"
ENV SX1302_DIR="$ROOT_DIR/sx1302"
ENV PYTHON_APP_DIR="$ROOT_DIR/pktfwd"
ENV PYTHON_DEPENDENCIES_DIR="$ROOT_DIR/pktfwd-dependencies"
ENV SX1301_RESET_LGW_FILEPATH="$SX1301_DIR/reset_lgw.sh"

# Copy sx1301 packetforwader from builder
COPY --from=builder /opt/iotloragateway/packetforwarder .
# Variables required for pktfwd python app
ENV SX1301_REGION_CONFIGS_DIR="$PYTHON_APP_DIR/config/lora_templates_sx1301"
ENV SX1302_REGION_CONFIGS_DIR="$PYTHON_APP_DIR/config/lora_templates_sx1302"
ENV SX1302_LORA_PKT_FWD_FILEPATH="$SX1302_DIR/lora_pkt_fwd"
ENV UTIL_CHIP_ID_FILEPATH="$SX1302_DIR/chip_id"
# The sx1302_hal concentrator script requires reset_lgw to be in this location
ENV RESET_LGW_FILEPATH="$SX1302_DIR/reset_lgw.sh"

# Copy sx1301 regional config templates
COPY lora_templates_sx1301 lora_templates_sx1301/
WORKDIR "$ROOT_DIR"

# Use EU config as initial default
COPY lora_templates_sx1301/local_conf.json local_conf.json
COPY lora_templates_sx1301/EU-global_conf.json global_conf.json
# Copy python app
COPY pktfwd/ "$PYTHON_APP_DIR"

# Move to sx1302 directory
WORKDIR /opt/iotloragateway/packet_forwarder/sx1302
# Copy reset script
COPY reset_lgw.sh "$RESET_LGW_FILEPATH"

# Copy sx1302 hal from builder
COPY --from=builder /opt/iotloragateway/dev/sx1302_hal-2.1.0 .
# Copy sx1301 lora_pkt_fwd_SPI_BUS
# hadolint ignore=DL3022
COPY --from=nebraltd/packet_forwarder:e8f24fe37ba555e5ad1ddf8eed26d0136f30f8de "$SX1301_PACKET_FORWARDER_OUTPUT_DIR" "$SX1301_DIR"

# Copy sx1302 regional config templates
COPY lora_templates_sx1302 lora_templates_sx1302/
# Copy sx1302 chip_id, reset_lgw, and lora_pkt_fwd
# hadolint ignore=DL3022
COPY --from=nebraltd/sx1302_hal:69811057222f6f9cf8929ebfdb7fc6e36cc2618d "$SX1302_HAL_OUTPUT_DIR" "$SX1302_DIR"

# Use EU config as initial default
COPY lora_templates_sx1302/local_conf.json packet_forwarder/local_conf.json
COPY lora_templates_sx1302/EU-global_conf.json packet_forwarder/global_conf.json
# Copy pktfwd python app dependencies
COPY --from=pktfwd-builder "$PKTFWD_BUILDER_OUTPUT_DIR" "$PYTHON_DEPENDENCIES_DIR"

# Move to main packet forwarder directory and copy source code
WORKDIR /opt/iotloragateway/packet_forwarder
COPY files/* .
# Cleanup
RUN apt-get autoremove -y && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

# Copy venv from builder and update PATH to activate it
COPY --from=builder /opt/iotloragateway/dev/venv /opt/iotloragateway/dev/venv
ENV PATH="/opt/iotloragateway/dev/venv/bin:$PATH"
# Add python dependencies to PYTHONPATH
ENV PYTHONPATH="${PYTHONPATH}:${PYTHON_DEPENDENCIES_DIR}"

# Run run_pkt script
ENTRYPOINT ["sh", "/opt/iotloragateway/packet_forwarder/run_pkt.sh"]
# Run pktfwd/__main__.py
ENTRYPOINT ["python3", "pktfwd"]
67 changes: 64 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
# hm-pktfwd
Helium Miner Packet Forwarder

This compiles the packet forwarder container on the Nebra Miners.
This is a Python app that uses prebuilt utilities to detect the correct concentrator chip and region, then start the concentrator accordingly.

# Supported Region Plans
hm-pktfwd builds off three other repos which each built a portion of the code required to run the packet forwarder.

- [lora_gateway](https://github.com/NebraLtd/lora_gateway)
- [packet_forwarder](https://github.com/NebraLtd/packet_forwarder)
- [sx1302_hal](https://github.com/NebraLtd/sx1302_hal)

## reset_lgw.sh
`reset_lgw.sh` is a shared tool that is used on all concentrator chip versions.
On sx1301 chips, [its is recommended](https://github.com/NebraLtd/lora_gateway#31-reset_lgwsh) that the script is run before each time the concentrator is started.
On chips that use sx1302_hal, the reset script is [run automatically](https://github.com/NebraLtd/sx1302_hal/blob/3d73e6af43535f700ff7b6c2b49cc79d388cd70f/packet_forwarder/src/lora_pkt_fwd.c#L1656-L1662) when the concentrator starts and is expected to be located in the same directory as the `lora_pkt_fwd` module.

reset_lgw is used by all concentrators, and inspired by the [upstream](https://github.com/NebraLtd/lora_gateway/blob/971c52e3e0f953102c0b057c9fff9b1df8a84d66/reset_lgw.sh)
[versions](https://github.com/NebraLtd/sx1302_hal/blob/6324b7a568ee24dbd9c4da64df69169a22615311/tools/reset_lgw.sh).
That said, it is different from the originals, context specific to hm-pktfwd, and moved to this repo to avoid confusion about its intention.
Additional context [here](https://github.com/NebraLtd/sx1302_hal/pull/1#discussion_r733253225).

## Supported Region Plans

You can typically find the exact region plan you need to use at [What Helium Region](https://whatheliumregion.xyz/) or on the [Helium Miner GitHub repo](https://github.com/helium/miner/blob/master/priv/countries_reg_domains.csv) however the table below provides a rough guide...

Expand All @@ -27,10 +43,55 @@ Please note:
| --- | --- |
| CN779 | NOT YET SUPPORTED |

## Pre built containers
## Customization

The following environment variables control various aspects of the program's operation.

|Variable|Default|Required|Description|
| --- | --- | --- | --- |
| VARIANT| - | Yes | [See variants](https://github.com/NebraLtd/hm-pyhelper/blob/f8b2d8ceb90cfcd1da658a73e3741cc6de2ff1ff/hm_pyhelper/hardware_definitions.py#L1) |
| SX1301_REGION_CONFIGS_DIR | - | Yes | Path to [sx1301 configs](https://github.com/NebraLtd/hm-pktfwd/tree/900925b5bb3eab6c51cdabe24a59fede3fc85fe5/pktfwd/config/lora_templates_sx1301) |
| SX1302_REGION_CONFIGS_DIR | - | Yes | Path to [sx1302 configs](https://github.com/NebraLtd/hm-pktfwd/tree/900925b5bb3eab6c51cdabe24a59fede3fc85fe5/pktfwd/config/lora_templates_sx1302) |
| UTIL_CHIP_ID_FILEPATH | - | Yes | Path to [chip_id](https://github.com/NebraLtd/sx1302_hal/tree/69811057222f6f9cf8929ebfdb7fc6e36cc2618d/util_chip_id |
| RESET_LGW_FILEPATH | - | Yes | Path to [reset.sh](https://github.com/NebraLtd/hm-pktfwd/blob/900925b5bb3eab6c51cdabe24a59fede3fc85fe5/reset_lgw.sh). The same file is used for all sx130x versions. |
| ROOT_DIR | - | Yes | Directory the app will be run from. Should be the same location. `global_conf.json` will also be copied here. |
| SX1302_LORA_PKT_FWD_FILEPATH | - | Yes | Path to built [sx1302 lora_pkt_fwd](https://github.com/NebraLtd/sx1302_hal/blob/69811057222f6f9cf8929ebfdb7fc6e36cc2618d/packet_forwarder/src/lora_pkt_fwd.c) executable. |
| SX1301_LORA_PKT_FWD_DIR | - | Yes | Directory that contains [sx1301 lora_pkt_fwd](https://github.com/NebraLtd/packet_forwarder/tree/e8f24fe37ba555e5ad1ddf8eed26d0136f30f8de/lora_pkt_fwd) executables for all SPI buses. |
| LORA_PKT_FWD_BEFORE_CHECK_SLEEP_SECONDS | 5 | No | Duration after starting lora_pkt_fwd before establishing if it started successfully. |
| LORA_PKT_FWD_AFTER_SUCCESS_SLEEP_SECONDS | 30 | No | Duration to poll status after concentrator starts successfully. |
| LORA_PKT_FWD_AFTER_FAILURE_SLEEP_SECONDS | 2 | No | Duration to wait before restarting when concentrator exits with 0. If it exits with code greater than 0, program exits and container restarts. |
| LOGLEVEL | DEBUG | No | TRACE, DEBUG, INFO, WARN, etc. |
| REGION_FILEPATH | /var/pktfwd/region | No | Path where hm-miner [writes the region](https://github.com/NebraLtd/hm-miner/blob/8819d5439dc23b45a905ff126078aa59c5be3de8/gen-region.sh#L9). |
| DIAGNOSTICS_FILEPATH | /var/pktfwd/diagnostics | No | File containing "true" or "false" for whether lora_pkt_fwd is successfully running or not. |
| AWAIT_SYSTEM_SLEEP_SECONDS | 5 | No | How long [app sleeps](https://github.com/NebraLtd/hm-pktfwd/issues/63) before starting concentrator. |
| SENTRY_KEY | False | No | Key for Sentry. Sentry inactive if key is False. |
| REGION_OVERRIDE | False | No | Region override. eg `US915`. |
| BALENA_ID | From Balena | No | Only used with Sentry. |
| BALENA_APP_NAME | From Balena | No | Only used with Sentry. |


## Building

### Pre built containers

This repo automatically builds docker containers and uploads them to two repositories for easy access:
- [hm-pktfwd on DockerHub](https://hub.docker.com/r/nebraltd/hm-pktfwd)
- [hm-pktfwd on GitHub Packages](https://github.com/NebraLtd/hm-pktfwd/pkgs/container/hm-pktfwd)

The images are tagged using the docker long and short commit SHAs for that release. The current version deployed to miners can be found in the [helium-miner-software repo](https://github.com/NebraLtd/helium-miner-software/blob/production/docker-compose.yml).

### Manual build

When developing, it is faster to build locally instead of relying on the pre-built container to generate.

```bash
# Cross-compile
docker buildx build --platform linux/arm64/v8 --progress=plain -t DOCKERHUB_USER/hm-pktfwd .

# To stop at an intermediary stage
docker buildx build --platform linux/arm64/v8 --progress=plain --target pktfwd-builder -t pktfwd-builder .

# Tag and push image
docker image tag docker.io/DOCKERHUB_USER/hm-pktfwd DOCKERHUB_USER/hm-pktfwd:0.0.X
docker push DOCKERHUB_USER/hm-pktfwd:0.0.X
```
16 changes: 0 additions & 16 deletions buildfiles/compileSX1301.sh

This file was deleted.

26 changes: 0 additions & 26 deletions buildfiles/compileSX1302.sh

This file was deleted.

Loading