From c924d500fe36ef71a1104868c17df81091e4557f Mon Sep 17 00:00:00 2001 From: Olivier Sallou Date: Wed, 4 Sep 2013 14:46:54 +0200 Subject: [PATCH] add new plugin image_commands to execute cmd or scripts in image --- plugins/image_commands/README.md | 25 ++++++++++++++++++ plugins/image_commands/__init__.py | 11 ++++++++ plugins/image_commands/image_commands.py | 26 ++++++++++++++++++ plugins/image_commands/manifest-schema.json | 29 +++++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 plugins/image_commands/README.md create mode 100644 plugins/image_commands/__init__.py create mode 100644 plugins/image_commands/image_commands.py create mode 100644 plugins/image_commands/manifest-schema.json diff --git a/plugins/image_commands/README.md b/plugins/image_commands/README.md new file mode 100644 index 0000000..7efc592 --- /dev/null +++ b/plugins/image_commands/README.md @@ -0,0 +1,25 @@ +# Image script plugin + +This plugin gives the possibility to the user to execute commands. + +Plugin is defined in the manifest file, plugin section with: + + "image_commands": { + "enabled": true, + "commands": [ [ "touch", "/var/www/index.html" ]], + } + +The *commands* element is an array of commands. Each command is an array describing the executable and its arguments. + +Command is executed in current context. It is possible to use variables to access the image or execute chroot commands in the image. + +Available variables are: + {root} : image mount point (to copy files for example or chroot commands) + {filesystem} : used filesystem + {image} : image file path + +Example: + + [[ "touch", "{root}/var/www/hello" ], + [ "/usr/sbin/chroot", "{root}", "touch", "/var/www/hello.{filesystem}"]] + diff --git a/plugins/image_commands/__init__.py b/plugins/image_commands/__init__.py new file mode 100644 index 0000000..5353b48 --- /dev/null +++ b/plugins/image_commands/__init__.py @@ -0,0 +1,11 @@ + + +def tasks(tasklist, manifest): + from image_commands import ImageExecuteCommand + tasklist.add(ImageExecuteCommand()) + +def validate_manifest(data, schema_validate): + from os import path + schema_path = path.normpath(path.join(path.dirname(__file__), 'manifest-schema.json')) + schema_validate(data, schema_path) + diff --git a/plugins/image_commands/image_commands.py b/plugins/image_commands/image_commands.py new file mode 100644 index 0000000..5da4511 --- /dev/null +++ b/plugins/image_commands/image_commands.py @@ -0,0 +1,26 @@ +from base import Task +from common import phases +import os +from common.tasks.packages import ImagePackages +from common.tasks.host import CheckPackages +from common.tasks.filesystem import MountVolume + + +class ImageExecuteCommand(Task): + description = 'Execute command in the image' + phase = phases.system_modification + + def run(self, info): + from common.tools import log_check_call + + for user_cmd in info.manifest.plugins['image_commands']['commands']: + command = [] + for elt in user_cmd: + fragment = elt.format( + root=info.root, + image=info.loopback_file, + filesystem=info.manifest.volume['filesystem']) + command.append(fragment) + log_check_call(command) + + diff --git a/plugins/image_commands/manifest-schema.json b/plugins/image_commands/manifest-schema.json new file mode 100644 index 0000000..f57b2e8 --- /dev/null +++ b/plugins/image_commands/manifest-schema.json @@ -0,0 +1,29 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Image commands plugin manifest", + "type": "object", + "properties": { + "plugins": { + "type": "object", + "properties": { + "image_commands": { + "type": "object", + "properties": { + "commands": { + "type": "array", + "items": { + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "required": ["commands"] + } + }, + "required": ["image_commands"] + } + }, + "required": ["plugins"] +}