|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import json |
| 5 | +import stat |
| 6 | + |
| 7 | +template_root = "../../templates" |
| 8 | +generate_root = "../../root" |
| 9 | + |
| 10 | +def readfile(path, name): |
| 11 | + file = os.path.join(path, name) |
| 12 | + with open(file) as f: |
| 13 | + return f.read() |
| 14 | + |
| 15 | + |
| 16 | +def generate(path, name, content): |
| 17 | + if path != "": |
| 18 | + os.makedirs(path, exist_ok=True) |
| 19 | + file = os.path.join(path, name) |
| 20 | + print("Generate: {}".format(os.path.abspath(file))) |
| 21 | + with open(file, 'w+') as f: |
| 22 | + f.write(content) |
| 23 | + |
| 24 | + |
| 25 | +def generate_bash(path, name, content): |
| 26 | + content = "#!/usr/bin/env bash\n\nset -x\nset -e\n\n" + content + "\n" |
| 27 | + generate(path, name, content) |
| 28 | + file = os.path.join(path, name) |
| 29 | + os.chmod(file, os.stat(file).st_mode | stat.S_IEXEC) |
| 30 | + |
| 31 | + |
| 32 | +def symlink(src, dst): |
| 33 | + try: |
| 34 | + os.remove(dst) |
| 35 | + except OSError: |
| 36 | + pass |
| 37 | + os.symlink(src, dst) |
| 38 | + |
| 39 | + |
| 40 | +class Coordinator: |
| 41 | + def __init__(self, config): |
| 42 | + self.name = config.get("name", "") |
| 43 | + self.addr = config.get("addr", "") |
| 44 | + |
| 45 | + |
| 46 | +class Dashboard(): |
| 47 | + def __init__(self, product, config): |
| 48 | + self.product = product |
| 49 | + self.env = product.env |
| 50 | + self.admin_addr = config.get("admin_addr", "") |
| 51 | + |
| 52 | + self.sentinel_quorum = config.get("sentinel_quorum", 2) |
| 53 | + self.sentinel_down_after = config.get("sentinel_down_after", "30s") |
| 54 | + self.coordinator = Coordinator(config.get("coordinator", {})) |
| 55 | + |
| 56 | + if self.admin_addr == "": |
| 57 | + raise Exception("dashboard.admin_addr not found") |
| 58 | + if self.coordinator.name == "": |
| 59 | + raise Exception("coordinator.name not found") |
| 60 | + if self.coordinator.addr == "": |
| 61 | + raise Exception("coordinator.addr not found or empty") |
| 62 | + |
| 63 | + self.admin_port = self.admin_addr.rsplit(':', 1)[1] |
| 64 | + |
| 65 | + def render(self, proxylist): |
| 66 | + kwargs = { |
| 67 | + 'PRODUCT_NAME': self.product.name, |
| 68 | + 'PRODUCT_AUTH': self.product.auth, |
| 69 | + 'COORDINATOR_NAME': self.coordinator.name, |
| 70 | + 'COORDINATOR_ADDR': self.coordinator.addr, |
| 71 | + 'ADMIN_ADDR': self.admin_addr, |
| 72 | + 'ADMIN_PORT': self.admin_port, |
| 73 | + 'SENTINEL_QUORUM': self.sentinel_quorum, |
| 74 | + 'SENTINEL_DOWN_AFTER': self.sentinel_down_after, |
| 75 | + 'BIN_PATH': self.env.bin_path, |
| 76 | + 'ETC_PATH': self.env.etc_path, |
| 77 | + 'LOG_PATH': self.env.log_path, |
| 78 | + } |
| 79 | + base = os.path.join(generate_root, self.env.etc_path.lstrip('/'), self.admin_addr.replace(':', '_')) |
| 80 | + |
| 81 | + temp = readfile(template_root, "dashboard.toml.template") |
| 82 | + generate(base, "dashboard.toml", temp.format(**kwargs)) |
| 83 | + |
| 84 | + temp = readfile(template_root, "dashboard.service.template") |
| 85 | + generate(base, "codis_dashboard_{}.service".format(self.admin_port), temp.format(**kwargs)) |
| 86 | + |
| 87 | + admin = os.path.join(self.env.bin_path, "codis-admin") |
| 88 | + generate_bash(base, "dashboard_admin", "{} --dashboard={} $@".format(admin, self.admin_addr)) |
| 89 | + |
| 90 | + scripts = 'd=1\n' |
| 91 | + for p in proxylist: |
| 92 | + scripts += "sleep $d; {} --dashboard={} --online-proxy --addr={}".format(admin, self.admin_addr, p.admin_addr) |
| 93 | + scripts += "\n" |
| 94 | + generate_bash(base, "foreach_proxy_online", scripts) |
| 95 | + |
| 96 | + scripts = 'd=1\n' |
| 97 | + for p in proxylist: |
| 98 | + scripts += "sleep $d; {} --dashboard={} --reinit-proxy --addr={}".format(admin, self.admin_addr, p.admin_addr) |
| 99 | + scripts += "\n" |
| 100 | + generate_bash(base, "foreach_proxy_reinit", scripts) |
| 101 | + |
| 102 | + scripts = 'd=1\n' |
| 103 | + for p in proxylist: |
| 104 | + scripts += "sleep $d; {} --proxy={} $@".format(admin, p.admin_addr) |
| 105 | + scripts += "\n" |
| 106 | + generate_bash(base, "foreach_proxy", scripts) |
| 107 | + |
| 108 | + cwd = os.getcwd() |
| 109 | + os.chdir(os.path.join(base, "..")) |
| 110 | + symlink(self.admin_addr.replace(':', '_'), self.product.name) |
| 111 | + os.chdir(cwd) |
| 112 | + |
| 113 | + |
| 114 | +class Template: |
| 115 | + def __init__(self, config): |
| 116 | + self.min_cpu = config.get("min_cpu", 4) |
| 117 | + self.max_cpu = config.get("max_cpu", 0) |
| 118 | + self.max_clients = config.get("max_clients", 10000) |
| 119 | + self.max_pipeline = config.get("max_pipeline", 1024) |
| 120 | + self.log_level = config.get("log_level", "INFO") |
| 121 | + self.jodis_name = config.get("jodis_name", "") |
| 122 | + self.jodis_addr = config.get("jodis_addr", "") |
| 123 | + |
| 124 | + |
| 125 | +class Proxy(): |
| 126 | + def __init__(self, product, template, config): |
| 127 | + self.product = product |
| 128 | + self.env = product.env |
| 129 | + self.template = template |
| 130 | + self.datacenter = config.get("datacenter", "") |
| 131 | + self.admin_addr = config.get("admin_addr", "") |
| 132 | + self.proxy_addr = config.get("proxy_addr", "") |
| 133 | + |
| 134 | + if self.admin_addr == "": |
| 135 | + raise Exception("proxy.admin_addr not found") |
| 136 | + if self.proxy_addr == "": |
| 137 | + raise Exception("proxy.proxy_addr not found") |
| 138 | + |
| 139 | + self.proxy_port = self.proxy_addr.rsplit(':', 1)[1] |
| 140 | + |
| 141 | + def render(self): |
| 142 | + kwargs = { |
| 143 | + 'PRODUCT_NAME': self.product.name, |
| 144 | + 'PRODUCT_AUTH': self.product.auth, |
| 145 | + 'ADMIN_ADDR': self.admin_addr, |
| 146 | + 'PROXY_ADDR': self.proxy_addr, |
| 147 | + 'PROXY_PORT': self.proxy_port, |
| 148 | + 'DATACENTER': self.datacenter, |
| 149 | + 'MAX_CLIENTS': self.template.max_clients, |
| 150 | + 'MAX_PIPELINE': self.template.max_pipeline, |
| 151 | + 'JODIS_NAME': self.template.jodis_name, |
| 152 | + 'JODIS_ADDR': self.template.jodis_addr, |
| 153 | + 'MIN_CPU': self.template.min_cpu, |
| 154 | + 'MAX_CPU': self.template.max_cpu, |
| 155 | + 'LOG_LEVEL': self.template.log_level, |
| 156 | + 'BIN_PATH': self.env.bin_path, |
| 157 | + 'ETC_PATH': self.env.etc_path, |
| 158 | + 'LOG_PATH': self.env.log_path, |
| 159 | + } |
| 160 | + base = os.path.join(generate_root, self.env.etc_path.lstrip('/'), self.proxy_addr.replace(':', '_')) |
| 161 | + |
| 162 | + temp = readfile(template_root, "proxy.toml.template") |
| 163 | + generate(base, "proxy.toml", temp.format(**kwargs)) |
| 164 | + |
| 165 | + temp = readfile(template_root, "proxy.service.template") |
| 166 | + generate(base, "codis_proxy_{}.service".format(self.proxy_port), temp.format(**kwargs)) |
| 167 | + |
| 168 | + admin = os.path.join(self.env.bin_path, "codis-admin") |
| 169 | + generate_bash(base, "proxy_admin", "{} --proxy={} $@".format(admin, self.admin_addr)) |
| 170 | + |
| 171 | + |
| 172 | +class Env: |
| 173 | + def __init__(self, config): |
| 174 | + self.bin_path = os.path.abspath(config.get("bin_path", "/opt/codis/bin")) |
| 175 | + self.etc_path = os.path.abspath(config.get("etc_path", "/opt/codis/etc")) |
| 176 | + self.log_path = os.path.abspath(config.get("log_path", "/opt/codis/log")) |
| 177 | + self.log_level = config.get("log_level", "INFO").upper() |
| 178 | + |
| 179 | + |
| 180 | +class Product: |
| 181 | + def __init__(self, config): |
| 182 | + self.name = config.get("product_name", "") |
| 183 | + self.auth = config.get("product_auth", "") |
| 184 | + |
| 185 | + if self.name == "": |
| 186 | + raise Exception("product_name not found") |
| 187 | + |
| 188 | + self.env = Env(config.get("env", {})) |
| 189 | + self.dashboard = Dashboard(self, config.get("dashboard", {})) |
| 190 | + |
| 191 | + self.proxylist = [] |
| 192 | + if "proxy" in config: |
| 193 | + template = Template(config.get("proxy", {})) |
| 194 | + for p in config.get("instance", []): |
| 195 | + self.proxylist.append(Proxy(self, template, p)) |
| 196 | + self.proxylist.sort(key=lambda p: p.datacenter + "|" + p.proxy_addr) |
| 197 | + |
| 198 | + def render(self): |
| 199 | + self.dashboard.render(self.proxylist) |
| 200 | + for p in self.proxylist: |
| 201 | + p.render() |
| 202 | + |
| 203 | + |
| 204 | +config = {} |
| 205 | + |
| 206 | +with open('config.json') as f: |
| 207 | + config = json.loads(f.read()) |
| 208 | + |
| 209 | +with open('instance.json') as f: |
| 210 | + config["instance"] = json.loads(f.read()) |
| 211 | + |
| 212 | +product = Product(config) |
| 213 | +product.render() |
0 commit comments