#!/bin/bash
# Script assumes that you have set up an SSH key with and authorized it on the router.
# If your key requires a passphrase, use an agent. Otherwise, prepare to manually auth alot.
# Where are we (should be OpenWRT dev folder)
Z_WRT=/home/zefie/dev/openwrt
# Where is opkg-utils? (https://git.yoctoproject.org/cgit/cgit.cgi/opkg-utils)
Z_OPKG="${Z_WRT}/opkg-utils"
# Where on the Router should we put our repo?
Z_DEST=/root/.localopkg/
# Where is the router on the network?
Z_ROUTER=192.168.11.1
# What model (as defined by OpenWRT) are we building for? (Used for pkg directory)
Z_MODEL=ipq806x
# What packages should we import? [Local repo does not have priority over OpenWRT Public repo by default]
Z_PACKAGES=(
tgt
luci-theme-rosy
)
# Temporary staging directory (removed after task unless script is run with any argument)
# Should not need to change
Z_STAGE="zstaging"
# Vanity style to make our notice stand out from other text
Z_PRE=" * "
### END CONFIG ###
if [ ! -d "${Z_WRT}/staging_dir/packages/${Z_MODEL}" ]; then
echo "${Z_PRE}ERR: Package directory does not exist. Did you build?"
exit;
fi
Z_KEY="${Z_WRT}/key-build"
echo "${Z_PRE}Staging mini local repo..."
rm -rf "${Z_WRT}/${Z_STAGE}"
mkdir "${Z_WRT}/${Z_STAGE}"
for P in ${Z_PACKAGES[@]}; do
cp "${Z_WRT}/staging_dir/packages/${Z_MODEL}/${P}_"*.ipk "${Z_WRT}/${Z_STAGE}";
RES=$?
if [ ${RES} -ne 0 ]; then
echo "${Z_PRE}${P}: Failed";
else
echo "${Z_PRE}${P}: Success";
fi
done;
#cd "${Z_WRT}/${Z_STAGE}"
if [ $(ls -1 | wc -l) -eq 0 ]; then
echo "${Z_PRE}ERR: No packages in staging directory... nothing more to do.";
else
"${Z_OPKG}/opkg-make-index" -f "${Z_WRT}/${Z_STAGE}" > "${Z_WRT}/${Z_STAGE}/Packages"
gzip -9nc "${Z_WRT}/${Z_STAGE}/Packages" > "${Z_WRT}/${Z_STAGE}/Packages".gz
"${Z_WRT}/staging_dir/host/bin/usign" -S -m "${Z_WRT}/${Z_STAGE}/Packages" -s "${Z_KEY}" -x "${Z_WRT}/${Z_STAGE}/Packages.sig"
RES=$?
if [ ${RES} -ne 0 ]; then
echo "${Z_PRE}Signing failed";
else
echo "${Z_PRE}Signing successful";
fi
#cd ..
echo "${Z_PRE}Staging to ${Z_ROUTER}"
Z_RES=$(ssh "root@${ROUTER}" -- ls -1 "${Z_DEST}/" 2>&1)
if [ $(echo "${Z_RES}" | grep -i "No such file" | wc -l) -eq 0 ]; then
echo "${Z_PRE} - Clearing old repo"
ssh "root@${Z_ROUTER}" -- rm -rf "${Z_DEST}"
fi
echo "${Z_PRE} - Staging new repo"
ssh "root@${Z_ROUTER}" -- mkdir "${Z_DEST}"
scp -r "${Z_WRT}/${Z_STAGE}/"* "root@${Z_ROUTER}:${Z_DEST}"
Z_KEYID=$("${Z_WRT}/staging_dir/host/bin/usign" -F -s "${Z_KEY}")
if [ $(echo "${Z_RES}" | grep -i "No such file" | wc -l) -gt 0 ]; then
echo "${Z_PRE}Staging local build key ${Z_KEYID} to ${Z_ROUTER}"
scp -r "${Z_KEY}.pub" "root@${Z_ROUTER}:/etc/opkg/keys/${Z_KEYID}"
else
echo "${Z_PRE}Local build key ${Z_KEYID} exists on ${Z_ROUTER}"
fi
fi
if [ -z "${1}" ]; then
rm -rf "${Z_STAGE}"
fi
echo "${Z_PRE}Done"
Comments