tavinus / opkg-upgrade

List and install OpenWRT / LEDE opkg upgradable packages
323 stars 61 forks source link

Love this script - cron+email #1

Closed txcanyon closed 7 years ago

txcanyon commented 7 years ago

Masterful work. I wonder if it is possible to have it email you when there are updates. For example say I set a cron job to check for updates every Friday. Instead of sshing into the box and running your script manually, it could email me the list of update-able packages.

tavinus commented 7 years ago

Hi! Interesting idea.
I have no experience on mailing from OpenWRT/LEDE, do you currently use one of the solutions available?

From this article, our choices seem to be:

All of them would have options with or without SSL.

Seems a bit tricky to include the mailer into opkg-upgrade.
It would probably be easier to just create flags to check and list.

So the user can configure whatever mailer they want and use something like this:

opkg-upgrade --hasUpgrade && opkg-upgrade --list | mail -s "opkg-upgrade list $(date '+%Y-%m-%d_%H:%M:%S')" receiver_address@gmail.com 

(not so sure about the mail call, not tested)

What this does:

  1. Check if we have upgrades
  2. If yes, send email with the list of upgrades

So, then we need:

Having these options would make it possible to do what you want (I think).
But as mentioned, you would need to configure the mailer yourself and also create the cronjob.

References:

Let me know what you think

Cheers! Gus

tavinus commented 7 years ago

BIG MAYBE on sending mails with telnet, as described here:

This would increase the complexity of opkg-upgrade quite a lot, but it would be portable and it would not depend on external packages. Not sure if feasible yet though.

Also forgot to mention the package mini-sendmail, but that has no auth support and seems kind of deprecated (and not available for ChaosCalmer).

I will keep researching, but for now I will probably just implement the two flags mentioned above and let the user do the mailing part.

EDIT: My test router is currently with other system and the other one I can use to test stuff currently has no extroot and I can't really install much else there (to test e-mail or even opkg-upgrade). I may finally set up a x86 VM for easier development. Anyways, bear with me, may take a while, suggestions welcome in the meantime.

tavinus commented 7 years ago

Hi, I have a working version with the mentioned modes of operation and also the option to use ssmtp directly from opkg-upgrade. You would still need to install and configure the ssmtp package. I had some trouble to make it work with gmail (had no enable unsecure apps, etc), but worked in the end. After you have a working ssmtp config installed, it is as easy as calling:

opkg-upgrade -s 'mail@example.com'

And the report will be sent to mail@example.com, but only if there are new updates.
You can force it to always send the e-mail (even when no updates are available) with the -a flag, as in:

opkg-upgrade -a -s 'mail@example.com'

I still need to do some more testing and to clean up the code. I was thinking of also having the option to send an HTML e-mail, but maybe I will leave that for a future version.

As mentioned, I have also implemented the -u, --upgrade-check and -l, --list-upgrades modes, so it is possible to also use those with any mail client with something like:

opkg-upgrade -u && opkg-upgrade -l | ssmtp 'mail@example.com'

I will see if it is worthy to also implement the other available mailers.
@txcanyon , do you currently use any mailer already?

txcanyon commented 7 years ago

I have ssmtp configured now for using a gmail account I have for my server related emails. I have successfully sent a test message from my LEDE install.

Once you post the new code I'll give it a go.

tavinus commented 7 years ago

Cool! At least you are using ssmtp with gmail, so this should be a breeze..
It was my mother's birthday today so... u know.. had to separate some time to her...

tavinus commented 7 years ago

anyways,... I have a cool html email version for ya. in 1 or 2 days it will be there... (I am a bit drunk to finish it today)

tavinus commented 7 years ago

I have done the html email, this is an example of it: opkg-list

txcanyon commented 7 years ago

Looks great!

tavinus commented 7 years ago

@txcanyon , I have pushed v0.2.0 to the master branch.
Can you try it please?
~I will make a release after testing a bit more and finishing the readme.~

To send the email report, just use:

mail upgrade report only if have new updates

# opkg-upgrade -s 'mail@example.com'

mail upgrade report even if there are no new updates

# opkg-upgrade -a -s 'mail@example.com'

Please adjust the script name to your local install name/location.

