Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support channels in senza images/senza patch
- Loading branch information
1 parent
fff8cf6
commit ac7770c
Showing
4 changed files
with
67 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import boto3 | ||
from collections import namedtuple | ||
|
||
TaupageChannel = namedtuple("TaupageChannel", ("image_mapping", "ami_wildcard", "public_ami_wildcard")) | ||
|
||
|
||
def _channel(suffix): | ||
return TaupageChannel("LatestTaupage{}Image".format(suffix), | ||
"Taupage{}-AMI-*".format(suffix), | ||
"Taupage{}-Public-AMI-*".format(suffix)) | ||
|
||
|
||
DEFAULT_CHANNEL = _channel("") | ||
|
||
CHANNELS = { | ||
"latest": DEFAULT_CHANNEL, | ||
"staging": _channel("Staging"), | ||
"dev": _channel("Dev") | ||
} | ||
|
||
|
||
def find_image(region: str, channel: TaupageChannel = None): | ||
'''Find the latest Taupage AMI, first try private images, fallback to public''' | ||
|
||
if channel is None: | ||
channel = DEFAULT_CHANNEL | ||
|
||
ec2 = boto3.resource('ec2', region) | ||
filters = [{'Name': 'name', 'Values': [channel.ami_wildcard]}, | ||
{'Name': 'is-public', 'Values': ['false']}, | ||
{'Name': 'state', 'Values': ['available']}, | ||
{'Name': 'root-device-type', 'Values': ['ebs']}] | ||
images = list(ec2.images.filter(Filters=filters)) | ||
if not images: | ||
public_filters = [{'Name': 'name', 'Values': [channel.public_ami_wildcard]}, | ||
{'Name': 'is-public', 'Values': ['true']}, | ||
{'Name': 'state', 'Values': ['available']}, | ||
{'Name': 'root-device-type', 'Values': ['ebs']}] | ||
images = list(ec2.images.filter(Filters=public_filters)) | ||
|
||
if not images: | ||
return None | ||
|
||
most_recent_image = sorted(images, key=lambda i: i.name)[-1] | ||
return most_recent_image |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters