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.
At professional level is this script good enough? Yeah I am newbie therefore asking.
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.
A few thoughts on - not necessarily exhaustive answers to - your questions:
"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:
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.
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"
# 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.
---------- 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