From 4d86ae5bea57b08dac54162a0d6e2c943088366a Mon Sep 17 00:00:00 2001 From: Anders Ingemann Date: Thu, 27 Jun 2013 22:00:01 +0200 Subject: [PATCH] Implement MountVolume task, catch less exceptions --- base/manifest.py | 9 +++--- providers/ec2/__init__.py | 1 + providers/ec2/tasks/filesystem.py | 47 +++++++++++++++++++++++-------- providers/ec2/tasks/host.py | 3 +- 4 files changed, 43 insertions(+), 17 deletions(-) diff --git a/base/manifest.py b/base/manifest.py index 836bd00..aa77c90 100644 --- a/base/manifest.py +++ b/base/manifest.py @@ -40,10 +40,11 @@ class Manifest(object): raise ManifestError(e.message, self, e.path) def parse(self, data): - self.provider = data['provider'] - self.volume = data['volume'] - self.system = data['system'] - self.plugins = data['plugins'] + self.provider = data['provider'] + self.bootstrapdir = data['bootstrapdir'] + self.volume = data['volume'] + self.system = data['system'] + self.plugins = data['plugins'] def load_plugins(self): self.loaded_plugins = [] diff --git a/providers/ec2/__init__.py b/providers/ec2/__init__.py index 6f14570..3ad533e 100644 --- a/providers/ec2/__init__.py +++ b/providers/ec2/__init__.py @@ -17,6 +17,7 @@ def tasks(tasklist, manifest): import re if re.search('ext.', manifest.volume['filesystem'].lower()): tasklist.add(filesystem.TuneVolumeFS()) + tasklist.add(filesystem.CreateMountDir(), filesystem.MountVolume()) from common.tasks import TriggerRollback tasklist.add(TriggerRollback()) diff --git a/providers/ec2/tasks/filesystem.py b/providers/ec2/tasks/filesystem.py index f65d73d..7654911 100644 --- a/providers/ec2/tasks/filesystem.py +++ b/providers/ec2/tasks/filesystem.py @@ -6,18 +6,14 @@ from common.exceptions import TaskError class FormatVolume(Task): description = 'Formatting the volume' phase = phases.volume_preparation - after = [] def run(self, info): import subprocess from os import devnull dev_path = info.bootstrap_device['path'] mkfs = '/sbin/mkfs.{fs}'.format(fs=info.manifest.volume['filesystem']) - try: - with open(devnull, 'w') as dev_null: - subprocess.check_call([mkfs, dev_path], stdout=dev_null, stderr=dev_null) - except subprocess.CalledProcessError: - raise TaskError('Unable to format the bootstrap device') + with open(devnull, 'w') as dev_null: + subprocess.check_call([mkfs, dev_path], stdout=dev_null, stderr=dev_null) class TuneVolumeFS(Task): @@ -29,18 +25,45 @@ class TuneVolumeFS(Task): import subprocess from os import devnull dev_path = info.bootstrap_device['path'] - try: - with open(devnull, 'w') as dev_null: - subprocess.check_call(['/sbin/tune2fs', '-i', '0', dev_path], stdout=dev_null, stderr=dev_null) - except subprocess.CalledProcessError: - raise TaskError('Unable to disable the time based check interval for the bootstrap volume') + # Disable the time based filesystem check + with open(devnull, 'w') as dev_null: + subprocess.check_call(['/sbin/tune2fs', '-i', '0', dev_path], stdout=dev_null, stderr=dev_null) class AddXFSProgs(Task): description = 'Adding `xfsprogs\' to the image packages' phase = phases.preparation - after = [] def run(self, info): include, exclude = info.img_packages include.add('xfsprogs') + + +class CreateMountDir(Task): + description = 'Creating mountpoint for the bootstrap volume' + phase = phases.volume_mounting + + def run(self, info): + import os + info.root = '{bs_dir}/{vol_id}'.format(bs_dir=info.manifest.bootstrapdir, vol_id=info.volume.id) + # Works recursively, fails if last part exists, which is exaclty what we want. + os.makedirs(info.root) + + +class MountVolume(Task): + description = 'Creating mountpoint for the bootstrap volume' + phase = phases.volume_mounting + after = [CreateMountDir] + + def run(self, info): + with open('/proc/mounts') as mounts: + for mount in mounts: + if info.root in mount: + msg = 'Something is already mount at {root}'.format(root=info.root) + raise TaskError(msg) + + import subprocess + from os import devnull + 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) diff --git a/providers/ec2/tasks/host.py b/providers/ec2/tasks/host.py index 23508fb..84fadd0 100644 --- a/providers/ec2/tasks/host.py +++ b/providers/ec2/tasks/host.py @@ -1,5 +1,6 @@ from base import Task from common import phases +from common.exceptions import TaskError import packages @@ -17,7 +18,7 @@ class CheckPackages(Task): 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) + raise TaskError(msg) class GetInfo(Task):