A simple shell based configuration management tool

shmgmt.sh 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/bin/sh
  2. CONFIG=~/.shmgmt
  3. INSTALLDIR="/usr/local/libexec/shmgmt"
  4. INSTALLBIN="/usr/local/bin/shmgmt"
  5. SHMGMTREPO="https://git.mauras.ch/shmgmt/shmgmt"
  6. STATEREPO="https://git.mauras.ch/shmgmt/states_example"
  7. STATEDIR="/tmp/shmgmt"
  8. GITCLONEURL="https://git.mauras.ch/Various/git-clone/raw/branch/master/git-clone"
  9. GITCLONEBIN="/usr/local/bin/git-clone"
  10. SHELL="/bin/sh"
  11. _install_gitclone() {
  12. # Check if git-clone is installed, if not install it
  13. if [ ! -f $GITCLONEBIN ]; then
  14. echo "==> Installing git-clone binary in PATH"
  15. [ ! -d `dirname $GITCLONEBIN` ] && mkdir -p `dirname $GITCLONEBIN`
  16. wget ${GITCLONEURL}_${LIBC} -O $GITCLONEBIN || exit 1
  17. chmod 755 $GITCLONEBIN || exit 1
  18. fi
  19. }
  20. _clone_repo() {
  21. URL=$1
  22. DESTDIR=$2
  23. # Ensure that $DESTDIR is clean
  24. if [ ! -d $DESTDIR ]; then
  25. mkdir -p $DESTDIR
  26. else
  27. rm -rf $DESTDIR
  28. mkdir -p $DESTDIR
  29. fi
  30. echo "==> Cloning $URL to $DESTDIR"
  31. $GITCLONEBIN $URL $DESTDIR > /dev/null 2>&1
  32. RET=$?
  33. if [ $RET -ne 0 ]; then
  34. echo " ! Error cloning repo"
  35. exit 1
  36. fi
  37. }
  38. _install() {
  39. INSTALLDIR="$1"
  40. echo "==> Install shmgmt locally"
  41. # Detect libc version - glibc or musl
  42. echo "==> Detect libc version"
  43. if `ldd --version 2>&1 | head -1 | grep musl >/dev/null 2>&1`; then
  44. LIBC="musl"
  45. elif `ldd --version 2>&1 | head -1 | grep GNU >/dev/null 2>&1`; then
  46. LIBC="glibc"
  47. else
  48. echo " ! Error unknown libc version"
  49. exit 1
  50. fi
  51. # Ensure git-clone binary is deployed
  52. _install_gitclone
  53. # Now install shmgmt locally
  54. _clone_repo $SHMGMTREPO $INSTALLDIR
  55. # Create shmgmt symlink
  56. ln -sf ${INSTALLDIR}/shmgmt.sh $INSTALLBIN || exit 1
  57. exit 0
  58. }
  59. _process_states() {
  60. INSTALLDIR=$1
  61. STATEDIR=$2
  62. # Now install shmgmt locally
  63. _clone_repo $STATEREPO $STATEDIR
  64. # Load libs
  65. for LIB in ${INSTALLDIR}/libs/*; do
  66. . ${LIB}
  67. done
  68. for MODULE in ${INSTALLDIR}/modules/*; do
  69. if [ `echo $MODULE | grep ${DIST}` ]; then
  70. export `basename $MODULE | cut -f 2- -d _`="${SHELL} ${MODULE}"
  71. else
  72. export `basename $MODULE`="${SHELL} ${MODULE}"
  73. fi
  74. done
  75. # Loop over states
  76. for STATE in ${STATEDIR}/states/* ; do
  77. BSTATE=`basename ${STATE}`
  78. echo "==> Applying ${BSTATE}"
  79. STATEDIR=${STATEDIR} \
  80. DIST=${DIST} \
  81. $SHELL ${STATE}
  82. RET=$?
  83. if [ $RET -eq 0 ]; then
  84. echo "==> ${BSTATE}: OK"
  85. elif [ $RET -eq 1 ]; then
  86. echo "==> ${BSTATE}: ERROR!"
  87. else
  88. echo "==> ${BSTATE}: Unknown return - $RET"
  89. fi
  90. done
  91. }
  92. _cleanup() {
  93. # Cleanup local git clone
  94. rm -rf $STATEDIR
  95. }
  96. # Retrieve arguments
  97. while getopts ":c:r:di" opt; do
  98. case $opt in
  99. c)
  100. CONFIG=$OPTARG
  101. ;;
  102. r)
  103. STATEREPO=$OPTARG
  104. ;;
  105. d)
  106. STATEDIR=$OPTARG
  107. ;;
  108. i)
  109. _install $INSTALLDIR
  110. ;;
  111. *)
  112. echo " ! Unknown parameter \"$PARAM\""
  113. exit 1
  114. ;;
  115. esac
  116. done
  117. shift $((OPTIND -1))
  118. # Load config if it exists
  119. if [ ! -z $CONFIG ] && [ -f $CONFIG ]; then
  120. echo "==> Loading user config"
  121. . $CONFIG
  122. fi
  123. # Sanity check
  124. if [ -z $STATEREPO ] || [ -z $STATEDIR ] || [ -z $INSTALLDIR ]; then
  125. echo "!! Missing needed variables STATEREPO, STATEDIR or INSTALLDIR to work"
  126. fi
  127. # Clone and process states
  128. _process_states $INSTALLDIR $STATEDIR