summaryrefslogtreecommitdiff
path: root/src/local/rz-mux/rz-mux-tool.py
blob: 590d53c49cfae633102ba07686476d9f883eca8e (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""
 Multiplexed-Rhizi instance configuration generator:
    - setup Apache instance configuration
    - setup Neo4J instance,  instance configuration
    - setup Rhizi instance configuration
"""
import argparse
from jinja2 import Environment, FileSystemLoader
import os
import pwd
import random
import uuid

class Config__port_map(): pass

class Template_Task():

    def __init__(self, template_path, dst_path, kv_set):
        self.jinja_tmpl = env.get_template(template_path)
        self.dst_path = dst_path
        self.kv_set = kv_set

    def do_exec(self):

        dst_dir = os.path.dirname(self.dst_path)
        if not os.path.exists(dst_dir):
            print('rz-mux-tool: warning: target dir missing, installing: %s, please adjust usr/grp' % (dst_dir))
            os.makedirs(dst_dir)

        with open(self.dst_path, 'w') as f_out:
            f_out.write(self.jinja_tmpl.render(**self.kv_set))

            print('rz-mux-tool: generated: %s' % (f_out.name))

def gen_dom_config__neo4j(domain_name, port_map):
    """
    Generate domain-specific neo4j configuration file set
    """

    neo4j_template_path_prefix = 'neo4j-conf-template.d'
    target_dir = os.path.join('etc/neo4j/mux-conf.d', args.domain)

    conf_task_set = [Template_Task(os.path.join(neo4j_template_path_prefix, 'neo4j-server.properties.jinja'),
                                   os.path.join(install_prefix, target_dir, 'neo4j-server.properties'),
                                   {'org_neo4j_server_webserver_https_port': port_map.neo4j_port__https,
                                    'org_neo4j_server_webserver_port': port_map.neo4j_port__http}),
                     Template_Task(os.path.join(neo4j_template_path_prefix, 'neo4j.properties.jinja'),
                                   os.path.join(install_prefix, target_dir, 'neo4j.properties'),
                                   {'remote_shell_port': port_map.neo4j_port__shell}),
                     Template_Task(os.path.join(neo4j_template_path_prefix, 'neo4j-service_deomain-init-script.jinja'),
                                   os.path.join(install_prefix, 'etc/init.d/', 'neo4j-service__%s' % (domain_name)),
                                   {'domain_name': domain_name}),
                     ]

    for conf_task_obj in conf_task_set:
        conf_task_obj.do_exec()

def gen_dom_config__apache(domain_name, port_map):
    """
    Generate domain-specific neo4j configuration file set
    """
    apache_template_path_prefix = 'apache-conf-template.d'
    rhizi_top_domain_name = 'rhizi.net'
    domain_fqdn = '%s.%s' % (domain_name, rhizi_top_domain_name)

    conf_task_set = [Template_Task(os.path.join(apache_template_path_prefix, 'x.%s.conf' % (rhizi_top_domain_name)),
                                   os.path.join(install_prefix, 'etc/apache2/sites-available', domain_fqdn + '.conf'),
                                   {'domain_name': domain_name,
                                    'rz_port__http': port_map.rz_port__http}),
                     ]

    for conf_task_obj in conf_task_set:
        conf_task_obj.do_exec()

def gen_dom_config__rhizi(domain_name, port_map):
    rz_template_path_prefix = 'rhizi-conf-template.d'
    rhizi_top_domain_name = 'rhizi.net'
    domain_fqdn = '%s.%s' % (domain_name, rhizi_top_domain_name)

    rz_server_secret = str(uuid.uuid4()).replace('-', '')
    root_path = os.path.join('/srv/www/rhizi/mux-root.d/', domain_name, 'webapp')
    user_db_path = os.path.join('/srv/www/rhizi/mux-root.d/', domain_name, 'auth', 'user_db.db')

    conf_task_set = [Template_Task(os.path.join(rz_template_path_prefix, 'rhizi-server.conf.jinja'),
                                   os.path.join(install_prefix, 'etc/rhizi/mux-conf.d', domain_name, 'rhizi-server.conf'),
                                   {'domain_name': domain_name,
                                    'neo4j_port__http': port_map.neo4j_port__http,
                                    'root_path': root_path,
                                    'rz_port__http': port_map.rz_port__http,
                                    'rz_server_secret': rz_server_secret,
                                    'user_db_path': user_db_path,
                                    }),
                     Template_Task(os.path.join(rz_template_path_prefix, 'rhizi.init.jinja'),
                                   os.path.join(install_prefix, 'etc/init.d/', 'rhizi__%s' % (domain_name)),
                                   {'domain_name': domain_name}),
                     Template_Task(os.path.join(rz_template_path_prefix, 'rhizi-cron-bkp.daily.sh.jinja'),
                                   os.path.join(install_prefix, '/etc/cron.daily/', 'rhizi__%s' % (domain_name)),
                                   {'domain_name': domain_name,
                                    'neo4j_port__shell': port_map.neo4j_port__shell}),
                     ]

    for conf_task_obj in conf_task_set:
        conf_task_obj.do_exec()

if __name__ == '__main__':
    global env
    global install_prefix

    p = argparse.ArgumentParser(description='rz-cli tool')
    p.add_argument('-d', '--domain', required=True, help='target domain name')
    p.add_argument('--install-prefix', default='/', help='install dir path prefix, default=\'/\'')
    p.add_argument('--template-dir', default='.', help='path to template dir')  # default devied from installed pkg layout
    args = p.parse_args()

    env = Environment(loader=FileSystemLoader(args.template_dir))
    install_prefix = args.install_prefix
    domain_name = args.domain

    #
    # generate optimistic-collision-avoiding instance port map
    #
    port_seed = 40000 + random.randint(1000, 9990)
    cfg_port_map = Config__port_map()
    setattr(cfg_port_map, 'neo4j_port__https', port_seed)  # REST API port
    setattr(cfg_port_map, 'neo4j_port__http', port_seed + 1)
    setattr(cfg_port_map, 'neo4j_port__shell', port_seed + 2)
    setattr(cfg_port_map, 'rz_port__http', 48000 + port_seed % 1000)

    gen_dom_config__neo4j(domain_name, cfg_port_map)
    gen_dom_config__rhizi(domain_name, cfg_port_map)
    gen_dom_config__apache(domain_name, cfg_port_map)