From 5a23232edf00c6e508deca852cdac320233073e2 Mon Sep 17 00:00:00 2001 From: Rob Vermaas Date: Mon, 27 Oct 2014 11:48:43 +0100 Subject: [PATCH] Refactor EC2 image generation script. Now uses same procedure for both hvm and pv-grub images. Root disks are now SSD backed and 20GB by default, both on hvm and pv-grub (previously was 8GB for HVM). Added new eu-central region to the locations to copy images to. Also the root disk for HVM instances was not deleted on termination with previous images, this is fixed as well. --- .../scripts/ec2/amazon-hvm-install-config.nix | 3 +- .../scripts/ec2/create-ebs-amis.py | 90 ++++++++----------- 2 files changed, 38 insertions(+), 55 deletions(-) diff --git a/nixos/maintainers/scripts/ec2/amazon-hvm-install-config.nix b/nixos/maintainers/scripts/ec2/amazon-hvm-install-config.nix index d9feba164a76..530769cec5b7 100644 --- a/nixos/maintainers/scripts/ec2/amazon-hvm-install-config.nix +++ b/nixos/maintainers/scripts/ec2/amazon-hvm-install-config.nix @@ -19,7 +19,8 @@ in { imports = [ ./amazon-base-config.nix ]; ec2.hvm = true; - boot.loader.grub.device = lib.mkOverride 0 "nodev"; + boot.loader.grub.device = lib.mkOverride 0 "/dev/xvdg"; + boot.kernelParams = [ "console=ttyS0" ]; boot.initrd.extraUtilsCommands = '' cp -v ${pkgs.gawk}/bin/gawk $out/bin/gawk diff --git a/nixos/maintainers/scripts/ec2/create-ebs-amis.py b/nixos/maintainers/scripts/ec2/create-ebs-amis.py index 14607b9a3678..62525651ae03 100755 --- a/nixos/maintainers/scripts/ec2/create-ebs-amis.py +++ b/nixos/maintainers/scripts/ec2/create-ebs-amis.py @@ -19,8 +19,17 @@ parser.add_argument('--key', dest='key_name', action='store_true', help='Keypair args = parser.parse_args() instance_type = "m3.medium" if args.hvm else "m1.small" -ebs_size = 8 if args.hvm else 20 +if args.hvm: + virtualization_type = "hvm" + root_block = "/dev/sda1" + image_type = 'hvm' +else: + virtualization_type = "paravirtual" + root_block = "/dev/sda" + image_type = 'ebs' + +ebs_size = 20 # Start a NixOS machine in the given region. f = open("ebs-creator-config.nix", "w") @@ -76,10 +85,6 @@ if args.hvm: m.upload_file("./amazon-hvm-config.nix", "/mnt/etc/nixos/configuration.nix") m.upload_file("./amazon-hvm-install-config.nix", "/mnt/etc/nixos/amazon-hvm-install-config.nix") m.run_command("NIXOS_CONFIG=/etc/nixos/amazon-hvm-install-config.nix nixos-install") - m.run_command('nix-env -iA nixos.pkgs.grub') - m.run_command('cp /nix/store/*-grub-0.97*/lib/grub/i386-pc/* /mnt/boot/grub') - m.run_command('echo "(hd1) /dev/xvdg" > device.map') - m.run_command('echo -e "root (hd1,0)\nsetup (hd1)" | grub --device-map=device.map --batch') else: m.upload_file("./amazon-base-config.nix", "/mnt/etc/nixos/configuration.nix") m.run_command("nixos-install") @@ -87,7 +92,7 @@ else: m.run_command("umount /mnt") if args.hvm: - ami_name = "nixos-{0}-x86_64-ebs-hvm".format(version) + ami_name = "nixos-{0}-x86_64-hvm".format(version) description = "NixOS {0} (x86_64; EBS root; hvm)".format(version) else: ami_name = "nixos-{0}-x86_64-ebs".format(version) @@ -102,58 +107,40 @@ def check(): m.connect() volume = m._conn.get_all_volumes([], filters={'attachment.instance-id': m.resource_id, 'attachment.device': "/dev/sdg"})[0] -if args.hvm: - instance = m._conn.run_instances( image_id="ami-5f491f36" - , instance_type=instance_type - , key_name=args.key_name - , placement=m.zone - , security_groups=["eelco-test"]).instances[0] - nixops.util.check_wait(lambda: instance.update() == 'running', max_tries=120) - instance.stop() - nixops.util.check_wait(lambda: instance.update() == 'stopped', max_tries=120) - old_root_volume = m._conn.get_all_volumes([], filters={'attachment.instance-id': instance.id, 'attachment.device': "/dev/sda1"})[0] - old_root_volume.detach() - volume.detach() - nixops.util.check_wait(lambda: volume.update() == 'available', max_tries=120) - nixops.util.check_wait(lambda: old_root_volume.update() == 'available', max_tries=120) - volume.attach(instance.id, '/dev/sda1') - nixops.util.check_wait(lambda: volume.update() == 'in-use', max_tries=120) - ami_id = m._conn.create_image(instance.id, ami_name, description) - time.sleep(5) - image = m._conn.get_all_images([ami_id])[0] - nixops.util.check_wait(lambda: image.update() == 'available', max_tries=120) - instance.terminate() +# Create a snapshot. +snapshot = volume.create_snapshot(description=description) +print >> sys.stderr, "created snapshot {0}".format(snapshot.id) -else: - # Create a snapshot. - snapshot = volume.create_snapshot(description=description) - print >> sys.stderr, "created snapshot {0}".format(snapshot.id) +nixops.util.check_wait(check, max_tries=120) - nixops.util.check_wait(check, max_tries=120) +m._conn.create_tags([snapshot.id], {'Name': ami_name}) - m._conn.create_tags([snapshot.id], {'Name': ami_name}) +if not args.keep: depl.destroy_resources() - if not args.keep: depl.destroy_resources() +# Register the image. +aki = m._conn.get_all_images(filters={'manifest-location': 'ec2*pv-grub-hd0_1.03-x86_64*'})[0] +print >> sys.stderr, "using kernel image {0} - {1}".format(aki.id, aki.location) - # Register the image. - aki = m._conn.get_all_images(filters={'manifest-location': '*pv-grub-hd0_1.03-x86_64*'})[0] - print >> sys.stderr, "using kernel image {0} - {1}".format(aki.id, aki.location) +block_map = BlockDeviceMapping() +block_map[root_block] = BlockDeviceType(snapshot_id=snapshot.id, delete_on_termination=True, size=ebs_size, volume_type="gp2") +block_map['/dev/sdb'] = BlockDeviceType(ephemeral_name="ephemeral0") +block_map['/dev/sdc'] = BlockDeviceType(ephemeral_name="ephemeral1") +block_map['/dev/sdd'] = BlockDeviceType(ephemeral_name="ephemeral2") +block_map['/dev/sde'] = BlockDeviceType(ephemeral_name="ephemeral3") - block_map = BlockDeviceMapping() - block_map['/dev/sda'] = BlockDeviceType(snapshot_id=snapshot.id, delete_on_termination=True) - block_map['/dev/sdb'] = BlockDeviceType(ephemeral_name="ephemeral0") - block_map['/dev/sdc'] = BlockDeviceType(ephemeral_name="ephemeral1") - block_map['/dev/sdd'] = BlockDeviceType(ephemeral_name="ephemeral2") - block_map['/dev/sde'] = BlockDeviceType(ephemeral_name="ephemeral3") - - ami_id = m._conn.register_image( +common_args = dict( name=ami_name, description=description, architecture="x86_64", - root_device_name="/dev/sda", - kernel_id=aki.id, - block_device_map=block_map) + root_device_name=root_block, + block_device_map=block_map, + virtualization_type=virtualization_type, + delete_root_volume_on_termination=True + ) +if not args.hvm: + common_args['kernel_id']=aki.id +ami_id = m._conn.register_image(**common_args) print >> sys.stderr, "registered AMI {0}".format(ami_id) @@ -197,17 +184,12 @@ test_depl.nix_exprs = [os.path.abspath("./ebs-test.nix")] test_depl.deploy(create_only=True) test_depl.machines['machine'].run_command("nixos-version") -if args.hvm: - image_type = 'hvm' -else: - image_type = 'ebs' - # Log the AMI ID. f = open("{0}.{1}.ami-id".format(args.region, image_type), "w") f.write("{0}".format(ami_id)) f.close() -for dest in [ 'us-east-1', 'us-west-1', 'us-west-2', 'eu-west-1', 'ap-southeast-1', 'ap-southeast-2', 'ap-northeast-1', 'sa-east-1']: +for dest in [ 'us-east-1', 'us-west-1', 'us-west-2', 'eu-west-1', 'eu-central-1', 'ap-southeast-1', 'ap-southeast-2', 'ap-northeast-1', 'sa-east-1']: if args.region != dest: print >> sys.stderr, "copying image from region {0} to {1}".format(args.region, dest) conn = boto.ec2.connect_to_region(dest)