Skip to content

Instantly share code, notes, and snippets.

@mnaser
Created October 29, 2019 20:05
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 mnaser/4182fb82462f51c43edded5733a2c84c to your computer and use it in GitHub Desktop.
Save mnaser/4182fb82462f51c43edded5733a2c84c to your computer and use it in GitHub Desktop.
Get total cost usage for a Heat stack
import argparse
from datetime import datetime
import sys
import urllib.request
import json
import openstack
stack_name = sys.argv[1]
parser = argparse.ArgumentParser()
cloud = openstack.connect(options=parser)
def get_server_cost(server_id):
flavor, seconds = get_server_info(server_id)
url = "https://pricing.vexxhost.net/v1/pricing/%s/cost?seconds=%d"
with urllib.request.urlopen(url % (flavor, seconds)) as response:
data = json.loads(response.read())
return data['cost']
def parse_iso8601_time(time):
return datetime.strptime(time, "%Y-%m-%dT%H:%M:%S.%f")
def get_server_info(server_id):
server = cloud.compute.find_server(server_id)
flavor = cloud.compute.get_flavor(server.flavor['id'])
diff = (datetime.utcnow() - parse_iso8601_time(server.launched_at))
return flavor.name, diff.total_seconds()
def get_server_ids(stack_name):
servers = get_resources_by_type(stack_name, 'OS::Nova::Server')
return [s['physical_resource_id'] for s in servers]
def get_resources_by_type(stack_name, resource_type):
resources = get_stack_resources(stack_name)
return [r for r in resources if r.resource_type == resource_type]
def get_stack_resources(stack_name):
resources = []
def _is_nested(resource):
link_types = [l['rel'] for l in resource.links]
if 'nested' in link_types:
return True
return False
for r in cloud.orchestration.resources(stack_name):
if _is_nested(r):
resources += get_stack_resources(r.physical_resource_id)
continue
resources.append(r)
return resources
if __name__ == "__main__":
total_cost = 0.0
for server in get_server_ids(stack_name):
total_cost += get_server_cost(server)
print("total:", total_cost)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment