deploy.sh 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/bin/bash
  2. _maybe() {
  3. #
  4. # Call the deploy command $1 $@ unless in dry mode
  5. #
  6. debug "$@"
  7. local cmd="$1"; shift
  8. $MKIT_DRY && return
  9. case "$cmd" in
  10. cp|rm|rmdir|chmod|mkdir) $cmd "$@" ;;
  11. install) command -p install "$@" ;;
  12. *) die "bad command called";;
  13. esac
  14. }
  15. deploy_item() {
  16. #
  17. # Deploy item and make it look like wanted
  18. #
  19. # usage: deploy_item src dst [mode]
  20. #
  21. # Both src and dst must be names of actual items[1],
  22. # whereas dst must not exist. On update, dst is
  23. # usually to be replaced but that is uninstall's
  24. # job!
  25. #
  26. # [1] Ie. src=foo and dst=/foo/bar does *always*
  27. # mean that foo will become 'bar'. This is
  28. # different than traditional `cp` behavior,
  29. # when this depends if 'bar' already exists
  30. # as a directory.
  31. #
  32. # If mode is omitted or empty, MKIT_DEFAULT_MODE is
  33. # used instead.
  34. #
  35. # Directories are copied recursively, and mode is
  36. # applied only to files.
  37. #
  38. local src="$1"
  39. local dst="$2"
  40. local mode="${3:-$MKIT_DEFAULT_MODE}"
  41. if test -d "$src";
  42. then
  43. _maybe mkdir -vp "$(dirname "$dst")"
  44. _maybe cp -Tvr "$src" "$dst"
  45. find "$dst" -type f \
  46. | while read chmod_item;
  47. do
  48. _maybe chmod "$mode" "$chmod_item"
  49. done
  50. else
  51. _maybe install -DTvm "$mode" "$src" "$dst"
  52. fi
  53. }
  54. get_dst() {
  55. #
  56. # Find out target path for src file $2 of group $1
  57. #
  58. local grp=$1
  59. local src=$2
  60. echo "$(get_root "$grp")/$(ini 1value "files:$grp:$src")"
  61. }
  62. get_root() {
  63. #
  64. # Find out target rooot for group $1
  65. #
  66. local grp="$1"
  67. local root=$(ini 1value "roots:$grp")
  68. test -n "$root" || die "missing in config.ini: roots:$grp"
  69. echo "$(ini 1value ENV:DESTDIR)$root"
  70. }
  71. install() {
  72. #
  73. # Install product
  74. #
  75. local dst group mode src
  76. ini values "lists:group" \
  77. | while read group;
  78. do
  79. mode=$(ini 1value "modes:$group")
  80. ini lskeys "files:$group" \
  81. | while read src;
  82. do
  83. dst=$(get_dst "$group" "$src")
  84. deploy_item "$src" "$dst" "$mode"
  85. done
  86. done
  87. test -f "$MKIT_LOCAL/autoclean" && clean
  88. true
  89. }
  90. uninstall() {
  91. #
  92. # Uninstall product
  93. #
  94. local dst group src
  95. ini values "lists:group" \
  96. | while read group;
  97. do
  98. ini lskeys "files:$group" \
  99. | while read src;
  100. do
  101. dst=$(get_dst "$group" "$src")
  102. _maybe rm -vrf "$dst"
  103. done
  104. done
  105. }