Skip to content

Instantly share code, notes, and snippets.

@fuzzy
Created May 15, 2015 21:29
Show Gist options
  • Save fuzzy/b18daa7f5a229b89f88e to your computer and use it in GitHub Desktop.
Save fuzzy/b18daa7f5a229b89f88e to your computer and use it in GitHub Desktop.
must() {
case "${1}" in
(have)
case "${2}" in
(file) test -f ${3} || err "Missing file: ${3}" ;;
(link) test -L ${3} || err "Missing symlink: ${3}" ;;
(dir) test -d ${3} || err "Missing dir: ${3}" ;;
(prog) which ${3} >/dev/null 2>&1 || err "Missing ${2}: ${3}" ;;
(user) test ! -z "$(grep ${3} /etc/passwd)" || err "Missing ${2}: ${3}" ;;
(group) test ! -z "$(grep ${3} /etc/group)" || err "Missing ${2}: ${3}" ;;
(pkg)
case "${QC_DISTRO}" in
(redhat)
$(which rpm) -q ${3} >/dev/null 2>&1 || err "Missing ${2}: ${3}"
;;
(*) err "This is an unsupported platform." ;;
esac
;;
(*) cat ${QC_BASE_DIR}/help/must-have.txt 2>/dev/null ;;
esac
;;
(lack)
case "${2}" in
(file) test ! -f ${3} || err "Present file: ${3}" ;;
(link) test ! -L ${3} || err "Present symlink: ${3}" ;;
(dir) test ! -d ${3} || err "Present dir: ${3}" ;;
(prog) which ${3} >/dev/null 2>&1 || err "Present ${2}: ${3}" ;;
(user) test -z "$(grep ${3} /etc/passwd)" || err "Present ${2}: ${3}" ;;
(group) test -z "$(grep ${3} /etc/group)" || err "Present ${2}: ${3}" ;;
(pkg)
case "${QC_DISTRO}" in
(redhat)
($(which rpm) -q ${3} >/dev/null 2>&1 && test $? -ne 0) || err "Present ${2}: ${3}"
;;
(*) err "This is an unsupported platform." ;;
esac
;;
(*) cat ${QC_BASE_DIR}/help/must-lack.txt 2>/dev/null ;;
esac
;;
(be)
case "${2}" in
(running)
if test -x /etc/init.d/${3}; then
/etc/init.d/${3} status 1>/dev/null 2>/dev/null
if test $? -ne 0; then
err "Service (${3}) is not running."
fi
else
err "The service (${3}) is not available."
fi
;;
(stopped)
if test -x /etc/init.d/${3}; then
/etc/init.d/${3} status 1>/dev/null 2>/dev/null
if test $? -eq 0; then
err "Service (${3}) is still running."
fi
else
info "The service (${3}) is not available."
fi
;;
(enabled)
case "${QC_DISTRO}" in
(redhat) test -z "$(chkconfig --list|grep ${3}|grep on)" && error "Service (${3}) not enabled at boot." ;;
(debian)
case "$(initctl list|grep ${3}|awk '{print $2}'|awk -f'/' '{print $1}')" in
(stop) error "Service (${3}) not enabled at boot." ;;
esac
;;
esac
;;
(disabled)
case "${QC_DISTRO}" in
(redhat) test ! -z "$(chkconfig --list|grep ${3}|grep on)" && error "Service (${3}) still enabled at boot." ;;
(debian)
case "$(initctl list|grep ${3}|awk '{print $2}'|awk -f'/' '{print $1}')" in
(start) error "Service (${3}) still enabled at boot." ;;
esac
;;
esac
;;
(*) cat ${QC_BASE_DIR}/help/must-be.txt 2>/dev/null ;;
esac
;;
(*) cat ${QC_BASE_DIR}/help/must.txt 2>/dev/null ;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment