diff --git a/plugins/ntp/__init__.py b/plugins/ntp/__init__.py new file mode 100644 index 0000000..9a68d0f --- /dev/null +++ b/plugins/ntp/__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.AddNtpPackage) + if manifest.plugins['ntp'].get('servers', False): + taskset.add(tasks.SetNtpServers) diff --git a/plugins/ntp/manifest-schema.json b/plugins/ntp/manifest-schema.json new file mode 100644 index 0000000..d15be21 --- /dev/null +++ b/plugins/ntp/manifest-schema.json @@ -0,0 +1,24 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "NTP plugin manifest", + "type": "object", + "properties": { + "plugins": { + "type": "object", + "properties": { + "ntp": { + "type": "object", + "properties": { + "servers": { + "type": "array", + "items": {"type": "string"}, + "minItems": 1 + } + } + } + }, + "required": ["ntp"] + } + }, + "required": ["plugins"] +} diff --git a/plugins/ntp/tasks.py b/plugins/ntp/tasks.py new file mode 100644 index 0000000..e143343 --- /dev/null +++ b/plugins/ntp/tasks.py @@ -0,0 +1,34 @@ +from base import Task +from common import phases +from common.tasks import packages + + +class AddNtpPackage(Task): + description = 'Adding NTP Package' + phase = phases.package_installation + successors = [packages.InstallPackages] + + @classmethod + def run(cls, info): + info.packages.add('ntp') + + +class SetNtpServers(Task): + description = 'Setting NTP servers' + phase = phases.system_modification + + @classmethod + def run(cls, info): + import fileinput + import os + import re + ntp_path = os.path.join(info.root, 'etc/ntp.conf') + servers = list(info.manifest.plugins['ntp']['servers']) + debian_ntp_server = re.compile('.*[0-9]\.debian\.pool\.ntp\.org.*') + for line in fileinput.input(files=ntp_path, inplace=True): + # Will write all the specified servers on the first match, then supress all other default servers + if re.match(debian_ntp_server, line): + while servers: + print 'server {server_address} iburst'.format(server_address=servers.pop(0)) + else: + print line,