Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 236 lines (186 sloc) 5.17 KB
#!/bin/bash
#
# Mike Grozak <mike.grozak@gmail.com>
#
# - disablerepo
# required - no
# Repoid of repositories to disable for the install/update operation. These repos will not persist beyond the transaction. Multiple repos separated with a ',' (added in Ansible 0.9).
# - enablerepo
# required: no
# Repoid of repositories to enable for the install/update operation. These repos will not persist beyond the transaction multiple repos separated with a ',' (added in Ansible 0.9)
# - name
# requried: yes
# Package name, or package specifier with version, like name-1.0.
# - state
# required: no
# default: present
# choices: present latest absent
# Whether to install (present, latest), or remove (absent) a package.
# - options
# Various options to yum
# - gpgcheck
# required: no
# default: not specified
# choices: no
# Disable the gpg check of yum by suppliying --nogpgcheck CLI option
#
# Uncomment the following two strings in order to debug the module
#exec 2>/tmp/yum2.log
#set -x
function exit_success() {
print_json 'msg' 'changed'
exit 0
}
function exit_failed() {
print_json 'failed' 'msg'
exit 1
}
function print_json() {
echo '{'
coma=""
for arg in $@
do
[[ -n "${!arg}" ]] && val="${!arg}" || continue
if [[ $val =~ ^\[ ]]; then
echo " ${coma} \"${arg}\": ${val}"
else
if [[ $arg == "changed" || $arg == "failed" ]]; then
echo " ${coma} \"${arg}\": ${val}"
else
echo " ${coma} \"${arg}\": \"${val}\""
fi
fi
[[ -z "${coma}" ]] && coma=","
done
echo '}'
}
source ${1}
YUM_REPOS=/etc/yum.repos.d
# Check the correctness of the input first
if [[ -z "${name}" ]]; then
failed="true"
msg="Please, specify the name of the package to deal with"
exit_failed
fi
[[ -z "${state}" ]] && state="present"
case $state in
'present'|'latest'|'absent'|'installed')
;;
*)
failed="true"
msg="Please, specify the correct state: present, absent or latest"
exit_failed
;;
esac
if [[ ! -z "${enablerepo}" || ! -z "${disablerepo}" ]]; then
for i in ${enablerepo//,/ } ${disablerepo//,/ }
do
if ! grep -q "\[$i\]" $YUM_REPOS/*.repo; then
failed="true"
failed_repos="$failed_repos $i"
fi
done
if [[ ! -z "${failed}" ]]; then
failed="true"
msg="The following repos could not be found in yum configuration in $YUM_REPOS directory:${failed_repos}"
exit_failed
fi
fi
# Do the actions, finally
if [[ ! -z ${name} ]]; then
name=${name//,/ }
fi
cmd="yum -q -y"
to_install=""
to_update=""
to_remove=""
[[ ! -z "${enablerepo}" && "${state}" != "absent" ]] && cmd="$cmd --enablerepo=${enablerepo}"
[[ ! -z "${disablerepo}" && "${state}" != "absent" ]] && cmd="$cmd --disablerepo=${disablerepo}"
[[ ! -z "${gpgcheck}" && "${gpgcheck}" == "no" ]] && cmd="$cmd --nogpgcheck"
case ${state} in
'present'|'installed')
# collect the names of the packages to install - do not install already installed
for i in ${name}
do
if [[ "$i" =~ ^.*/.*\.rpm$ ]]; then
real_name=$(rpm -q --queryformat "%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n" -p $i)
! rpmquery --quiet $real_name && to_install="${to_install} ${i}"
else
! rpmquery --quiet $i && to_install="${to_install} ${i}"
fi
done
;;
'latest')
# Collect the packages to install/update first
for i in ${name}
do
if ! rpmquery --quiet $i; then
to_install="${to_install} $i"
else
to_update="${to_update} $i"
fi
done
;;
'absent')
to_remove=''
for i in ${name}
do
rpmquery --quiet $i && to_remove="${to_remove} $i"
done
;;
esac
# remove leading space from our strings
to_install="${to_install# }"
to_update="${to_update# }"
to_remove="${to_remove# }"
if [[ ! -z "${to_install}" ]]; then
cmd="${cmd} install"
${cmd} ${to_install} 2>&1 &>/dev/null
# check, that the packages were installed sucessfully
x=""
for i in ${to_install}
do
# if it's local installation - normalize the name
if [[ "$i" =~ ^.*/.*\.rpm$ ]]; then
i=${i##*/}
i=${i/\.rpm/}
fi
! rpmquery --quiet $i && x="$x $i"
done
x="${x# }"
if [[ ! -z "$x" ]]; then
msg="$x packages has been failed for installation;"
[[ "${x}" != "${to_install}" ]] && changed="true" || failed="true"
else
changed="true"
fi
fi
if [[ ! -z "${to_update}" ]]; then
# this respect globs with version numbers
cmd2="${cmd} update-to"
version="$(rpmquery --queryformat=\"%{VERSION}-%{RELEASE}\" ${to_update})"
if ${cmd2} ${to_update} 2>&1 &>/dev/null; then
version_now="$(rpmquery --queryformat=\"%{VERSION}-%{RELEASE}\" ${to_update})"
[[ "${version}" != "${version_now}" ]] && changed="true"
fi
fi
if [[ ! -z "${to_remove}" ]]; then
cmd="$cmd remove"
x=""
${cmd} ${to_remove} 2>&1 &>/dev/null && changed="true"
for i in "${to_remove}"
do
rpmquery --quiet $i && x="$x $i"
if [[ ! -z "${x}" ]]; then
msg="$x packages has been failed to remove;"
[[ "${x}" == "${to_remove}" ]] && failed="true"
fi
done
fi
[[ -z "${msg}" ]] && msg="OK"
[[ -z "${changed}" ]] && changed="false"
if [[ -z ${failed} ]]; then
exit_success
else
exit_failed
fi
You can’t perform that action at this time.