Browse Source

Major refactor of code. Separate modules and states. Support self installation

Olivier Mauras 1 year ago
parent
commit
3883967a56
4 changed files with 127 additions and 66 deletions
  1. 0
    1
      files/etc/iptables/iptables.rules
  2. 0
    0
      libs/dist_check
  3. 127
    52
      shmgmt.sh
  4. 0
    13
      states/iptables

+ 0
- 1
files/etc/iptables/iptables.rules View File

@@ -1 +0,0 @@
1
-dummy

modules/dist_check → libs/dist_check View File


+ 127
- 52
shmgmt.sh View File

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

+ 0
- 13
states/iptables View File

@@ -1,13 +0,0 @@
1
-#!/bin/sh
2
-set -e
3
-
4
-# First install package and exit if fails as it's required for subsequent steps
5
-$SHELL ${TMPDIR}/modules/${DIST}_package install iptables || exit 1
6
-
7
-# Then deploy file but ensure directory is present
8
-$SHELL ${TMPDIR}/modules/dir -d /etc/iptables
9
-$SHELL ${TMPDIR}/modules/file -b -s ${TMPDIR}/files/etc/iptables/iptables.rules \
10
-                              -d /etc/iptables/iptables.rules
11
-
12
-# Then if successful restart service
13
-$SHELL ${TMPDIR}/modules/${DIST}_service -e restart iptables