summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnthony Wilson <wilsonan@us.ibm.com>2018-09-20 15:19:28 -0500
committerAnthony Wilson <wilsonan@us.ibm.com>2018-10-25 08:47:01 -0500
commitacf54d089bbd4d6f3ac790229ee7008d96ed760b (patch)
tree1f2a24d7217110d06e7cd606abec5d64517ed634
parentea87db4096ffd74de16847a3f86a86eb0f129b3c (diff)
downloadphosphor-state-manager-acf54d089bbd4d6f3ac790229ee7008d96ed760b.tar.gz
phosphor-state-manager-acf54d089bbd4d6f3ac790229ee7008d96ed760b.zip
obmcutil: Add wait/timeout option
The option to block/wait for a state transition to complete has been added. The transition is expected to occur within a set timeout window and will terminate if the desired state has not been observed within that window. It may be possible for the state transition to complete outside this window, but it is assumed to be a failure. Change-Id: Icb9ff29f51b8712469fd9957b7e4b287704cb314 Signed-off-by: Anthony Wilson <wilsonan@us.ibm.com>
-rw-r--r--obmcutil82
1 files changed, 79 insertions, 3 deletions
diff --git a/obmcutil b/obmcutil
index 26fd760..ed5dbc6 100644
--- a/obmcutil
+++ b/obmcutil
@@ -5,7 +5,7 @@ set -euo pipefail
OPTS="bmcstate,bootprogress,chassisoff,chassison,chassisstate,hoststate,\
power,poweroff,poweron,state,status"
-USAGE="Usage: obmcutil [-h]
+USAGE="Usage: obmcutil [-h] [--wait]
{$OPTS}"
INTERFACE_ROOT=xyz.openbmc_project
@@ -14,6 +14,23 @@ STATE_INTERFACE=$INTERFACE_ROOT.State
OBJECT_ROOT=/xyz/openbmc_project
STATE_OBJECT=$OBJECT_ROOT/state
+## NOTE: The following global variables are used only in the run_timeout cmd.
+## By declaring these globally instead of passing them through the
+## intermediary functions, which may not be "best practice", the readability
+## and cleanliness of the code should at least be increased.
+
+# The command passed in to be executed (e.g. poweron/off, status, etc.)
+# This will be be used in some instances of error reporting
+G_ORIG_CMD=
+# The state an interface should be in after executing the requested command.
+G_REQUESTED_STATE=
+# The query to run during a poweron/off or chassison/off to check that
+# the requested state (G_REQUESTED_STATE) of the interface has been reached.
+G_QUERY=
+# Wait the set period of time for state transitions to be successful before
+# continuing on with the program or reporting an error if timeout reached.
+G_WAIT=
+
print_help ()
{
echo "$USAGE"
@@ -23,17 +40,63 @@ print_help ()
echo ""
echo "optional arguments:"
echo " -h, --help show this help message and exit"
+ echo " -w, --wait block until state transition succeeds or fails"
exit 0
}
+run_timeout ()
+{
+ local timeout="$1"; shift
+ local cmd="$@"
+
+ $cmd
+
+ # Run a background query for the transition to the expected state
+ # This will be killed if the transition doesn't succeed within
+ # a timeout period.
+ (
+ while ! grep -q $G_REQUESTED_STATE <<< $(handle_cmd $G_QUERY) ; do
+ sleep 1
+ done
+ ) &
+ child=$!
+
+ # Could be bad if process is killed before 'timeout' occurs if
+ # transition doesn't succeed.
+ trap -- "" SIGTERM
+
+ # Workaround for lack of 'timeout' command.
+ (
+ sleep $timeout
+ kill $child
+ ) > /dev/null 2>&1 &
+
+ if ! wait $child; then
+ echo "Unable to confirm '$G_ORIG_CMD' success" \
+ "within timeout period (${timeout}s)"
+ fi
+}
+
+run_cmd ()
+{
+ local cmd="$@";
+
+ if [ -n "$G_WAIT" ]; then
+ run_timeout $G_WAIT "$cmd"
+ else
+ $cmd
+ fi
+}
+
set_property ()
{
- busctl set-property "$@"
+ run_cmd busctl set-property "$@"
}
get_property ()
{
- busctl get-property "$@"
+ G_WAIT=""
+ run_cmd busctl get-property "$@"
}
state_query ()
@@ -58,6 +121,8 @@ handle_cmd ()
INTERFACE=$STATE_INTERFACE.Chassis
PROPERTY=RequestedPowerTransition
VALUE=$INTERFACE.Transition.Off
+ G_REQUESTED_STATE=$INTERFACE.PowerState.Off
+ G_QUERY="chassisstate"
set_property $SERVICE $OBJECT $INTERFACE $PROPERTY "s" $VALUE
;;
chassison)
@@ -66,6 +131,8 @@ handle_cmd ()
INTERFACE=$STATE_INTERFACE.Chassis
PROPERTY=RequestedPowerTransition
VALUE=$INTERFACE.Transition.On
+ G_REQUESTED_STATE=$INTERFACE.PowerState.On
+ G_QUERY="chassisstate"
set_property $SERVICE $OBJECT $INTERFACE $PROPERTY "s" $VALUE
;;
poweroff)
@@ -74,6 +141,8 @@ handle_cmd ()
INTERFACE=$STATE_INTERFACE.Host
PROPERTY=RequestedHostTransition
VALUE=$INTERFACE.Transition.Off
+ G_REQUESTED_STATE=$INTERFACE.HostState.Off
+ G_QUERY="hoststate"
set_property $SERVICE $OBJECT $INTERFACE $PROPERTY "s" $VALUE
;;
poweron)
@@ -82,6 +151,8 @@ handle_cmd ()
INTERFACE=$STATE_INTERFACE.Host
PROPERTY=RequestedHostTransition
VALUE=$INTERFACE.Transition.On
+ G_REQUESTED_STATE=$INTERFACE.HostState.Running
+ G_QUERY="hoststate"
set_property $SERVICE $OBJECT $INTERFACE $PROPERTY "s" $VALUE
;;
bmcstate)
@@ -141,6 +212,10 @@ handle_cmd ()
for arg in "$@"; do
case $arg in
+ -w|--wait)
+ G_WAIT=30
+ continue
+ ;;
-h|--help)
print_help
;;
@@ -148,6 +223,7 @@ for arg in "$@"; do
print_usage_err "Unknown option: $arg"
;;
*)
+ G_ORIG_CMD=$arg
handle_cmd $arg
break
;;
OpenPOWER on IntegriCloud