Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/dev/smc/conf/cloud.sagemath.com/gen_conf.py
Views: 687
#!/usr/bin/env python21"""2Determine which hosts web[n] exist, i.e., for which DNS resolves web[n], for n=0,1,2, etc.34Then read the file 'haproxy.cfg.template' and make an uncommented copy of all the web0 lines5but for all the web[n] hosts that exist.67(c) William Stein, 20168"""9import sys10import os1112# EXCLUDE=['web6']13EXCLUDE = []141516def host_exists(hostname):17"""18Return true if and only if hostname resolves and is pingable.19"""20return os.system("ping -c 1 -W 1 '%s' 2>/dev/null 1>/dev/null" % hostname) == 0212223def web_hosts_2(bound=9):24import subprocess as sp25import json26cmd = sp.Popen(27"gcloud compute instances list --filter='name ~ ^web' --format=json",28shell=True, stdout=sp.PIPE, stderr=sp.PIPE)29webs, err = cmd.communicate()30try:31webs = json.loads(webs)32assert len(webs) >= 333except Exception as e:34print("ERROR, fallback -- %s" % e)35webs = ["web%s" % n for n in range(bound)]36# maybe filter additionally on something?37names = [w['name'] for w in webs if w['status'] == "RUNNING"]38return [name for name in names if name not in EXCLUDE]394041def web_hosts(bound=20):42"""43Return all web hosts of the form web[n] that exists for n<bound.44"""45v = ["web%s" % n for n in range(bound) if host_exists("web%s" % n)]46return [x for x in v if x not in EXCLUDE]474849def gen_haproxy(x=''):50if not x:51hosts = web_hosts()52else:53hosts = [x]54v = []55for x in open('haproxy.cfg.template').xreadlines():56if 'web0' in x:57# generate version of x with leading # deleted and web0 replaced by58# each web hostname59i = x.find('#')60t = x[:i] + x[i + 1:]61for h in hosts:62n = h[3:]63v.append(t.replace('web0', h).replace('nginx0', 'nginx' + n)64.replace('proxy0', 'proxy' + n).replace('hub0', 'hub' + n))65else:66v.append(x)67# write out our new haproxy config file68open('haproxy.cfg', 'w').write(''.join(v))6970if __name__ == "__main__":71if len(sys.argv) == 1:72gen_haproxy()73else:74gen_haproxy(sys.argv[1])757677