bootstrap-vz/providers/ec2/tasks/ebs.py

79 lines
1.9 KiB
Python
Raw Normal View History

from base import Task
from common import phases
from common.exceptions import TaskError
from connection import Connect
from filesystem import UnmountVolume
import time
class CreateVolume(Task):
2013-06-23 22:12:29 +02:00
phase = phases.volume_creation
after = [Connect]
description = 'Creating an EBS volume for bootstrapping'
2013-06-26 20:14:37 +02:00
def run(self, info):
2013-06-24 19:06:12 +02:00
volume_size = int(info.manifest.volume['size']/1024)
info.volume = info.connection.create_volume(volume_size, info.host['availabilityZone'])
while info.volume.volume_state() != 'available':
time.sleep(5)
info.volume.update()
2013-06-26 20:14:37 +02:00
2013-06-24 19:06:12 +02:00
class AttachVolume(Task):
phase = phases.volume_creation
after = [CreateVolume]
description = 'Attaching the EBS volume'
2013-06-26 20:14:37 +02:00
2013-06-24 19:06:12 +02:00
def run(self, info):
def char_range(c1, c2):
"""Generates the characters from `c1` to `c2`, inclusive."""
2013-06-24 19:06:12 +02:00
for c in xrange(ord(c1), ord(c2)+1):
yield chr(c)
import os.path
info.bootstrap_device = {}
for letter in char_range('f', 'z'):
2013-06-24 19:06:12 +02:00
dev_path = os.path.join('/dev', 'xvd' + letter)
if not os.path.exists(dev_path):
info.bootstrap_device['path'] = dev_path
info.bootstrap_device['ec2_path'] = os.path.join('/dev', 'sd' + letter)
2013-06-24 19:06:12 +02:00
break
if 'path' not in info.bootstrap_device:
raise VolumeError('Unable to find a free block device path for mounting the bootstrap volume')
info.volume.attach(info.host['instanceId'], info.bootstrap_device['ec2_path'])
while info.volume.attachment_state() != 'attached':
time.sleep(2)
info.volume.update()
2013-06-26 20:14:37 +02:00
class DetachVolume(Task):
phase = phases.volume_unmounting
after = [UnmountVolume]
description = 'Detaching the EBS volume'
def run(self, info):
info.volume.detach()
while info.volume.attachment_state() is not None:
time.sleep(2)
info.volume.update()
2013-06-26 20:14:37 +02:00
class DeleteVolume(Task):
phase = phases.cleaning
after = []
description = 'Deleting the EBS volume'
def run(self, info):
info.volume.delete()
del info.volume
class VolumeError(TaskError):
2013-06-24 19:10:04 +02:00
pass