Just sharing a snippet of code.
Use case: Usually we have two instances (load balanced) when provisioning applications. We have a task that needs to run once at the end of provisioning the last instance and takes ages to complete.
Adding this snippet in the provisioning workflow will fire the task at the last running instance. It stores the serverIds on which it has ran in Cypher and cleans up once completed.
from pymorpheus import MorpheusClient
import json
cypherKey = "secret/" + morpheus["instance"]["name"]
morpheusclient = MorpheusClient(morpheus['morpheus']['applianceUrl'], token=morpheus['morpheus']['apiAccessToken'], sslverify=False)
results = morpheusclient.call("get", path="/cypher/%s" % cypherKey)
if 'data' in results:
instances = json.loads(results["data"])
else:
instances = []
if len(instances) >= len(morpheus["instance"]["containers"]) - 1:
print("Fire!")
morpheusclient.call("delete", path="/cypher/%s" % cypherKey)
else:
instances.append(morpheus["server"]["id"])
morpheusclient.call("post", path="/cypher/%s" % cypherKey, options=[("type", "string"), ("value", json.dumps(instances))])