summaryrefslogtreecommitdiffstats
path: root/tools/upgrade/1.4/migrate_sslca.py
blob: 958228c86ec64e4e07e13be7dd3c109d4fb4d701 (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
#!/usr/bin/env python

import os
import sys
import shutil
import Bcfg2.Options


def main():
    parser = Bcfg2.Options.get_parser(
        description="Migrate from the SSLCA plugin to built-in Cfg SSL cert "
        "generation")
    parser.add_options([Bcfg2.Options.Common.repository])
    parser.parse()

    sslcadir = os.path.join(Bcfg2.Options.setup.repository, 'SSLCA')
    cfgdir = os.path.join(Bcfg2.Options.setup.repository, 'Cfg')
    for root, _, files in os.walk(sslcadir):
        if not files:
            continue
        newpath = cfgdir + root[len(sslcadir):]
        if not os.path.exists(newpath):
            print("Creating %s and copying contents from %s" % (newpath, root))
            shutil.copytree(root, newpath)
        else:
            print("Copying contents from %s to %s" % (root, newpath))
            for fname in files:
                newfpath = os.path.exists(os.path.join(newpath, fname))
                if newfpath:
                    print("%s already exists, skipping" % newfpath)
                else:
                    shutil.copy(os.path.join(root, fname), newpath)
        cert = os.path.join(newpath, "cert.xml")
        newcert = os.path.join(newpath, "sslcert.xml")
        key = os.path.join(newpath, "key.xml")
        newkey = os.path.join(newpath, "sslkey.xml")
        if os.path.exists(cert):
            os.rename(cert, newcert)
        if os.path.exists(key):
            os.rename(key, newkey)


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