Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Automatically restarting services after an update
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
Rcomian
Apprentice
Apprentice


Joined: 10 Jan 2004
Posts: 174
Location: Uk, Northwest

PostPosted: Sat Jun 17, 2006 9:24 pm    Post subject: Automatically restarting services after an update Reply with quote

Carrying on from this topic.

Using portage's new ability to add hooks into the functionality, we can restart services as part of the normal upgrade process with no additional commands. It's also a far more targetted affair, getting around a few of the problems I was finding in the original thread.

The following script should be saved as your "/etc/portage/bashrc" script. You'll need to merge in the functionality if you already have a bashrc of your own.

Notes:
When a package is being installed which installs a service into /etc/init.d/, this script checks to see if the service is running, and if it is, pauses it, and schedules it for a later restart. Pausing happens just before the files are installed. Restart happens just after. This means that the service is stopped for the minimal time possible, and also works for services which can't be restarted if the package has been reinstalled (eg, Tor).
If you have cfg-update installed, this script will use cfg-update to automatically merge all config files it can before restarting any services.
An optional reverse dependencies lookup will also restart any services which depend on the package being installed. This is currently very slow (it recursively calls equery), so it's not recommended to go above 1 or 2 levels of dependencies.

Code:
pre_pkg_preinst() {
   echo "Stopping affected services"

   blacklist="/etc/restart-services-blacklist"

   tmp_svc_files=`tempfile`
   equery f $CATEGORY/$PN | grep /etc/init.d/ > $tmp_svc_files

   if [[ -n "$REVDEP_SCRIPT" && -f $REVDEP_SCRIPT ]]; then
      echo "Finding services that may depend on " $CATEGORY/$PN
      $REVDEP_SCRIPT $REVDEP_DEPTH $CATEGORY/$PN | xargs -n 1 equery f | grep /etc/init.d/ >>$tmp_svc_files
      echo "Finished services search"
   fi
   
   # Make a default blacklist if one not already there
   if [ ! -f $blacklist ]; then
      echo /etc/init.d/xdm >> $blacklist
      echo /etc/init.d/net >> $blacklist
      echo /etc/init.d/local >> $blacklist
   fi

   tmp_svc_stop=`tempfile`
   tmp_svc_start=`tempfile`
   tmp_svc_boot=`tempfile`

   ls -l /etc/runlevels/boot | sed "s/^\(.*\) -> \(.*\)$/\2/" >$tmp_svc_boot

   ls -l /var/lib/init.d/started | grep -f $tmp_svc_files | sed "s/^\(.*\) -> \(.*\)$/\2 pause/" | grep -v -F -f $blacklist | grep -v -F -f $tmp_svc_boot >$tmp_svc_stop
   ls -l /var/lib/init.d/started | grep -f $tmp_svc_files | sed "s/^\(.*\) -> \(.*\)$/\2 restart/" | grep -v -F -f $blacklist | grep -v -F -f $tmp_svc_boot >$tmp_svc_start

   cat $tmp_svc_stop
   bash $tmp_svc_stop
   
   rm -Rf $tmp_svc_files
   rm -Rf $tmp_svc_stop
   rm -Rf $tmp_svc_boot
   export tmp_svc_start
}

post_pkg_postinst() {
   if [[ -f /usr/bin/cfg-update ]]; then
      /usr/bin/cfg-update -ua
   fi

   cat $tmp_svc_start
   chmod u+x $tmp_svc_start
   tmp_svc_log=`tempfile`
   bash -c $tmp_svc_start >$tmp_svc_log 2>&1
   cat $tmp_svc_log

   rm -Rf $tmp_svc_start
   rm -Rf $tmp_svc_log

}


If you want to enable reverse dependencies (eg, re-installing "readline" will cause a restart in "mysql"), then save this script somewhere:
Code:
#!/bin/bash

#Make sure we haven't gone too deep
depth=`dc -e "$1 1 - p"`

if [[ $depth -lt 0 ]]; then
   exit
fi

#Convert the inbound pkgspec to a full but non-versioned spec
pkg=`equery l $2 | grep -v '* installed packages' | sed "s/^\(.*\)-\([0123456789].*\)$/\1/"`

outfile=`tempfile`

if [[ -f /usr/bin/qdepends ]]; then
   output=`qdepends -C -Q "$2" 2>/dev/null | sed "s/^\(.*\)-\([0123456789].*\)$/\1/"`
else
   output=`equery d $pkg | sed "s/^\(.*\)-\([0123456789].*\)$/\1/"`
fi

if [[ "$output" != "" && "$output" != "$pkg" ]]; then
   echo $output | xargs -n 1 echo >>$outfile

   echo $output | xargs -n 1 $0 $depth >>$outfile
fi

sort -u $outfile

rm -Rf $outfile



Then add the following lines to your/etc/make.conf
Code:
REVDEP_SCRIPT="/root/revdep" #Path to your revdep script
REVDEP_DEPTH=1 # Number of levels deep. 1 means immediate dependencies. This gets very slow very quickly


Enjoy!

Update
The revdep script now uses the faster qdepends if it's available. Install portage-utils to get hold of that. Many thanks to synss for pointing that out.


Last edited by Rcomian on Tue Jun 20, 2006 9:05 pm; edited 1 time in total
Back to top
View user's profile Send private message
mudrii
l33t
l33t


Joined: 26 Jun 2003
Posts: 789
Location: Singapore

PostPosted: Tue Jun 20, 2006 2:12 pm    Post subject: Reply with quote

Nice work
How about the dispach-config when new version is installed with modified config file ?
_________________
www.gentoo.ro
Back to top
View user's profile Send private message
Rcomian
Apprentice
Apprentice


Joined: 10 Jan 2004
Posts: 174
Location: Uk, Northwest

PostPosted: Tue Jun 20, 2006 8:11 pm    Post subject: Reply with quote

Thanks mudrii,

I didn't do a dispatch-conf option since it's interactive, and that means the user will have to be present during the entire ebuild - if you're updating hundreds of packages (as I have just done on the house server) then that a big chore (even bigger than sorting out all the packages that don't build).
For this reason I'm using cfg-update instead - it has an automatic mode that tries to do the sane thing where it can, which is a good compromise. My aim for this script is to be able to apply security updates automatically, so an interactive tool isn't much use.

If dispatch-conf has an automatic mode (I can't see anything about it in the man page) then of course it's an option. To use dispatch-conf, simply edit the script and replace each instance of "cfg-update" with "dispatch-conf", adding the appropriate command line options where needed (the man page says dispatch-conf doesn't have any).
Back to top
View user's profile Send private message
synss
Apprentice
Apprentice


Joined: 08 Mar 2006
Posts: 282
Location: Dijon > Berlin > Tokyo > Nürnberg > München

PostPosted: Tue Jun 20, 2006 8:38 pm    Post subject: Reply with quote

Rcomian wrote:
This is currently very slow (it recursively calls equery), so it's not recommended to go above 1 or 2 levels of dependencies.


Couldn't you use qdepends from app-portage/portage-utils? It is faster.
_________________
Compress portage tree
Elog viewer
Autodetect swap
Back to top
View user's profile Send private message
Rcomian
Apprentice
Apprentice


Joined: 10 Jan 2004
Posts: 174
Location: Uk, Northwest

PostPosted: Tue Jun 20, 2006 9:03 pm    Post subject: Reply with quote

synss:

Oh yes, I'll have some of that, thank you!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum