123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- #!/bin/bash
-
- shellfu import jat
- shellfu import pretty
-
- #
- # upgrade path utilities
- #
-
- preupg_upath__find() {
- #
- # Find module according to criteria
- #
- # Look into installed upgrade path modules and print all ids or ids
- # selected by a condition.
- #
- # preupg_upath__find [-FIELD RE].. [-print FIELD[:FIELD]..]
- #
- # Where FIELD is one of supported fields and RE is basic regular expression
- # to be matched against that field. List of all modules found on the
- # system is collected; then each module is checked against all conditions.
- # If it matches all conditions, its short id is printed, unless `-print`
- # is provided, in which case the argument that follows is interpreted as
- # colon-separated list of fields that should be printed. Multiple fields
- # on output are separated by colon.
- #
- # List of supported conditions:
- #
- # * `id` - short id,
- # * `xid` - long "xccdf_preupg_rule_" id,
- # * `relpath` - relative path from upgrade path root
- # * `path` - absolute path
- # * `name` - only the name part (directory name)
- # * `group` - "group" part of the id,
- # * `upath` - upgrade path names.
- #
- local Conds=()
- local cond_name
- local cond_arg
- local dir
- local usage="usage: preupg_upath__find [-FIELD RE].."
- while true; do case $1 in
- -*)
- cond_name=${1:1}
- cond_arg=$2
- shift 2 || {
- jat__log_error "$usage"
- return 2
- }
- Conds+=("$cond_name:$cond_arg")
- ;;
- "")
- break
- ;;
- *)
- shift 2 || {
- jat__log_error "$usage"
- return 2
- }
- esac done
- __preupg_upath__find_dirs \
- | while read -r dir;
- do
- __preupg_upath__find_ckconds "$dir"
- done
- }
-
-
- # # your brain upgrade path #
- # INTERNAL # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
- # # does not cross this line #
-
- __preupg_upath__find_dirs() {
- #
- # List all paths to module directories
- #
- local path
- rpm -qal "preupgrade-assistant-el6toel*" \
- | grep -v '/usr/share/preupgrade/[[:alnum:]]\+/common' \
- | while read -r path;
- do
- test -d "$path" || continue
- test -f "$path/group.xml" || continue
- test -f "$path/check" || continue
- echo "$path"
- done
- }
-
- __preupg_upath__find_ckconds() {
- #
- # Check all condition pairs
- #
- local mpath=$1
- local cond
- local cond_name
- local cond_arg
- local ok=true
- local fields=upath:id
- local field
- for cond in "${Conds[@]}";
- do
- cond_name=${cond%%:*}
- cond_arg=${cond#*:}
- case $cond_name in
- id|xid|group|name|path|relpath|upath)
- __preupg_upath__find_getfield "$cond_name" "$mpath" \
- | grep -q "$cond_arg" \
- || ok=false
- ;;
- print)
- fields=$cond_arg
- ;;
- *)
- jat__log_error "unknown condition: $cond"
- return 2
- ;;
- esac
- done
- $ok || return 1
- __preupg_upath__find_fmt "$fields" "$mpath"
- }
-
- __preupg_upath__find_fmt() {
- #
- # Format meta data of module $2 according to field list $1
- #
- local fields=$1
- local mpath=$2
- local field
- for field in ${fields//:/ };
- do
- __preupg_upath__find_getfield "$field" "$mpath"
- done \
- | paste -sd:
- }
-
- __preupg_upath__find_getfield() {
- #
- # Get field $1 from module at path $1
- #
- local field=$1
- local mpath=$2
- local getfield=__preupg_upath__find_getfield
- case $field in
- id)
- $getfield relpath "$mpath" \
- | sed 's:/:_:g'
- ;;
- xid)
- echo -n "xccdf_preupg_rule_"
- echo -n "$($getfield id "$mpath")"
- echo -n "_check"
- ;;
- name)
- echo "${mpath##*/}"
- ;;
- group)
- $getfield relpath "$mpath" \
- | sed 's:/[^/]*$::; s:/:_:g'
- ;;
- upath)
- cut -d/ -f5 <<<"${mpath}" \
- | __preupg_upath__find_upath_old2new
- ;;
- relpath)
- cut -d/ -f6- <<<"${mpath}"
- ;;
- path)
- echo "$mpath"
- ;;
- *)
- jat__log_error "unknown field: $field"
- return 2
- ;;
- esac
- }
-
- __preupg_upath__find_upath_old2new() {
- #
- # Convert from old style upgrade path name to new
- #
- # Usage: echo el6toel7 | __preupg_upath__find_upath_old2new
- #
- local oupath
- while read -r oupath;
- do
- case $oupath in
- RHEL?_?) echo "el${oupath:4:1}toel${oupath:6}" ;;
- *) jat__log_error "unknown old-style upgrade path name: $oupath" ;;
- esac
- done
- }
-
- #shellfu module-version=__MKIT_PROJ_VERSION__
|