preupg_upath.sh.skel 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/bin/bash
  2. shellfu import jat
  3. shellfu import pretty
  4. #
  5. # upgrade path utilities
  6. #
  7. preupg_upath__find() {
  8. #
  9. # Find module according to criteria
  10. #
  11. # Look into installed upgrade path modules and print all ids or ids
  12. # selected by a condition.
  13. #
  14. # preupg_upath__find [-FIELD RE].. [-print FIELD[:FIELD]..]
  15. #
  16. # Where FIELD is one of supported fields and RE is basic regular expression
  17. # to be matched against that field. List of all modules found on the
  18. # system is collected; then each module is checked against all conditions.
  19. # If it matches all conditions, its short id is printed, unless `-print`
  20. # is provided, in which case the argument that follows is interpreted as
  21. # colon-separated list of fields that should be printed. Multiple fields
  22. # on output are separated by colon.
  23. #
  24. # List of supported conditions:
  25. #
  26. # * `id` - short id,
  27. # * `xid` - long "xccdf_preupg_rule_" id,
  28. # * `relpath` - relative path from upgrade path root
  29. # * `path` - absolute path
  30. # * `name` - only the name part (directory name)
  31. # * `group` - "group" part of the id,
  32. # * `upath` - upgrade path names.
  33. #
  34. local Conds=()
  35. local cond_name
  36. local cond_arg
  37. local dir
  38. local usage="usage: preupg_upath__find [-FIELD RE].."
  39. while true; do case $1 in
  40. -*)
  41. cond_name=${1:1}
  42. cond_arg=$2
  43. shift 2 || {
  44. jat__log_error "$usage"
  45. return 2
  46. }
  47. Conds+=("$cond_name:$cond_arg")
  48. ;;
  49. "")
  50. break
  51. ;;
  52. *)
  53. shift 2 || {
  54. jat__log_error "$usage"
  55. return 2
  56. }
  57. esac done
  58. __preupg_upath__find_dirs \
  59. | while read -r dir;
  60. do
  61. __preupg_upath__find_ckconds "$dir"
  62. done
  63. }
  64. # # your brain upgrade path #
  65. # INTERNAL # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
  66. # # does not cross this line #
  67. __preupg_upath__find_dirs() {
  68. #
  69. # List all paths to module directories
  70. #
  71. local path
  72. rpm -qal "preupgrade-assistant-el6toel*" \
  73. | grep -v '/usr/share/preupgrade/[[:alnum:]]\+/common' \
  74. | while read -r path;
  75. do
  76. test -d "$path" || continue
  77. test -f "$path/group.xml" || continue
  78. test -f "$path/check" || continue
  79. echo "$path"
  80. done
  81. }
  82. __preupg_upath__find_ckconds() {
  83. #
  84. # Check all condition pairs
  85. #
  86. local mpath=$1
  87. local cond
  88. local cond_name
  89. local cond_arg
  90. local ok=true
  91. local fields=upath:id
  92. local field
  93. for cond in "${Conds[@]}";
  94. do
  95. cond_name=${cond%%:*}
  96. cond_arg=${cond#*:}
  97. case $cond_name in
  98. id|xid|group|name|path|relpath|upath)
  99. __preupg_upath__find_getfield "$cond_name" "$mpath" \
  100. | grep -q "$cond_arg" \
  101. || ok=false
  102. ;;
  103. print)
  104. fields=$cond_arg
  105. ;;
  106. *)
  107. jat__log_error "unknown condition: $cond"
  108. return 2
  109. ;;
  110. esac
  111. done
  112. $ok || return 1
  113. __preupg_upath__find_fmt "$fields" "$mpath"
  114. }
  115. __preupg_upath__find_fmt() {
  116. #
  117. # Format meta data of module $2 according to field list $1
  118. #
  119. local fields=$1
  120. local mpath=$2
  121. local field
  122. for field in ${fields//:/ };
  123. do
  124. __preupg_upath__find_getfield "$field" "$mpath"
  125. done \
  126. | paste -sd:
  127. }
  128. __preupg_upath__find_getfield() {
  129. #
  130. # Get field $1 from module at path $1
  131. #
  132. local field=$1
  133. local mpath=$2
  134. local getfield=__preupg_upath__find_getfield
  135. case $field in
  136. id)
  137. $getfield relpath "$mpath" \
  138. | sed 's:/:_:g'
  139. ;;
  140. xid)
  141. echo -n "xccdf_preupg_rule_"
  142. echo -n "$($getfield id "$mpath")"
  143. echo -n "_check"
  144. ;;
  145. name)
  146. echo "${mpath##*/}"
  147. ;;
  148. group)
  149. $getfield relpath "$mpath" \
  150. | sed 's:/[^/]*$::; s:/:_:g'
  151. ;;
  152. upath)
  153. cut -d/ -f5 <<<"${mpath}" \
  154. | __preupg_upath__find_upath_old2new
  155. ;;
  156. relpath)
  157. cut -d/ -f6- <<<"${mpath}"
  158. ;;
  159. path)
  160. echo "$mpath"
  161. ;;
  162. *)
  163. jat__log_error "unknown field: $field"
  164. return 2
  165. ;;
  166. esac
  167. }
  168. __preupg_upath__find_upath_old2new() {
  169. #
  170. # Convert from old style upgrade path name to new
  171. #
  172. # Usage: echo el6toel7 | __preupg_upath__find_upath_old2new
  173. #
  174. local oupath
  175. while read -r oupath;
  176. do
  177. case $oupath in
  178. RHEL?_?) echo "el${oupath:4:1}toel${oupath:6}" ;;
  179. *) jat__log_error "unknown old-style upgrade path name: $oupath" ;;
  180. esac
  181. done
  182. }
  183. #shellfu module-version=__MKIT_PROJ_VERSION__