summaryrefslogtreecommitdiff
path: root/branding/scripts/vendorize.py
blob: 28c5d2a79e2ee61dc4b2b671f726db409fc91164 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3

# TODO: to be deprecated! use gen-providers-json.py instead

import os
import sys

from string import Template
import configparser

from provider import getDefaultProvider
from provider import getProviderData

OUTFILE = 'config.go'
INFILE = '../templates/bitmaskvpn/config.go'
CONFIGFILE = '../config/vendor.conf'
SCRIPT_NAME = 'vendorize'


def addCaData(data, configfile):
    provider = data.get('name').lower()
    folder, f = os.path.split(configfile)
    caFile = os.path.join(folder, provider + '-ca.crt')
    if not os.path.isfile(caFile):
        bail('[!] Cannot find CA file in {path}'.format(path=caFile))
    with open(caFile) as ca:
        data['caCertString'] = ca.read().strip()


def writeOutput(data, infile, outfile):

    with open(infile) as infile:
        s = Template(infile.read())

    with open(outfile, 'w') as outf:
        outf.write(s.substitute(data))


def bail(msg=None):
    if not msg:
        print('Usage: {scriptname}.py <template> <config> <output>'.format(
            scriptname=SCRIPT_NAME))
    else:
        print(msg)
    sys.exit(1)


if __name__ == "__main__":
    infile = outfile = ""

    if len(sys.argv) > 4:
        bail()

    elif len(sys.argv) == 1:
        infile = INFILE
        outfile = OUTFILE
        configfile = CONFIGFILE
    else:
        try:
            infile = sys.argv[1]
            configfile = sys.argv[2]
            outfile = sys.argv[3]
        except IndexError:
            bail()

    env_provider_conf = os.environ.get('PROVIDER_CONFIG')
    if env_provider_conf:
        if os.path.isfile(env_provider_conf):
            print("[+] Overriding provider config per "
                  "PROVIDER_CONFIG variable")
            configfile = env_provider_conf

    if not os.path.isfile(infile):
        bail('[!] Cannot find template in {path}'.format(
            path=os.path.abspath(infile)))
    elif not os.path.isfile(configfile):
        bail('[!] Cannot find config in {path}'.format(
            path=os.path.abspath(configfile)))
    else:
        print('[+] Using {path} as template'.format(
            path=os.path.abspath(infile)))
        print('[+] Using {path} as config'.format(
            path=os.path.abspath(configfile)))

    config = configparser.ConfigParser()
    config.read(configfile)

    provider = getDefaultProvider(config)
    data = getProviderData(provider, config)
    addCaData(data, configfile)
    writeOutput(data, infile, outfile)

    print('[+] Wrote configuration for {provider} to {outf}'.format(
        provider=data.get('name'),
        outf=os.path.abspath(outfile)))