ovirt-node 2.2.0 release
[ovirt-node.git] / scripts / ovirt-rpmquery
blob4e1372655dbccaea3a01c7508fe18a53b3c1da94
1 #!/bin/bash
3 # rpm query replacement for the Node image without rpmdb
4 # list of RPMs is stored during the image creatation in /rpm-qa.txt
5 # only rpm -q is available, any other option returns error
7 PROG=$(basename $0)
8 # rpmdb snapshot created during image creation:
9 # rpm -qa --qf '%{NAME}\t%{VERSION}\t%{RELEASE}\t%{BUILDTIME}\n'
10 RPMDB="/rpm-qa.txt"
11 if [ ! -e $RPMDB ]; then
12 echo "$PROG: $RPMDB not found"
13 exit 2
16 OPTS=$(getopt -n $PROG -o qav --long query,all,quiet,verbose,qf:,queryformat: -- "$@")
17 eval set -- $OPTS
19 query=
20 all=
21 qf=
22 quiet=
23 verbose=
24 while [ "$#" -gt 0 ]; do
25 case "$1" in
26 -q|--query) query=1;;
27 -a|--all) all=1;;
28 --qf|--queryformat) qf="$2"; shift;;
29 --quiet) quiet=1;;
30 -v|--verbose) verbose=1;;
31 --) shift; break;;
32 *) echo "$PROG: invalid option, only --query is available"
33 exit 2;;
34 esac
35 shift
36 done
38 function print_pkg() {
39 local pkg="$1"
40 local regex
41 if [ "$pkg" ]; then
42 regex="^$pkg"$'\t'
43 else
44 regex=""
47 rc=0
48 if [ "$quiet" ]; then
49 grep -E -q "$regex" $RPMDB || rc=1
50 elif [ "$qf" ]; then
51 # actual queryformat is ignored
52 if ! grep -E "$regex" $RPMDB; then
53 echo "package $pkg is not installed"
54 rc=1
56 else
57 awk -v p="$regex" '
58 BEGIN { rc=1 }
59 match($0,p) { print $1"-"$2"-"$3; rc=0 } END { exit rc }
60 ' $RPMDB || rc=1
62 return $rc
66 if [ "$query" ]; then
67 if [ "$#" -eq 0 ]; then
68 if [ "$all" ]; then
69 print_pkg ""
70 exit
71 else
72 echo "$PROG: no arguments given for query"
73 exit 1
76 rc=0
77 for pkg in "$@"; do
78 print_pkg "$pkg" || rc=1
79 done
80 else
81 echo "$PROG: invalid option, only --query is available"
82 rc=1
85 exit $rc