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

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; }

Related

What is an alternative to curl | bash to execute scripts for upgrades in Dockerfiles?

I would like to know what are the alternatives to using curl ... | bash ... to upgrade a package to a given version in a Dockerfile. It has been found that someone placed a backdoor in Codecov Bash Uploader, which would allow an attacker to export info from a target's CI environment. I am on RHEL 8 and for the moment, my Dockerfile uses something like:
RUN curl -sL <script-url> | bash - && \
yum remove -y <package> && \
yum install -y <package> && \
...

Error in yaml code could not find expected ':'

yaml code
- hosts: all
tasks:
#Import Remi GPG key - see: http://rpms.famillecollet.com/RPM-GPG-KEY-remi
wget http://rpms.famillecollet.com/RPM-GPG-KEY-remi \ -O /etc/pki/rpm-gpg/RPM-GPG-KEY-remi
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-remi
#Install Remi repo
rpm -Uvh --quiet \
http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
Install EPEL repo.
yum install epel-release
Install Node.js (npm plus all its dependencies).
yum --enablerepo=epel install node
I am getting following error when compiling: ERROR! Syntax Error while loading YAML.
The error appears to have been in '/home/shahzad/playbook.yml': line
7, column 3, but may be elsewhere in the file depending on the exact
syntax problem.
The offending line appears to be:
wget http://rpms.famillecollet.com/RPM-GPG-KEY-remi \ -O /etc/pki/rpm-gpg/RPM-GPG-KEY-remi
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-remi
^ here
exception type: <class 'yaml.scanner.ScannerError'>
exception: while scanning a simple key
in "<unicode string>", line 6, column 3
could not find expected ':'
in "<unicode string>", line 7, column 3
I installed everything from the instructions above, but i used the installer alien for converting and installing rpm packages on Ubuntu 18.04.
But you will not be able to install with yum, since some packages are not in its list.
use alien:
# apt install alien # apt install -y
# cd /tmp
# wget http://rpms.famillecollet.com/RPM-GPG-KEY-remi \ -O /etc/pki/rpm-gpg/RPM-GPG-KEY-remi
# wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
# alien -kiv remi-release-6.rpm
# ls -l
# wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
# alien epel-release-latest-8.noarch.rpm
# ls -l
# alien -k epel-release-latest-8.noarch.rpm ; alien -i epel-release-latest-8.noarch.rpm
# cd /home/user
# apt install curl gcc g++ make # apt install -y
# curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
# apt install nodejs # apt install -y
# curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
# echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
# apt update ; sudo apt install yarn # apt install -y
# apt install nodejs ; apt upgrade ; passwd -dl root ; reboot # apt install -y
But i still have the same error Invalid YAML: could not find expected ':':, but on command networkctl it became better for me to see , it says failed (although before installing node.js, remi-release, epel-release it didn't sign it like that) which interfaces are not configured correctly.
IDX LINK TYPE OPERATIONAL SETUP
1 lo loopback carrier unmanaged
2 ens11 ether off unmanaged
3 enp2t1 ether routable configured
4 br0 ether off failed
5 vlan5 ether off configuring
These installed packages let you see the interface error in depth, this method works!!!!!!!! Shahzad Adil shaikh thank your!
I was getting same error while running commands using PowerShell task in yaml.
- task: PowerShell#1
inputs:
scriptType: inlineScript
inlineScript: |
Command1
Commands2
I fixed this error by indenting the commands/script block.
You need to indent Command1 one lever under inlineScript: |.
If you wish to use shell commands in your yaml playbook such as wget, you'll need to use the shell module:
- name: Import Remi GPG key
shell: wget ...
":" is a special character in yaml, please read the YAML Syntax page in the official ansible documentation, for quoting.
As for yum commands, you may use ansible's yum module.
As a best practice, you may use http://www.yamllint.com/ for debugging your YAML syntax, checking for the exact line & column where the parser fails.

remove multiple rpm packages using one bash command

I'd like to use a single bash command to uninstall several packages.
# rpm -qa | grep php
php-common-5.4.16-45.el7.x86_64
php-5.4.16-45.el7.x86_64
php-mysql-5.4.16-45.el7.x86_64
php-pdo-5.4.16-45.el7.x86_64
php-cli-5.4.16-45.el7.x86_64
will give me an output of all the pakcages I'd like to remove, however, how can I pipe that into a remove package command? Something like this:
# rpm -qa | grep php | yum remove ${package}
I tried this and it worked.
rpm -qa | grep php | while read -r line; do yum remove -y $line; done

How to sudo yum install a list of packages only if they are not installed?

I am trying to improve a build script (bash or Makefile) to install some prerequisites if necessary.
I do have a long list of packages (10-15) and I do want to install them only if they are not installed.
As the build script may be run by non root I do want to run sudo yum install only once and only if needed (at least one package is not installed).
My current implementation is clearly not optimal:
for PACKAGE in a b c ... ; do
yum list installed $PACKAGE >/dev/null 2>&1 || sudo yum install -q -y $PACKAGE ;
done
How can I do this?
You could, of course, just put the list of installed packages into a file and run grep against that in a loop, but should you wish to avoid creating any files, running rpm should work:
for p in a b c ... ; do
rpm -q > /dev/null 2>&1 $PACKAGE || sudo yum install -q -y $p
done
When querying package information, rpm returns zero when the package is found and non-zero otherwise.

Auto-install packages from inside makefile

Goal: when the user types 'make packages', automatically search for the package libx11-dev (required for my program to compile) and, if not found, install it. Here's a stripped-down version of my makefile:
PACKAGES = $(shell if [ -z $(dpkg -l | grep libx11-dev) ]; then sudo apt-get install libx11-dev; fi)
[other definitions and targets]
packages: $(PACKAGES)
When I type 'make packages', I'm prompted for the super-user password. If entered correctly, it then hangs indefinitely.
Is what I'm trying to do even possible from within the makefile? If so, how?
Thanks so much.
The problem is that the shell function acts like backticks in the shell: it takes the output to stdout and returns it as the value of the function. So, apt-get is not hanging, it's waiting for you to enter a response to some question. But you cannot see the question because make has taken the output.
The way you're doing this is not going to work. Why are you using shell instead of just writing it as a rule?
packages:
[ -z `dpkg -l | grep libx11-dev` ] && sudo apt-get install libx11-dev
.PHONY: packages
I figured out a better way, which avoids the problem of having unexpected arguments to the if statement:
if ! dpkg -l | grep libx11-dev -c >>/dev/null; then sudo apt-get install libx11-dev; fi
The -c flag on grep makes it return the number of lines in dpkg -l which contain the string libx11-dev, which will either be 0 (if uninstalled) or 1 (if installed), allowing
dpkg -l | grep libx11-dev -c
to be treated like an ordinary boolean variable.

Resources