|
1 | 1 | import os |
| 2 | +import venv |
2 | 3 | import json |
3 | 4 | import shutil |
4 | 5 | import requests |
@@ -31,6 +32,10 @@ def update_ccfdataset() -> None: |
31 | 32 | write_ccfdataset() |
32 | 33 |
|
33 | 34 |
|
| 35 | +def update_instances() -> None: |
| 36 | + write_instances() |
| 37 | + |
| 38 | + |
34 | 39 | def gen_digitalocean_regions() -> dict: |
35 | 40 | print("Processing DigitalOcean regions") |
36 | 41 | regions = {} |
@@ -224,6 +229,35 @@ def write_ccfdataset() -> None: |
224 | 229 | f.write("\n") |
225 | 230 |
|
226 | 231 |
|
| 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 | + |
227 | 261 | def get_ccfdataset() -> dict: |
228 | 262 | print("Checking if git and npm are installed") |
229 | 263 | for tool in ("git", "npm"): |
@@ -283,5 +317,49 @@ def get_ccfdataset() -> dict: |
283 | 317 | return json.loads(result.stdout) |
284 | 318 |
|
285 | 319 |
|
| 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 | + |
286 | 364 | if __name__ == "__main__": |
287 | 365 | main() |
0 commit comments