sml156

opkg-upgrade.sh

Nov 9th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.49 KB | None | 0 0
  1. #!/bin/sh
  2. ###############################################
  3. # Gustavo Arnosti Neves
  4. #
  5. # Created: May / 2017
  6. # Updated: Jun / 2017
  7. #
  8. # Upgrade packages listed by:
  9. # opkg list-upgradable
  10. #
  11. # Will list packages and ask for confirmation
  12. # Use ./opkg-upgrade.sh --help for more info
  13. #
  14. # This Script:
  15. # https://github.com/tavinus/opkg-upgrade
  16.  
  17.  
  18. ### Initialization
  19. OPKGUPVERSION="0.2.3"
  20. OPKGBIN="$(which opkg 2>/dev/null)"
  21. SSMTPBIN="$(which ssmtp 2>/dev/null)"
  22. BANNERSTRING="Simple OPKG Updater v$OPKGUPVERSION"
  23. TIMESTAMP="$(date '+%Y/%m/%d %H:%M:%S' 2>/dev/null)"
  24. OPKGUP_INSTALL_DIR='/usr/sbin'
  25.  
  26. ### Silly SH
  27. TRUE=0
  28. FALSE=1
  29.  
  30. ### Execution FLAGS
  31. QUIET_MODE=$FALSE
  32. CHECK_UPDATES_FLAG=$TRUE
  33. FORCE_FLAG=$FALSE
  34. JUST_CHECK_FLAG=$FALSE
  35. JUST_PRINT_FLAG=$FALSE
  36. SSMTP_SEND_FLAG=$FALSE
  37. SEND_TO=""
  38. ALWAYS_SEND_FLAG=$FALSE
  39. HTML_FORMAT=$TRUE
  40. JUST_PRINT_HTML_FLAG=$FALSE
  41.  
  42. ### This scripts name
  43. OPKGUP_NAME="$(basename $0)"
  44. OPKGUP_LOCATION="$(readlink -f $0)"
  45.  
  46. ### Execution vars, populated later
  47. PACKS=""
  48. PACKS_NAMES=""
  49. PACKS_COUNT=0
  50.  
  51.  
  52.  
  53.  
  54.  
  55. ########################### FUNCTIONS STARTS
  56.  
  57. # get opkg packages listings and upgradable info
  58. opkg_init() {
  59. check_for_opkg
  60. if should_run_update; then
  61. opkg_update
  62. else
  63. message_ends "Ignoring package lists update"
  64. fi
  65. opkg_upgradable
  66. }
  67.  
  68. # main function
  69. main() {
  70. print_banner
  71. opkg_init
  72. upgrade_check # may exit here
  73.  
  74. local uplist="$(list_upgrades)"
  75. if should_send_ssmtp || just_print_html; then
  76. if opkg_has_update || should_always_send || just_print_html; then
  77. QUIET_MODE=$FALSE
  78. local email_data=''
  79. if is_html_email; then
  80. email_data="$(print_html_email)"
  81. else
  82. email_data="$(print_txt_email "$uplist")"
  83. fi
  84. #local email_data="$(email_subject)"$'\n\n'"$(print_banner)"$'\n\n'"Report for: $(uname -n)"$'\n\n'"$uplist"$'\n\n'"Generated on: $TIMESTAMP"
  85. if just_print_html; then
  86. echo -e "$email_data"
  87. exit 0
  88. else
  89. echo -e "$email_data" | "$SSMTPBIN" "$SEND_TO"
  90. exit $?
  91. fi
  92. fi
  93. exit 0
  94. else
  95. echo -e "$uplist"
  96. fi
  97. just_print && exit 0
  98. opkg_has_update || { echo '' ; exit 0 ; }
  99.  
  100. if ! no_confirm; then
  101. if ! confirm_upgrade; then
  102. echo $'Cancelled by user!\n'
  103. exit 3
  104. fi
  105. else
  106. echo ""
  107. fi
  108. do_upgrade
  109. exit $?
  110. }
  111.  
  112. # Just checks if upgrades are available and exits with
  113. # 0 - if there are updates available
  114. # 1 - if there are NO updates available
  115. # Depends on the $JUST_CHECK_FLAG to run
  116. upgrade_check() {
  117. if just_check; then
  118. opkg_has_update && exit 0
  119. exit 1
  120. fi
  121. }
  122.  
  123. # Prints the list of upgrades available,
  124. # returns $TRUE if we have updates available, $FALSE otherwise
  125. list_upgrades() {
  126. if opkg_has_update; then
  127. echo "Packages available for upgrade: $PACKS_COUNT"$'\n'
  128. echo -e "$PACKS"
  129. return $TRUE
  130. fi
  131. echo $'No packages to install!\n\n'
  132. return $FALSE
  133. }
  134.  
  135. ### Parse CLI options
  136. get_options() {
  137. while :; do
  138. case "$1" in
  139. -V|--version|--Version)
  140. print_banner 'nopadding' ; exit 0 ;;
  141. -h|--help|--Help)
  142. print_help ; exit 0 ;;
  143. -i|--install)
  144. self_install "$2" ; exit $? ;;
  145. -u|--upgrade-check|--upgrade-Check|--upgradecheck|--upgradeCheck)
  146. QUIET_MODE=$TRUE ; JUST_CHECK_FLAG=$TRUE ; shift ;;
  147. -l|--list-upgrades|--list-Upgrades|--listupgrades|--listUpgrades)
  148. QUIET_MODE=$TRUE ; JUST_PRINT_FLAG=$TRUE ; shift ;;
  149. -e|--email-list|--email-List|--emaillist|--emailList)
  150. QUIET_MODE=$TRUE ; JUST_PRINT_HTML_FLAG=$TRUE ; shift ;;
  151. -s|--ssmtp)
  152. ssmtp_check "$2" ; shift ; shift ;;
  153. -a|--always-send|--always-Send|--alwayssend|--alwaysSend)
  154. ALWAYS_SEND_FLAG=$TRUE ; shift ;;
  155. -t|--text-only|--text-Only|--textonly|--textOnly)
  156. HTML_FORMAT=$FALSE ; shift ;;
  157. -n|--no-pkg-update)
  158. CHECK_UPDATES_FLAG=$FALSE ; shift ;;
  159. -f|--force)
  160. FORCE_FLAG=$TRUE ; shift ;;
  161. -q|--quiet)
  162. QUIET_MODE=$TRUE ; shift ;;
  163. *)
  164. check_invalid_opts "$1" ; break ;;
  165. esac
  166. done
  167. }
  168.  
  169. # In this script's case, just check we have and emty string and we should be fine
  170. check_invalid_opts() {
  171. if is_not_empty "$1"; then
  172. print_banner
  173. echo "Invalid Option: $1"$'\n'
  174. exit 2
  175. fi
  176. return 0
  177. }
  178.  
  179.  
  180.  
  181. ###### PRINTING MESSAGES
  182.  
  183. # prints message to stderr
  184. print_error() {
  185. echo "$@" >&2
  186. }
  187.  
  188. # prints program name and version
  189. print_banner() {
  190. [[ "$1" = 'error' ]] && { print_error $'\n'"$BANNERSTRING"$'\n' ; return $TRUE ; }
  191. is_quiet && ! just_print && return $TRUE
  192. local str=""
  193. if [[ "$1" = 'nopadding' ]]; then
  194. str="$BANNERSTRING"
  195. else
  196. str=$'\n'"$BANNERSTRING"$'\n'
  197. fi
  198. echo "$str"
  199. }
  200.  
  201. # prints help to screen and exits
  202. print_help() {
  203. print_banner
  204. echo "Usage: $OPKGUP_NAME [options]
  205.  
  206. Options:
  207. -V, --version Show program name and version and exits
  208. -h, --help Show this help screen and exits
  209. -i, --install [dir] Install opkg-upgrade to [dir] or /usr/sbin
  210. Leave [dir] empty for default (/usr/sbin)
  211. -u, --upgrade-check Returns SUCCESS if there are updates available
  212. Quiet execution, returns 0 or 1
  213. -l, --list-upgrades Prints the list of available updates and exits
  214. -e, --email-list Prints the list of updates in html email format
  215. Includes subject, mime type and html formated data
  216. -s, --ssmtp <email> Use the system's ssmtp to send update reports
  217. You need to install and configure ssmtp beforehand
  218. -a, --always-send Send e-mail even if there are no updates
  219. By default e-mails are only sent when updates are available
  220. -t, --text-only Send e-mail in plain text format.
  221. By default, e-mails are sent in html format.
  222. -n, --no-opkg-update Skip opkg update at the beginning,
  223. may not find packages if not up to date
  224. -f, --force Do not ask for confirmation,
  225. will update everything available
  226.  
  227. Notes:
  228. - Short options should not be grouped. You must pass each parameter on its own.
  229. - You must have a working ssmtp install to use the ssmtp functionality. Make
  230. sure you can send e-mails from it before trying from opkg-upgrade.
  231.  
  232. Examples:
  233. $OPKGUP_NAME -n -f # run without updating listings and asking for upgrade
  234. $OPKGUP_NAME --install # install to /usr/sbin/opkg-upgrade
  235. $OPKGUP_NAME -l # just print upgrades available
  236. $OPKGUP_NAME -e # just print html formatted email
  237. $OPKGUP_NAME -s '[email protected]' # mail upgrade report if have updates
  238. $OPKGUP_NAME -a -s '[email protected]' # mail upgrade report even if NO updates
  239. $OPKGUP_NAME -u && echo 'upgrades are available' || echo 'no upgrades available'
  240.  
  241. "
  242. }
  243.  
  244. # prints message to screen with trailling '\r' to change the line later
  245. message_starts() {
  246. is_quiet && return $TRUE
  247. echo -n ".... | $1"$'\r'
  248. }
  249.  
  250. # prints message to screen, closes a task message
  251. message_ends() {
  252. is_quiet && return $TRUE
  253. is_empty "$1" && mess="" || mess="$1"
  254. echo "Done | $mess"
  255. }
  256.  
  257. # checks if we have opkg available
  258. check_for_opkg() {
  259. if ! is_executable "$OPKGBIN"; then
  260. print_error $'ERROR! Could not find or run OPKG binary\n'
  261. exit 1
  262. fi
  263. }
  264.  
  265.  
  266.  
  267. ##### ERROR HANDLING
  268.  
  269. # Displays a runtinme error and exits execution
  270. rt_exception() {
  271. local r=1
  272. is_empty "$2" || r=$2
  273. print_error "$1"
  274. exit $r
  275. }
  276.  
  277.  
  278.  
  279. ###### OPKG UPDATE AND UPGRADEABLE
  280.  
  281. # update package listings
  282. opkg_update() {
  283. message_starts "Updating package lists"
  284. local err="$("$OPKGBIN" update 2>&1 >/dev/null)";
  285. is_empty "$err" || rt_exception $'Error when trying to update the package listings.\nDebug Info:\n'"$err"
  286. message_ends
  287. }
  288.  
  289. # get list of upgradable packages
  290. opkg_upgradable() {
  291. message_starts "Getting upgradable packages list"
  292. PACKS="$($OPKGBIN list-upgradable)"
  293. [ $? -eq 0 ] || rt_exception $'Error when trying list upgradable packages. Permissions?\n'
  294. #PACKS="$(cat pkg-example.txt)" # testing
  295. if ! is_empty "$PACKS"; then
  296. PACKS_NAMES="$(echo -ne "$PACKS" | awk '{ printf "%s ", $1 }')"
  297. #echo -ne "$PACKS" | awk '{ printf "pack: %s from: %s to: %s \n", $1, $3, $5 }'
  298. PACKS_COUNT="$(echo "$PACKS" | wc -l)"
  299. fi
  300. message_ends
  301. is_quiet || echo ""
  302. return $TRUE
  303. }
  304.  
  305.  
  306.  
  307. ###### CREATING EMAILS
  308.  
  309. # prints an email report in html format
  310. print_html_email() {
  311. echo "$(email_subject)""$(print_html_mime)""$(print_html_header)""$(print_html_table)""$(print_html_timestamp)"
  312. }
  313.  
  314. # prints an email report in txt format
  315. print_txt_email() {
  316. echo "$(email_subject)"$'\n\n'"$(print_banner)"$'\n\n'"Report for: $(uname -n)"$'\n\n'"$1"$'\n\n'"Generated on: $TIMESTAMP"
  317. }
  318.  
  319. # prints the packages html table
  320. print_html_table() {
  321. echo '<br>'
  322. is_empty "$PACKS" && { echo '<br><br><h3>No packages to install.</h3>' ; return $TRUE ; }
  323. local td_padding='padding-left:8px;padding-right:10px;padding-top:12px;padding-bottom:12px;'
  324. local td_open='<td style=\"'"$td_padding"'\">'
  325. local th_open='<th style="'"$td_padding"'">'
  326. echo '<table border="1" width="600px" cellpadding="0" cellspacing="0" style="border-collapse:collapse;">'
  327. echo '<tr style="background-color:#2f3263; color:#EEE;" align="left" margin=0 padding=0>'$'\n\t'"$th_open"'#</th>'$'\n\t'"$th_open"'Pack</th>'$'\n\t'"$th_open"'Current</th>'$'\n\t'"$th_open"'Update</th>'$'\n''</tr>'
  328. # most of the table is generated using awk
  329. echo -ne "$PACKS" | \
  330. awk 'BEGIN{ i=1; l=""; } { if (i % 2) l=""; else l=" style=\"background-color:#dedfe8;\""; printf "<tr margin=0 padding=0 align=\"left\"%s>\n\t'"$td_open"'%s</td>\n\t'"$td_open"'%s</td>\n\t'"$td_open"'%s</td>\n\t'"$td_open"'%s</td>\n</tr>\n", l, i++, $1, $3, $5 }'
  331. echo '</table>'
  332. return $TRUE
  333. }
  334.  
  335. # prints html email info
  336. print_html_header() {
  337. echo $'\n\n''<h2>'"$(print_banner 'nopadding')"'</h2>'
  338. echo '<h3>'"Report for: $(uname -n)"'</h3>'
  339. echo '<h3>'"Packages available for upgrade: $PACKS_COUNT"'</h3>'
  340. }
  341.  
  342. # prints html email mime type and format
  343. print_html_mime() {
  344. echo $'\nMIME-Version: 1.0\nContent-Type: text/html; charset=utf-8'
  345. }
  346.  
  347. # prints html email info footer
  348. print_html_timestamp() {
  349. echo $'\n''<h4>'"Generated on: $TIMESTAMP by "'<a href="https://github.com/tavinus/opkg-upgrade">opkg-upgrade</a></h4>'$'\n'
  350. }
  351.  
  352. # prints the email subject
  353. email_subject() {
  354. echo "Subject: opkg-upgrade report - $TIMESTAMP"
  355. }
  356.  
  357.  
  358.  
  359. ###### UPGRADING
  360.  
  361. # asks the user to confirm the upgrade
  362. confirm_upgrade() {
  363. read -p $'\nProceed with upgrade? (Y/y to proceed) ' -n 1 -r
  364. echo $'\n'
  365. if [[ "$REPLY" = Y || "$REPLY" = y ]]; then
  366. return $TRUE
  367. fi
  368. return $FALSE
  369. }
  370.  
  371. # run the upgrade
  372. do_upgrade() {
  373. message_starts $'Upgrading packages\n\n'
  374. "$OPKGBIN" install $PACKS_NAMES
  375. ret=$?
  376. message_ends $'\nUpgrade finished\n'
  377. echo $'Please check for config file conflicts!\n'
  378. return $ret
  379. }
  380.  
  381.  
  382.  
  383. ###### SELF INSTALL
  384.  
  385. # Locates and copies itself to target location and make it executable
  386. self_install() {
  387. print_banner
  388. is_empty "$1" || OPKGUP_INSTALL_DIR="$1"
  389. echo "Current Location: $OPKGUP_LOCATION"
  390. echo " Target Dir: $OPKGUP_INSTALL_DIR"
  391. if ! is_dir "$OPKGUP_INSTALL_DIR"; then
  392. print_error $'\n'"Error! Target directory not found!"
  393. print_error " -> $OPKGUP_INSTALL_DIR"
  394. exit 40
  395. fi
  396. local t="$OPKGUP_INSTALL_DIR/opkg-upgrade"
  397. local status="$(cp "$OPKGUP_LOCATION" "$t" 2>&1)"
  398. #echo "status: $status"
  399. if is_not_empty "$status"; then
  400. print_error " Could not copy: $t"
  401. print_error $'\n'"Error: $status"
  402. exit 33
  403. fi
  404. echo " Installed to: $t"
  405. chmod +x "$t" 2>/dev/null
  406. [ $? -eq 0 ] && status="OK" || status="Fail"
  407. echo "chmod executable: $status"
  408. echo $'\nInstalled with success, bye!\n'
  409. exit $?
  410. }
  411.  
  412.  
  413.  
  414. ###### VALIDATORS
  415.  
  416. # returns $TRUE if we have a valid e-mail address, $FALSE otherwise
  417. is_valid_email() {
  418. is_empty "$1" && return $FALSE
  419. local f=$(echo "$1" | grep -E -o "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$")
  420. is_empty "$f" && return $FALSE
  421. return $TRUE
  422. }
  423.  
  424. # returns $TRUE if it is a valid folder, $FALSE otherwise
  425. is_dir() {
  426. [ -d "$1" ] && return $TRUE
  427. return $FALSE
  428. }
  429.  
  430. # Returns $TRUE if the file exists and is executable, $FALSE otherwise
  431. is_executable() {
  432. [ -f "$1" ] && [ -x "$1" ] && return $TRUE
  433. return $FALSE
  434. }
  435.  
  436. # returns $TRUE for an empty var, $FALSE otherwise
  437. is_empty() {
  438. [ -z "$1" ] && return $TRUE
  439. return $FALSE
  440. }
  441.  
  442. # returns $FALSE for an empty var, $TRUE otherwise
  443. is_not_empty() {
  444. [ -z "$1" ] && return $FALSE
  445. return $TRUE
  446. }
  447.  
  448. # returns $TRUE if $PACKS is not empty
  449. opkg_has_update() {
  450. is_empty "$PACKS" && return $FALSE
  451. return $TRUE
  452. }
  453.  
  454.  
  455.  
  456. ###### OPERATION FLAGS
  457.  
  458. # returns $TRUE if we should use the HTML format, $FALSE for TXT format
  459. is_html_email() {
  460. return $HTML_FORMAT
  461. }
  462.  
  463. # returns $TRUE if we are running in quiet mode, $FALSE otherwise
  464. is_quiet() {
  465. return $QUIET_MODE
  466. }
  467.  
  468. # returns $TRUE if we should update the package listings, $FALSE otherwise
  469. should_run_update() {
  470. return $CHECK_UPDATES_FLAG
  471. }
  472.  
  473. # return $TRUE if we should ask the user before upgrading, $FALSE otherwise
  474. no_confirm() {
  475. return $FORCE_FLAG
  476. }
  477.  
  478. # returns $TRUE if we should just check for updates and exit, $FALSE otherwise
  479. just_check() {
  480. return $JUST_CHECK_FLAG
  481. }
  482.  
  483. # returns $TRUE if we should just print the list and exit, $FALSE otherwise
  484. just_print() {
  485. return $JUST_PRINT_FLAG
  486. }
  487.  
  488. # returns $TRUE if we should send email with ssmtp, $FALSE otherwise
  489. should_send_ssmtp() {
  490. return $SSMTP_SEND_FLAG
  491. }
  492.  
  493. # returns $TRUE if we should send the e-mail even if there is no updates, $FALSE otherwise
  494. should_always_send() {
  495. return $ALWAYS_SEND_FLAG
  496. }
  497.  
  498. # returns $TRUE if we should just print the html report and exit, $FALSE otherwise
  499. just_print_html() {
  500. return $JUST_PRINT_HTML_FLAG
  501. }
  502.  
  503.  
  504.  
  505. ###### SSMTP functions
  506.  
  507. # Finds and checks for ssmtp executable, returns $TRUE if found, $FALSE otherwise
  508. find_ssmtp() {
  509. is_executable "$SSMTPBIN" && return $TRUE
  510. SSMTPBIN='/usr/sbin/ssmtp'
  511. is_executable "$SSMTPBIN" && return $TRUE
  512. return $FALSE
  513. }
  514.  
  515. # Checks for ssmtp program, validates the target email and sets globals for emails
  516. ssmtp_check() {
  517. if ! find_ssmtp; then
  518. print_banner 'error'
  519. print_error "Error! Could not find or run the SSMTP executable, make sure it is installed!"
  520. exit 30
  521. fi
  522. if ! is_valid_email "$1"; then
  523. print_banner 'error'
  524. print_error "Error! You need to specify a valid target e-mail address!"
  525. print_error "Invalid address -> $1"
  526. exit 30
  527. fi
  528. SSMTP_SEND_FLAG=$TRUE
  529. SEND_TO="$1"
  530. QUIET_MODE=$TRUE
  531. }
  532.  
  533.  
  534.  
  535.  
  536.  
  537. ###### START EXECUTION
  538.  
  539. get_options "$@"
  540. main
  541.  
  542. exit 20 # should never get here
Advertisement
Add Comment
Please, Sign In to add comment