0

I wrote a bash script to install multiple packages and calling the bash script in %post. But its not executing the script. I am new to this area and I am not sure what I am missing.

If i manually execute the script its working fine but not through the rpm package.

InstallRPM.spec file:

Name:           InstallRPM
Version:        1
Release:        1%{?dist}
Summary:        Install RPM Packages

License:        Script
URL:            NA
Source0:        InstallRPM-1.tar.gz
BuildArch:      noarch
BuildRoot:      %{_tmppath}/%{name}-buildroot
Requires:       /bin/sh

%description
Install RPM files from the /tmp/ folder

%prep
%setup -q

%install
mkdir -p "$RPM_BUILD_ROOT"
cp -R * "$RPM_BUILD_ROOT"


%post
echo "Executing the script /tmp/InstallRPM.sh"
/tmp/InstallRPM.sh

%clean
rm -rf $RPM_BUILD_ROOT

%files
%defattr(-,root,root,-)
/tmp/InstallRPM.sh

Shell Script:

#!/bin/bash
echo "Installing package using YUM"
package="vim zip"

echo "Install $package ..."
if [ "`cat /etc/centos-release | awk '{print $1}'`" == "CentOS" ]
then
{
        for i in $package
        do
        {
                #if [ "`/bin/rpm -qa $i | cut -d. -f1`" == "" ]
                if [ "`/bin/rpm -qa $i`" == "" ]
                then
                {
                        echo "Clean Install"
                        /usr/bin/sudo /bin/yum install -y $i
                }
                else
                {
                        echo "Upgrade"
                        /usr/bin/sudo /bin/yum update -y $i
                }
                fi
                echo "****************"
        }
        done
}
fi

I ran the rpmbuild -ba InstallRPM.spec and it created a rpm file which I executed but nothing happened. Its getting hung in the below mentioned spot.

Downloading packages:
vim-enhanced-7.4.629-6.el7.x86_64.rpm                                                                                               | 1.1 MB  00:00:02
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
^C
Sandosh Kumar P
  • 137
  • 1
  • 7
  • Tangentially, that's a pretty horrible [useless `cat`](/questions/11710552/useless-use-of-cat) – tripleee Sep 27 '19 at 16:30
  • 1
    I'm guessing `yum` doesn't want `yum` to be called from inside a package script. Isn't the proper fix [dependencies](https://rpm.org/user_doc/dependencies.html) anyway? – tripleee Sep 27 '19 at 16:34
  • What I have noticed is when I execute the rpm its calling the ```yum``` install like this. ```/usr/bin/python /bin/yum install -y vim``` Is there a way to get rid of ```/usr/bin/python``` while executing? – Sandosh Kumar P Sep 27 '19 at 18:01
  • 1
    That seems like entirely a different problem. But again, a package which simply `Requires:` the latest versions of `zip` and `vim` seems a lot simpler and more straightforward. – tripleee Sep 28 '19 at 07:29
  • 2
    The RPM database is locked; you _cannot_ call `rpm` from within a specfile. Do it correctly as others noted by using dependencies. – Aaron D. Marasco Sep 29 '19 at 23:14

1 Answers1

0

You are using rpm the wrong way. Don't install dependencies in a %post script. I suggest you to read the rpm packaging guide first.

You should use Requires in your spec file, like this:

Requires: vim
Requires: zip
Chris Maes
  • 35,025
  • 12
  • 111
  • 136
  • @SandoshKumarP did this solve your problem? If so please accept the answer so other people know that this question is solved. – Chris Maes Oct 15 '19 at 09:23