Cleanup config restore functions.
authorValentin Vidic <Valentin.Vidic@CARNet.hr>
Tue, 12 May 2009 09:26:16 +0000 (11:26 +0200)
committerValentin Vidic <Valentin.Vidic@CARNet.hr>
Tue, 12 May 2009 10:44:04 +0000 (12:44 +0200)
src/functions.sh

index 2b557d7..beccd14 100644 (file)
@@ -733,113 +733,157 @@ fix_etc_default_raid2 () {
   fi
 }
 
-# restore original config file (if the new package version is not
-# installed already)
-restore_config () {
-  local file file_backup file_expect file_restore config_changed
-  local cn_package cn_version package version
+# restore a configuration file if it contains only CN modifications
+restore_file () {
+  local file file_expect file_restore file_backup
 
-  # check if new package version is installed
-  cn_package=$1
-  package=$2
-  version=$3
+  file=$1
+  file_expect=$2
+  file_restore=$3
+  file_backup=$file.$backup_ext
 
-  if [ "$cn_package" ]; then
-     if ! pkg $cn_package; then
-        # do nothing if cn package is not installed
-        return 0
-     fi
-  fi
+  [ "$file_expect" ] || file_expect=$cnup/files/$file.expect
+  [ "$file_restore" ] || file_restore=$cnup/files/$file.restore
 
-  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
+  # check if all required files are there
+  if ! [ -e $file -a -e $file_expect -a -e $file_restore ]; then
+    log "Required files missing, skipping $file restore."
+    return 1
+  fi 
+
+  # is restore needed
+  if ! cmp $file_expect $file >/dev/null; then
+    log "Config doesn't match the template, skipping $file restore."
+    return 1
   fi
-  shift 3
 
-  # restore package files
-  for file in $*; do
-    file_expect=/usr/share/carnet-upgrade/files/$file.expect
-    file_restore=/usr/share/carnet-upgrade/files/$file.restore
-    file_backup=$file.$backup_ext
+  # backup and restore
+  if [ ! -e $file_backup ]; then
+    log "Restoring config file $file."
 
-    # is config files are there
-    if ! [ -e $file -a -e $file_expect -a -e $file_restore ]; then
-        continue
-    fi 
+    # backup local changes
+    cp -av $file $file_backup
 
-    # is restore needed
-    if ! cmp $file_expect $file >/dev/null; then
-        continue
-    fi
+    # restore original
+    cp -v $file_restore $file
 
-    # backup and restore
-    if [ ! -e $file_backup ]; then
-      log "Restoring config file $file"
+    # restore succedded
+    return 0
+  else 
+    log "Backup file exists, skipping $file restore."
+    return 1
+  fi
+}
 
-      # backup local changes
-      cp -av $file $file_backup
+# force reconfiguration at the end if package is not upgraded automatically
+postupgrade_reconfigure () {
+  local cn_package cn_version
 
-      # restore original
-      cp -v $file_restore $file
+  cn_package=$1
+  cn_version=$( dpkg -s "$cn_package" | awk '/^Version:/ {print $2}' )
+  post_upgrade "pkg $cn_package gt $cn_version || dpkg-reconfigure $cn_package"
+}
 
-      config_changed=1
-    fi
-  done
+# copy template into a temporary file
+copy_template () {
+  local path file template config_new
 
-  # 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 gt $cn_version || dpkg-reconfigure $cn_package"
-  fi
+  path=$1
+  file=$(basename $path)
+  template=$cnup/files/$path.template
+  config_new=$(mktemp /var/lib/carnet-upgrade/$file.XXXXXX)
+  cp $template $config_new
+
+  echo $config_new
 }
 
 # 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
 
   # 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
-  restore_config '' base-files 5lenny2 /etc/issue /etc/issue.net
-  restore_config php5-cn php5-cli 5.2.6.dfsg.1-1+lenny2 /etc/php5/cli/php.ini
-  restore_config php5-cn libapache2-mod-php5 5.2.6.dfsg.1-1+lenny2 /etc/php5/apache2/php.ini
+  if pkg kernel-2.6-cn && pkg procps lt 1:3.2.7-11; then
+     if restore_file /etc/sysctl.conf; then
+        postupgrade_reconfigure kernel-2.6-cn
+     fi
+  fi
+
+  if pkg ntp-cn && pkg ntp lt 1:4.2.4p4+dfsg-8; then
+     if restore_file /etc/ntp.conf; then
+        postupgrade_reconfigure ntp-cn
+     fi
+  fi
+
+  if pkg apache2-cn && pkg apache2.2-common lt 2.2.9-10+lenny2; then
+     if restore_file /etc/apache2/ports.conf; then
+        postupgrade_reconfigure apache2-cn
+     fi
+  fi
+
+  if pkg postfix-cn && pkg sasl2-bin lt 2.1.22.dfsg1-23; then
+     if restore_file /etc/default/saslauthd; then
+        postupgrade_reconfigure postfix-cn
+     fi
+  fi
+
+  if pkg bind9-cn && pkg bind9 lt 1:9.5.1.dfsg.P1-1; then
+     if restore_file /etc/bind/named.conf.options; then
+        postupgrade_reconfigure bind9-cn
+     fi
+  fi
+
+  if pkg amavisd-cn && pkg amavisd-new lt 1:2.6.1.dfsg-1; then
+     if restore_file /etc/cron.daily/amavisd-new; then
+        rm -f /etc/cron.daily/amavisd-new.$backup_ext
+        postupgrade_reconfigure amavisd-cn
+     fi
+  fi
+
+  if pkg spamassassin-cn && pkg razor lt 1:2.85-1; then
+     if restore_file /etc/razor/razor-agent.conf; then
+        postupgrade_reconfigure spamassassin-cn
+     fi
+  fi
+
+  if pkg kernel-2.6-cn && pkg libpam-modules lt 1.0.1-5; then
+     if restore_file /etc/security/limits.conf; then
+        postupgrade_reconfigure kernel-2.6-cn
+     fi
+  fi
+
+  if pkg samba-cn && pkg samba-common lt 2:3.2.5-4lenny2; then
+     if restore_file /etc/samba/smb.conf; then
+        postupgrade_reconfigure samba-cn
+     fi
+  fi
+
+  if pkg base-files lt 5lenny2; then
+     restore_file /etc/issue
+     restore_file /etc/issue.net
+  fi
+
+  if pkg php5-cn && pkg php5-cli lt 5.2.6.dfsg.1-1+lenny2; then
+     if restore_file /etc/php5/cli/php.ini; then
+        postupgrade_reconfigure php5-cn
+     fi
+  fi
+
+  if pkg php5-cn && pkg libapache2-mod-php5 lt 5.2.6.dfsg.1-1+lenny2; then
+     if restore_file /etc/php5/apache2/php.ini; then
+        postupgrade_reconfigure php5-cn
+     fi
+  fi
 
   # 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)
+  if [ -f /etc/monit/monitrc ]; then
+     # regenerate config from template
      hostname=$(hostname)
      domain=$(hostname --domain)
-
-     # generate config from template
-     cp $template $config_new
+     config_new=$(copy_template /etc/monit/monitrc)
      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
+     restore_file /etc/monit/monitrc $config_new
      rm -f $config_new
   fi
 
@@ -1044,51 +1088,32 @@ upgrade_freeradius () {
   pkg freeradius-aai lt 2.1.3-0lenny0 || return 0
 
   # handle static configs
-  restore_config freeradius-aai freeradius 2.1.3-0lenny0 \
-    /etc/freeradius/clients.conf \
-    /etc/freeradius/hints \
-    /etc/freeradius/ldap.attrmap
+  restore_file /etc/freeradius/clients.conf
+  restore_file /etc/freeradius/hints
+  restore_file /etc/freeradius/ldap.attrmap
 
   # handle template based configs
-  template=/usr/share/carnet-upgrade/files/etc/freeradius/radiusd.conf.template
-  config_new=$(mktemp /var/lib/carnet-upgrade/radiusd.conf.XXXXXX)
   basedn=$(sed -n '/^[[:space:]]*suffix[[:space:]]*/ { 
     s///; s/"//g; p; q }' /etc/ldap/slapd.conf)
   hostname=$(hostname -f)
-  cp $template $config_new
+  config_new=$(copy_template /etc/freeradius/radiusd.conf)
   sed -i "s/#HOSTNAME#/$hostname/" $config_new
   sed -i "s/#BASEDN#/$basedn/" $config_new
-  if cmp -s $config_new /etc/freeradius/radiusd.conf >/dev/null; then
-     log "Restoring config file /etc/freeradius/radiusd.conf"
-     cp -v /usr/share/carnet-upgrade/files/etc/freeradius/radiusd.conf.restore \
-           /etc/freeradius/radiusd.conf
-  fi
+  restore_file /etc/freeradius/radiusd.conf $config_new
   rm -f $config_new
 
-  template=/usr/share/carnet-upgrade/files/etc/freeradius/eap.conf.template
-  config_new=$(mktemp /var/lib/carnet-upgrade/eap.conf.XXXXXX)
   password=$(grep -s '^[[:space:]]*private_key_password[[:space:]]*=' \
     /etc/freeradius/eap.conf)
-  cp $template $config_new
+  config_new=$(copy_template /etc/freeradius/eap.conf)
   sed -i "s/.*#PASSWORD#.*/$password/" $config_new
-  if cmp -s $config_new /etc/freeradius/eap.conf >/dev/null; then
-     log "Restoring config file /etc/freeradius/eap.conf"
-     cp -v /usr/share/carnet-upgrade/files/etc/freeradius/eap.conf.restore \
-           /etc/freeradius/eap.conf
-  fi
+  restore_file /etc/freeradius/eap.conf $config_new
   rm -f $config_new
   
-  template=/usr/share/carnet-upgrade/files/etc/freeradius/proxy.conf.template
-  config_new=$(mktemp /var/lib/carnet-upgrade/proxy.conf.XXXXXX)
   realm=$(sed -n '/^[[:space:]]*suffix[[:space:]]*/ { 
     s///; s/"//g; s/,dc=/./g; s/dc=//; s/.hr$//; p; q }' /etc/ldap/slapd.conf)
-  cp $template $config_new
+  config_new=$(copy_template /etc/freeradius/proxy.conf)
   sed -i "s/#REALM#/$realm/" $config_new
-  if cmp -s $config_new /etc/freeradius/proxy.conf >/dev/null; then
-     log "Restoring config file /etc/freeradius/proxy.conf"
-     cp -v /usr/share/carnet-upgrade/files/etc/freeradius/proxy.conf.restore \
-           /etc/freeradius/proxy.conf
-  fi
+  restore_file /etc/freeradius/proxy.conf $config_new
   rm -f $config_new
 
   # install the new packages
@@ -1187,7 +1212,7 @@ upgrade_dovecot () {
   default_mail_env=$(get_variable default_mail_env /etc/dovecot/dovecot.conf)
 
   # silently upgrade package
-  restore_config dovecot-cn dovecot-common 1.0 /etc/dovecot/dovecot.conf
+  restore_file /etc/dovecot/dovecot.conf
   pkgadd dovecot-cn
 
   # insert old mail location