After you have tested that, you can just use that on your cron line.
It is probably a good a idea to use the full path on cron, like: /usr/sbin/opkg-upgrade.
By default, there is no output when running it, unless there is an error.

Cheers! Gus

txcanyon commented 7 years ago

Not getting the html output to show-up however that may be a byproduct of having no updates available. Is the following supposed to be shown?:

root@Router:~# ./opkg-upgrade.sh -e -a -s xxxxxx@gmail.com
Subject: opkg-upgrade report - 2017/06/25 19:49:57
MIME-Version: 1.0
Content-Type: text/html; charset=utf-8

<h2>Simple OPKG Updater v0.2.0</h2>
<h3>Report for: Router</h3>
<h3>Packages available for upgrade: 1</h3><br>
<br><br><h3>No packages to install.</h3>
<h4>Generated on: 2017/06/25 19:49:57 by <a href="https://github.com/tavinus/opkg-upgrade">opkg-upgrade</a></h4>

Note I do not get an email when using the -e flag

Also getting this error when running the script from commandline:

Proceed with upgrade? (Y/y to proceed)

tavinus commented 7 years ago

You should never really use the -e option with the -s option.

-e can also be used to test/see the output of the email without actually sending it.

So as I posted above, try using just this:

./opkg-upgrade.sh -a -s 'mail@example.com'

It should send an email even if there are no updates.
Instead of the table with packages you will get the message 'No packages to install'.
I have just realized that it counts as 1 package though (I'll fix that).

And then, are you being asked if you want to continue the upgrade even when sending e-mails?
Not sure I understand, but I don't get that here...

txcanyon commented 7 years ago

Ok understood. Looks like it's working as planned. I just won't see anything until there's an actual update to a package.

The Y/y was just a question if it should be Y/n instead. And only happens when running from the command line with no switches.

tavinus commented 7 years ago

Just pushed v0.2.2 with two fixes:

You will receive a message with 'No packages to install.' if using the -a option. If you want it to send e-mails only when updates are available, just remove the -a option.

About the confirmation message, it works and reads as it says.
Y and y will update, anything else will cancel.
You made me notice that it was asking that even when no updates were available though, fixed.

~EDIT I have upgraded stuff and now I can't push from there because it went back to dropbear's ssh. I will fix and push soon.~ done! had to --force-reinstall the package openssh-client for git to use it again.

Please let me know if you have any problems.
v0.2.3 released with a minor fix.

Cheers! Gus

txcanyon commented 7 years ago

Works great! I made a cronjob for a quick test and it also works.

Thanks for your hard work. I would definitely let LEDE and OpenWrt users know what you've created. I am sure there are lots who would use this as well.

tavinus commented 7 years ago

Cool! Thanks for the feedback and suggestions.

I wish there was an easier way to resolve config file conflicts though.
I may create some functionality for helping with that.

For now I can suggest this little script for using git diff to compare config files.
It is a lot better than the diff program in diffutils and if git is already there, no need for extra packages.

You use it like this:

# diff-git /etc/config/dhcp /etc/config/dhcp-opkg

Even so, can still be annoying to decide if should merge/discard/migrate to a new config file.

txcanyon commented 7 years ago

Just had an update come through. Script works as expected. Nice and neat html table.

I think you can close this enhancement now as a rousing success!

tavinus commented 7 years ago

Thx for the feedback.
Glad it works! :)

Things are a bit busy right now, but I can think of many ways to improve this functionality.
I am pretty glad on what could be done in a couple of days anyways.

And for people wanting to use other mailers, the option -e will print the e-mail so you can pipe it to your mailer (or to a temporary file you can use). If you want to send e-mail only when new updates are available you will need a -u check before. To speed things up the second call can include the -n parameter, since the package list will be updated on the first call anyways.
So, it would be something like this:

opkg-upgrade -u && opkg-upgrade -n -e | othermailer 'mailer@example.com' 

For plain-text e-mails, there is the -t flag. Plain text flag works for both -e and -s.

If by any reason the email string provided is not good for anyone, there is still the option of using the -l flag to just list the packages.

thanks @txcanyon for all the ideas and feedback, feel free to post here even though I am closing this.

Cheers! Gus