|
4 | 4 | import sys
|
5 | 5 | import time
|
6 | 6 | import argparse
|
7 |
| -import charon.util |
8 |
| -from charon import deployment |
| 7 | +import nixops.util |
| 8 | +from nixops import deployment |
9 | 9 | from boto.ec2.blockdevicemapping import BlockDeviceMapping, BlockDeviceType
|
| 10 | +import boto.ec2 |
10 | 11 |
|
11 | 12 | parser = argparse.ArgumentParser(description='Create an EBS-backed NixOS AMI')
|
12 |
| -parser.add_argument('--region', dest='region', required=True, help='EC2 region') |
13 |
| -parser.add_argument('--keep', dest='keep', action='store_true', help='Keep Charon machine after use') |
| 13 | +parser.add_argument('--region', dest='region', required=True, help='EC2 region to create the image in') |
| 14 | +parser.add_argument('--keep', dest='keep', action='store_true', help='Keep NixOps machine after use') |
14 | 15 | parser.add_argument('--hvm', dest='hvm', action='store_true', help='Create HVM image')
|
| 16 | +parser.add_argument('--key', dest='key_name', action='store_true', help='Keypair used for HVM instance creation', default="rob") |
15 | 17 | args = parser.parse_args()
|
16 | 18 |
|
17 | 19 | instance_type = "cc1.4xlarge" if args.hvm else "m1.small"
|
|
67 | 69 | m.run_command("nixos-rebuild switch")
|
68 | 70 | version = m.run_command("nixos-version", capture_stdout=True).replace('"', '').rstrip()
|
69 | 71 | print >> sys.stderr, "NixOS version is {0}".format(version)
|
70 |
| -m.run_command("cp -f $(nix-instantiate --find-file nixos/modules/virtualisation/amazon-config.nix) /mnt/etc/nixos/configuration.nix") |
| 72 | +m.upload_file("./amazon-base-config.nix", "/mnt/etc/nixos/configuration.nix") |
71 | 73 | m.run_command("nixos-install")
|
72 | 74 | if args.hvm:
|
73 | 75 | m.run_command('cp /mnt/nix/store/*-grub-0.97*/lib/grub/i386-pc/* /mnt/boot/grub')
|
@@ -98,32 +100,32 @@ def check():
|
98 | 100 | if args.hvm:
|
99 | 101 | instance = m._conn.run_instances( image_id="ami-6a9e4503"
|
100 | 102 | , instance_type=instance_type
|
101 |
| - , key_name=key_name |
| 103 | + , key_name=args.key_name |
102 | 104 | , placement=m.zone
|
103 | 105 | , security_groups=["eelco-test"]).instances[0]
|
104 |
| - charon.util.check_wait(lambda: instance.update() == 'running', max_tries=120) |
| 106 | + nixops.util.check_wait(lambda: instance.update() == 'running', max_tries=120) |
105 | 107 | instance.stop()
|
106 |
| - charon.util.check_wait(lambda: instance.update() == 'stopped', max_tries=120) |
| 108 | + nixops.util.check_wait(lambda: instance.update() == 'stopped', max_tries=120) |
107 | 109 | old_root_volume = m._conn.get_all_volumes([], filters={'attachment.instance-id': instance.id, 'attachment.device': "/dev/sda1"})[0]
|
108 | 110 | old_root_volume.detach()
|
109 | 111 | volume.detach()
|
110 |
| - charon.util.check_wait(lambda: volume.update() == 'available', max_tries=120) |
111 |
| - charon.util.check_wait(lambda: old_root_volume.update() == 'available', max_tries=120) |
| 112 | + nixops.util.check_wait(lambda: volume.update() == 'available', max_tries=120) |
| 113 | + nixops.util.check_wait(lambda: old_root_volume.update() == 'available', max_tries=120) |
112 | 114 | volume.attach(instance.id, '/dev/sda1')
|
113 |
| - charon.util.check_wait(lambda: volume.update() == 'in-use', max_tries=120) |
| 115 | + nixops.util.check_wait(lambda: volume.update() == 'in-use', max_tries=120) |
114 | 116 |
|
115 | 117 | ami_id = m._conn.create_image(instance.id, ami_name, description)
|
116 | 118 | time.sleep(5)
|
117 | 119 | image = m._conn.get_all_images([ami_id])[0]
|
118 |
| - charon.util.check_wait(lambda: image.update() == 'available', max_tries=120) |
| 120 | + nixops.util.check_wait(lambda: image.update() == 'available', max_tries=120) |
119 | 121 | instance.terminate()
|
120 | 122 |
|
121 | 123 | else:
|
122 | 124 | # Create a snapshot.
|
123 | 125 | snapshot = volume.create_snapshot(description=description)
|
124 | 126 | print >> sys.stderr, "created snapshot {0}".format(snapshot.id)
|
125 | 127 |
|
126 |
| - charon.util.check_wait(check, max_tries=120) |
| 128 | + nixops.util.check_wait(check, max_tries=120) |
127 | 129 |
|
128 | 130 | m._conn.create_tags([snapshot.id], {'Name': ami_name})
|
129 | 131 |
|
@@ -160,7 +162,6 @@ def check():
|
160 | 162 | image = m._conn.get_all_images(image_ids=[ami_id])[0]
|
161 | 163 | image.set_launch_permissions(user_ids=[], group_names=["all"])
|
162 | 164 |
|
163 |
| - |
164 | 165 | # Do a test deployment to make sure that the AMI works.
|
165 | 166 | f = open("ebs-test.nix", "w")
|
166 | 167 | f.write(
|
@@ -190,11 +191,30 @@ def check():
|
190 | 191 | test_depl.nix_exprs = [os.path.abspath("./ebs-test.nix")]
|
191 | 192 | test_depl.deploy(create_only=True)
|
192 | 193 | test_depl.machines['machine'].run_command("nixos-version")
|
193 |
| -if not args.keep: |
194 |
| - test_depl.destroy_resources() |
195 |
| - test_depl.delete() |
| 194 | + |
| 195 | +if args.hvm: |
| 196 | + image_type = 'hvm' |
| 197 | +else: |
| 198 | + image_type = 'ebs' |
196 | 199 |
|
197 | 200 | # Log the AMI ID.
|
198 |
| -f = open("{0}.ebs.ami-id".format(args.region), "w") |
| 201 | +f = open("{0}.{1}.ami-id".format(args.region, image_type), "w") |
199 | 202 | f.write("{0}".format(ami_id))
|
200 | 203 | f.close()
|
| 204 | + |
| 205 | +for dest in [ 'us-east-1', 'us-west-1', 'us-west-2', 'eu-west-1']: |
| 206 | + if args.region != dest: |
| 207 | + print >> sys.stderr, "copying image from region {0} to {1}".format(args.region, dest) |
| 208 | + conn = boto.ec2.connect_to_region(dest) |
| 209 | + copy_image = conn.copy_image(args.region, ami_id, ami_name, description=None, client_token=None) |
| 210 | + |
| 211 | + # Log the AMI ID. |
| 212 | + f = open("{0}.{1}.ami-id".format(dest, image_type), "w") |
| 213 | + f.write("{0}".format(copy_image.image_id)) |
| 214 | + f.close() |
| 215 | + |
| 216 | + |
| 217 | +if not args.keep: |
| 218 | + test_depl.destroy_resources() |
| 219 | + test_depl.delete() |
| 220 | + |
0 commit comments