Script to automatically update FreePBX modules

Original post removed due to being based on a copyrighted script – my apologies to Schmooze.com! Thanks for pointing that out, Philippe.

#####################################################################################

Copyright 2012 Schmooze Com, Inc. This upgrade script and all concepts are property of

Schmooze Com, Inc. and are not to be copied for other use. This upgrade script is free

to use for upgrading FreePBX Distro systems only but carries no guarnatee on performance

and is used at your own risk. This script carries NO WARRANTY.

#####################################################################################

If anyone from Schmooze.com is reading this, how do you feel about a script that uses the concepts of the two lines below plus logging the output being posted as a module update script?

sudo -u asterisk /var/lib/asterisk/bin/module_admin upgradeall
sudo -u asterisk /var/lib/asterisk/bin/module_admin reload

Regards,
Tim Miller Dyck

Tim,

I think you are making this more complicated to people then it needs to be. Also, I think there may be an issue with what you posted.

First off, if all you want to do is upgrade all the modules you simply need to type:

sudo -u asterisk /var/lib/asterisk/bin/module_admin upgradeall

Then if you want those to take affect all you have to do is type:

sudo -u asterisk /var/lib/asterisk/module_admin reload

Most of the rest of the script is not necessary other than corner case situations like for new language translations to take affect which would need apache restarted.

But … speaking of that script, if I am not mistaken, that script is copyrighted to Schmooze. If you are going to post it here, I think you should probably do a couple things. First, probably check with Schmooze if that is ok. Second, keep their copyright with the script you are posting.

As a general rule, putting something like this here will usually result in more bad then good. The standard commands that I listed are maintained by FreePBX and are kept up-to-date. What is in scripts that come on a distro tend to me a lot more fragile.

You’ll also run into problems sometimes if you do the above with modules like the sysadmin module which may require up-to-date RPMs to also be installed and the upgrades you listed above do none of that where as the Distro scripts would make sure to pull any necessary RPMs as part of that particular upgrade.

Anyhow … for now, can you go back and at least add any Copyright that you may have found with that script and it would probably be courteous to check with Schmooze if they are ok leaving it up. Thanks! and thanks for trying to help others!

1 Like

Appreciate the copyright alert – I missed that.

You’ll also run into problems sometimes if you do the above with modules like the sysadmin module which may require up-to-date RPMs to also be installed and the upgrades you listed above do none of that where as the Distro scripts would make sure to pull any necessary RPMs as part of that particular upgrade.

Thanks for mentioning that as a possible dependency.

Would the same dependency breakage occur if someone where to update FreePBX modules through the normal Module Admin web page? I’m trying just to automate that module update process, assuming that is safe to do.

As the distro update script is much more invasive (Asterisk core updates, OS package updates, etc.), I would think that would always be done by hand by the admin.

Regards,
Tim

other than speciality modules, there should typically be no outside dependencies that are not handled some how in module admin.

Using the listed command uses the identical functions as doing it in the GUI, so they are the same.

Tim

I have no issues with that but based on what Philippe had stated in the original post you can use what we do as it handles a few errors we saw in the past we just ask that our copyright stay in place.

Thanks for asking

EDIT: SEE THE BOTTOM OF THIS THREAD FOR THE BEST VERSION OF THIS SCRIPT

Thank you for that generosity, Tony!

Based on these two statements below, I wasn’t sure if the right approach was to keep the extra commands in the script (chown, restart Apache, etc.) or just call “module_admin upgradeall” followed by “module_admin reload” and not the other commands.

Philippe mentioned:
The standard commands that I listed are maintained by FreePBX and are kept up-to-date. What is in scripts that come on a distro tend to me a lot more fragile.

Tony mentioned:
but based on what Philippe had stated in the original post you can use what we do as it handles a few errors we saw in the past

Here is a revised script to have something concrete to see:

... EDIT: SEE THE BOTTOM OF THIS THREAD FOR THE BEST VERSION OF THIS SCRIPT ...

