Skip to content

Commit 46ae292

Browse files
authored
Merge pull request #7 from someengineering/lloesche/ec2instancesinfo
[feat] Add ec2instanceinfo data
2 parents a17e2c1 + cf886e0 commit 46ae292

File tree

8 files changed

+2298227
-15
lines changed

8 files changed

+2298227
-15
lines changed

resotodata/__main__.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import venv
23
import json
34
import shutil
45
import requests
@@ -31,6 +32,10 @@ def update_ccfdataset() -> None:
3132
write_ccfdataset()
3233

3334

35+
def update_instances() -> None:
36+
write_instances()
37+
38+
3439
def gen_digitalocean_regions() -> dict:
3540
print("Processing DigitalOcean regions")
3641
regions = {}
@@ -224,6 +229,35 @@ def write_ccfdataset() -> None:
224229
f.write("\n")
225230

226231

232+
def write_instances() -> None:
233+
instances = get_instances()
234+
235+
instances_file = resource_filename("resotodata", "data/instances.json")
236+
print(f"Writing instances dataset to {instances_file}")
237+
with open(instances_file, "w") as f:
238+
json.dump(instances, f, indent=4)
239+
f.write("\n")
240+
241+
strip_instances(instances)
242+
243+
stripped_instances_file = resource_filename("resotodata", "data/stripped_instances.json")
244+
print(f"Writing instances dataset to {stripped_instances_file}")
245+
with open(stripped_instances_file, "w") as f:
246+
json.dump(instances, f, indent=4)
247+
f.write("\n")
248+
249+
250+
def strip_instances(instances: dict) -> None:
251+
print("Stripping instance data")
252+
for cloud, cloud_data in instances.items():
253+
for instance_type, instance_type_data in cloud_data.items():
254+
for region, region_pricing_data in instance_type_data.get("pricing", {}).items():
255+
for key in list(region_pricing_data.keys()):
256+
if key not in ("dedicated", "linux", "unknown"):
257+
print(f"Removing {key} from {cloud} {instance_type} {region} pricing data")
258+
del region_pricing_data[key]
259+
260+
227261
def get_ccfdataset() -> dict:
228262
print("Checking if git and npm are installed")
229263
for tool in ("git", "npm"):
@@ -283,5 +317,49 @@ def get_ccfdataset() -> dict:
283317
return json.loads(result.stdout)
284318

285319

320+
def get_instances() -> dict:
321+
return {"aws": get_aws_instances()}
322+
323+
324+
def get_aws_instances() -> dict:
325+
print("Checking if git is installed")
326+
for tool in ("git",):
327+
if not shutil.which(tool):
328+
raise RuntimeError(f"{tool} not found in path")
329+
330+
ec2instancesrepo = "https://github.com/vantage-sh/ec2instances.info.git"
331+
332+
with tempfile.TemporaryDirectory() as tmpdir:
333+
print(f"Cloning {ec2instancesrepo} to {tmpdir}")
334+
subprocess.run(["git", "clone", ec2instancesrepo, tmpdir], check=True)
335+
336+
# Create venv
337+
venv_dir = os.path.join(tmpdir, "venv")
338+
venv.create(venv_dir, with_pip=True)
339+
340+
# Paths to python and pip executables in venv
341+
venv_python = os.path.join(venv_dir, "bin", "python")
342+
venv_pip = os.path.join(venv_dir, "bin", "pip")
343+
344+
print(f"Installing dependencies in {tmpdir}")
345+
subprocess.run([venv_pip, "install", "-r", "requirements.txt"], cwd=tmpdir, check=True)
346+
print("Invoking build")
347+
subprocess.run([venv_python, "-m", "invoke", "-T", "3600", "build"], cwd=tmpdir, check=True)
348+
349+
instances_file = os.path.join(tmpdir, "www", "instances.json")
350+
with open(instances_file) as f:
351+
instance_types = json.load(f)
352+
353+
instances = {}
354+
for instance_type in instance_types:
355+
instance_type_name = instance_type.get("instance_type")
356+
if instance_type_name is None:
357+
print(f"Skipping invalid instance type: {instance_type}")
358+
continue
359+
instances[instance_type_name] = instance_type
360+
361+
return instances
362+
363+
286364
if __name__ == "__main__":
287365
main()

resotodata/cloud.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import json
2-
from pkg_resources import resource_filename
1+
from resotodata.utils import LazyLoadedDict
32

4-
data_file = resource_filename("resotodata", "data/regions.json")
5-
with open(data_file) as f:
6-
regions = json.load(f)
3+
4+
regions = LazyLoadedDict("regions.json")
5+
instances = LazyLoadedDict("instances.json")
6+
stripped_instances = LazyLoadedDict("stripped_instances.json")

resotodata/co2.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import json
2-
from pkg_resources import resource_filename
1+
from resotodata.utils import LazyLoadedDict
32

4-
data_file = resource_filename("resotodata", "data/ccfdataset.json")
5-
with open(data_file) as f:
6-
ccfdataset = json.load(f)
3+
4+
ccfdataset = LazyLoadedDict("ccfdataset.json")

resotodata/colors.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import json
2-
from pkg_resources import resource_filename
1+
from resotodata.utils import LazyLoadedDict
32

4-
data_file = resource_filename("resotodata", "data/colors.json")
5-
with open(data_file) as f:
6-
colors = json.load(f)
3+
4+
colors = LazyLoadedDict("colors.json")

0 commit comments

Comments
 (0)