forked from marko-bast/aws-emr-cost-calculator
-
Notifications
You must be signed in to change notification settings - Fork 6
/
aws-emr-cost-calculator2
executable file
·56 lines (49 loc) · 2.13 KB
/
aws-emr-cost-calculator2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python
"""EMR cost calculator
Usage:
aws-emr-cost-calculator total --created_after=<ca> --created_before=<cb>
[--profile=<profile>]
[--region=<region>]
aws-emr-cost-calculator cluster --cluster_id=<ci> [--profile=<profile>]
[--region=<region>]
aws-emr-cost-calculator -h | --help
Options:
-h --help Show this screen
total Calculate the total EMR cost for a period of
time
cluster Calculate the cost of single cluster given
the cluster id
--profile=<profile> Use a specific AWS profile from your config file
--region=<region> Your favorite AWS region
credential file instead of a default one.
--created_after=<ca> The calculator will compute the cost for all
the cluster created after the created_after day
--created_before=<cb> The calculator will compute the cost for all
the cluster created before the created_before day
--cluster_id=<ci> The id of the cluster you want to calculate
the cost for
"""
from calculator.calculator import EmrCostCalculator, validate_date
import boto3
from docopt import docopt
import sys
if __name__ == '__main__':
args = docopt(__doc__)
profile = args.get('--profile')
region = args.get('--region')
if profile is not None:
boto3.setup_default_session(profile_name=profile)
if args.get('total'):
created_after_arg = validate_date(args.get('--created_after'))
created_before_arg = validate_date(args.get('--created_before'))
calc = EmrCostCalculator(region=region)
print("TOTAL COST: {:.2f}".format(calc.get_total_cost_by_dates(
created_after_arg, created_before_arg)))
elif args.get('cluster'):
calc = EmrCostCalculator(region=region)
calculated_prices = calc.get_cluster_cost(args.get('--cluster_id'))
for key in sorted(calculated_prices.keys()):
print("{:12s}: {:6.2f}".format(key, calculated_prices[key]))
else:
print('[ERROR] Invalid operation, please check usage again',
file=sys.stderr)