Skip to content

Instantly share code, notes, and snippets.

@prydin
Last active January 6, 2022 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prydin/c4d284bd8f7345ca5ef9a46148626211 to your computer and use it in GitHub Desktop.
Save prydin/c4d284bd8f7345ca5ef9a46148626211 to your computer and use it in GitHub Desktop.
Prevent power down on change
import json
def handler(context, inputs):
# We only care about updates
if inputs["eventType"] != "UPDATE_DEPLOYMENT":
return inputs
result = context.request("/blueprint/api/blueprint-requests/%s/resources-plan?expand=true" % inputs["id"], "GET", "")
if result["status"] != 200:
raise Exception(result["content"])
envelope = json.loads(result["content"])
print(envelope)
for resource in envelope["resources"]:
# We only care about machines
if not resource["resourceType"].endswith(".Machine"):
continue
new_props = resource["newProperties"]
old_props = resource["oldProperties"]
print("Old cpu: %d, new cpu %d" % (old_props["cpuCount"], new_props["cpuCount"]))
# If the machine is already powered off, we're fine
if new_props["powerState"] == "OFF":
continue
# If the VM doesn't support hot add AND we're changing the number of CPUs, we need to stop this
# deployment, since it would power down the VM.
if new_props.get("__cpuHotAddEnabled", "false") != "true" and old_props.get("cpuCount", -1) != new_props.get("cpuCount", -1):
raise Exception("Changing number of CPUs on a powered on machine isn't allowed")
# If the VM doesn't support hot add AND we're changing the amount of memory, we need to stop this
# deployment, since it would power down the VM.
if new_props.get("__memoryHotAddEnabled", "false") != "true" and old_props.get("totalMemoryMB", -1) != new_props.get("totalMemoryMB", -1):
raise Exception("Changing the amound of memory on a powered on machine isn't allowed")
return inputs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment