]> git.friedersdorff.com Git - max/saltfiles.git/commitdiff
Add utils
authorMaximilian Friedersdorff <max@friedersdorff.com>
Fri, 1 Nov 2019 10:11:01 +0000 (10:11 +0000)
committerMaximilian Friedersdorff <max@friedersdorff.com>
Fri, 1 Nov 2019 10:11:01 +0000 (10:11 +0000)
states/setup.sls
states/utils/arch.sls [new file with mode: 0644]
states/utils/files/onchanges.bash [new file with mode: 0644]
states/utils/init.sls [new file with mode: 0644]

index 4c3b2f2d761214172974d62bf429c2d965c2c2bc..137cda94a18a02f0eeebbf60940049490deeb379 100644 (file)
@@ -4,3 +4,4 @@ include:
   - pass
   - firefox
   - glocker
+  - utils
diff --git a/states/utils/arch.sls b/states/utils/arch.sls
new file mode 100644 (file)
index 0000000..0a0225a
--- /dev/null
@@ -0,0 +1,3 @@
+install inotify tools:
+  pkg.installed:
+    - name: inotify-tools
diff --git a/states/utils/files/onchanges.bash b/states/utils/files/onchanges.bash
new file mode 100644 (file)
index 0000000..d7c6bb9
--- /dev/null
@@ -0,0 +1,51 @@
+#!/bin/bash
+#
+# Watch current directory (recursively) for file changes, and execute
+# a command when a file or directory is created, modified or deleted.
+#
+# Written by: Senko Rasic <senko.rasic@dobarkod.hr>
+#
+# Requires Linux, bash and inotifywait (from inotify-tools package).
+#
+# To avoid executing the command multiple times when a sequence of
+# events happen, the script waits one second after the change - if
+# more changes happen, the timeout is extended by a second again.
+#
+# Installation:
+#     chmod a+rx onchange.sh
+#     sudo cp onchange.sh /usr/local/bin
+#
+# Example use - rsync local changes to the remote server:
+#    
+#    onchange.sh rsync -avt . host:/remote/dir
+#
+# Released to Public Domain. Use it as you like.
+#
+
+EVENTS="CREATE,CLOSE_WRITE,DELETE,MODIFY,MOVED_FROM,MOVED_TO"
+
+if [ -z "$1" ]; then
+    echo "Usage: $0 cmd ..."
+    exit -1;
+fi
+
+inotifywait -e "$EVENTS" -m -r --format '%:e %f' . | (
+    WAITING="";
+    while true; do
+        LINE="";
+        read -t 1 LINE;
+        if test -z "$LINE"; then
+            if test ! -z "$WAITING"; then
+                    echo "CHANGE";
+                    WAITING="";
+            fi;
+        else
+            WAITING=1;
+        fi;
+    done) | (
+    while true; do
+        read TMP;
+        echo $@
+        eval $@
+    done
+)
diff --git a/states/utils/init.sls b/states/utils/init.sls
new file mode 100644 (file)
index 0000000..16cfe94
--- /dev/null
@@ -0,0 +1,11 @@
+include:
+  - utils.{{ grains['os'] | lower }}
+
+install on changes:
+  file.managed:
+    - name: {{ grains['homedir'] }}/.local/bin/onchanges
+    - source: salt://utils/files/onchanges.bash
+    - mode: 750
+    - owner: {{ grains['user'] }}
+    - group: {{ grains['user'] }}
+