Creating an auto-deployment script for centos

Hi Guys,

Is this script good enough??

I build a CentOS VM every now and then and I what I want to achieve is to create a package(much preferred) but to begin with I want to create an auto deployment script which would do all the work for me while I do rest of the things.


[root@agent2 jim]# cat installation.sh 
#!/bin/bash

set -x

function failed
{
STATUS=$(echo $?)

if [[ "${STATUS}" ==  "0" ]]; then
	echo "All good continue"
else
	exit 1
fi
}

cd /tmp
failed

yum -y install epel-release #Adding EPEL Repositories
failed

cd /tmp/
failed

wget https://centos7.iuscommunity.org/ius-release.rpm
failed

sudo rpm -Uvh ius-release*.rpm
failed

wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
failed

sudo rpm -Uvh remi-release-7*.rpm
failed

cd /tmp/ && wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm
failed

yum repolist
failed

rpm -Uvh rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm
failed

yum -y install http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
failed

yum -y install http://linuxdownload.adobe.com/linux/x86_64/adobe-release-x86_64-1.0-1.noarch.rpm
failed

yum -y install flash-plugin
failed

yum -y install icedtea-web
failed

yum -y install vlc smplayer ffmpeg HandBrake-{gui,cli}
failed

yum -y install libdvdcss gstreamer{,1}-plugins-ugly gstreamer-plugins-bad-nonfree gstreamer1-plugins-bad-freeworld
failed

yum groupinstall "Development Tools"
failed

yum -y update
failed

exit 0
[root@agent2 jim]#

Output:

+ sudo rpm -Uvh ius-release.rpm
warning: ius-release.rpm: Header V4 DSA/SHA1 Signature, key ID 9cd4953f: NOKEY
Preparing...                          ################################# [100%]
	package ius-release-1.0-14.ius.centos7.noarch is already installed
+ failed
++ echo 1
+ STATUS=1
+ [[ 1 == \0 ]]
+ exit 1
[root@agent2 jim]#
  1. At professional level is this script good enough? Yeah I am newbie therefore asking.
  2. Why I am getting error at the execution step ? The installation shouldn't failed because a package is already existing but carry on with rest of the script.

Any help would be very much appreciated.

A few thoughts on - not necessarily exhaustive answers to - your questions:

  1. "good enough" for me means: fulfills its purpose to satisfaction. Does it do the installation (on a sunny day)? If yes: sounds "good enough". Is it fail-safe? Not on first sight. Do you want the reason why it fails, and does it show that: no, so "not good enough" on rainy days. Any action to repair a failure: No.
    My experience is it takes way more than half the programming effort to do error checking and handling. Which immediately leads to the second question:
  2. While applications may give a plethora of exit codes indicating what went wrong and how severe this was, you're ignoring this diversity, only testing the result being zero, if not treat this as a failure, and quit with an exit code 1. So your failed function may be a bit too simplistic.

Here is the new version of the script:

[root@agent2 jim]# cat installation.sh
#!/bin/bash

set -x

function failed
{
STATUS=$(echo $?)

if [[ "${STATUS}" ==  "0" ]]; then
	echo "All good continue"
else
	exit 1
fi
}

function isInstalled-yum
{
isinstalled_yum=$(yum list installed |grep $1)

if [[ "${isinstalled_yum}" ==  "0" ]]; then
	echo -e "$1 package is already installed"
else
	yum -y install $1
fi
}

function isInstalled-rpm
{

isinstalled_rpm=$(rpm -q -p $1 --queryformat "%{NAME} %{VERSION} %{RELEASE} %{ARCH}\n")

if [[ "${isinstalled_rpm}" ==  "0" ]]; then
	echo -e "$1 package is already installed"
else
	rpm -Uvh $1
fi

}

cd /tmp
failed
isInstalled-yum epel-release
failed

cd /tmp/
failed

wget https://centos7.iuscommunity.org/ius-release.rpm
failed
isInstalled-rpm ius-release.rpm
failed

wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
failed
isInstalled-rpm remi-release-7.rpm
failed

cd /tmp/ && wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm
failed

isInstalled-rpm rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm
failed

wget http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
failed

isInstalled-rpm nux-dextop-release-0-5.el7.nux.noarch.rpm
failed

cd /tmp
failed
pwd
failed

wget http://linuxdownload.adobe.com/linux/x86_64/adobe-release-x86_64-1.0-1.noarch.rpm
failed
isInstalled-rpm adobe-release-x86_64-1.0-1.noarch.rpm
failed

isInstalled-yum flash-plugin
failed

isInstalled-yum icedtea-web
failed

isInstalled-yum vlc smplayer ffmpeg HandBrake-{gui,cli}
failed

isInstalled-yum libdvdcss gstreamer{,1}-plugins-ugly gstreamer-plugins-bad-nonfree gstreamer1-plugins-bad-freeworld
failed

isInstalled-yum "Development Tools"
failed

yum repolist

yum -y update
failed

exit 0
[root@agent2 jim]# 

Here is the Output:

Limited to the area where it has failed.


