deploy.sh 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. local dst=$3
  48. test -n "$dst" || dst=${src##*/}
  49. echo "$(_get_root "$grp")/$dst"
  50. }
  51. _get_root() {
  52. #
  53. # Find out target root for group $1
  54. #
  55. local grp="$1"
  56. local root=$(ini 1value "roots:$grp")
  57. local destdir=$(ini 1value ENV:DESTDIR)
  58. destdir=${destdir%/}
  59. case $destdir:$root in
  60. *:) die "missing in config.ini: roots:$grp" ;;
  61. :*) echo "$root" ;;
  62. *:*) echo "$destdir/$root" ;;
  63. esac
  64. }
  65. _maybe() {
  66. #
  67. # Call the deploy command $1 $@ unless in dry mode
  68. #
  69. debug "$@"
  70. local cmd="$1"; shift
  71. $MKIT_DRY && return
  72. case "$cmd" in
  73. cp|rm|rmdir|chmod|mkdir) $cmd "$@" ;;
  74. install) command -p install "$@" ;;
  75. *) die "bad command called";;
  76. esac
  77. }
  78. install() {
  79. #
  80. # Install product
  81. #
  82. local dst group mode src
  83. ini lskeys "files" \
  84. | sort \
  85. | uniq \
  86. | while read group;
  87. do
  88. mode=$(ini 1value "modes:$group")
  89. ini values "files:$group" \
  90. | while read src dst;
  91. do
  92. dst=$(_get_dst "$group" "$src" "$dst")
  93. _deploy_item "$src" "$dst" "$mode"
  94. done
  95. done
  96. test -f "$MKIT_LOCAL/autoclean" && clean
  97. true
  98. }
  99. uninstall() {
  100. #
  101. # Uninstall product
  102. #
  103. local dst group src
  104. ini lskeys "files" \
  105. | sort \
  106. | uniq \
  107. | while read group;
  108. do
  109. ini values "files:$group" \
  110. | while read src dst;
  111. do
  112. dst=$(_get_dst "$group" "$src" "$dst")
  113. _maybe rm -vrf "$dst"
  114. done
  115. done
  116. }