Skip to content

Commit

Permalink
Add acap-ssh-utils bin and lib crate (#19)
Browse files Browse the repository at this point in the history
This is primarily intended as a building block for a more tightly
integrated development tool, where it will come in handy for
implementing analogs of `cargo run` and `cargo test`, both of which
will cross-compile a binary and execute it on target. But, since it
solves a common problem among ACAP developers using any language, it is
factored out into its own lib crate with a thin binary crate wrapper.

The crate is focused around the package as a unit that is patched and
run. Since every ACAP developer have systems set up to produce `.eap`
files I expect this to be an accessible interface. An earlier version
allowed individual files to be replaced. This was driven by working
with build systems, including my own, that did not package tests as
`.eap` files or were slow to produce `.eap` files. I now think that
these issues are better addressed in the build system since the action
of creating the tar archive should in theory be cheap. 

In `install-system-packages.sh`:
- Add `sshpass` because it is needed by `acap-ssh-utils` which in turn
  needs it because I have not found a way to add authorized keys for
  users other than root.

In `.gitignore`:
- Add `/.env` because defining the `AXIS_DEVICE_*` variables in a file
  makes it reduces the friction of rebuilding the dev container and to
  run tests.

In `Makefile`:
- Export the `AXIS_*` variables to make sure they are available for
  scripts when the default values are used. This is particularly useful
  for the password argument since it allows programs such as
  `acap-ssh-utils` to pick this up from the environment, which is
  better from a security perspective (that tool still passes it as an
  argument to other commands though).
- Always set `RUST_LOG` and `RUST_LOG_STYLE` when running on target
  because I found that most of the time this is what I wanted and
  oftentimes I did not want to set it for `cargo-acap-build` and
  `acap-ssh-utils` at the same time.
  • Loading branch information
apljungquist authored Jul 18, 2024
1 parent 9b0350d commit 4400657
Show file tree
Hide file tree
Showing 15 changed files with 775 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .devhost/install-system-packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ apt-get install \
iputils-ping \
libglib2.0-dev \
pkg-config \
python3-venv
python3-venv \
sshpass
3 changes: 2 additions & 1 deletion .devhost/install-venv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ rm /tmp/node-v18.16.1-linux-x64.tar.gz
# Install `devcontainer` into venv
npm install -g @devcontainers/[email protected]

# Install `cargo-acap-build` into venv
# Install packages from this project into venv
cargo install --root ${VIRTUAL_ENV} --target-dir /tmp/target --path ../crates/acap-ssh-utils
cargo install --root ${VIRTUAL_ENV} --target-dir /tmp/target --path ../crates/cargo-acap-build

rm -r /tmp/target
Expand Down
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*
!/.devhost/
!/crates/acap-ssh-utils
!/crates/cargo-acap-build
!/rust-toolchain.toml
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.env
/.idea/
/init_env.sh
/target/
179 changes: 176 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 18 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
# Name of package containing the app to be built.
# Rust does not enforce that the path to the package matches the package name, but
# this makefile does to keep things simple.
AXIS_PACKAGE ?= hello_world
export AXIS_PACKAGE ?= hello_world

# The architecture that will be assumed when interacting with the device.
AXIS_DEVICE_ARCH ?= aarch64
export AXIS_DEVICE_ARCH ?= aarch64

# The IP address of the device to interact with.
AXIS_DEVICE_IP ?= 192.168.0.90
export AXIS_DEVICE_IP ?= 192.168.0.90

# The username to use when interacting with the device.
export AXIS_DEVICE_USER ?= root

# The password to use when interacting with the device.
AXIS_DEVICE_PASS ?= pass
export AXIS_DEVICE_PASS ?= pass

# Other
# -----
Expand Down Expand Up @@ -91,9 +94,11 @@ stop:
## * The device is added to `knownhosts`.
run:
cargo-acap-build --target $(AXIS_DEVICE_ARCH) -- -p $(AXIS_PACKAGE)
scp target/$(AXIS_DEVICE_ARCH)/$(AXIS_PACKAGE)/$(AXIS_PACKAGE) root@$(AXIS_DEVICE_IP):/usr/local/packages/$(AXIS_PACKAGE)/$(AXIS_PACKAGE)
ssh root@$(AXIS_DEVICE_IP) \
"cd /usr/local/packages/$(AXIS_PACKAGE) && su - acap-$(AXIS_PACKAGE) -s /bin/sh --preserve-environment -c '$(if $(RUST_LOG_STYLE),RUST_LOG_STYLE=$(RUST_LOG_STYLE) )$(if $(RUST_LOG),RUST_LOG=$(RUST_LOG) )./$(AXIS_PACKAGE)'"
acap-ssh-utils patch target/$(AXIS_DEVICE_ARCH)/$(AXIS_PACKAGE)/*.eap
acap-ssh-utils run-app \
--environment RUST_LOG=debug \
--environment RUST_LOG_STYLE=always \
$(AXIS_PACKAGE)

## Build and execute unit tests for <AXIS_PACKAGE> on <AXIS_DEVICE_IP> assuming architecture <AXIS_DEVICE_ARCH>
##
Expand All @@ -113,9 +118,11 @@ test:
# The `scp` command below needs the wildcard to match exactly one file.
rm -r target/$(AXIS_DEVICE_ARCH)/$(AXIS_PACKAGE)-*/$(AXIS_PACKAGE) ||:
cargo-acap-build --target $(AXIS_DEVICE_ARCH) -- -p $(AXIS_PACKAGE) --tests
scp target/$(AXIS_DEVICE_ARCH)/$(AXIS_PACKAGE)-*/$(AXIS_PACKAGE) root@$(AXIS_DEVICE_IP):/usr/local/packages/$(AXIS_PACKAGE)/$(AXIS_PACKAGE)
ssh root@$(AXIS_DEVICE_IP) \
"cd /usr/local/packages/$(AXIS_PACKAGE) && su - acap-$(AXIS_PACKAGE) -s /bin/sh --preserve-environment -c '$(if $(RUST_LOG_STYLE),RUST_LOG_STYLE=$(RUST_LOG_STYLE) )$(if $(RUST_LOG),RUST_LOG=$(RUST_LOG) )./$(AXIS_PACKAGE)'"
acap-ssh-utils patch target/$(AXIS_DEVICE_ARCH)/$(AXIS_PACKAGE)-*/*.eap
acap-ssh-utils run-app \
--environment RUST_LOG=debug \
--environment RUST_LOG_STYLE=always \
$(AXIS_PACKAGE)

## Checks
## ------
Expand All @@ -137,6 +144,7 @@ check_build:
cargo-acap-build \
--target aarch64 \
-- \
--exclude acap-ssh-utils \
--exclude cargo-acap-build \
--workspace

Expand Down
9 changes: 9 additions & 0 deletions apps/inspect_env/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "inspect_env"
version = "1.0.0"
edition.workspace = true

[dependencies]
log = { workspace = true }

acap-logging = { workspace = true }
Loading

0 comments on commit 4400657

Please sign in to comment.