moto/scripts/get_instance_info.py

171 lines
4.3 KiB
Python
Raw Normal View History

2017-09-27 16:27:36 +00:00
#!/usr/bin/env python
2020-04-15 21:18:33 +00:00
2017-09-27 16:27:36 +00:00
import json
import os
import subprocess
import requests
from bs4 import BeautifulSoup
class Instance(object):
def __init__(self, instance):
self.instance = instance
def _get_td(self, td):
2020-04-15 21:18:33 +00:00
return self.instance.find("td", attrs={"class": td})
2017-09-27 16:27:36 +00:00
def _get_sort(self, td):
2020-04-15 21:18:33 +00:00
return float(self.instance.find("td", attrs={"class": td}).find("span")["sort"])
2017-09-27 16:27:36 +00:00
@property
def name(self):
2020-04-15 21:18:33 +00:00
return self._get_td("name").text.strip()
2017-09-27 16:27:36 +00:00
@property
def apiname(self):
2020-04-15 21:18:33 +00:00
return self._get_td("apiname").text.strip()
2017-09-27 16:27:36 +00:00
@property
def memory(self):
2020-04-15 21:18:33 +00:00
return self._get_sort("memory")
2017-09-27 16:27:36 +00:00
@property
def computeunits(self):
2020-04-15 21:18:33 +00:00
return self._get_sort("computeunits")
2017-09-27 16:27:36 +00:00
@property
def vcpus(self):
2020-04-15 21:18:33 +00:00
return self._get_sort("vcpus")
2017-09-27 16:27:36 +00:00
@property
def gpus(self):
2020-04-15 21:18:33 +00:00
return int(self._get_td("gpus").text.strip())
2017-09-27 16:27:36 +00:00
@property
def fpga(self):
2020-04-15 21:18:33 +00:00
return int(self._get_td("fpga").text.strip())
2017-09-27 16:27:36 +00:00
@property
def ecu_per_vcpu(self):
2020-04-15 21:18:33 +00:00
return self._get_sort("ecu-per-vcpu")
2017-09-27 16:27:36 +00:00
@property
def physical_processor(self):
2020-04-15 21:18:33 +00:00
return self._get_td("physical_processor").text.strip()
2017-09-27 16:27:36 +00:00
@property
def clock_speed_ghz(self):
2020-04-15 21:18:33 +00:00
return self._get_td("clock_speed_ghz").text.strip()
2017-09-27 16:27:36 +00:00
@property
def intel_avx(self):
2020-04-15 21:18:33 +00:00
return self._get_td("intel_avx").text.strip()
2017-09-27 16:27:36 +00:00
@property
def intel_avx2(self):
2020-04-15 21:18:33 +00:00
return self._get_td("intel_avx2").text.strip()
2017-09-27 16:27:36 +00:00
@property
def intel_turbo(self):
2020-04-15 21:18:33 +00:00
return self._get_td("intel_turbo").text.strip()
2017-09-27 16:27:36 +00:00
@property
def storage(self):
2020-04-15 21:18:33 +00:00
return self._get_sort("storage")
2017-09-27 16:27:36 +00:00
@property
def architecture(self):
2020-04-15 21:18:33 +00:00
return self._get_td("architecture").text.strip()
2017-09-27 16:27:36 +00:00
@property
def network_perf(self): # 2 == low
2020-04-15 21:18:33 +00:00
return self._get_sort("networkperf")
2017-09-27 16:27:36 +00:00
@property
def ebs_max_bandwidth(self):
2020-04-15 21:18:33 +00:00
return self._get_sort("ebs-max-bandwidth")
2017-09-27 16:27:36 +00:00
@property
def ebs_throughput(self):
2020-04-15 21:18:33 +00:00
return self._get_sort("ebs-throughput")
2017-09-27 16:27:36 +00:00
@property
def ebs_iops(self):
2020-04-15 21:18:33 +00:00
return self._get_sort("ebs-iops")
2017-09-27 16:27:36 +00:00
@property
def max_ips(self):
2020-04-15 21:18:33 +00:00
return int(self._get_td("maxips").text.strip())
2017-09-27 16:27:36 +00:00
@property
def enhanced_networking(self):
2020-04-15 21:18:33 +00:00
return self._get_td("enhanced-networking").text.strip() != "No"
2017-09-27 16:27:36 +00:00
@property
def vpc_only(self):
2020-04-15 21:18:33 +00:00
return self._get_td("vpc-only").text.strip() != "No"
2017-09-27 16:27:36 +00:00
@property
def ipv6_support(self):
2020-04-15 21:18:33 +00:00
return self._get_td("ipv6-support").text.strip() != "No"
2017-09-27 16:27:36 +00:00
@property
def placement_group_support(self):
2020-04-15 21:18:33 +00:00
return self._get_td("placement-group-support").text.strip() != "No"
2017-09-27 16:27:36 +00:00
@property
def linux_virtualization(self):
2020-04-15 21:18:33 +00:00
return self._get_td("linux-virtualization").text.strip()
2017-09-27 16:27:36 +00:00
def to_dict(self):
result = {}
2020-04-15 21:18:33 +00:00
for attr in [
x
for x in self.__class__.__dict__.keys()
if not x.startswith("_") and x != "to_dict"
]:
try:
result[attr] = getattr(self, attr)
except ValueError as ex:
if "'N/A'" in str(ex):
print(
"Skipping attribute '{0}' for instance type '{1}' (not found)".format(
attr, self.name
)
)
else:
raise
2017-09-27 16:27:36 +00:00
return self.apiname, result
def main():
print("Getting HTML from http://www.ec2instances.info")
2020-04-15 21:18:33 +00:00
page_request = requests.get("http://www.ec2instances.info")
soup = BeautifulSoup(page_request.text, "html.parser")
data_table = soup.find(id="data")
2017-09-27 16:27:36 +00:00
print("Finding data in table")
2020-04-15 21:18:33 +00:00
instances = data_table.find("tbody").find_all("tr")
2017-09-27 16:27:36 +00:00
print("Parsing data")
result = {}
for instance in instances:
instance_id, instance_data = Instance(instance).to_dict()
result[instance_id] = instance_data
2020-04-15 21:18:33 +00:00
root_dir = (
subprocess.check_output(["git", "rev-parse", "--show-toplevel"])
.decode()
.strip()
)
dest = os.path.join(root_dir, "moto/ec2/resources/instance_types.json")
2017-09-27 16:27:36 +00:00
print("Writing data to {0}".format(dest))
2020-04-15 21:18:33 +00:00
with open(dest, "w") as open_file:
json.dump(result, open_file, sort_keys=True)
2017-09-27 16:27:36 +00:00
2020-04-15 21:18:33 +00:00
if __name__ == "__main__":
2017-09-27 16:27:36 +00:00
main()