[cloud] Autodetect CPU architecture from AMI disk image

Signed-off-by: Michael Brown <mcb30@ipxe.org>
pull/373/head
Michael Brown 2021-05-02 09:39:10 +01:00
parent 6dad316e66
commit 438513f6f6
1 changed files with 17 additions and 6 deletions

View File

@ -6,12 +6,22 @@ from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import date from datetime import date
from hashlib import sha256 from hashlib import sha256
from itertools import count from itertools import count
import subprocess
import boto3 import boto3
BLOCKSIZE = 512 * 1024 BLOCKSIZE = 512 * 1024
def detect_architecture(image):
"""Detect CPU architecture"""
mdir = subprocess.run(['mdir', '-b', '-i', image, '::/EFI/BOOT'],
capture_output=True)
if any(b'BOOTAA64.EFI' in x for x in mdir.stdout.splitlines()):
return 'arm64'
return 'x86_64'
def create_snapshot(region, description, image): def create_snapshot(region, description, image):
"""Create an EBS snapshot""" """Create an EBS snapshot"""
client = boto3.client('ebs', region_name=region) client = boto3.client('ebs', region_name=region)
@ -74,8 +84,6 @@ def launch_link(region, image_id):
# Parse command-line arguments # Parse command-line arguments
parser = argparse.ArgumentParser(description="Import AWS EC2 image (AMI)") parser = argparse.ArgumentParser(description="Import AWS EC2 image (AMI)")
parser.add_argument('--architecture', '-a', default='x86_64',
help="CPU architecture")
parser.add_argument('--name', '-n', parser.add_argument('--name', '-n',
help="Image name") help="Image name")
parser.add_argument('--public', '-p', action='store_true', parser.add_argument('--public', '-p', action='store_true',
@ -87,11 +95,14 @@ parser.add_argument('--wiki', '-w', metavar='FILE',
parser.add_argument('image', help="iPXE disk image") parser.add_argument('image', help="iPXE disk image")
args = parser.parse_args() args = parser.parse_args()
# Detect CPU architecture
architecture = detect_architecture(args.image)
# Use default name if none specified # Use default name if none specified
if not args.name: if not args.name:
args.name = 'iPXE (%s %s)' % ( args.name = 'iPXE (%s %s)' % (
date.today().strftime('%Y-%m-%d'), date.today().strftime('%Y-%m-%d'),
args.architecture, architecture,
) )
# Use all regions if none specified # Use all regions if none specified
@ -104,7 +115,7 @@ with ThreadPoolExecutor(max_workers=len(args.region)) as executor:
futures = {executor.submit(import_image, futures = {executor.submit(import_image,
region=region, region=region,
name=args.name, name=args.name,
architecture=args.architecture, architecture=architecture,
image=args.image, image=args.image,
public=args.public): region public=args.public): region
for region in args.region} for region in args.region}
@ -115,7 +126,7 @@ with ThreadPoolExecutor(max_workers=len(args.region)) as executor:
wikitab = ["^ AWS region ^ CPU architecture ^ AMI ID ^\n"] + list( wikitab = ["^ AWS region ^ CPU architecture ^ AMI ID ^\n"] + list(
"| ''%s'' | ''%s'' | ''[[%s|%s]]'' |\n" % ( "| ''%s'' | ''%s'' | ''[[%s|%s]]'' |\n" % (
region, region,
args.architecture, architecture,
launch_link(region, results[region]), launch_link(region, results[region]),
results[region], results[region],
) for region in args.region) ) for region in args.region)
@ -125,4 +136,4 @@ if args.wiki:
# Show created images # Show created images
for region in args.region: for region in args.region:
print("%s: %s" % (region, results[region])) print("%s %s %s" % (region, architecture, results[region]))