summaryrefslogtreecommitdiffstats
path: root/scripts/ldadp-launcher
blob: f233900741a0561dfccd0d62cadeeea0c73414a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash

set -x
exec 2> /tmp/pops

if [ $# -lt 2 ]; then
	echo "Invalid parameter count: '$#'" >&2
	exit 1
fi

BASE="/opt/ldadp"
MODE="start"
while [ $# -gt 0 ]; do
	var="$1"
	shift
	case "$var" in
		--check) MODE="check" ;;
		--start) MODE="start" ;;
		--restart) MODE="restart" ;;
		--base)
			BASE="$1"
			shift
			;;
		--) break ;;
		--*)
			echo "Unknown option '$var'" >&2
			exit 1
			;;
	esac
done

if [ -z "$BASE" ] || [ ! -r "$BASE" ] || [ ! -d "$BASE" ]; then
	echo "Basedir invalid: '$BASE'" >&2
	exit 2
fi
cd "$BASE"

# At this point $@ should only be ldadp instance ids

inArray () {
	local e
	for e in "${@:2}"; do
		[[ "x$e" == "x$1" ]] && return 0
	done
	return 1
}

RETVAL=0

if [ "$MODE" = "check" ]; then
	# Dump list of known instances
	systemctl list-units --state=loaded --no-legend | awk '{if ($1 ~ /^ldadp@.*\.service$/) print $1 " " $4}'
	exit "$RETVAL"
elif [ "$MODE" = "restart" ]; then
	# Restart only the given ID(s)
	for id in "$@"; do
		if ! [ -r "${BASE}/configs/${id}.cfg" ]; then
			echo "Told to start ldadp for module '${id}', but no config file found; skipping." >&2
			continue
		fi
		name="ldadp@${id}.service"
		wanted+=( "${name}" )
		echo "Restarting $name"
		systemctl restart "$name" || RETVAL=1
	done
	exit "$RETVAL"
fi

# Regular Start/Stop logic

# Get loaded instances
declare -a known wanted
known=( $(systemctl list-units --state=loaded --no-legend | awk '{if ($1 ~ /^ldadp@.*\.service$/) print $1}') )

# Prepare names of wanted services and launch them
for id in "$@"; do
	if ! [ -r "${BASE}/configs/${id}.cfg" ]; then
		echo "Told to start ldadp for module '${id}', but no config file found; skipping." >&2
		continue
	fi
	name="ldadp@${id}.service"
	wanted+=( "${name}" )
	echo "Starting $name"
	systemctl enable "$name"
	systemctl start "$name" || RETVAL=1
done

# Now stop the ones we don't need anymore
for name in "${known[@]}"; do
	inArray "$name" "${wanted[@]}" && continue
	echo "Stopping $name"
	systemctl disable "$name"
	systemctl stop "$name"
done

exit "$RETVAL"