-
Notifications
You must be signed in to change notification settings - Fork 22
/
zenrun
executable file
·95 lines (85 loc) · 2.18 KB
/
zenrun
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
#! /usr/bin/env bash
##############################################################################
#
# Copyright (C) Zenoss, Inc. 2014, all rights reserved.
#
# This content is made available according to terms specified in
# License.zenoss under the directory where your Zenoss product is installed.
#
##############################################################################
# This script runs custom commands passed in to the container from the host.
#
# Error Codes:
# 255 - BADPARAM Bad params
# 254 - NOTFOUND Script not found
# 253 - INVALID Invalid script
# 252 - NOCOMMIT NOCOMMIT environment variable was set
#
# Each script follows a specific design template.
#
# Example:
# #! /bin/bash
#
# # Default action for undefined program calls
# __DEFAULT__() {
# /bin/helloworld $@
# }
#
# # ---
# # Transaction-based commands
# # ---
#
# # Usage: zenrun /bin/helloworld savestate
# savestate() {
# /bin/helloworld $@
# return $?
# }
# Parse the arguments
# Usage: zenrun PROGRAM [ARGS]
if [[ $# -lt 1 ]]; then
echo -e "Missing program argument" >&2
exit 255
fi
DESIRED_USER=${DESIRED_USER:-"zenoss"}
if [[ "$(whoami)" != "$DESIRED_USER" ]]; then
exec su - "$DESIRED_USER" -c "DESIRED_USER=$DESIRED_USER NOCOMMIT=$NOCOMMIT $0 $*"
fi
RUNPATH=${RUNPATH:-$(dirname $0)/zenrun.d}
PROGRAM=$1
shift
# Load the program
RC=nil
if [[ -r $RUNPATH/$PROGRAM ]]; then
source $RUNPATH/$PROGRAM
elif [[ -r $PROGRAM ]]; then
source $PROGRAM
elif which $PROGRAM > /dev/null ; then
$PROGRAM "$@"
RC="$?"
else
echo -e "Program not found: $PROGRAM" >&2
exit 254
fi
# Look up the command and run
declare -f __DEFAULT__ &> /dev/null
defaultExists=$?
if [[ $RC != nil ]]; then
: # Already have return code; do nothing
elif [[ $defaultExists=0 && -z "$1" ]]; then
__DEFAULT__
RC="$?"
elif declare -f -- "$1" &> /dev/null; then
"$@"
RC="$?"
elif [[ $defaultExists=0 ]]; then
__DEFAULT__ "$@"
RC="$?"
else
echo -e "Missing __DEFAULT__ declaration in $PROGRAM" >&2
exit 253
fi
if ${NOCOMMIT:-false} ; then
echo Command definition deliberately disables container commit.
exit 252
fi
exit "$RC"