我试图在rpm.spec文件中找到%pre/%post scriptlet的最佳实践。
具体来说,我有一个系统,有一个相当复杂的安装。除此之外,它还需要做很多“增强安全性的linux”定制、iptable编辑、用户创建(使用密码)和“chown”等工作。
问题是在哪里做?这应该放在前/后脚本中吗?最好的做法似乎是将它们保持为simple as possible and definitely don't make it interactive
我注意到postgres安装程序(和其他一些安装程序)执行“手动安装后”步骤,根用户需要运行脚本来执行某些操作,如设置postgres用户的密码等。您可以在下面的RPM打印的地方看到它:To initialize, run /etc/init.d/postgres-9.1-openscg start as root user.
以下是提取的scriptlet:

rpm -qp --scripts postgres-9.1.2-1.i386.openscg.rpm

预安装scriptlet(使用/bin/sh):
if [ "$1" = "2" ]; then
  #Perform maintenance tasks before server upgrade begins.
  #Determine if server is running, stops it.
  /etc/init.d/postgres-9.1-openscg status &> /dev/null
  if [ "$?" = "0" ];
  then
   /etc/init.d/postgres-9.1-openscg stop
   touch /tmp/pg_9.1.stopped
  fi
fi

安装后scriptlet(使用/bin/sh):
if type "/usr/bin/chcon" &> /dev/null ; then
  /usr/bin/chcon -t textrel_shlib_t $RPM_INSTALL_PREFIX/lib/libedit.so &> /dev/null
fi

#Create a soft link to init script
if [ ! -f /etc/init.d/postgres-9.1-openscg ]
then
  ln -s $RPM_INSTALL_PREFIX/bin/postgres-9.1-openscg /etc/init.d/postgres-9.1-openscg
fi


#In case of upgrade, dump environment values file
#if [ "$1" = "2" ];
#then
  #Fix for psql dumb terminal issue
  LD_PRELOAD_VALUE=""
  for libreadline in `find -L /lib  -type f -name libreadline.\* 2> /dev/null`
  do
    LD_PRELOAD_VALUE="$libreadline:$LD_PRELOAD_VALUE"
  done
  if [ x"$LD_PRELOAD_VALUE" != x"" ];
  then
    LD_PRELOAD_VALUE="export LD_PRELOAD=$LD_PRELOAD_VALUE"
  fi

  #Dump environment values
cat <<ENVEOF > $RPM_INSTALL_PREFIX/pg91-openscg.env
#!/bin/bash
$LD_PRELOAD_VALUE
export PGHOME=$RPM_INSTALL_PREFIX
export PGDATA=$RPM_INSTALL_PREFIX/data
export PATH=$RPM_INSTALL_PREFIX/bin:\$PATH
export LD_LIBRARY_PATH=$RPM_INSTALL_PREFIX/lib:\$LD_LIBRARY_PATH
export PGUSER=postgres
export PGDATABASE=postgres
ENVEOF

 #Determine port from postgresql.conf
 PGPORT_VALUE=""
 if [ -f $RPM_INSTALL_PREFIX/data/postgresql.conf ]; then
  PGPORT_VALUE=`grep "port =" $RPM_INSTALL_PREFIX/data/postgresql.conf | sed -e      "s/^.*port[[:space:]]=[[:space:]]\([0-9]\+\).*$/\1/"`
  PGPORT_VALUE="export PGPORT=$PGPORT_VALUE"
  cat <<ENVEOF >> $RPM_INSTALL_PREFIX/pg91-openscg.env
$PGPORT_VALUE
ENVEOF
 fi

#fi

# If it is an upgrade, and we stopped a running server, start it.
if [ "$1" = "2" -a -f /tmp/pg_9.1.stopped ];
then
  rm /tmp/pg_9.1.stopped
  /etc/init.d/postgres-9.1-openscg start
fi

if [ "$1" = "1" ];
then
  echo "PostgreSQL 9.1 is now installed in $RPM_INSTALL_PREFIX."
  echo
  echo "To initialize, run /etc/init.d/postgres-9.1-openscg start"
  echo "as root user."
fi
if [ "$1" = "2" ];
then
  echo "PostgreSQL 9.1 is upgraded in $RPM_INSTALL_PREFIX."
fi

预卸载scriptlet(使用/bin/sh):
if [ "$1" = "0" ]; then
  #Action is uninstallation, not called due to upgrade of a new package

  #Determine if server is running, stops it.
  /etc/init.d/postgres-9.1-openscg status &> /dev/null
  if [ "$?" = "0" ];
  then
   echo "Attempting to stop server..."
   /etc/init.d/postgres-9.1-openscg stop
  fi

  echo "Attempting to update server startup status..."
  if type "/sbin/chkconfig" &> /dev/null ; then
   /sbin/chkconfig --del postgres-9.1-openscg
  fi
fi

假设安装scriptlet(使用/bin/sh):
if [ "$1" = "0" ]; then
  #Action is uninstallation, not called due to upgrade of a new package
  rm /etc/init.d/postgres-9.1-openscg
  echo "Uninstallation complete."
fi

最佳答案

如果您指的是与RPM安装时的用户交互相关的最佳实践,那是毫无疑问的。别这样。在scriptlet中执行任何不需要用户交互的操作,或者告诉用户在安装后运行脚本,或者在应用程序的第一次运行中获取所有信息。

关于linux - RPM%post脚本最佳实践,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18967389/

10-10 18:28