Page MenuHomeDevCentral

D1023.diff
No OneTemporary

D1023.diff

diff --git a/Makefile b/Makefile
new file mode 100644
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,22 @@
+# -------------------------------------------------------------
+# Makefile for salt-wrapper
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+# Project: salt-wrapper
+# Licence: BSD-2-Clause
+# -------------------------------------------------------------
+
+INSTALL=/usr/bin/install
+RM=/bin/rm -f
+PREFIX?=/usr/local
+
+# -------------------------------------------------------------
+# Installation main targets
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+install:
+ ${INSTALL} salt-get-config-dir.py ${PREFIX}/bin/salt-get-config-dir
+ ${INSTALL} salt-wrapper.sh ${PREFIX}/bin/salt-wrapper
+
+deinstall:
+ ${RM} ${PREFIX}/bin/salt-get-config-dir
+ ${RM} ${PREFIX}/bin/salt-wrapper
diff --git a/salt-get-config-dir.py b/salt-get-config-dir.py
new file mode 100755
--- /dev/null
+++ b/salt-get-config-dir.py
@@ -0,0 +1,55 @@
+#!/usr/bin/env python3
+
+# -------------------------------------------------------------
+# salt-get-config-dir command
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+# Project: salt-wrapper
+# Licence: BSD-2-Clause
+# -------------------------------------------------------------
+
+
+import json
+import os
+import sys
+
+
+# -------------------------------------------------------------
+# Load configuration
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+
+config_file_path = os.environ.get(
+ "SALT_WRAPPER_CONF",
+ "/usr/local/etc/salt-wrapper.conf"
+)
+
+if not os.path.isfile(config_file_path):
+ print(
+ "The configuration file {0} doesn't exist.".format(config_file_path),
+ file=sys.stderr
+ )
+ print(
+ "See https://docs.nasqueron.org/salt-wrapper/admin.html#configuration-file", # noqa
+ file=sys.stderr
+ )
+ sys.exit(2)
+
+try:
+ with open(config_file_path) as config_file:
+ config = json.load(config_file)
+except OSError as err:
+ print("Can't open configuration file: {0}".format(err))
+ sys.exit(4)
+
+
+# -------------------------------------------------------------
+# Find configuration directory
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+
+for root in config['roots']:
+ if os.getcwd().startswith(root['states']):
+ print(root['config'])
+ sys.exit(0)
+
+sys.exit(1)
diff --git a/salt-wrapper.sh b/salt-wrapper.sh
new file mode 100755
--- /dev/null
+++ b/salt-wrapper.sh
@@ -0,0 +1,53 @@
+#!/bin/sh
+
+# -------------------------------------------------------------
+# Salt wrapper
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+# Created: 2016-04-10
+# License: BSD-2-Clause
+# Usage: salt-wrapper salt-call --local state.highstate
+# -------------------------------------------------------------
+
+# -------------------------------------------------------------
+# Helper functions
+#
+# :: contains
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+# contains(string, substring)
+#
+# Returns 0 if the specified string contains the specified substring,
+# otherwise returns 1.
+# See: https://stackoverflow.com/a/8811800/1930997
+contains() {
+ string="$1"
+ substring="$2"
+ if test "${string#*$substring}" != "$string"
+ then
+ return 0 # $substring is in $string
+ else
+ return 1 # $substring is not in $string
+ fi
+}
+
+# -------------------------------------------------------------
+# Wrapper code
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+CONFIG_DIR=$(salt-get-config-dir)
+CODE=$?
+if [ $CODE -ne 0 ]; then
+ exit $CODE
+fi
+
+EXECUTABLE=$1
+shift
+
+# The default user is salt, but if we need to target the Salt
+# master without a minion running, we need to run as root.
+SALT_USER=salt
+if [ "$EXECUTABLE" = "salt-call" ]; then
+ contains "$*" "--local" && SALT_USER=root
+fi
+
+sudo -u $SALT_USER "$EXECUTABLE" --config-dir="$CONFIG_DIR" "$@"

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 23, 00:01 (18 h, 20 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2256693
Default Alt Text
D1023.diff (4 KB)

Event Timeline