deploy.sh 2.8KB

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