How to test if package has NOT been installed, in .ebextensions?

347 Views Asked by At

How would I check to see if a package has not been installed, in the .ebextensions folder of an Elastic Beanstalk setup? What I want to do is similar to this command, but I only want to run the command if the package does not exist, rather than if it exists.

commands:
    install_package:
        test: rpm -qa | grep -c example_package
        command: yum install -y example_package.rpm

So in pseudocode, this is what I am after:

commands:
    install_package:
        test: not(rpm -qa | grep -c example_package)
        command: yum install -y example_package.rpm

Update: I have gotten this to work without the test parameter, using a double pipe in the command itself instead, but it isn't as neat as I'd like; I'd rather use the test parameter instead to make it more explicit:

commands:
    install_package:
        command: rpm -qa | grep -c example_package || { yum install -y example_package.rpm; }
1

There are 1 best solutions below

0
CAVASIN Florian On

You can run shell script in the test option:
https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#linux-commands-options

test

(Optional) A command that must return the value true (exit code 0) in order for Elastic Beanstalk to process the command, such as a shell script, contained in the command key.

If you want to count the grep result (as you write in the question), you can condition with -eq (equal), -gt (greater than) or -ge (greater or equal).

Example for 3 or more lines:

commands:
    install_package:
        test: |
            rpm -qa | grep -c example_package)
            test $? -ge 3
        command: yum install -y example_package.rpm

If you want the result found/not found from grep, you should use the quiet option -q, --quiet, --silent to only retrieve the exit code from grep:

If the pattern is found, grep returns an exit status of 0, indicating success; if grep cannot find the pattern, it returns 1 as its exit status; and if the file cannot be found, grep returns an exit status of 2.

At the end, if you want to run the command ONLY if the package is NOT installed, you should test if grep returns 1:

commands:
    install_package:
        test: |
            rpm -qa | grep -q example_package
            test $? -eq 1
        command: yum install -y example_package.rpm