1"""
2Migration tools
3"""
4
5import os.path
6import shutil
7
8import salt.syspaths as syspaths
9
10
11def migrate_paths(opts):
12    """
13    Migrate old minion and master pki file paths to new ones.
14    """
15    oldpki_dir = os.path.join(syspaths.CONFIG_DIR, "pki")
16
17    if not os.path.exists(oldpki_dir):
18        # There's not even a pki directory, don't bother migrating
19        return
20
21    newpki_dir = opts["pki_dir"]
22
23    if opts["default_include"].startswith("master"):
24        keepers = [
25            "master.pem",
26            "master.pub",
27            "syndic_master.pub",
28            "minions",
29            "minions_pre",
30            "minions_rejected",
31        ]
32        if not os.path.exists(newpki_dir):
33            os.makedirs(newpki_dir)
34        for item in keepers:
35            oi_path = os.path.join(oldpki_dir, item)
36            ni_path = os.path.join(newpki_dir, item)
37            if os.path.exists(oi_path) and not os.path.exists(ni_path):
38                shutil.move(oi_path, ni_path)
39
40    if opts["default_include"].startswith("minion"):
41        keepers = [
42            "minion_master.pub",
43            "minion.pem",
44            "minion.pub",
45        ]
46        if not os.path.exists(newpki_dir):
47            os.makedirs(newpki_dir)
48        for item in keepers:
49            oi_path = os.path.join(oldpki_dir, item)
50            ni_path = os.path.join(newpki_dir, item)
51            if os.path.exists(oi_path) and not os.path.exists(ni_path):
52                shutil.move(oi_path, ni_path)
53