From 88014671e8e75385e536b55276d2f9a0c45c1c95 Mon Sep 17 00:00:00 2001 From: Tiago Ilieve Date: Thu, 27 Mar 2014 23:44:38 -0300 Subject: [PATCH] New plugin: apt_proxy --- plugins/apt_proxy/__init__.py | 11 ++++++++++ plugins/apt_proxy/manifest-schema.json | 29 ++++++++++++++++++++++++++ plugins/apt_proxy/tasks.py | 28 +++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 plugins/apt_proxy/__init__.py create mode 100644 plugins/apt_proxy/manifest-schema.json create mode 100644 plugins/apt_proxy/tasks.py diff --git a/plugins/apt_proxy/__init__.py b/plugins/apt_proxy/__init__.py new file mode 100644 index 0000000..a5b086c --- /dev/null +++ b/plugins/apt_proxy/__init__.py @@ -0,0 +1,11 @@ +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): + import tasks + taskset.add(tasks.SetAptProxy) + if not manifest.plugins['apt_proxy'].get('persistent', False): + taskset.add(tasks.RemoveAptProxy) diff --git a/plugins/apt_proxy/manifest-schema.json b/plugins/apt_proxy/manifest-schema.json new file mode 100644 index 0000000..ae407eb --- /dev/null +++ b/plugins/apt_proxy/manifest-schema.json @@ -0,0 +1,29 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "APT proxy plugin manifest", + "type": "object", + "properties": { + "plugins": { + "type": "object", + "properties": { + "apt_proxy": { + "type": "object", + "properties": { + "address": { + "type": "string" + }, + "persistent": { + "type": "boolean" + }, + "port": { + "type": "integer" + } + }, + "required": ["address", "port"] + } + }, + "required": ["apt_proxy"] + } + }, + "required": ["plugins"] +} diff --git a/plugins/apt_proxy/tasks.py b/plugins/apt_proxy/tasks.py new file mode 100644 index 0000000..46b33e4 --- /dev/null +++ b/plugins/apt_proxy/tasks.py @@ -0,0 +1,28 @@ +from base import Task +from common import phases +from common.tasks import apt +import os + + +class SetAptProxy(Task): + description = 'Setting proxy for APT' + phase = phases.package_installation + successors = [apt.AptUpdate] + + @classmethod + def run(cls, info): + proxy_path = os.path.join(info.root, 'etc/apt/apt.conf.d/02proxy') + proxy_address = info.manifest.plugins['apt_proxy']['address'] + proxy_port = info.manifest.plugins['apt_proxy']['port'] + with open(proxy_path, 'w') as proxy_file: + proxy_file.write('Acquire::http {{ Proxy "http://{address}:{port}"; }};\n' + .format(address=proxy_address, port=proxy_port)) + + +class RemoveAptProxy(Task): + description = 'Removing APT proxy configuration file' + phase = phases.system_cleaning + + @classmethod + def run(cls, info): + os.remove(os.path.join(info.root, 'etc/apt/apt.conf.d/02proxy'))