charts.d.dryrun-helper.sh 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # shellcheck disable=SC2181
  4. # will stop the script for any error
  5. set -e
  6. me="$0"
  7. name="$1"
  8. chart="$2"
  9. conf="$3"
  10. can_diff=1
  11. tmp1="$(mktemp)"
  12. tmp2="$(mktemp)"
  13. myset() {
  14. set | grep -v "^_=" | grep -v "^PIPESTATUS=" | grep -v "^BASH_LINENO="
  15. }
  16. # save 2 'set'
  17. myset >"$tmp1"
  18. myset >"$tmp2"
  19. # make sure they don't differ
  20. diff "$tmp1" "$tmp2" >/dev/null 2>&1
  21. if [ $? -ne 0 ]; then
  22. # they differ, we cannot do the check
  23. echo >&2 "$me: cannot check with diff."
  24. can_diff=0
  25. fi
  26. # do it again, now including the script
  27. myset >"$tmp1"
  28. # include the plugin and its config
  29. if [ -f "$conf" ]; then
  30. # shellcheck source=/dev/null
  31. . "$conf"
  32. if [ $? -ne 0 ]; then
  33. echo >&2 "$me: cannot load config file $conf"
  34. rm "$tmp1" "$tmp2"
  35. exit 1
  36. fi
  37. fi
  38. # shellcheck source=/dev/null
  39. . "$chart"
  40. if [ $? -ne 0 ]; then
  41. echo >&2 "$me: cannot load chart file $chart"
  42. rm "$tmp1" "$tmp2"
  43. exit 1
  44. fi
  45. # remove all variables starting with the plugin name
  46. myset | grep -v "^$name" >"$tmp2"
  47. if [ $can_diff -eq 1 ]; then
  48. # check if they are different
  49. # make sure they don't differ
  50. diff "$tmp1" "$tmp2" >&2
  51. if [ $? -ne 0 ]; then
  52. # they differ
  53. rm "$tmp1" "$tmp2"
  54. exit 1
  55. fi
  56. fi
  57. rm "$tmp1" "$tmp2"
  58. exit 0