Technically you can remove the

sudo -u asterisk /var/lib/asterisk/bin/retrieve_conf > /dev/null

that was put in for some bugs we ran into with older 2.7 or 2.8 days of FreePBX. The module_admin calls the restrieve_conf

Thanks for that update, Jason.

I was curious about your experience because this script has been working fine for me through crond. I checked /etc/sudoers on two FreePBX Distro 2.10 servers and found that the default configuration there was to have this line commented out entirely:

...

Disable "ssh hostname sudo ", because it will show the password in clear.

You have to run "ssh -t hostname sudo ".

#Defaults requiretty


In the above configuration, the module update script works OK through crond.

Both these servers are FreePBX Distro 2.10-based installs, and were that way from the start of their life (that is, not upgraded from FreePBX Distro 2.9).

Are you maybe using a different base distribution, or a different version of FreePBX Distro?

Regards,
Tim Miller Dyck

Thanks Tony. Here is revised script:

==== Create the module upgrade script ====

vi /usr/local/bin/update_freepbx_modules.sh #!/bin/sh

Automatically upgrade all FreePBX modules to the current repository versions without user intervention

The commands below perform the same steps as done if manually upgrading all FreePBX modules using the FreePBX “Module Administration” web GUI page

USAGE

- The script is intended to be run as a cron job but can be run manually at any time as well

- Output to stdout is avoided to not generate cron job output e-mails unless modules are actually updated, in which case (if run through

a cron job, an e-mail alert should be sent)

WARNINGS

- It is possible that some FreePBX modules may also require related package updates (for example, the sysadmin module),

which will not be performed by this script. FreePBX Distro as a whole should be updated using the full update scripts

posted at the “stable-release-versions” URL below.

- Tested only with FreePBX Distro 2.10

COPYRIGHT

- This script is based on FreePBX Distro full upgrade scripts at

http://www.freepbx.org/forum/freepbx-distro/distro-discussion-help/stable-release-versions

- The scripts are copyright by Schmooze Com, Inc. Used here by permission from Tony Lewis, Schmooze Com, Inc., as per the comment at

http://www.freepbx.org/forum/freepbx/tips-and-tricks/script-to-automatically-update-freepbx-modules#comment-49483

Original copyright notice reproduced from the FreePBX Distro upgrade scripts:

#####################################################################################

Copyright 2012 Schmooze Com, Inc. This upgrade script and all concepts are property of

Schmooze Com, Inc. and are not to be copied for other use. This upgrade script is free

to use for upgrading FreePBX Distro systems only but carries no guarnatee on performance

and is used at your own risk. This script carries NO WARRANTY.

#####################################################################################

Many thanks to Schmooze Com, Inc. for their great work on FreePBX and FreePBX Distro!

get the current version for use in the output log file name

version=cat /etc/asterisk/freepbxdistro-version
logfile=/var/log/pbx/upgrade/"$version"-moduleupdates.log

start update check

echo >> "$logfile"
echo “date: FreePBX module update check starting…” >> “$logfile”

###########################################################################

set our permissions to avoid disaster, or at the very least breaking things

which amportal chown > /dev/null
###########################################################################

Upgrade All FreePBX GUI Modules

Change Permission of amportal.conf

chown asterisk:asterisk /etc/amportal.conf

Installing all new modules for 2.10 now. Running it a few times to make sure we get all the modules.

Write output both to the log file and to stdout to have the cron job send an e-mail to the specified user in the cron job definition

sudo -u asterisk /var/lib/asterisk/bin/module_admin upgradeall | grep “successfully installed” | tee --append "$logfile"
sudo -u asterisk /var/lib/asterisk/bin/module_admin reload > /dev/null

Write output both to the log file and to stdout to have the cron job send an e-mail to the specified user in the cron job definition

