Permalink
Browse files

Add a deleteOnTermination option

Fixes #13.
  • Loading branch information...
1 parent 24b4de5 commit c70c624fed20158390fa6e0a5c3496ad5c83d991 @edolstra edolstra committed Apr 18, 2012
Showing with 55 additions and 31 deletions.
  1. +13 −5 charon/backends/ec2.py
  2. +42 −26 nix/options.nix
@@ -35,7 +35,8 @@ def __init__(self, xml):
def f(xml):
return {'disk': xml.find("attrs/attr[@name='disk']/string").get("value"),
'size': int(xml.find("attrs/attr[@name='size']/int").get("value")),
- 'fsType': xml.find("attrs/attr[@name='fsType']/string").get("value")}
+ 'fsType': xml.find("attrs/attr[@name='fsType']/string").get("value"),
+ 'deleteOnTermination': xml.find("attrs/attr[@name='deleteOnTermination']/bool").get("value") == "true"}
self.block_device_mapping = {_xvd_to_sd(k.get("name")): f(k) for k in x.findall("attr[@name='blockDeviceMapping']/attrs/attr")}
def make_state():
@@ -275,7 +276,8 @@ def create(self, defn, check):
raise Exception("non-ephemeral disk not allowed on device ‘{0}’; use /dev/xvdf or higher".format(_sd_to_xvd(k)))
if v['disk'] == '':
if ami.root_device_type == "ebs":
- devmap[k] = boto.ec2.blockdevicemapping.BlockDeviceType(size=v['size'], delete_on_termination=True)
+ devmap[k] = boto.ec2.blockdevicemapping.BlockDeviceType(
+ size=v['size'], delete_on_termination=v['deleteOnTermination'])
v['needsInit'] = True
self._block_device_mapping[k] = v
# Otherwise, it's instance store backed, and we'll create the volume later.
@@ -367,7 +369,11 @@ def create(self, defn, check):
self.connect()
volume = self._conn.create_volume(size=v['size'], zone=self._zone)
v['needsInit'] = True
- v['deleteOnTermination'] = True
+ # The flag charonDeleteOnTermination denotes that on
+ # instance termination, we have to delete the volume
+ # ourselves. For volumes created at instance creation
+ # time, EC2 will do it for us.
+ v['charonDeleteOnTermination'] = v['deleteOnTermination']
v['needsAttach'] = True
v['volumeId'] = volume.id
self._block_device_mapping[k] = v
@@ -404,6 +410,8 @@ def check_dev():
charon.util.check_wait(check_dev)
del v['needsAttach']
self.write()
+
+ # FIXME: process changes to the deleteOnTermination flag.
# Detach volumes that are no longer in the deployment spec.
for k, v in self._block_device_mapping.items():
@@ -418,7 +426,7 @@ def check_dev():
raise Exception("unable to detach device ‘{0}’ from EC2 machine ‘{1}".format(v['disk'], self.name))
# FIXME: Wait until the volume is actually detached.
- if v.get('deleteOnTermination', False):
+ if v.get('charonDeleteOnTermination', False):
self._delete_volume(v['volumeId'])
del self._block_device_mapping[k]
@@ -478,7 +486,7 @@ def destroy(self):
# Destroy volumes created for this instance.
for k, v in self._block_device_mapping.items():
- if v.get('deleteOnTermination', False):
+ if v.get('charonDeleteOnTermination', False):
self._delete_volume(v['volumeId'])
View
@@ -6,36 +6,52 @@ let
cfg = config.deployment;
- ec2DiskOptions = {
+ ec2DiskOptions = { config, ... }: {
+
+ options = {
- disk = mkOption {
- default = "";
- example = "vol-d04895b8";
- type = types.uniq types.string;
- description = ''
- EC2 identifier of the disk to be mounted. This can be an
- ephemeral disk (e.g. <literal>ephemeral0</literal>), a
- snapshot ID (e.g. <literal>snap-1cbda474</literal>) or a
- volume ID (e.g. <literal>vol-d04895b8</literal>). Leave
- empty to create an EBS volume automatically.
- '';
- };
+ disk = mkOption {
+ default = "";
+ example = "vol-d04895b8";
+ type = types.uniq types.string;
+ description = ''
+ EC2 identifier of the disk to be mounted. This can be an
+ ephemeral disk (e.g. <literal>ephemeral0</literal>), a
+ snapshot ID (e.g. <literal>snap-1cbda474</literal>) or a
+ volume ID (e.g. <literal>vol-d04895b8</literal>). Leave
+ empty to create an EBS volume automatically.
+ '';
+ };
+
+ size = mkOption {
+ default = 0;
+ type = types.uniq types.int;
+ description = ''
+ Filesystem size (in gigabytes) for automatically created
+ EBS volumes.
+ '';
+ };
+
+ fsType = mkOption {
+ default = "ext4";
+ type = types.uniq types.string;
+ description = ''
+ Filesystem type for automatically created EBS volumes.
+ '';
+ };
+
+ deleteOnTermination = mkOption {
+ type = types.bool;
+ description = ''
+ For automatically created EBS volumes, determines whether the
+ volume should be deleted on instance termination.
+ '';
+ };
- size = mkOption {
- default = 0;
- type = types.uniq types.int;
- description = ''
- Filesystem size (in gigabytes) for automatically created
- EBS volumes.
- '';
};
- fsType = mkOption {
- default = "ext4";
- type = types.uniq types.string;
- description = ''
- Filesystem type for automatically created EBS volumes.
- '';
+ config = {
+ deleteOnTermination = mkDefault (config.disk == "");
};
};

0 comments on commit c70c624

Please sign in to comment.