Skip to content

Instantly share code, notes, and snippets.

@ahonor
Created September 25, 2012 22:45
Show Gist options
  • Save ahonor/3784926 to your computer and use it in GitHub Desktop.
Save ahonor/3784926 to your computer and use it in GitHub Desktop.
Rundeck option provider. Generates JSON about packages in a yum repo
#!/bin/bash
#
# repoqery --
#
# This CGI generates JSON format suitable for Rundeck
# job option data.
#
# -----------------------------------------------------------
#
# die --
#
# This function prints a message and returns.
# If a message is specified it is written to stdout
#
# Results:
# exit 1
#
# -----------------------------------------------------------
function die {
echo "Status: 500"
echo "Content-Type: text/plain"
echo
echo "$*"
exit 1
}
#
# Parse the QUERY_STRINGS environment variable set by
# the cgi processor (or invoking shell).
#
for VAR in `echo $QUERY_STRING | tr "&" "\t"`
do
NAME=$(echo $VAR | tr = " " | awk '{print $1}';);
VALUE=$(echo $VAR | tr = " " | awk '{ print $2}' | tr + " ");
declare $NAME="$VALUE";
done
#
# Check required parameter
#
[ -z "$repo" ] && die "repo not specified"
#
# Lookup the baseurl from the yum client config
#
baseurl=$(awk -F '=' '/baseurl/ {print $2}' /etc/yum.repos.d/$repo.repo)
[ -z "$baseurl" ] && die "error looking up repo base url: /etc/yum.repos.d/$repo.repo"
#
# Set a temp directory to manage working files
#
TMPDIR=$(mktemp -d /tmp/repoquery.XXXXXX)
#
# Run the repoquery command and sort the results. Store the out and err data.
#
/usr/bin/repoquery --disablerepo=\* --enablerepo="$repo" -a --show-duplicates \
--qf '%{name}-%{version}-%{release}.rpm' | sort >$TMPDIR/repoquery.out 2>$TMPDIR/repoquery.err
[ ! $? -eq 0 ] && die "repoquery failure: $(cat $TMPDIR/repoquery.err)"
#
# Generate the response
#
echo Content-type: application/json
echo
echo "["
while read package
do
printf '{"name":"%s", "value":"%s/%s"},\n' "$package" "$baseurl" "$package"
done < $TMPDIR/repoquery.out
echo "]"
#
# Clean up the temp directory
#
rm -rf $TMPDIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment