1#!/bin/bash
2# Copyright 2022 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16[ -n "${gbmc_psu_hardreset-}" ] && return
17
18gbmc_psu_hardreset_needed=
19
20COORDINATED_POWERCYCLE=@COORDINATED_POWERCYCLE@
21
22gbmc_psu_hardreset_hook() {
23  # We don't always need a powercycle, allow skipping
24  if [ -z "${gbmc_psu_hardreset_needed-}" ]; then
25    echo "Skipping tray powercycle" >&2
26    return 0
27  fi
28
29  if [[ "${COORDINATED_POWERCYCLE}" == "true" ]]; then
30    echo "Requesting powercycle" >&2
31    update-dhcp-status 'POWERCYCLE' "netboot powercycle" || return
32  else
33    echo "Powercycling" >&2
34    systemctl start gbmc-psu-hardreset.target || return
35  fi
36
37  # Ensure that we don't continue the DHCP process while waiting for the
38  # powercycle.
39  exit 0
40}
41
42GBMC_BR_DHCP_HOOKS+=(gbmc_psu_hardreset_hook)
43
44gbmc_psu_hardreset=1
45