Updated config restore functionality.
authorValentin Vidic <Valentin.Vidic@CARNet.hr>
Wed, 22 Apr 2009 15:13:38 +0000 (17:13 +0200)
committerValentin Vidic <Valentin.Vidic@CARNet.hr>
Wed, 22 Apr 2009 15:13:38 +0000 (17:13 +0200)
src/functions.sh

index e097bc0..5369717 100644 (file)
@@ -692,25 +692,45 @@ fix_etc_default_raid2 () {
 # restore original config file (if the new package version is not
 # installed already)
 restore_config () {
-  local file file_backup file_orig
-  local old_umask
+  local file file_backup file_expect file_restore config_changed
+  local cn_package cn_version package version
+
+  # check if new package version is installed
+  cn_package=$1
+  package=$2
+  version=$3
+
+  if [ "$cn_package" ]; then
+     if ! pkg $cn_package; then
+        # do nothing if cn package is not installed
+        return 0
+     fi
+  fi
 
-  # create new files safely
-  old_umask=`umask`
-  umask 0077
+  if [ "$package" ]; then
+     if [ "$version" ]; then
+        # do nothing if package is already upgraded to new version
+        pkg $package lt $version || return 0
+     else
+        # do nothing if package is not installed
+        pkg $package || return 0
+     fi
+  fi
+  shift 3
 
   # restore package files
   for file in $*; do
-    file_orig=/usr/share/carnet-upgrade/files/$file
+    file_expect=/usr/share/carnet-upgrade/files/$file.expect
+    file_restore=/usr/share/carnet-upgrade/files/$file.restore
     file_backup=$file.$backup_ext
 
-    # is config there
-    if [ ! -e $file ]; then
+    # is config files are there
+    if ! [ -e $file -a -e $file_expect -a -e $file_restore ]; then
         continue
     fi 
 
     # is restore needed
-    if cmp $file_orig $file >/dev/null; then
+    if ! cmp $file_expect $file >/dev/null; then
         continue
     fi
 
@@ -719,72 +739,107 @@ restore_config () {
       log "Restoring config file $file"
 
       # backup local changes
-      mv -v $file $file_backup
+      cp -av $file $file_backup
 
       # restore original
-      cp -av $file_orig $file
-      chown --reference $file_backup $file
-      chmod --reference $file_backup $file
+      cp -v $file_restore $file
+
+      config_changed=1
     fi
   done
 
-  umask $old_umask
+  # force reconfiguration at the end if package is not upgraded automatically
+  if [ "$config_changed" -a "$cn_package" ]; then
+     cn_version=$( dpkg -s "$cn_package" | awk '/^Version:/ {print $2}' )
+     post_upgrade "pkg $cn_package eq $cn_version && dpkg-reconfigure $cn_package"
+  fi
 }
 
 # restore modified config to their package defaults
 # so the upgrade doesn't complain so much
 restore_configs () {
+  local hostname domain template config_new
   local config_backup
 
-  pkg base-files lt 4 && restore_config /etc/issue /etc/issue.net
-  pkg libpam-modules lt 0.79 && restore_config /etc/security/limits.conf
-  pkg login lt 1:4.0.18.1 && restore_config /etc/pam.d/login
-  pkg mysql-server lt 5.0.3 && restore_config /etc/init.d/mysql \
-                                    /etc/logrotate.d/mysql-server \
-                                    /etc/mysql/my.cnf
-  pkg ntp lt 1:4.2.2 && restore_config /etc/ntp.conf
-  pkg ntpdate lt 1:4.2.2 && restore_config /etc/default/ntpdate
-  pkg oidentd lt 2.0.8 && restore_config /etc/default/oidentd
-  pkg postgrey lt 1.27 && restore_config /etc/default/postgrey \
-                               /etc/postgrey/whitelist_clients
-  pkg procps lt 1:3.2.7 && restore_config /etc/sysctl.conf
-  pkg sasl2-bin lt 2.1.22 && restore_config /etc/default/saslauthd
-  pkg slapd lt 2.3.30 && restore_config /etc/default/slapd /etc/init.d/slapd
-  pkg squirrelmail lt 2:1.4.9a && restore_config /etc/squirrelmail/apache.conf
-  pkg vsftpd lt 2.0.5 && restore_config /etc/vsftpd.conf
-  pkg xinetd lt 1:2.3.14 && restore_config /etc/xinetd.conf
-
-  # orphaned config file - no owner
-  if pkg logcheck-database lt 1.2.54; then
-      rm -vf /etc/logcheck/ignore.d.server/imap
+  # restore simple configs
+  restore_config kernel-2.6-cn procps 1:3.2.7-11 /etc/sysctl.conf
+  restore_config ntp-cn ntp 1:4.2.4p4+dfsg-8 /etc/ntp.conf
+  restore_config apache2-cn apache2.2-common 2.2.9-10+lenny2 /etc/apache2/ports.conf
+  restore_config postfix-cn sasl2-bin 2.1.22.dfsg1-23 /etc/default/saslauthd
+  restore_config bind9-cn bind9 1:9.5.1.dfsg.P1-1 /etc/bind/named.conf.options
+  restore_config amavisd-cn amavisd-new 1:2.6.1.dfsg-1 /etc/cron.daily/amavisd-new
+  rm -f /etc/cron.daily/amavisd-new.$backup_ext
+  restore_config spamassassin-cn razor 1:2.85-1 /etc/razor/razor-agent.conf
+  restore_config kernel-2.6-cn libpam-modules 1.0.1-5 /etc/security/limits.conf
+  restore_config samba-cn samba-common 2:3.2.5-4lenny2 /etc/samba/smb.conf
+
+  # check if monitrc is template based
+  if [ -f /etc/monit/monitrc -a ! -f /etc/monit/monitrc.$backup_ext ]; then
+     template=/usr/share/carnet-upgrade/files/etc/monit/monitrc.template
+     config_new=$(mktemp /var/lib/carnet-upgrade/monitrc.XXXXXX)
+     hostname=$(hostname)
+     domain=$(hostname --domain)
+
+     # generate config from template
+     cp $template $config_new
+     check_and_sed "@localhost" \
+       "s/@localhost/@$hostname.$domain/g" $config_new || true
+
+     # compare with running config
+     if cmp -s /etc/monit/monitrc $config_new; then
+        # restore if running config is generated template
+        log "Restoring config file /etc/monit/monitrc"
+        cp -av /etc/monit/monitrc /etc/monit/monitrc.$backup_ext
+        cp -v /usr/share/carnet-upgrade/files/etc/monit/monitrc.restore \
+              /etc/monit/monitrc
+     fi
+     rm -f $config_new
   fi
 
-  # aide switched to ucf, move old configs aside
-  if pkg aide lt 0.13.1; then
-      for file in /etc/aide/aide.conf \
-                  /etc/cron.daily/aide \
-                  /etc/default/aide;
-      do
-          if [ ! -e "$file.$backup_ext" ]; then
-              mv -v "$file" "$file.$backup_ext"
-          fi
-
-          rm -vf "$file"
-      done
-  fi
-
-  # register cn changes in ucf managed files
-  for config in /etc/apache/modules.conf \
-                /etc/clamav/clamd.conf \
-                /etc/clamav/freshclam.conf
-  do 
-      config_backup=$config.$backup_ext
-      if [ -e $config -a ! -e $config_backup ]; then
-          log "Saving config file $config"
-          cp -av $config $config_backup
-          ucf $config_backup $config
-      fi
-  done
+#  restore_config base-files 4 /etc/issue /etc/issue.net
+#  restore_config base-files 4 /etc/issue /etc/issue.net
+#  restore_config login 1:4.0.18.1 /etc/pam.d/login
+#  restore_config mysql-server 5.0.3 /etc/init.d/mysql \
+#                                    /etc/logrotate.d/mysql-server \
+#                                    /etc/mysql/my.cnf
+#  restore_config ntpdate 1:4.2.2 /etc/default/ntpdate
+#  restore_config oidentd 2.0.8 /etc/default/oidentd
+#  restore_config slapd 2.3.30 /etc/default/slapd /etc/init.d/slapd
+#  restore_config squirrelmail 2:1.4.9a /etc/squirrelmail/apache.conf
+#  restore_config vsftpd 2.0.5 /etc/vsftpd.conf
+#  restore_config xinetd 1:2.3.14 /etc/xinetd.conf
+#
+#  # orphaned config file - no owner
+#  if pkg logcheck-database lt 1.2.54; then
+#      rm -vf /etc/logcheck/ignore.d.server/imap
+#  fi
+#
+#  # aide switched to ucf, move old configs aside
+#  if pkg aide lt 0.13.1; then
+#      for file in /etc/aide/aide.conf \
+#                  /etc/cron.daily/aide \
+#                  /etc/default/aide;
+#      do
+#          if [ ! -e "$file.$backup_ext" ]; then
+#              mv -v "$file" "$file.$backup_ext"
+#          fi
+#
+#          rm -vf "$file"
+#      done
+#  fi
+#
+#  # register cn changes in ucf managed files
+#  for config in /etc/apache/modules.conf \
+#                /etc/clamav/clamd.conf \
+#                /etc/clamav/freshclam.conf
+#  do 
+#      config_backup=$config.$backup_ext
+#      if [ -e $config -a ! -e $config_backup ]; then
+#          log "Saving config file $config"
+#          cp -av $config $config_backup
+#          ucf $config_backup $config
+#      fi
+#  done
 }
 
 # temporary disable listchanges packages to reduce clutter during upgrade
@@ -989,7 +1044,7 @@ upgrade_dovecot () {
   default_mail_env=$(get_variable default_mail_env /etc/dovecot/dovecot.conf)
 
   # silently upgrade package
-  restore_config /etc/dovecot/dovecot.conf
+  restore_config dovecot-cn dovecot-common 1.0 /etc/dovecot/dovecot.conf
   pkgadd dovecot-cn
 
   # insert old mail location