2013-06-30 23:26:45 +02:00
|
|
|
from base import Task
|
|
|
|
from common import phases
|
2013-06-30 23:56:09 +02:00
|
|
|
from providers.ec2.tasks import ebs
|
2013-09-15 13:19:45 +02:00
|
|
|
from common.tasks import volume
|
2013-08-11 19:42:48 +02:00
|
|
|
from common.tasks import bootstrap
|
2013-06-30 23:26:45 +02:00
|
|
|
import time
|
2013-07-09 20:45:57 +02:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
2013-06-30 23:26:45 +02:00
|
|
|
|
|
|
|
|
2013-07-14 23:16:37 +02:00
|
|
|
class Snapshot(ebs.Snapshot):
|
|
|
|
description = 'Creating a snapshot of the bootstrapped volume'
|
|
|
|
phase = phases.os_installation
|
|
|
|
after = [bootstrap.Bootstrap]
|
|
|
|
|
|
|
|
def run(self, info):
|
|
|
|
super(Snapshot, self).run(info)
|
|
|
|
msg = 'A snapshot of the bootstrapped volume was created. ID: {id}'.format(id=info.snapshot.id)
|
|
|
|
log.info(msg)
|
|
|
|
|
|
|
|
|
2013-07-13 13:55:12 +02:00
|
|
|
class CreateFromSnapshot(Task):
|
2013-07-09 20:32:50 +02:00
|
|
|
description = 'Creating EBS volume from a snapshot'
|
2013-06-30 23:26:45 +02:00
|
|
|
phase = phases.volume_creation
|
2013-09-15 13:19:45 +02:00
|
|
|
before = [volume.Attach]
|
2013-06-30 23:26:45 +02:00
|
|
|
|
|
|
|
def run(self, info):
|
2013-08-17 17:28:46 +02:00
|
|
|
volume_size = int(info.manifest.volume['size'] / 1024)
|
2013-06-30 23:56:09 +02:00
|
|
|
snapshot = info.manifest.plugins['prebootstrapped']['snapshot']
|
2013-09-15 13:19:45 +02:00
|
|
|
info.volume.volume = info.connection.create_volume(volume_size,
|
|
|
|
info.host['availabilityZone'],
|
|
|
|
snapshot=snapshot)
|
2013-06-30 23:26:45 +02:00
|
|
|
while info.volume.volume_state() != 'available':
|
|
|
|
time.sleep(5)
|
|
|
|
info.volume.update()
|
2013-07-09 20:45:57 +02:00
|
|
|
|
2013-09-15 13:19:45 +02:00
|
|
|
info.volume.force_state('detached_fmt')
|
|
|
|
partitions_state = 'formatted'
|
|
|
|
if 'partitions' in info.manifest.volume:
|
|
|
|
partitions_state = 'unmapped_fmt'
|
|
|
|
for partition in info.volume.partition_map.partitions:
|
|
|
|
partition.force_state(partitions_state)
|
|
|
|
|
2013-07-09 20:45:57 +02:00
|
|
|
|
2013-07-14 23:16:37 +02:00
|
|
|
class CopyImage(Task):
|
2013-07-09 20:45:57 +02:00
|
|
|
description = 'Creating a snapshot of the bootstrapped volume'
|
|
|
|
phase = phases.os_installation
|
|
|
|
after = [bootstrap.Bootstrap]
|
|
|
|
|
|
|
|
def run(self, info):
|
2013-07-14 23:16:37 +02:00
|
|
|
import os.path
|
|
|
|
from shutil import copyfile
|
|
|
|
loopback_backup_name = 'loopback-{id:x}.img.backup'.format(id=info.run_id)
|
2013-09-15 16:59:56 +02:00
|
|
|
destination = os.path.join(info.manifest.bootstrapper['workspace'], loopback_backup_name)
|
|
|
|
copyfile(info.volume.image_path, destination)
|
|
|
|
msg = 'A copy of the bootstrapped volume was created. Path: {path}'.format(path=destination)
|
2013-07-09 20:45:57 +02:00
|
|
|
log.info(msg)
|
2013-07-14 23:16:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CreateFromImage(Task):
|
|
|
|
description = 'Creating loopback image from a copy'
|
|
|
|
phase = phases.volume_creation
|
2013-09-15 13:19:45 +02:00
|
|
|
before = [volume.Attach]
|
2013-07-14 23:16:37 +02:00
|
|
|
|
|
|
|
def run(self, info):
|
|
|
|
import os.path
|
|
|
|
from shutil import copyfile
|
2013-09-15 16:59:56 +02:00
|
|
|
info.volume.image_path = os.path.join(info.workspace, 'volume.{ext}'.format(ext=info.volume.extension))
|
2013-09-15 13:19:45 +02:00
|
|
|
loopback_backup_path = info.manifest.plugins['prebootstrapped']['image']
|
|
|
|
copyfile(loopback_backup_path, info.volume.image_path)
|
|
|
|
|
|
|
|
info.volume.force_state('detached_fmt')
|
|
|
|
partitions_state = 'formatted'
|
|
|
|
if 'partitions' in info.manifest.volume:
|
|
|
|
partitions_state = 'unmapped_fmt'
|
|
|
|
for partition in info.volume.partition_map.partitions:
|
|
|
|
partition.force_state(partitions_state)
|