From 29b52a6a705aef82077f84cb8a9c37551df6f292 Mon Sep 17 00:00:00 2001 From: Anders Ingemann Date: Mon, 24 Jun 2013 19:06:12 +0200 Subject: [PATCH] Create volume --- common/exceptions.py | 4 +++- manifests/ec2-ebs-pvm.manifest.json | 2 +- providers/ec2/tasks/ebs.py | 37 +++++++++++++++++++---------- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/common/exceptions.py b/common/exceptions.py index b85b3df..7f2cc9e 100644 --- a/common/exceptions.py +++ b/common/exceptions.py @@ -12,9 +12,11 @@ class ManifestError(Exception): return "{2}\n\tFile: {0}\n\tJSON path: {1}".format(self.manifest.path, path, self.message) return "{0}: {1}".format(self.manifest.path, self.message) - class TaskListError(Exception): def __init__(self, message): self.message = message def __str__(self): return "Error in tasklist: {0}".format(self.message) + +class VolumeError(Exception): + pass diff --git a/manifests/ec2-ebs-pvm.manifest.json b/manifests/ec2-ebs-pvm.manifest.json index 2686187..9bc54f9 100644 --- a/manifests/ec2-ebs-pvm.manifest.json +++ b/manifests/ec2-ebs-pvm.manifest.json @@ -22,7 +22,7 @@ "volume": { "backing" : "ebs", "filesystem": "ext4", - "size" : 1 + "size" : 1024 }, "plugins": { "admin_user": { diff --git a/providers/ec2/tasks/ebs.py b/providers/ec2/tasks/ebs.py index 543c3a1..7a859ee 100644 --- a/providers/ec2/tasks/ebs.py +++ b/providers/ec2/tasks/ebs.py @@ -8,17 +8,30 @@ class CreateVolume(Task): after = [Connect] def run(self, info): - # info.conn.create_volume(50, "us-west-2") -# volume_id=`euca-create-volume --size $volume_size --zone "$availability_zone" | awk '{print $2}'` -# [ -z "$volume_id" ] && die "Unable to create volume." -# log "The EBS volume id is $volume_id" + volume_size = int(info.manifest.volume['size']/1024) + info.volume = info.conn.create_volume(volume_size, info.host['availabilityZone']) -# for package in info.host_packages: -# try: -# with open(devnull, 'w') as dev_null: -# subprocess.check_call(['/usr/bin/dpkg', '-s', package], stdout=dev_null, stderr=dev_null) -# except subprocess.CalledProcessError: -# msg = "The package ``{0}\'\' is not installed".format(package) -# raise RuntimeError(msg) - pass +class AttachVolume(Task): + description = 'Attaching the EBS volume' + phase = phases.volume_creation + after = [CreateVolume] + + def run(self, info): + def char_range(c1, c2): + """Generates the characters from `c1` to `c2`, inclusive.""" + for c in xrange(ord(c1), ord(c2)+1): + yield chr(c) + + import os.path + import os.stat + from stat import S_ISBLK + for letter in char_range('a', 'z'): + dev_path = os.path.join('/dev', 'xvd' + letter) + mode = os.stat(dev_path).st_mode + if S_ISBLK(mode): + info.bootstrap_device = {'path': dev_path} + break + if 'path' not in info.bootstrap_device: + raise VolumeError('Unable to find a free block device path for mounting the bootstrap volume') + info.conn.volume.attach(info.host['instanceId'], info.bootstrap_device['path'])