1

我正在尝试找出 rpm.spec 文件中 %pre/%post 脚本的最佳实践。

具体来说,我有一个安装非常复杂的系统。除此之外,它还需要做很多“Security-Enhanced Linux”自定义、iptable 编辑、用户创建(使用密码)和“chown”-ing 等。

问题是在哪里做这个?这应该放入前/后脚本中吗?最好的做法似乎是让这些尽可能简单,绝对不要让它交互

我注意到 Postgres 安装程序(和其他一些安装程序)执行“手动安装后”步骤,其中 root 用户需要运行脚本来执行某些操作,例如设置 postgres 用户的密码等。您可以在 rpm 打印的下面看到它这个:To initialize, run /etc/init.d/postgres-9.1-openscg start as root user.

以下是提取的脚本:

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

安装后脚本(使用 /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

预卸载脚本(使用 /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

卸载后脚本(使用 /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
4

1 回答 1

6

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

于 2013-09-25T05:47:49.060 回答