Created
January 9, 2018 18:24
-
-
Save 3h4x/dbeff2e08d53f32ae7adf341829df3a4 to your computer and use it in GitHub Desktop.
Ubuntu AMI finder CLI
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
import json | |
import re | |
import click | |
import dataset | |
import demjson | |
import requests | |
@click.command() | |
@click.option('-n', '--name') | |
@click.option('-r', '--region') | |
@click.option('-v', '--version') | |
@click.option('-a', '--arch') | |
@click.option('-i', '--instance-type') | |
@click.option('--release') | |
@click.option('--ami-id') | |
@click.option('--aki-id') | |
def main(name, region, version, arch, instance_type, release, ami_id, aki_id): | |
db = dataset.connect('sqlite:///:memory:') | |
table = db['ubuntu'] | |
response = requests.get('https://cloud-images.ubuntu.com/locator/ec2/releasesTable') | |
for row in demjson.decode(response.content).get('aaData', []): | |
table.insert({ | |
'region': row[0], | |
'name': row[1], | |
'version': row[2], | |
'arch': row[3], | |
'instance_type': row[4], | |
'release': row[5], | |
'ami_id': re.findall('<a.+>(.+?)</[a]>', row[6])[0], | |
'aki_id': row[7], | |
}) | |
kwargs = {} | |
if name: | |
kwargs['name'] = name | |
if region: | |
kwargs['region'] = region | |
if version: | |
kwargs['version'] = version | |
if arch: | |
kwargs['arch'] = arch | |
if instance_type: | |
kwargs['instance_type'] = instance_type | |
if release: | |
kwargs['release'] = release | |
if ami_id: | |
kwargs['ami_id'] = ami_id | |
if aki_id: | |
kwargs['aki_id'] = aki_id | |
for row in table.find(**kwargs): | |
row.pop('id') | |
print(json.dumps(row, indent=4)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment