#!/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__