From 9caf94ee42073b45bcc0b4eb6e37b947d42962f9 Mon Sep 17 00:00:00 2001 From: Nicolas Braud-Santoni Date: Mon, 12 Sep 2016 00:02:24 +0200 Subject: [PATCH] file_copy: Make pathes relative to the manifest To that effect, the file validation was moved from validate_manifest to the validation phase. --- bootstrapvz/plugins/file_copy/README.rst | 2 +- bootstrapvz/plugins/file_copy/__init__.py | 7 +------ bootstrapvz/plugins/file_copy/tasks.py | 23 ++++++++++++++++++++--- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/bootstrapvz/plugins/file_copy/README.rst b/bootstrapvz/plugins/file_copy/README.rst index 8daefa5..166984e 100644 --- a/bootstrapvz/plugins/file_copy/README.rst +++ b/bootstrapvz/plugins/file_copy/README.rst @@ -17,7 +17,7 @@ The ``file_copy`` plugin takes a (non-empty) ``files`` list, and optionally a `` Files (items in the ``files`` list) must be objects with the following properties: - ``src`` and ``dst`` (required) are the source and destination paths. - ``src`` is relative to the current directory, whereas ``dst`` is a path in the VM. + ``src`` is relative to the manifest, whereas ``dst`` is a path in the VM. - ``permissions`` (optional) is a permission string in a format appropriate for ``chmod(1)``. - ``owner`` and ``group`` (optional) are respectively a user and group specification, in a format appropriate for ``chown(1)`` and ``chgrp(1)``. diff --git a/bootstrapvz/plugins/file_copy/__init__.py b/bootstrapvz/plugins/file_copy/__init__.py index 6bdbc60..30fa6d1 100644 --- a/bootstrapvz/plugins/file_copy/__init__.py +++ b/bootstrapvz/plugins/file_copy/__init__.py @@ -7,15 +7,10 @@ def validate_manifest(data, validator, error): schema_path = os.path.normpath(os.path.join(os.path.dirname(__file__), 'manifest-schema.yml')) validator(data, schema_path) - for i, file_entry in enumerate(data['plugins']['file_copy']['files']): - srcfile = file_entry['src'] - if not os.path.exists(srcfile): - msg = 'The source file %s does not exist.' % srcfile - error(msg, ['plugins', 'file_copy', 'files', i]) - def resolve_tasks(taskset, manifest): if ('mkdirs' in manifest.plugins['file_copy']): taskset.add(tasks.MkdirCommand) if ('files' in manifest.plugins['file_copy']): + taskset.add(tasks.ValidateFiles) taskset.add(tasks.FileCopyCommand) diff --git a/bootstrapvz/plugins/file_copy/tasks.py b/bootstrapvz/plugins/file_copy/tasks.py index 179cffd..484bdab 100644 --- a/bootstrapvz/plugins/file_copy/tasks.py +++ b/bootstrapvz/plugins/file_copy/tasks.py @@ -36,6 +36,20 @@ class MkdirCommand(Task): modify_path(info, dir_entry['dir'], dir_entry) +class ValidateFiles(Task): + description = 'Check that the required files exist' + phase = phases.validation + + @classmethod + def run(cls, info): + from bootstrapvz.common.tools import rel_path + + for i, file_entry in enumerate(info.manifest.plugins['file_copy']['files']): + if not os.path.exists(rel_path(info.manifest.path, file_entry['src'])): + msg = 'The source file %s does not exist.' % file_entry['src'] + info.manifest.validation_error(msg, ['plugins', 'file_copy', 'files', i]) + + class FileCopyCommand(Task): description = 'Copying user specified files into the image' phase = phases.user_modification @@ -43,13 +57,16 @@ class FileCopyCommand(Task): @classmethod def run(cls, info): + from bootstrapvz.common.tools import rel_path + for file_entry in info.manifest.plugins['file_copy']['files']: # note that we don't use os.path.join because it can't # handle absolute paths, which 'dst' most likely is. final_destination = os.path.normpath("%s/%s" % (info.root, file_entry['dst'])) - if os.path.isfile(file_entry['src']): - shutil.copy(file_entry['src'], final_destination) + src_path = rel_path(info.manifest.path, file_entry['src']) + if os.path.isfile(src_path): + shutil.copy(src_path, final_destination) else: - shutil.copytree(file_entry['src'], final_destination) + shutil.copytree(src_path, final_destination) modify_path(info, file_entry['dst'], file_entry)