Skip to content

Commit

Permalink
Create Debian package (#56)
Browse files Browse the repository at this point in the history
Closes #54 

* Update package metadata

* First steps to building Debian packages

* Rename workflow

* Remove unsupported option from cargo build

* Specify directory where to download binaries

* Move comment

* Split logic over two steps

* More aggressive caching of previously built artifacts

* Use standard GitHub Action caches

* More detailed inspection of cache results

* Creating the package requires Rust ARM-target to be installed

* A bit less troubleshooting

* No need to build the project again, just the Debian package

* Do not strip the binary

This avoids re-installing cross-compilation tools

* Better inspection of intermediary results

* Verbose invocation of cargo deb

* First shot at creating a Debian service
  • Loading branch information
mthmulders authored Aug 10, 2023
1 parent 66704ad commit 360c5b2
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 32 deletions.
87 changes: 87 additions & 0 deletions .github/workflows/build-packages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
name: Build Debian packages for Raspberry Pi

on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened]

jobs:
build:
runs-on: ubuntu-latest
# Consider creating a matrix to also include armv7-unknown-linux-musleabihf

steps:
- name: Checkout sources
uses: actions/checkout@v3.5.3

- name: Set up Cargo cache
uses: actions/cache@v3.3.1
continue-on-error: false
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-cargo-

- name: Install cross-compilation tools
uses: taiki-e/setup-cross-toolchain-action@v1.15.0
with:
target: arm-unknown-linux-musleabihf

- name: Perform build
run: |
cargo build --target arm-unknown-linux-musleabihf --release --features ""
package:
runs-on: ubuntu-latest
needs: [ build ]
steps:
- name: Checkout sources
uses: actions/checkout@v3.5.3
with:
fetch-depth: 0

- name: Set up Cargo cache
uses: actions/cache@v3.3.1
continue-on-error: false
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-cargo-

- name: Inspect downloaded binaries
run: |
file target/arm-unknown-linux-musleabihf/release/dsmr-rs
- name: Prepare packaging
run: |
sudo apt update -y
sudo apt install -y dpkg dpkg-dev liblzma-dev libssl-dev
cargo install cargo-deb
rustup target add arm-unknown-linux-musleabihf
- name: Create Debian package
run: |
cargo deb --target arm-unknown-linux-musleabihf --no-build --no-strip -v
- name: Inspect generated packages
run: |
find target/ -name "*.deb" -exec file {} \;
- name: Upload Debian packages
uses: actions/upload-artifact@v3.1.2
if: github.ref == 'refs/heads/main'
with:
name: binaries
path: target/debian/
32 changes: 0 additions & 32 deletions .github/workflows/build-raspberry.yml

This file was deleted.

10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@ name = "dsmr-rs"
version = "0.1.0"
authors = ["Maarten Mulders <mthmulders@noreply.github.com>"]
edition = "2018"
description = "A utility to ship 'smart meter' readings over HTTP"
license = "MIT License"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[package.metadata.deb]
maintainer = "Maarten Mulders"
maintainer-scripts = "debian/"
copyright = "2021 - 2023 Maarten Mulders"

[package.metadata.deb.systemd-units]


[dependencies]
config = "0.13.3"
log = "0.4.19"
Expand Down
49 changes: 49 additions & 0 deletions debian/postinst
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Inspired by https://github.com/NLnetLabs/krill/blob/main/pkg/debian/postinst

#!/bin/sh
set -e

PKG_CONF="/etc/dsmr-rs.conf"
PKG_CONF_PERMS=640
PKG_USER="dsmr-rs"

create_user() {
if id ${PKG_USER} > /dev/null 2>&1; then return; fi
adduser --system --no-create-home --group ${PKG_USER}
}

create_configuration() {
if [ ! -f "${PKG_CONF}" ]; then
cat <<EOF > ${PKG_CONF}
# The DSMR-reader API to forward telegrams to
DATALOGGER_API_HOSTS=https://my.host.name

# The serial port to read telegrams from
DATALOGGER_SERIAL_PORT=/dev/ttyUSB0

# The API key to authenticate against the DSMR-reader API
DATALOGGER_API_KEYS=something-secret

# The input method for reading telegrams. Expected to always be 'serial', so effectively ignored.
DATALOGGER_INPUT_METHOD=serial

# Baudrate for reading telegrams from the serial line.
DATALOGGER_SERIAL_BAUDRATE=9600

EOF
# Ensure that the config file has the correct ownership
chown ${PKG_USER}:${PKG_USER} ${PKG_CONF}

# Ensure that the config file has the correct permissions
chmod ${PKG_CONF_PERMS} ${PKG_CONF}
fi
}

case "$1" in
configure)
create_user
create_configuration
;;
esac

#DEBHELPER#
9 changes: 9 additions & 0 deletions debian/service
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Unit]
Description=dsmr-rs

[Service]
EnvironmentFile=/etc/dsmr-rs.conf
ExecStart=/usr/bin/dsmr-rs

[Install]
WantedBy=multi-user.target

0 comments on commit 360c5b2

Please sign in to comment.