2016-06-26 21:43:59 (9.16 MB/s) - �ius-release.rpm.7' saved [8264/8264]

+ failed
++ echo 0
+ STATUS=0
+ [[ 0 == \0 ]]
+ echo 'All good continue'
All good continue
+ isInstalled-rpm ius-release.rpm
++ rpm -q -p ius-release.rpm --queryformat '%{NAME} %{VERSION} %{RELEASE} %{ARCH}\n'
warning: ius-release.rpm: Header V4 DSA/SHA1 Signature, key ID 9cd4953f: NOKEY
+ isinstalled_rpm='ius-release 1.0 14.ius.centos7 noarch'
+ [[ ius-release 1.0 14.ius.centos7 noarch == \0 ]]
+ rpm -Uvh ius-release.rpm
warning: ius-release.rpm: Header V4 DSA/SHA1 Signature, key ID 9cd4953f: NOKEY
Preparing...                          ################################# [100%]
	package ius-release-1.0-14.ius.centos7.noarch is already installed
+ failed
++ echo 1
+ STATUS=1
+ [[ 1 == \0 ]]
+ exit 1
[root@agent2 jim]# 

No idea why failed subroutine is return 1??

Hi,

I'm a sysadmin who does a lot of automating too.

Your script maybe good for a start. I have some points for you:

  • Yous script could use some improvements to make it simpler. But hey! It works? It may stay as it is.
  • Maybe you want a result mailed to you when the installation is read, so you know when something had failed and you'll be informed to fix it. Of course your system needs a configured mailer to do this.
  • Another buzzword of automation "idempotency": Run it as often as you want, you get always the correct result. What's regarding your script, this point should be fulfilled. Nothing bad should happen if you call it more than once, except some download trash is accumulating. So cleaning up after your script runs is a good idea.
  • You may go with your own scripts, but things tend to get more complex. So you may choose one of the configuration management or automated install systems. I'm using FAI(Nice,Primarily for Debian/Ubuntu, but also possible for CentOS(I use it to install CentOS 6+7) for automated install and chef(quite complex, steep learning curve) for configuration management. If you stay with centos, kickstart may be more convenient for you for the topic of automatic installation. And what I've read ansible is a more easy approach than chef, if you want to manage your nodes after installation.

Isn't that clear? rpm gives an exit code of 1 which is different from 0 so you exit 1 .
So you need to differentiate the handling of the exit codes in failed depending on the application called upfront. Or you need different check functions for different applications.

I know puppet at very beginner level.And it can do the job but I want to lessen the burden on puppet. My main aim is once I install CentOS, all my configurations/package installations should occur via just one script.

Yep, script is idempotent. The mail aspect is easy to configure but I am not doing it right now. My main aim is to have this script ready so I can execute it multiple number of times without any issue.

---------- Post updated at 10:25 PM ---------- Previous update was at 10:16 PM ----------

Yeah, I did figured that out now. But not sure how do i handle it...

---------- Post updated at 11:17 PM ---------- Previous update was at 10:25 PM ----------

I now know what is the problem. rpm has different type of return codes.Therefore I either need to explore more rpm switches or write another subroutine code for "failed"

Any idea how to get list of return codes for rpm?

Exit codes are not printed. Exit codes are set:

You tried:

CODE="$(execute something)"
echo $CODE

That does not work. The Code is got by this:

# Just get the exit code
execute something
CODE=$?
echo $CODE

# ... or directly do something with it:
if execute something; then
    echo "it works"
else
    echo "did not work"
fi

# ... or for short, do something if it did work:
execute something && do this if the former command worked

# ... or for short, do something if it did NOT work:
execute something || do this if the former command failed

If the exit code in a function is not set(return command), the exit code is the that of the last command executed in that function.

So ......

#!/bin/bash

function is_installed_rpm { rpm -qi "$(rpm -qp "$1" 2>/dev/null)" >/dev/null 2>&1; }
function install_rpm      { is_installed_rpm || rpm -Uhv "$1" ; }

install_rpm $RPM_FILE 

---------- Post updated at 04:14 PM ---------- Previous update was at 03:48 PM ----------

of course you can simplify your script greatly....

function is_installed_rpm { rpm -qi "$(rpm -qp "$1" 2>/dev/null)" >/dev/null 2>&1; } 
function install_rpm      { is_installed_rpm "$1" || rpm -Uhv "$1" ; } 

# The following is an array of urls
PKG_URLS=( \
"https://....bla.rpm" \
"ftp://server.blub.com...rpm" \
)

for URL in "${PKG_URLS[@]}";do
        # rpm can install a package right from the url.
        install_rpm "$URL"
        failed
done

# yum does not care if a pkg is already installed, 
# so just install all in one command, even if all 
# pkgs are already installed, yum exits successfully.
#
# Maybe you have to split the yum-packages into two 
# steps, because you have to install the packages for
# repo-extension first before the other packages become
# available

YUM_PKGS="epel-release flash-plugin ...."

yum install $YUM_PACKAGES
failed

Thanks mate. Really appreciate it, shall try first thing tomorrow :slight_smile: