Skip to content

Commit 385d609

Browse files
authored
Adding logic to handle gzip'd botocore ec2 service-2.json (#2073)
1 parent c9f08e6 commit 385d609

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

runway/blueprints/k8s/k8s_workers.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22
"""Module with k8s nodegroup."""
3+
import gzip
34
import json
45
import os
56
from typing import Any
@@ -20,18 +21,24 @@
2021

2122

2223
def get_valid_instance_types() -> Any:
23-
"""Return list of instance types."""
24-
ec2_service_file = os.path.join(
25-
os.path.dirname(botocore.__file__),
26-
"data",
27-
"ec2",
28-
"2016-11-15",
29-
"service-2.json",
24+
"""Return list of instance types from either a JSON or gzipped JSON file."""
25+
base_path = os.path.join(
26+
os.path.dirname(botocore.__file__), "data", "ec2", "2016-11-15"
3027
)
31-
# This encoding needs to be explicitly called out as utf-8 on Windows
32-
# (or it will try cp1252 instead)
33-
with open(ec2_service_file, "r", encoding="utf-8") as stream:
34-
return json.load(stream)["shapes"]["InstanceType"]["enum"]
28+
29+
json_path = os.path.join(base_path, "service-2.json")
30+
gzip_path = os.path.join(base_path, "service-2.json.gz")
31+
32+
if os.path.exists(gzip_path):
33+
with gzip.open(gzip_path, "rt", encoding="utf-8") as stream:
34+
data = json.load(stream)
35+
elif os.path.exists(json_path):
36+
with open(json_path, "r", encoding="utf-8") as stream:
37+
data = json.load(stream)
38+
else:
39+
raise FileNotFoundError("Neither JSON nor gzipped JSON file found.")
40+
41+
return data["shapes"]["InstanceType"]["enum"]
3542

3643

3744
class NodeGroup(Blueprint):

0 commit comments

Comments
 (0)