Whamcloud - gitweb
b=24091 find_linux_rpms utility
[fs/lustre-release.git] / build / funcs.sh
diff --git a/build/funcs.sh b/build/funcs.sh
new file mode 100644 (file)
index 0000000..acb1c51
--- /dev/null
@@ -0,0 +1,177 @@
+cleanup() {
+
+    true
+}
+
+error() {
+    local msg="$1"
+
+    [ -n "$msg" ] && echo -e "\n${0##*/}: $msg" >&$STDOUT
+
+}
+
+fatal() {
+
+    cleanup
+    error "$2"
+    exit $1
+
+}
+
+#
+# in a given directory, find the first rpm matching given requirements
+#
+find_rpm() {
+    local dir="$1"
+    local match_type="$2"
+    local match="$3"
+
+    pushd "$dir" > /dev/null || \
+        fatal 1 "Unable to chdir to directory \"$dir\" in find_rpm()"
+
+    local file
+    for file in $(ls *.rpm); do
+        if [ ! -f "$file" ]; then
+            continue
+        fi
+        case "$match_type" in
+            provides)
+                # match is any valid ERE (i.e. given to egrep) match
+                if rpm -q --provides -p "$file" 2>&$STDOUT | egrep "$match" >&$STDOUT; then
+                    echo "$file"
+                    popd >/dev/null
+                    return 0
+                fi
+                ;;
+            *)
+                popd >/dev/null
+                fatal 1 "Unknown match type \"$match_type\" given to find_rpm()"
+                ;;
+        esac
+    done
+
+    popd >/dev/null
+    return 1
+}
+
+find_linux_rpms() {
+    local prefix="$1"
+    local pathtorpms=${2:-"${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}/${TARGET_ARCH}"}
+
+    local kernel_rpms=$(find_linux_rpm "$prefix" "$pathtorpms")
+    # call a distro specific hook, if available
+    if type -p find_linux_rpms-$DISTRO; then
+        local rpm
+        if rpm=$(find_linux_rpms-$DISTRO "$prefix" "$pathtorpms"); then
+            kernel_rpms="$kernel_rpms $rpm"
+        else
+            return 255
+        fi
+    fi
+
+    echo "$kernel_rpms"
+    return 0
+
+}
+
+# a noop function which can be overridden by a distro method implementation
+resolve_arch() {
+    local arch="$1"
+
+    echo "$arch"
+}
+
+# XXX this needs to be re-written as a wrapper around find_rpm
+#     or just gotten rid of.  :-)
+find_linux_rpm() {
+    local prefix="$1"
+    local pathtorpms=${2:-"${KERNELRPMSBASE}/${lnxmaj}/${DISTRO}/${TARGET_ARCH}"}
+
+    [ -d $pathtorpms ] || return 255
+
+    local kernelbinaryrpm rpmfile
+    local wanted_kernel="${lnxmaj}${lnxmin}-${lnxrel}"
+
+    local arch ret=1
+        local found_rpm="" rpm
+        for rpm in $(ls ${pathtorpms}/*.$(resolve_arch $TARGET_ARCH $PATCHLESS).rpm); do
+            if rpm -q --provides -p "$rpm" 2>&$STDOUT | grep -q "kernel${prefix} = $wanted_kernel" 2>&$STDOUT; then
+                found_rpm="$rpm"
+                ret=0
+                break
+            fi
+        done
+    # see above "XXX"
+    #     [ -f "$found_rpm" ] && break
+    # done
+
+    echo "$found_rpm"
+    return $ret
+
+}
+
+# autodetect used Distro
+autodetect_distro() {
+
+    local name
+    local version
+
+    if which lsb_release >/dev/null 2>&1; then
+        name="$(lsb_release -s -i)"
+        version="$(lsb_release -s -r)"
+        case "$name" in
+            "EnterpriseEnterpriseServer")
+                name="oel"
+                version="${version%%.*}"
+                ;;
+            "RedHatEnterpriseServer" | "ScientificSL")
+                name="rhel"
+                version="${version%%.*}"
+                ;;
+            "SUSE LINUX")
+                name="sles"
+                ;;
+            *)
+                fatal 1 "I don't know what distro name $name and version $version is.\nEither update autodetect_distro() or use the --distro argument."
+                ;;
+        esac
+    else
+        echo "You really ought to install lsb_release for accurate distro identification"
+        # try some heuristics
+        if [ -f /etc/SuSE-release ]; then
+            name=sles
+            version=$(grep ^VERSION /etc/SuSE-release)
+            version=${version#*= }
+        elif [ -f /etc/redhat-release ]; then
+            #name=$(head -1 /etc/redhat-release)
+            name=rhel
+            version=$(echo "$distroname" |
+                      sed -e 's/^[^0-9.]*//g' | sed -e 's/[ \.].*//')
+        fi
+        if [ -z "$name" -o -z "$version" ]; then
+            fatal 1 "I don't know how to determine distro type/version.\nEither update autodetect_distro() or use the --distro argument."
+        fi
+    fi
+
+    echo ${name}${version}
+    return 0
+
+}
+
+# autodetect target
+autodetect_target() {
+    local distro="$1"
+
+    local target=""
+    case ${distro} in
+          oel5) target="2.6-oel5";;
+         rhel5) target="2.6-rhel5";;
+        sles10) target="2.6-sles10";;
+        sles11) target="2.6-sles11";;
+            *) fatal 1 "I don't know what distro $distro is.\nEither update autodetect_target() or use the --target argument.";;
+    esac
+
+    echo ${target}
+    return 0
+
+}