From b56bf53573894a0ec699bba42aa57a86e2611e18 Mon Sep 17 00:00:00 2001 From: Jay Bonci Date: Tue, 1 Apr 2014 00:23:24 +0000 Subject: [PATCH 1/2] Simple chef plugin based on the puppet plugin --- plugins/chef/__init__.py | 14 ++++++++ plugins/chef/manifest-schema.json | 26 +++++++++++++++ plugins/chef/tasks.py | 55 +++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 plugins/chef/__init__.py create mode 100644 plugins/chef/manifest-schema.json create mode 100644 plugins/chef/tasks.py diff --git a/plugins/chef/__init__.py b/plugins/chef/__init__.py new file mode 100644 index 0000000..7ba2396 --- /dev/null +++ b/plugins/chef/__init__.py @@ -0,0 +1,14 @@ +import tasks + + +def validate_manifest(data, validator, error): + import os.path + schema_path = os.path.normpath(os.path.join(os.path.dirname(__file__), 'manifest-schema.json')) + validator(data, schema_path) + + +def resolve_tasks(taskset, manifest): + taskset.add(tasks.AddPackages) + if 'assets' in manifest.plugins['chef']: + taskset.add(tasks.CheckAssetsPath) + taskset.add(tasks.CopyChefAssets) diff --git a/plugins/chef/manifest-schema.json b/plugins/chef/manifest-schema.json new file mode 100644 index 0000000..9bc9d47 --- /dev/null +++ b/plugins/chef/manifest-schema.json @@ -0,0 +1,26 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Puppet plugin manifest", + "type": "object", + "properties": { + "plugins": { + "type": "object", + "properties": { + "chef": { + "type": "object", + "properties": { + "assets": { "$ref": "#/definitions/absolute_path" } + }, + "minProperties": 1, + "additionalProperties": false + } + } + } + }, + "definitions": { + "absolute_path": { + "type": "string", + "pattern": "^/[^\\0]+$" + } + } +} diff --git a/plugins/chef/tasks.py b/plugins/chef/tasks.py new file mode 100644 index 0000000..198bc7f --- /dev/null +++ b/plugins/chef/tasks.py @@ -0,0 +1,55 @@ +from base import Task +from common import phases +from common.tasks import apt +import os + + +class CheckAssetsPath(Task): + description = 'Checking whether the assets path exist' + phase = phases.preparation + + @classmethod + def run(cls, info): + from common.exceptions import TaskError + assets = info.manifest.plugins['chef']['assets'] + if not os.path.exists(assets): + msg = 'The assets directory {assets} does not exist.'.format(assets=assets) + raise TaskError(msg) + if not os.path.isdir(assets): + msg = 'The assets path {assets} does not point to a directory.'.format(assets=assets) + raise TaskError(msg) + + +class AddPackages(Task): + description = 'Add chef package' + phase = phases.preparation + predecessors = [apt.AddDefaultSources] + + @classmethod + def run(cls, info): + info.packages.add('chef') + + +class CopyChefAssets(Task): + description = 'Copying chef assets' + phase = phases.system_modification + + @classmethod + def run(cls, info): + from shutil import copy + chef_path = os.path.join(info.root, 'etc/chef') + chef_assets = info.manifest.plugins['chef']['assets'] + for abs_prefix, dirs, files in os.walk(chef_assets): + prefix = os.path.normpath(os.path.relpath(abs_prefix, chef_assets)) + for path in dirs: + full_path = os.path.join(chef_path, prefix, path) + if os.path.exists(full_path): + if os.path.isdir(full_path): + continue + else: + os.remove(full_path) + os.mkdir(full_path) + for path in files: + copy(os.path.join(abs_prefix, path), + os.path.join(chef_path, prefix, path)) + From c8b916b5a25938fc86b2e4d6815619ac19f51975 Mon Sep 17 00:00:00 2001 From: Jay Bonci Date: Tue, 1 Apr 2014 01:19:40 +0000 Subject: [PATCH 2/2] Abstracted some of the copy_tree stuff out --- common/tools.py | 20 ++++++++++++++++++-- plugins/chef/tasks.py | 19 ++----------------- plugins/puppet/tasks.py | 18 ++---------------- 3 files changed, 22 insertions(+), 35 deletions(-) diff --git a/common/tools.py b/common/tools.py index 6b39db5..8721144 100644 --- a/common/tools.py +++ b/common/tools.py @@ -1,5 +1,3 @@ - - def log_check_call(command, stdin=None, env=None, shell=False): status, stdout, stderr = log_call(command, stdin, env, shell) if status != 0: @@ -71,3 +69,21 @@ def config_get(path, config_path): for key in config_path: config = config.get(key) return config + +def copy_tree(from_path,to_path): + from shutil import copy + import os + for abs_prefix, dirs, files in os.walk(from_path): + prefix = os.path.normpath(os.path.relpath(abs_prefix, from_path)) + for path in dirs: + full_path = os.path.join(to_path, prefix, path) + if os.path.exists(full_path): + if os.path.isdir(full_path): + continue + else: + os.remove(full_path) + os.mkdir(full_path) + for path in files: + copy(os.path.join(abs_prefix, path), + os.path.join(to_path, prefix, path)) + diff --git a/plugins/chef/tasks.py b/plugins/chef/tasks.py index 198bc7f..15fde45 100644 --- a/plugins/chef/tasks.py +++ b/plugins/chef/tasks.py @@ -36,20 +36,5 @@ class CopyChefAssets(Task): @classmethod def run(cls, info): - from shutil import copy - chef_path = os.path.join(info.root, 'etc/chef') - chef_assets = info.manifest.plugins['chef']['assets'] - for abs_prefix, dirs, files in os.walk(chef_assets): - prefix = os.path.normpath(os.path.relpath(abs_prefix, chef_assets)) - for path in dirs: - full_path = os.path.join(chef_path, prefix, path) - if os.path.exists(full_path): - if os.path.isdir(full_path): - continue - else: - os.remove(full_path) - os.mkdir(full_path) - for path in files: - copy(os.path.join(abs_prefix, path), - os.path.join(chef_path, prefix, path)) - + from common.tools import copy_tree + copy_tree(info.manifest.plugins['chef']['assets'], os.path.join(info.root, 'etc/chef')) diff --git a/plugins/puppet/tasks.py b/plugins/puppet/tasks.py index e2f0e22..abc90cb 100644 --- a/plugins/puppet/tasks.py +++ b/plugins/puppet/tasks.py @@ -53,22 +53,8 @@ class CopyPuppetAssets(Task): @classmethod def run(cls, info): - from shutil import copy - puppet_path = os.path.join(info.root, 'etc/puppet') - puppet_assets = info.manifest.plugins['puppet']['assets'] - for abs_prefix, dirs, files in os.walk(puppet_assets): - prefix = os.path.normpath(os.path.relpath(abs_prefix, puppet_assets)) - for path in dirs: - full_path = os.path.join(puppet_path, prefix, path) - if os.path.exists(full_path): - if os.path.isdir(full_path): - continue - else: - os.remove(full_path) - os.mkdir(full_path) - for path in files: - copy(os.path.join(abs_prefix, path), - os.path.join(puppet_path, prefix, path)) + from common.tools import copy_tree + copy_tree(info.manifest.plugins['puppet']['assets'], os.path.join(info.root, 'etc/puppet')) class ApplyPuppetManifest(Task):