file_copy: Make pathes relative to the manifest

To that effect, the file validation was moved
from validate_manifest to the validation phase.
This commit is contained in:
Nicolas Braud-Santoni 2016-09-12 00:02:24 +02:00
parent 7fd7ba5aa1
commit 9caf94ee42
No known key found for this signature in database
GPG key ID: 9D4F88010CFE19E3
3 changed files with 22 additions and 10 deletions

View file

@ -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)``.

View file

@ -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)

View file

@ -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)