summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Server/Plugins/Cfg/CfgExternalCommandVerifier.py
blob: f0c1109ec7a7880247d1ca2e025a5f282c290104 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import os
import shlex
import logging
import Bcfg2.Server.Plugin
from subprocess import Popen, PIPE
from Bcfg2.Server.Plugins.Cfg import CfgVerifier, CfgVerificationError

logger = logging.getLogger(__name__)

class CfgExternalCommandVerifier(CfgVerifier):
    __basenames__ = [':test']

    def verify_entry(self, entry, metadata, data):
        proc = Popen(self.cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
        err = proc.communicate(input=data)[1]
        rv = proc.wait()
        if rv != 0:
            raise CfgVerificationError(err)

    def handle_event(self, event):
        if event.code2str() == 'deleted':
            return
        self.cmd = []
        if not os.access(self.name, os.X_OK):
            bangpath = open(self.name).readline().strip()
            if bangpath.startswith("#!"):
                self.cmd.extend(shlex.split(bangpath[2:].strip()))
            else:
                msg = "Cannot execute %s" % self.name
                logger.error(msg)
                raise Bcfg2.Server.Plugin.PluginExecutionError(msg)
        self.cmd.append(self.name)