diff --git a/templates/instanceha/bin/instanceha.py b/templates/instanceha/bin/instanceha.py index 8b722df0..c29311ff 100755 --- a/templates/instanceha/bin/instanceha.py +++ b/templates/instanceha/bin/instanceha.py @@ -498,6 +498,21 @@ def _redfish_reset(url, user, passwd, timeout, action): r = requests.post(url, data=json.dumps(payload), headers=headers, auth=(user, passwd), verify=False, timeout=timeout) return r +def _bmh_fence(token, host, action): + + url = "https://kubernetes.default.svc/apis/metal3.io/v1alpha1/namespaces/openstack/baremetalhosts/%s?fieldManager=kubectl-patch" % host + headers={'Authorization': 'Bearer '+token, 'Content-Type': 'application/merge-patch+json'} + cacert = '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt' + + if action == 'off': + ann={"metadata":{"annotations":{"reboot.metal3.io/iha":"{\"mode\": \"hard\"}"}}} + r = requests.patch(url, headers=headers, verify=cacert, data=json.dumps(ann)) + return r + + else: + ann={"metadata":{"annotations":{"reboot.metal3.io/iha":None}}} + r = requests.patch(url, headers=headers, verify=cacert, data=json.dumps(ann)) + return r def _host_fence(host, action): logging.info('Fencing host %s %s' % (host, action)) @@ -589,6 +604,26 @@ def _host_fence(host, action): logging.warning('Could not power on %s' % host) #return True + elif 'bmh' in fencing_data["agent"]: + + token = str(fencing_data["token"]) + host = str(fencing_data["host"]) + + if action == 'off': + r = _bmh_fence(token, host, "off") + if r.status_code == 200: + logging.info('Power off of %s ok' % host) + return True + else: + logging.error('Could not power off %s' % host) + return False + else: + r = _bmh_fence(token, host, "on") + if r.status_code == 200: + logging.info('Power on of %s ok' % host) + else: + logging.warning('Could not power on %s' % host) + else: logging.error('No valid fencing method detected for %s' % host) return False