-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathincludes.py
337 lines (288 loc) · 12 KB
/
includes.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
"""
AWS EC2 Price Finder Helper Module
This module provides utility functions and constants for the AWS EC2 Price Finder tool.
It handles AWS pricing API interactions, database operations, and various helper functions
for retrieving and managing EC2 instance pricing information.
"""
import json
import sqlite3
from datetime import date
from typing import List, Dict, Tuple, Optional, Any, DefaultDict
from collections import defaultdict
import yaml
import boto3
import requests
from colorama import Fore, Style
# Default configuration values
P_VCPU = 2
P_RAM = 4
P_OS = 'Linux'
DB_NAME = 'awsprices.db'
REGION_NVIRGINIA = 'US East (N. Virginia)'
P_REGION = REGION_NVIRGINIA
DB_RECORD_EXPIRY_DAYS = 7
MAX_RESULTS = 100
# AWS specific constants
AWS_SERVICE_CODE = 'AmazonEC2'
SPOT_ADVISOR_URL = "https://spot-bid-advisor.s3.amazonaws.com/spot-advisor-data.json"
# EC2 filter constants
EC2_FILTERS = {
'preInstalledSw': 'NA',
'storage': 'EBS only',
'productFamily': 'Compute Instance',
'termType': 'OnDemand',
'licenseModel': 'No License required',
'tenancy': 'Shared',
'capacitystatus': 'Used'
}
# Mapping dictionaries
os_map = {
'Linux': 'Linux/UNIX (Amazon VPC)',
'SUSE': 'SUSE Linux (Amazon VPC)',
'Windows': 'Windows (Amazon VPC)',
'RHEL': 'Red Hat Enterprise Linux'
}
region_map = {
'US East (Ohio)': 'us-east-2',
'US East (N. Virginia)': 'us-east-1',
'US West (N. California)': 'us-west-1',
'US West (Oregon)': 'us-west-2',
'Asia Pacific (Hong Kong)': 'ap-east-1',
'Asia Pacific (Mumbai)': 'ap-south-1',
'Asia Pacific (Osaka-Local)': 'ap-northeast-3',
'Asia Pacific (Seoul)': 'ap-northeast-2',
'Asia Pacific (Singapore)': 'ap-southeast-1',
'Asia Pacific (Sydney)': 'ap-southeast-2',
'Asia Pacific (Tokyo)': 'ap-northeast-1',
'Canada (Central)': 'ca-central-1',
'China (Beijing)': 'cn-north-1',
'China (Ningxia)': 'cn-northwest-1',
'EU (Frankfurt)': 'eu-central-1',
'EU (Ireland)': 'eu-west-1',
'EU (London)': 'eu-west-2',
'EU (Paris)': 'eu-west-3',
'EU (Stockholm)': 'eu-north-1',
'Middle East (Bahrain)': 'me-south-1',
'South America (Sao Paulo)': 'sa-east-1'
}
# Available regions and OS options
list_regions = list(region_map.keys())
list_os = list(os_map.keys())
def adapt_date(val: date) -> str:
"""Convert date to string format for SQLite storage."""
return val.isoformat()
def convert_date(val: str) -> date:
"""Convert string from SQLite to date object."""
return date.fromisoformat(val.decode())
# Register the adapters with SQLite
sqlite3.register_adapter(date, adapt_date)
sqlite3.register_converter("DATE", convert_date)
class DatabaseManager:
"""Handles all database operations for EC2 pricing data."""
def __init__(self, db_name: str = DB_NAME):
self.db_name = db_name
self.create_db()
def _get_connection(self) -> sqlite3.Connection:
"""Create a connection with proper date handling."""
return sqlite3.connect(
self.db_name,
detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES
)
def create_db(self) -> None:
"""Create the database and required tables if they don't exist."""
with self._get_connection() as conn:
cursor = conn.cursor()
sql_query = """
CREATE TABLE IF NOT EXISTS ec2(
id INTEGER PRIMARY KEY,
instanceType TEXT,
vcpu REAL,
memory REAL,
os TEXT,
price REAL,
region TEXT,
add_date DATE
)
"""
cursor.execute(sql_query)
conn.commit()
def insert_records(self, records: List[Tuple]) -> None:
"""Insert multiple EC2 pricing records into the database."""
with self._get_connection() as conn:
cursor = conn.cursor()
cursor.executemany(
"""INSERT INTO ec2(instanceType, vcpu, memory, os, price, region, add_date)
VALUES(?, ?, ?, ?, ?, ?, ?)""",
records
)
conn.commit()
def delete_records(self, region: str) -> None:
"""Delete records for a specific region."""
with self._get_connection() as conn:
cursor = conn.cursor()
cursor.execute("DELETE FROM ec2 WHERE region=?", (region,))
conn.commit()
def are_records_old(self, region: str) -> bool:
"""Check if records for a region are older than DB_RECORD_EXPIRY_DAYS."""
with self._get_connection() as conn:
cursor = conn.cursor()
cursor.execute("SELECT add_date FROM ec2 WHERE region=? LIMIT 1", (region,))
result = cursor.fetchone()
if not result:
return True
record_date = result[0] # Now returns a date object directly
return (date.today() - record_date).days >= DB_RECORD_EXPIRY_DAYS
def find_ec2(self, cpu: float, ram: float, os: str, region: str, limit: int) -> List[Tuple]:
"""Find EC2 instances matching the specified criteria."""
with self._get_connection() as conn:
cursor = conn.cursor()
sql_query = """
SELECT * FROM ec2
WHERE vcpu >= ? AND memory >= ?
AND region = ? AND os = ?
ORDER BY price LIMIT ?
"""
cursor.execute(sql_query, (cpu, ram, region, os, limit))
return cursor.fetchall()
class AWSPricing:
"""Handles AWS pricing API interactions."""
def __init__(self):
self.db = DatabaseManager()
self.credentials = self._load_credentials()
def _load_credentials(self) -> Dict:
"""Load AWS credentials from yaml file."""
try:
with open('credentials.yaml', 'r') as stream:
return yaml.safe_load(stream)['credentials']
except (yaml.YAMLError, FileNotFoundError) as e:
raise Exception("Failed to load credentials: " + str(e))
def get_boto_clients(self, region: Optional[str] = None) -> Tuple[Any, Any]:
"""Create boto3 clients for pricing and EC2."""
session = boto3.Session(
aws_access_key_id=self.credentials['access_key'],
aws_secret_access_key=self.credentials['secret_key'],
region_name=region_map.get(region, self.credentials['default_region'])
)
return session.client('pricing'), session.client('ec2')
def get_ec2_pricing(self, region: str = P_REGION) -> None:
"""Fetch and store EC2 pricing information."""
if not self.db.are_records_old(region):
print("Records are up-to-date")
return
print("Getting price updates for EC2s")
self.db.delete_records(region)
pricing, _ = self.get_boto_clients(region)
filters = [
{'Type': 'TERM_MATCH', 'Field': key, 'Value': value}
for key, value in {**EC2_FILTERS, 'location': region}.items()
]
records = []
next_token = None
while True:
kwargs = {
'ServiceCode': AWS_SERVICE_CODE,
'Filters': filters
}
if next_token:
kwargs['NextToken'] = next_token
response = pricing.get_products(**kwargs)
for price in response['PriceList']:
record = self._parse_price_list_item(price, region)
if record:
records.append(record)
next_token = response.get('NextToken')
if not next_token:
break
self.db.insert_records(records)
def _parse_price_list_item(self, price: str, region: str) -> Optional[Tuple]:
"""Parse a price list item into a database record."""
details = json.loads(price)
try:
pricedimensions = next(iter(details['terms']['OnDemand'].values()))['priceDimensions']
pricing_details = next(iter(pricedimensions.values()))
instance_price = float(pricing_details['pricePerUnit']['USD'])
if instance_price <= 0:
return None
attributes = details['product']['attributes']
return (
attributes['instanceType'],
float(attributes['vcpu']),
float(attributes['memory'].split()[0]),
attributes['operatingSystem'],
instance_price,
region,
date.today()
)
except (KeyError, ValueError, StopIteration):
return None
def get_spot_prices(self, instances: List[str], os: str, region: str) -> DefaultDict:
"""Get spot prices for specified instances."""
_, ec2 = self.get_boto_clients(region)
results = defaultdict(float)
for instance in instances:
try:
spot = ec2.describe_spot_price_history(
InstanceTypes=[instance],
MaxResults=1,
ProductDescriptions=[os_map[os]]
)
if spot['SpotPriceHistory']:
results[instance] = float(spot['SpotPriceHistory'][0]['SpotPrice'])
except (IndexError, KeyError):
continue
return results
def get_spot_interruption_rates(self, instances: List[str], os: str, region: str) -> DefaultDict:
"""Get spot interruption rates for specified instances."""
results = defaultdict(str)
rates = {
0: "<5%",
1: "5-10%",
2: "10-15%",
3: "15-20%",
4: ">20%"
}
try:
response = requests.get(SPOT_ADVISOR_URL)
spot_advisor = json.loads(response.text)['spot_advisor']
for instance in instances:
try:
rate = spot_advisor[region][os][instance]['r']
results[instance] = rates[rate]
except (KeyError, IndexError):
continue
except requests.exceptions.RequestException:
pass
return results
def print_help() -> None:
"""Print help information to the terminal."""
print("----------------------------------")
print(Fore.GREEN + "Sample command:\n$ python awsEC2pricing.py -t 1 16 Windows 'US East (N. Virginia)'")
print(Style.RESET_ALL + "----------------------------------")
print(Fore.GREEN + " -t --> run in terminal")
print(" 1 --> vCPU")
print(" 16 --> RAM")
print(" Windows --> OS")
print(" 'US East (N. Virginia)' --> Region")
print(Style.RESET_ALL + "----------------------------------")
print(Fore.GREEN + " rename credentials.yaml.example to credentials.yaml and fill your aws key and secret")
print(" your user in AWS needs rights for reading price")
print(Style.RESET_ALL + "----------------------------------")
print(Fore.GREEN + "Available Regions:")
for region in list_regions:
print(f" {region}")
print(Style.RESET_ALL + "----------------------------------")
# Convenience functions that use the classes above
def find_ec2(cpu: float = P_VCPU, ram: float = P_RAM,
os: str = P_OS, region: str = P_REGION, limit: int = 6) -> List[Tuple]:
"""Find EC2 instances matching the specified criteria."""
aws_pricing = AWSPricing()
aws_pricing.get_ec2_pricing(region)
return aws_pricing.db.find_ec2(cpu, ram, os, region, limit)
def get_ec2_spot_price(instances: List[str], os: str, region: str) -> DefaultDict:
"""Get spot prices for specified instances."""
aws_pricing = AWSPricing()
return aws_pricing.get_spot_prices(instances, os, region)
def get_ec2_spot_interruption(instances: List[str], os: str, region: str) -> DefaultDict:
"""Get spot interruption rates for specified instances."""
aws_pricing = AWSPricing()
return aws_pricing.get_spot_interruption_rates(instances, os, region)