Skip to content

Commit

Permalink
Remove cruft and no-ops.
Browse files Browse the repository at this point in the history
  • Loading branch information
DailyDreaming committed Jan 30, 2025
1 parent 335ddd0 commit e97e405
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 46 deletions.
49 changes: 11 additions & 38 deletions src/toil/lib/aws/ami.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
logger = logging.getLogger(__name__)

class ReleaseFeedUnavailableError(RuntimeError):
"""
Raised when a Flatcar releases can't be located.
"""
"""Raised when a Flatcar releases can't be located."""
pass

@retry(errors=[ReleaseFeedUnavailableError])
Expand All @@ -32,16 +30,13 @@ def get_flatcar_ami(ec2_client: BaseClient, architecture: str = "amd64") -> str:
2. Official AMI from stable.release.flatcar-linux.net
3. Search the AWS Marketplace
:raises ReleaseFeedUnavailableError: if all of these sources fail.
:param ec2_client: Boto3 EC2 Client
:param architecture: The architecture type for the new AWS machine. Can be either amd64 or arm64
"""

# Take a user override
ami = os.environ.get("TOIL_AWS_AMI")
try_number = 0
if not ami:
logger.debug(
"No AMI found in TOIL_AWS_AMI; checking stable Flatcar release feed"
Expand All @@ -56,10 +51,6 @@ def get_flatcar_ami(ec2_client: BaseClient, architecture: str = "amd64") -> str:
ami = aws_marketplace_flatcar_ami_search(
ec2_client=ec2_client, architecture=architecture
)
logger.debug("No AMI found in marketplace; checking Toil Flatcar release feed")
ami = feed_flatcar_ami_release(
ec2_client=ec2_client, architecture=architecture, source="toil"
)
if not ami:
logger.debug(
"No AMI found in Toil project feed; checking beta Flatcar release feed"
Expand Down Expand Up @@ -100,9 +91,8 @@ def _fetch_flatcar_feed(architecture: str = "amd64", source: str = "stable") ->
JSON_FEED_URL = {
"stable": f"https://stable.release.flatcar-linux.net/{architecture}-usr/current/flatcar_production_ami_all.json",
"beta": f"https://beta.release.flatcar-linux.net/{architecture}-usr/current/flatcar_production_ami_all.json",
"alpha": f"https://alpha.release.flatcar-linux.net/{architecture}-usr/current/flatcar_production_ami_all.json",
"archive": f"https://web.archive.org/web/20220625112618if_/https://stable.release.flatcar-linux.net/{architecture}-usr/current/flatcar_production_ami_all.json",
"toil": f"https://raw.githubusercontent.com/DataBiosphere/toil/master/contrib/flatcar/{architecture}-usr/current/flatcar_production_ami_all.json",
# "alpha": f"https://alpha.release.flatcar-linux.net/{architecture}-usr/current/flatcar_production_ami_all.json",
"archive": f"https://web.archive.org/web/20220625112618if_/https://stable.release.flatcar-linux.net/{architecture}-usr/current/flatcar_production_ami_all.json"
}[source]
return cast(bytes, urllib.request.urlopen(JSON_FEED_URL).read())

Expand Down Expand Up @@ -135,8 +125,7 @@ def flatcar_release_feed_amis(
except HTTPError:
# Flatcar servers did not return the feed
logger.exception(f"Could not retrieve {source} Flatcar release feed JSON")
# This is probably a permanent error, or at least unlikely to go
# away immediately.
# This is probably a permanent error, or at least unlikely to go away immediately.
return
except json.JSONDecodeError:
# Feed is not JSON
Expand All @@ -158,16 +147,10 @@ def flatcar_release_feed_amis(

for ami_record in feed.get("amis", []):
# Scan the list of regions
if ami_record.get("name", None) == region:
# When we find ours, return the AMI ID
if "hvm" in ami_record:
yield ami_record["hvm"]
# And stop, there should be one per region.
return
if ami_record.get("name") == region:
return ami_record.get("hvm")
# We didn't find our region
logger.warning(
f"Flatcar {source} release feed does not have an image for region {region}"
)
logger.warning(f"Flatcar {source} release feed does not have an image for region {region}")


def feed_flatcar_ami_release(
Expand Down Expand Up @@ -196,28 +179,18 @@ def feed_flatcar_ami_release(
# verify it exists on AWS
try:
response = ec2_client.describe_images(Filters=[{"Name": "image-id", "Values": [ami]}]) # type: ignore
if (
len(response["Images"]) == 1
and response["Images"][0]["State"] == "available"
):
if (len(response["Images"]) == 1 and response["Images"][0]["State"] == "available"):
return ami
else:
logger.warning(
f"Flatcar release feed suggests image {ami} which does not exist on AWS in {region}"
)
logger.warning(f"Flatcar release feed suggests image {ami} which does not exist on AWS in {region}")
except (ClientError, EndpointConnectionError):
# Sometimes we get back nonsense like:
# botocore.exceptions.ClientError: An error occurred (AuthFailure) when calling the DescribeImages operation: AWS was not able to validate the provided access credentials
# Don't hold that against the AMI.
logger.exception(
f"Unable to check if AMI {ami} exists on AWS in {region}; assuming it does"
)
logger.exception(f"Unable to check if AMI {ami} exists on AWS in {region}; assuming it does")
return ami
# We didn't find it
logger.warning(
f"Flatcar release feed does not have an image for region {region} that exists on AWS"
)
return None
logger.warning(f"Flatcar release feed does not have an image for region {region} that exists on AWS")


def aws_marketplace_flatcar_ami_search(
Expand Down
13 changes: 5 additions & 8 deletions src/toil/test/lib/test_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import logging
import os

import pytest
from unittest import mock

from toil.lib.aws.ami import (
aws_marketplace_flatcar_ami_search,
Expand Down Expand Up @@ -59,7 +59,7 @@ def test_parse_stable_feed(self):
self.assertTrue(ami.startswith("ami-"))


@needs_aws_ec2
# @needs_aws_ec2
class AMITest(ToilTest):
@classmethod
def setUpClass(cls):
Expand All @@ -69,15 +69,12 @@ def setUpClass(cls):
cls.ec2_client = session.client("ec2")

def test_fetch_flatcar(self):
print('anything at all')
# raise RuntimeError('wat')
with self.subTest("Test flatcar AMI from user is prioritized."):
try:
os.environ["TOIL_AWS_AMI"] = "overridden"
with mock.patch.dict(os.environ, {"TOIL_AWS_AMI": "overridden"}):
ami = get_flatcar_ami(self.ec2_client)
self.assertEqual(ami, "overridden")
del os.environ["TOIL_AWS_AMI"]
except ReleaseFeedUnavailableError:
# Ignore any remote systems being down.
pass

with self.subTest("Test flatcar AMI returns an AMI-looking AMI."):
try:
Expand Down

0 comments on commit e97e405

Please sign in to comment.