diff --git a/providers/ec2/__init__.py b/providers/ec2/__init__.py index 3ad533e..d50cf9c 100644 --- a/providers/ec2/__init__.py +++ b/providers/ec2/__init__.py @@ -25,8 +25,13 @@ def tasks(tasklist, manifest): def rollback_tasks(tasklist, tasks_completed, manifest): completed = [type(task) for task in tasks_completed] + + def counter_task(task, counter): + if task in completed and counter not in completed: + tasklist.add(counter()) + if manifest.volume['backing'].lower() == 'ebs': - if ebs.CreateVolume in completed and ebs.DeleteVolume not in completed: - tasklist.add(ebs.DeleteVolume()) - if ebs.AttachVolume in completed and ebs.DetachVolume not in completed: - tasklist.add(ebs.DetachVolume()) + counter_task(ebs.CreateVolume, ebs.DeleteVolume) + counter_task(ebs.AttachVolume, ebs.DetachVolume) + counter_task(filesystem.CreateMountDir, filesystem.DeleteMountDir) + counter_task(filesystem.MountVolume, filesystem.UnmountVolume) diff --git a/providers/ec2/tasks/ebs.py b/providers/ec2/tasks/ebs.py index 1c0758b..4f30094 100644 --- a/providers/ec2/tasks/ebs.py +++ b/providers/ec2/tasks/ebs.py @@ -2,6 +2,7 @@ from base import Task from common import phases from common.exceptions import TaskError from connection import Connect +from filesystem import UnmountVolume import time @@ -57,7 +58,7 @@ class AttachVolume(Task): class DetachVolume(Task): phase = phases.volume_unmounting - after = [] + after = [UnmountVolume] description = 'Detaching the EBS volume' diff --git a/providers/ec2/tasks/filesystem.py b/providers/ec2/tasks/filesystem.py index 7654911..b0e79ee 100644 --- a/providers/ec2/tasks/filesystem.py +++ b/providers/ec2/tasks/filesystem.py @@ -51,7 +51,7 @@ class CreateMountDir(Task): class MountVolume(Task): - description = 'Creating mountpoint for the bootstrap volume' + description = 'Mounting the bootstrap volume' phase = phases.volume_mounting after = [CreateMountDir] @@ -67,3 +67,25 @@ class MountVolume(Task): dev_path = info.bootstrap_device['path'] with open(devnull, 'w') as dev_null: subprocess.check_call(['mount', dev_path, info.root], stdout=dev_null, stderr=dev_null) + + +class UnmountVolume(Task): + description = 'Unmounting the bootstrap volume' + phase = phases.volume_unmounting + + def run(self, info): + import subprocess + from os import devnull + with open(devnull, 'w') as dev_null: + subprocess.check_call(['umount', info.root], stdout=dev_null, stderr=dev_null) + + +class DeleteMountDir(Task): + description = 'Deleting mountpoint for the bootstrap volume' + phase = phases.volume_unmounting + after = [UnmountVolume] + + def run(self, info): + import os + os.rmdir(info.root) + del info.root