charts.d.dryrun-helper.sh 1.2 KB

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