title: RPM %post scriptlet best practice
body: <p>I'm trying to track down what is the best practices for the %pre/%post scriptlets in an rpm.spec file.</p>
  
  <p>Specifically I have a system that has a pretty complicated installation. Among other things, it needs to do a lot of "Security-Enhanced Linux" customizations, iptable editing, user creation (with passwords) and "chown"-ing, etc.</p>
  
  <p>The question is where to do this? Should this be put into the pre/post scriptlets? The best practice seems to be to keep these as <a href="http://rpm5.org/docs/rpm-guide.html#id3030792" rel="nofollow">simple as possible and definitely don't make it interactive</a>.</p>
  
  <p>I have noticed that the Postgres installer (and some others) do a "manual post install" step where the root user needs to run a script to do certain things like set the postgres user's password etc. You can see it below where the rpm prints this: <code>To initialize, run /etc/init.d/postgres-9.1-openscg start as root user.</code></p>
  
  <p>Here are the extracted scriptlets:</p>
  
  <pre><code>rpm -qp --scripts postgres-9.1.2-1.i386.openscg.rpm
  </code></pre>
  
  <p>preinstall scriptlet (using /bin/sh):</p>
  
  <pre><code>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 &amp;&gt; /dev/null
    if [ "$?" = "0" ];
    then
     /etc/init.d/postgres-9.1-openscg stop
     touch /tmp/pg_9.1.stopped
    fi
  fi
  </code></pre>
  
  <p>postinstall scriptlet (using /bin/sh):</p>
  
  <pre><code>if type "/usr/bin/chcon" &amp;&gt; /dev/null ; then
    /usr/bin/chcon -t textrel_shlib_t $RPM_INSTALL_PREFIX/lib/libedit.so &amp;&gt; /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&gt; /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 &lt;&lt;ENVEOF &gt; $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 &lt;&lt;ENVEOF &gt;&gt; $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
  </code></pre>
  
  <p>preuninstall scriptlet (using /bin/sh):</p>
  
  <pre><code>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 &amp;&gt; /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" &amp;&gt; /dev/null ; then
     /sbin/chkconfig --del postgres-9.1-openscg 
    fi
  fi
  </code></pre>
  
  <p>postuninstall scriptlet (using /bin/sh):</p>
  
  <pre><code>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
  </code></pre>