summaryrefslogtreecommitdiffstats
path: root/obmcutil
blob: 6f1fbd752e7b41ea785494d5e4df9411bb9b9869 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/bin/sh -e

set -euo pipefail

OPTS="bmcstate,bootprogress,chassiskill,chassisoff,chassison,chassisstate,hoststate,\
osstate,power,poweroff,poweron,state,status"

USAGE="Usage: obmcutil [-h] [--wait]
                {$OPTS}"

INTERFACE_ROOT=xyz.openbmc_project
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"
    echo ""
    echo "positional arguments:"
    echo "  {$OPTS}"
    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 ()
{
    run_cmd busctl set-property "$@"
}

get_property ()
{
    G_WAIT=""
    run_cmd busctl get-property "$@"
}

state_query ()
{
    local state=$(get_property "$@" | cut -d '"' -f2)
    printf "%-20s: %s\n" $4 $state
}

print_usage_err ()
{
    echo "ERROR: $1" >&2
    echo "$USAGE"
    exit 1
}

handle_cmd ()
{
    case "$1" in
        chassisoff)
            OBJECT=$STATE_OBJECT/chassis0
            SERVICE=$(mapper get-service $OBJECT)
            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)
            OBJECT=$STATE_OBJECT/chassis0
            SERVICE=$(mapper get-service $OBJECT)
            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)
            OBJECT=$STATE_OBJECT/host0
            SERVICE=$(mapper get-service $OBJECT)
            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)
            OBJECT=$STATE_OBJECT/host0
            SERVICE=$(mapper get-service $OBJECT)
            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)
            OBJECT=$STATE_OBJECT/bmc0
            SERVICE=$(mapper get-service $OBJECT)
            INTERFACE=$STATE_INTERFACE.BMC
            PROPERTY=CurrentBMCState
            state_query $SERVICE $OBJECT $INTERFACE $PROPERTY
            ;;
        chassisstate)
            OBJECT=$STATE_OBJECT/chassis0
            SERVICE=$(mapper get-service $OBJECT)
            INTERFACE=$STATE_INTERFACE.Chassis
            PROPERTY=CurrentPowerState
            state_query $SERVICE $OBJECT $INTERFACE $PROPERTY
            ;;
        hoststate)
            OBJECT=$STATE_OBJECT/host0
            SERVICE=$(mapper get-service $OBJECT)
            INTERFACE=$STATE_INTERFACE.Host
            PROPERTY=CurrentHostState
            state_query $SERVICE $OBJECT $INTERFACE $PROPERTY
            ;;
        osstate)
            OBJECT=$STATE_OBJECT/host0
            SERVICE=$(mapper get-service $OBJECT)
            INTERFACE=$STATE_INTERFACE.OperatingSystem.Status
            PROPERTY=OperatingSystemState
            state_query $SERVICE $OBJECT $INTERFACE $PROPERTY
            ;;
        state|status)
            for query in bmcstate chassisstate hoststate bootprogress osstate
            do
                handle_cmd $query
            done
            ;;
        bootprogress)
            OBJECT=$STATE_OBJECT/host0
            SERVICE=$(mapper get-service $OBJECT)
            INTERFACE=$STATE_INTERFACE.Boot.Progress
            PROPERTY=BootProgress
            state_query $SERVICE $OBJECT $INTERFACE $PROPERTY
            ;;
        power)
            OBJECT=/org/openbmc/control/power0
            SERVICE=$(mapper get-service $OBJECT)
            INTERFACE=org.openbmc.control.Power
            for property in pgood state pgood_timeout
            do
                # get_property can potentially return several
                # different formats of values, so we do the parsing outside
                # of get_property depending on the query. These queries
                # return 'i VALUE' formatted strings.
                STATE=$(get_property $SERVICE $OBJECT $INTERFACE $property \
                    | sed 's/i[ ^I]*//')
                printf "%s = %s\n" $property $STATE
            done
            ;;
        chassiskill)
            /usr/libexec/chassiskill
            ;;
        *)
            print_usage_err "Invalid command '$1'"
            ;;
    esac
}

for arg in "$@"; do
    case $arg in
        -w|--wait)
            G_WAIT=30
            continue
            ;;
        -h|--help)
            print_help
            ;;
        -*)
            print_usage_err "Unknown option: $arg"
            ;;
        *)
            G_ORIG_CMD=$arg
            handle_cmd $arg
            break
            ;;
    esac
done
OpenPOWER on IntegriCloud