summaryrefslogtreecommitdiffstats
path: root/tools/upgrade/1.4/migrate_decisions.py
blob: d0915f202e3a90f9b975c732b5904889b4942a43 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python

import os
import re
import sys
import glob
import lxml.etree
import Bcfg2.Options


SPECIFIC = re.compile(r'.*\/(white|black)list'
                      r'(\.(H_(?P<host>.*)|G\d+_(?P<group>.*)))?$')


def convert(files, xdata):
    hosts = []
    groups = []
    for oldfile in files:
        spec = SPECIFIC.match(oldfile)
        if spec and spec.group('host'):
            hosts.append(spec.group('host'))
        elif spec and spec.group('group'):
            groups.append(spec.group('group'))

    for oldfile in files:
        print("Converting %s" % oldfile)
        spec = SPECIFIC.match(oldfile)
        if not spec:
            print("Skipping unknown file %s" % oldfile)
            continue

        parent = xdata
        if spec.group('host'):
            for host in hosts:
                if host != spec.group('host'):
                    parent = lxml.etree.SubElement(parent, "Client",
                                                   name=host, negate="true")
            parent = lxml.etree.SubElement(parent, "Client",
                                           name=spec.group('host'))
        elif spec.group('group'):
            for host in hosts:
                parent = lxml.etree.SubElement(parent, "Client",
                                               name=host, negate="true")
            for group in groups:
                if group != spec.group('group'):
                    parent = lxml.etree.SubElement(parent, "Group",
                                                   name=group, negate="true")
            parent = lxml.etree.SubElement(parent, "Group",
                                           name=spec.group('group'))
        parent.append(lxml.etree.Comment("Converted from %s" % oldfile))
        olddata = lxml.etree.parse(oldfile, parser=Bcfg2.Server.XMLParser)
        for decision in olddata.xpath('//Decision'):
            parent.append(decision)
    return xdata


def main():
    parser = Bcfg2.Options.get_parser(
        description="Migrate from Bcfg2 1.3 Decisions list format to 1.4 "
        "format")
    parser.add_options([Bcfg2.Options.Common.repository])
    parser.parse()

    datadir = os.path.join(Bcfg2.Options.setup.repository, 'Decisions')
    whitelist = lxml.etree.Element("Decisions")
    blacklist = lxml.etree.Element("Decisions")
    if os.path.exists(datadir):
        convert(glob.glob(os.path.join(datadir, 'whitelist*')),
                whitelist)
        convert(glob.glob(os.path.join(datadir, 'blacklist*')),
                blacklist)

    print("Writing %s" % os.path.join(datadir, "whitelist.xml"))
    open(os.path.join(datadir, "whitelist.xml"),
         'w').write(lxml.etree.tostring(whitelist, pretty_print=True))
    print("Writing %s" % os.path.join(datadir, "blacklist.xml"))
    open(os.path.join(datadir, "blacklist.xml"),
         'w').write(lxml.etree.tostring(blacklist, pretty_print=True))


if __name__ == '__main__':
    sys.exit(main())