sudo -u asterisk /var/lib/asterisk/bin/module_admin upgradeall | grep “successfully installed” | tee --append "$logfile"
sudo -u asterisk /var/lib/asterisk/bin/module_admin reload > /dev/null

Restart apache so the language pack takes effect

/etc/init.d/httpd restart > /dev/null 2>&1
###########################################################################

set our permissions to avoid disaster, or at the very least breaking things

which amportal chown > /dev/null
###########################################################################

end update check

echo “date: FreePBX module update check completed. See above for any module installation messages. If no ‘Module … successfully installed’ messages are displayed, no modules were found that needed updating.” >> “$logfile”

  • make the script executable
chmod +x /usr/local/bin/update_freepbx_modules.sh

==== Manually run the upgrade script to test it ====

su - /usr/local/bin/update_freepbx_modules.sh
  • if any modules are updated, a status update line will display to the console, otherwise nothing will display to the console
    • sample expected log file output with some modules updated
cat /var/log/pbx/upgrade/*moduleupdates* Sun Apr 29 19:11:16 EDT 2012: FreePBX module update check starting... Module conferences successfully installed Sun Apr 29 19:11:19 EDT 2012: FreePBX module update check completed. See above for any module installation messages. If no 'Module ... successfully installed' messages are displayed, no modules were found that needed updating.

==== Create a cron job to automatically run the upgrade script ====

  • Ensure the local root user has e-mail forwarded to a monitored e-mail address
vi /etc/cron.d/update_freepbx_modules SHELL=/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin MAILTO=root HOME=/

Minute Hour Day of Month Month Day of Week User Command

apply FreePBX module updates every morning at the desired time (1:00 AM in the example below)

0 1 * * * root /usr/local/bin/update_freepbx_modules.sh

I had an issue running this script on freepbx distro. It worked when executed manually, but not from cron.

After some digging, I found the following in /var/log/secure:

Jun  5 01:00:01 freepbx sudo:     root : sorry, you must have a tty to run sudo ;
TTY=unknown ; PWD=/ ; USER=asterisk ; COMMAND=/var/lib/asterisk/bin/module_admin 
upgradeall

It turns out that sudo has the “requiretty” flag enabled by default in this distro. My solution was to disable this flag for root:

Run ‘visudo’ and add the following line below “Defaults requiretty”:

Defaults:root !requiretty

I believe this box was a FreePBX Distro 2.9 install. Maybe that’s the difference.

The script hangs when I run it. This is the log from /var/log/secure:

Jul 13 00:59:28  sshd[28542]: Accepted password for root from <redacted> port <redacted> ssh2
Jul 13 00:59:28  sshd[28542]: pam_unix(sshd:session): session opened for user root by (uid=0)
Jul 13 00:59:28  sshd[28546]: lastlog_filetype: Couldn't stat /var/log/lastlog: No such file or directory
Jul 13 00:59:28  sshd[28546]: lastlog_openseek: /var/log/lastlog is not a file or directory!
Jul 13 00:59:28  sshd[28546]: lastlog_filetype: Couldn't stat /var/log/lastlog: No such file or directory
Jul 13 00:59:28  sshd[28546]: lastlog_openseek: /var/log/lastlog is not a file or directory!
Jul 13 01:02:53  sshd[28634]: Did not receive identification string from UNKNOWN
Jul 13 01:03:23  sshd[28672]: Did not receive identification string from UNKNOWN
Jul 13 01:06:20  su: pam_unix(su-l:session): session opened for user root by root(uid=0)
Jul 13 01:11:02  sudo:     root : TTY=pts/0 ; PWD=/root ; USER=asterisk ; COMMAND=/var/lib/asterisk/bin/module_admin upgradeall
Jul 13 01:11:04  sudo:     root : TTY=pts/0 ; PWD=/root ; USER=asterisk ; COMMAND=/var/lib/asterisk/bin/module_admin reload
Jul 13 01:11:44  sshd[28831]: Did not receive identification string from UNKNOWN
Jul 13 01:12:04  sshd[28859]: Did not receive identification string from UNKNOWN
Jul 13 01:12:34  sshd[28900]: Did not receive identification string from UNKNOWN
Jul 13 01:13:05  sshd[28939]: Did not receive identification string from UNKNOWN
Jul 13 01:13:35  sshd[28977]: Did not receive identification string from UNKNOWN
Jul 13 01:14:05  sshd[29015]: Did not receive identification string from UNKNOWN
Jul 13 01:14:35  sshd[29052]: Did not receive identification string from UNKNOWN
Jul 13 01:15:06  sshd[29091]: Did not receive identification string from UNKNOWN
Jul 13 01:15:36  sshd[29128]: Did not receive identification string from UNKNOWN
Jul 13 01:16:06  sshd[29169]: Did not receive identification string from UNKNOWN
Jul 13 01:16:37  sshd[29220]: Did not receive identification string from UNKNOWN
Jul 13 01:17:08  sshd[29260]: Did not receive identification string from UNKNOWN
Jul 13 01:17:38  sshd[29300]: Did not receive identification string from UNKNOWN
Jul 13 01:17:45  sudo:     root : TTY=pts/0 ; PWD=/usr/local/bin ; USER=asterisk ; COMMAND=/var/lib/asterisk/bin/module_admin upgradeall
Jul 13 01:17:47  sudo:     root : TTY=pts/0 ; PWD=/usr/local/bin ; USER=asterisk ; COMMAND=/var/lib/asterisk/bin/module_admin reload
Jul 13 01:18:18  sshd[29406]: Did not receive identification string from UNKNOWN
Jul 13 01:19:14  sshd[29418]: Did not receive identification string from UNKNOWN
Jul 13 01:19:44  sshd[29463]: Did not receive identification string from UNKNOWN
Jul 13 01:20:14  sshd[29504]: Did not receive identification string from UNKNOWN
Jul 13 01:20:44  sshd[29541]: Did not receive identification string from UNKNOWN
Jul 13 01:21:15  sshd[29581]: Did not receive identification string from UNKNOWN
Jul 13 01:21:34  sudo:     root : TTY=pts/0 ; PWD=/usr/local/bin ; USER=asterisk ; COMMAND=/var/lib/asterisk/bin/module_admin upgradeall
Jul 13 01:21:35  sudo:     root : TTY=pts/0 ; PWD=/usr/local/bin ; USER=asterisk ; COMMAND=/var/lib/asterisk/bin/module_admin reload
Jul 13 01:21:45  sshd[29708]: Did not receive identification string from UNKNOWN
Jul 13 01:22:15  sshd[29747]: Did not receive identification string from UNKNOWN
Jul 13 01:22:42  sudo:     root : TTY=pts/0 ; PWD=/usr/local/bin ; USER=asterisk ; COMMAND=/var/lib/asterisk/bin/module_admin upgradeall
Jul 13 01:22:43  sudo:     root : TTY=pts/0 ; PWD=/usr/local/bin ; USER=asterisk ; COMMAND=/var/lib/asterisk/bin/module_admin reload
Jul 13 01:22:51  sshd[29874]: Did not receive identification string from UNKNOWN
Jul 13 01:23:26  sshd[29883]: Did not receive identification string from UNKNOWN
Jul 13 01:23:56  sshd[29922]: Did not receive identification string from UNKNOWN
Jul 13 01:24:27  sshd[29959]: Did not receive identification string from UNKNOWN
Jul 13 01:24:57  sshd[29998]: Did not receive identification string from UNKNOWN
Jul 13 01:25:27  sshd[30035]: Did not receive identification string from UNKNOWN
Jul 13 01:25:58  sshd[30072]: Did not receive identification string from UNKNOWN
Jul 13 01:26:28  sshd[30111]: Did not receive identification string from UNKNOWN

I’m not a linux guy. Any idea how to fix this problem?

I rebooted the server and now it works. ::Shrug::