New plugin: ntp

This commit is contained in:
Tiago Ilieve 2014-03-28 17:58:45 -03:00
parent 4ca99a8bb2
commit afffdf680d
3 changed files with 69 additions and 0 deletions

11
plugins/ntp/__init__.py Normal file
View file

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

View file

@ -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"]
}

34
plugins/ntp/tasks.py Normal file
View file

@ -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 = 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,