fix: address coderabbitai and CrazyWolf13 review feedback

- Remove inline comments from var_ declarations in ct/companion.sh
- Replace dual python3 JSON calls with single jq query parsing both
  RELEASE and ASSET_URL from the same package object, preventing
  version/URL mismatch across different package entries
- Add validation for both RELEASE and ASSET_URL (previously only
  ASSET_URL was checked in install script)
- Move version file from /opt/companion_version.txt to ~/.companion
  per project convention
- Use fetch_and_deploy_from_url framework helper instead of manual
  curl/tar/rm in both update_script and install script
- Remove redundant dependencies (curl, sudo, mc, python3); add jq
- Reload udev rules after copying (udevadm control --reload-rules &&
  udevadm trigger) so USB permissions apply immediately
- Replace manual apt-get autoremove/autoclean with cleanup_lxc
This commit is contained in:
Grant Labutis
2026-03-23 14:31:46 -04:00
parent 5987230f99
commit 7970b1b9e0
2 changed files with 25 additions and 70 deletions

View File

@@ -15,47 +15,31 @@ update_os
msg_info "Installing Dependencies"
$STD apt-get install -y \
curl \
sudo \
mc \
python3 \
jq \
libusb-1.0-0
msg_ok "Installed Dependencies"
msg_info "Fetching Latest Bitfocus Companion Release"
RELEASE_JSON=$(curl -fsSL "https://api.bitfocus.io/v1/product/companion/packages?limit=20")
RELEASE=$(echo "$RELEASE_JSON" | python3 -c "
import sys, json
data = json.load(sys.stdin)
for pkg in data.get('packages', data if isinstance(data, list) else []):
if pkg.get('target') == 'linux-tgz':
print(pkg.get('version', ''))
break
")
ASSET_URL=$(echo "$RELEASE_JSON" | python3 -c "
import sys, json
data = json.load(sys.stdin)
for pkg in data.get('packages', data if isinstance(data, list) else []):
if pkg.get('target') == 'linux-tgz':
print(pkg.get('uri', ''))
break
")
if [[ -z "$ASSET_URL" ]]; then
msg_error "Could not locate a Linux x64 release from the Bitfocus API."
PACKAGE_JSON=$(echo "$RELEASE_JSON" | jq -c '(if type == "array" then . else .packages end) | [.[] | select(.target=="linux-tgz" and (.uri | contains("linux-x64")))] | first')
RELEASE=$(echo "$PACKAGE_JSON" | jq -r '.version // empty')
ASSET_URL=$(echo "$PACKAGE_JSON" | jq -r '.uri // empty')
if [[ -z "$RELEASE" || -z "$ASSET_URL" ]]; then
msg_error "Could not resolve a matching Linux x64 Companion package from the Bitfocus API."
exit 1
fi
msg_ok "Found Companion v${RELEASE}"
msg_info "Downloading Bitfocus Companion v${RELEASE}"
mkdir -p /opt/companion
curl -fsSL "$ASSET_URL" -o /tmp/companion.tar.gz
$STD tar -xzf /tmp/companion.tar.gz -C /opt/companion --strip-components=1
rm -f /tmp/companion.tar.gz
fetch_and_deploy_from_url "$ASSET_URL" "/opt/companion"
msg_ok "Downloaded and Extracted Bitfocus Companion v${RELEASE}"
msg_info "Installing udev Rules"
[[ -f /opt/companion/50-companion-headless.rules ]] && cp /opt/companion/50-companion-headless.rules /etc/udev/rules.d/
if [[ -f /opt/companion/50-companion-headless.rules ]]; then
cp /opt/companion/50-companion-headless.rules /etc/udev/rules.d/
udevadm control --reload-rules
udevadm trigger
fi
msg_ok "Installed udev Rules"
msg_info "Creating companion User"
@@ -90,12 +74,8 @@ EOF
systemctl enable -q --now companion
msg_ok "Created Service"
echo "${RELEASE}" >/opt/companion_version.txt
echo "${RELEASE}" >~/.companion
motd_ssh
customize
msg_info "Cleaning up"
$STD apt-get -y autoremove
$STD apt-get -y autoclean
msg_ok "Cleaned"
cleanup_lxc