浏览代码

Initial commit

Sven Velt 8 年之前
当前提交
7697d71cc4
共有 6 个文件被更改,包括 196 次插入0 次删除
  1. 2 0
      .gitignore
  2. 21 0
      LICENSE
  3. 25 0
      README.md
  4. 146 0
      rc-runit
  5. 1 0
      rc-service
  6. 1 0
      rc-update

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+.*.sw?
+*~

+ 21 - 0
LICENSE

@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2016 Sven Velt
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.

+ 25 - 0
README.md

@@ -0,0 +1,25 @@
+## runit-openrc-wrapper
+
+A small wrapper for runit based systems (e.g. __Voidlinux__) to use a subset of
+openrc's `rc-service` and `rc-update` commands.
+
+## Motivation
+
+I wanted to use __Ansible__ for my __Voidlinux__ systems and patching
+__Ansible__'s "`service`" module seems to be a PITA. So I decided to write a
+small wrapper which is detected by __Ansible__ as openrc. Attention!
+"`ansible_service_mgr`" variable of "`setup`" module still contains "`runit`"!
+
+## Installation
+
+Just copy all `rc-*` files to `/usr/bin` or another location in your root's
+PATH.
+
+## Contributors
+
+  * Sven Velt
+
+## Licence
+
+See `LICENCE`
+

+ 146 - 0
rc-runit

@@ -0,0 +1,146 @@
+#!/bin/sh
+
+SVDIR=/etc/sv
+RUNSVDIR=/etc/runit/runsvdir
+
+for SERVICEDIR in /var/service /service NONE; do
+	if [ -d "${SERVICEDIR}" ]; then
+		break
+	fi
+done
+
+if [ "${SERVICEDIR}" = "NONE" ]; then
+	echo "No service directory found"
+	exit 99
+fi
+
+########################################
+
+if [ "$(basename $0)" = "rc-service" ]; then
+	SVC="$1"
+	ACTION="$2"
+
+	if [ "$UID" != "0" ]; then
+		echo "Need root privileges"
+		exit 13
+	fi
+elif [ "$(basename $0)" = "rc-update" ]; then
+	ACTION="$1"
+	SVC="$2"
+	RUNLEVEL="$3"
+else
+	echo "Unknown basename \"$0\""
+	exit 11
+fi
+
+if [ -z "${RUNLEVEL}" ]; then
+	RUNLEVEL=$(readlink ${RUNSVDIR}/current)
+	RUNLEVEL=$(basename ${RUNLEVEL})
+fi
+
+########################################
+
+### DEBUG
+if [ -r /tmp/service.debug.on ]; then
+	printf 2>/dev/null >>/tmp/service.log "%-8s %s\n" "${ACTION}" "${SVC}"
+fi
+
+########################################
+
+if [ "${ACTION}" != "show" ]; then
+	if [ ! -d "${SVDIR}/${SVC}" ]; then
+		echo "Could not find service \"${SVC}\""
+		exit 1
+	fi
+fi
+
+echo "${ACTION}" | grep -q "start\|stop\|status\|show\|add\|delete"
+if [ $? -ne 0 ]; then
+	echo "Unknown action \"${ACTION}\""
+	exit 10
+fi
+
+if echo "${ACTION}" | grep -qE "start|stop|status"; then
+	if [ ! -L "${SERVICEDIR}/${SVC}" ]; then
+		echo "Service \"${SVC}\" not enabled"
+		exit 9
+	fi
+fi
+
+case "${ACTION}" in
+	"start")
+		OUT=$(sv u "${SVC}" 2>&1)
+		echo "Service \"${SVC}\" started"
+		exit 0
+	;;
+	"restart")
+		OUT=$(sv restart "${SVC}" 2>&1)
+		echo "Service \"${SVC}\" restarted"
+		exit 0
+	;;
+	"stop")
+		OUT=$(sv d "${SVC}" 2>&1)
+		echo "Service \"${SVC}\" stopped"
+		exit 0
+	;;
+	"status")
+		OUT=$(sv s "${SVC}" 2>&1)
+		if echo "${OUT}" | grep -q "^run"; then
+			echo "Service \"${SVC}\" started"
+			exit 0
+		elif echo "${OUT}" | grep -q "^down.*normally up"; then
+			echo "Service \"${SVC}\" crashed"
+			exit 1
+		fi
+	;;
+
+	"add")
+		if [ ! -d "${RUNSVDIR}/${RUNLEVEL}" ]; then
+			echo "Unknown runlevel \"${RUNLEVEL}\""
+			exit 12
+		fi
+
+		if [ ! -L "${RUNSVDIR}/${RUNLEVEL}/${SVC}" ]; then
+			ln -s "${SVDIR}/${SVC}" "${RUNSVDIR}/${RUNLEVEL}/${SVC}"
+			echo " * service ${SVC} added to runlevel default"
+			exit 0
+		else
+			echo " * rc-update: ${SVC} already installed in runlevel 'default'; skipping"
+			exit 0
+		fi
+	;;
+	"delete")
+		if [ ! -d "${RUNSVDIR}/${RUNLEVEL}" ]; then
+			echo "Unknown runlevel \"${RUNLEVEL}\""
+			exit 12
+		fi
+
+		if [ -L "${RUNSVDIR}/${RUNLEVEL}/${SVC}" ]; then
+			rm "${RUNSVDIR}/${RUNLEVEL}/${SVC}"
+			echo " * service ${SVC} removed from runlevel default"
+			exit 0
+		else
+			echo " * rc-update: service '${SVC}' is not in the runlevel '${RUNLEVEL}'"
+			exit 0
+		fi
+	;;
+	"show")
+		for S in $(cd ${SVDIR}; ls -1); do
+			printf " %-20s |" "${S}"
+			for RL in $(cd ${RUNSVDIR}; find -name "${S}"); do
+				printf " %s" "$(echo ${RL} | cut -d/ -f2)"
+			done
+			printf "\n"
+		done
+		exit 0
+	;;
+
+	*)
+		### DEBUG
+		if [ -r /tmp/service.debug.on ]; then
+			printf >>/tmp/service.log "UNKNOWN ACTION ${ACTION}\n" "${ACTION}" "${SVC}"
+		fi
+	;;
+
+esac
+

+ 1 - 0
rc-service

@@ -0,0 +1 @@
+rc-runit

+ 1 - 0
rc-update

@@ -0,0 +1 @@
+rc-runit