Skip to content
This repository was archived by the owner on Aug 27, 2018. It is now read-only.

Commit 4a40a1f

Browse files
committed
Update to use NixOps in stead on charon, use copy_image method for copying to other regions.
1 parent 6d6e8c3 commit 4a40a1f

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

maintainers/scripts/ec2/create-ebs-amis.py

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
import sys
55
import time
66
import argparse
7-
import charon.util
8-
from charon import deployment
7+
import nixops.util
8+
from nixops import deployment
99
from boto.ec2.blockdevicemapping import BlockDeviceMapping, BlockDeviceType
10+
import boto.ec2
1011

1112
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')
1415
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")
1517
args = parser.parse_args()
1618

1719
instance_type = "cc1.4xlarge" if args.hvm else "m1.small"
@@ -67,7 +69,7 @@
6769
m.run_command("nixos-rebuild switch")
6870
version = m.run_command("nixos-version", capture_stdout=True).replace('"', '').rstrip()
6971
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")
7173
m.run_command("nixos-install")
7274
if args.hvm:
7375
m.run_command('cp /mnt/nix/store/*-grub-0.97*/lib/grub/i386-pc/* /mnt/boot/grub')
@@ -98,32 +100,32 @@ def check():
98100
if args.hvm:
99101
instance = m._conn.run_instances( image_id="ami-6a9e4503"
100102
, instance_type=instance_type
101-
, key_name=key_name
103+
, key_name=args.key_name
102104
, placement=m.zone
103105
, 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)
105107
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)
107109
old_root_volume = m._conn.get_all_volumes([], filters={'attachment.instance-id': instance.id, 'attachment.device': "/dev/sda1"})[0]
108110
old_root_volume.detach()
109111
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)
112114
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)
114116

115117
ami_id = m._conn.create_image(instance.id, ami_name, description)
116118
time.sleep(5)
117119
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)
119121
instance.terminate()
120122

121123
else:
122124
# Create a snapshot.
123125
snapshot = volume.create_snapshot(description=description)
124126
print >> sys.stderr, "created snapshot {0}".format(snapshot.id)
125127

126-
charon.util.check_wait(check, max_tries=120)
128+
nixops.util.check_wait(check, max_tries=120)
127129

128130
m._conn.create_tags([snapshot.id], {'Name': ami_name})
129131

@@ -160,7 +162,6 @@ def check():
160162
image = m._conn.get_all_images(image_ids=[ami_id])[0]
161163
image.set_launch_permissions(user_ids=[], group_names=["all"])
162164

163-
164165
# Do a test deployment to make sure that the AMI works.
165166
f = open("ebs-test.nix", "w")
166167
f.write(
@@ -190,11 +191,30 @@ def check():
190191
test_depl.nix_exprs = [os.path.abspath("./ebs-test.nix")]
191192
test_depl.deploy(create_only=True)
192193
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'
196199

197200
# 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")
199202
f.write("{0}".format(ami_id))
200203
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

Comments
 (0)