Advertisement
zefie

OpenWRT build helper script for local router repo

Jan 3rd, 2019
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.88 KB | None | 0 0
  1. #!/bin/bash
  2. # Script assumes that you have set up an SSH key with and authorized it on the router.
  3. # If your key requires a passphrase, use an agent. Otherwise, prepare to manually auth alot.
  4.  
  5. # Where are we (should be OpenWRT dev folder)
  6. Z_WRT=/home/zefie/dev/openwrt
  7.  
  8. # Where is opkg-utils? (https://git.yoctoproject.org/cgit/cgit.cgi/opkg-utils)
  9. Z_OPKG="${Z_WRT}/opkg-utils"
  10.  
  11. # Where on the Router should we put our repo?
  12. Z_DEST=/root/.localopkg/
  13.  
  14. # Where is the router on the network?
  15. Z_ROUTER=192.168.11.1
  16.  
  17. # What model (as defined by OpenWRT) are we building for? (Used for pkg directory)
  18. Z_MODEL=ipq806x
  19.  
  20. # What packages should we import? [Local repo does not have priority over OpenWRT Public repo by default]
  21. Z_PACKAGES=(
  22.     tgt
  23.     luci-theme-rosy
  24. )
  25.  
  26. # Temporary staging directory (removed after task unless script is run with any argument)
  27. # Should not need to change
  28. Z_STAGE="zstaging"
  29.  
  30. # Vanity style to make our notice stand out from other text
  31. Z_PRE=" * "
  32.  
  33. ### END CONFIG ###
  34.  
  35. if [ ! -d "${Z_WRT}/staging_dir/packages/${Z_MODEL}" ]; then
  36.     echo "${Z_PRE}ERR: Package directory does not exist. Did you build?"
  37.     exit;
  38. fi
  39.  
  40.  
  41. Z_KEY="${Z_WRT}/key-build"
  42. echo "${Z_PRE}Staging mini local repo..."
  43. rm -rf "${Z_WRT}/${Z_STAGE}"
  44. mkdir "${Z_WRT}/${Z_STAGE}"
  45. for P in ${Z_PACKAGES[@]}; do
  46.     cp "${Z_WRT}/staging_dir/packages/${Z_MODEL}/${P}_"*.ipk "${Z_WRT}/${Z_STAGE}";
  47.     RES=$?
  48.     if [ ${RES} -ne 0 ]; then
  49.         echo "${Z_PRE}${P}: Failed";
  50.     else
  51.         echo "${Z_PRE}${P}: Success";
  52.     fi
  53. done;
  54. #cd "${Z_WRT}/${Z_STAGE}"
  55.  
  56. if [ $(ls -1 | wc -l) -eq 0 ]; then
  57.     echo "${Z_PRE}ERR: No packages in staging directory... nothing more to do.";
  58. else
  59.     "${Z_OPKG}/opkg-make-index" -f "${Z_WRT}/${Z_STAGE}" > "${Z_WRT}/${Z_STAGE}/Packages"
  60.     gzip -9nc "${Z_WRT}/${Z_STAGE}/Packages" > "${Z_WRT}/${Z_STAGE}/Packages".gz
  61.     "${Z_WRT}/staging_dir/host/bin/usign" -S -m "${Z_WRT}/${Z_STAGE}/Packages" -s "${Z_KEY}" -x "${Z_WRT}/${Z_STAGE}/Packages.sig"
  62.     RES=$?
  63.     if [ ${RES} -ne 0 ]; then
  64.         echo "${Z_PRE}Signing failed";
  65.     else
  66.         echo "${Z_PRE}Signing successful";
  67.     fi
  68.     #cd ..
  69.     echo "${Z_PRE}Staging to ${Z_ROUTER}"
  70.     Z_RES=$(ssh "root@${ROUTER}" -- ls -1 "${Z_DEST}/" 2>&1)
  71.     if [ $(echo "${Z_RES}" | grep -i "No such file" | wc -l) -eq 0 ]; then
  72.         echo "${Z_PRE} - Clearing old repo"
  73.         ssh "root@${Z_ROUTER}" -- rm -rf "${Z_DEST}"
  74.     fi
  75.     echo "${Z_PRE} - Staging new repo"
  76.     ssh "root@${Z_ROUTER}" -- mkdir "${Z_DEST}"
  77.     scp -r "${Z_WRT}/${Z_STAGE}/"* "root@${Z_ROUTER}:${Z_DEST}"
  78.     Z_KEYID=$("${Z_WRT}/staging_dir/host/bin/usign" -F -s "${Z_KEY}")
  79.     if [ $(echo "${Z_RES}" | grep -i "No such file" | wc -l) -gt 0 ]; then
  80.         echo "${Z_PRE}Staging local build key ${Z_KEYID} to ${Z_ROUTER}"
  81.         scp -r "${Z_KEY}.pub" "root@${Z_ROUTER}:/etc/opkg/keys/${Z_KEYID}"
  82.     else
  83.         echo "${Z_PRE}Local build key ${Z_KEYID} exists on ${Z_ROUTER}"
  84.     fi
  85. fi
  86.  
  87. if [ -z "${1}" ]; then
  88.     rm -rf "${Z_STAGE}"
  89. fi
  90. echo "${Z_PRE}Done"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement