CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/dev/smc/conf/cloud.sagemath.com/push_conf.py
Views: 687
1
#!/usr/bin/env python2
2
3
import os
4
os.chdir(os.path.split(os.path.realpath(__file__))[0])
5
import gen_conf
6
7
public_hosts = ["web%s" % n for n in range(3)]
8
9
hacfg = "haproxy.cfg"
10
11
os.system("mkdir -p bkb")
12
13
14
def create(host = ''):
15
bkbfn = "bkb/haproxy-%s.cfg" % host
16
# generate the configuration file
17
gen_conf.gen_haproxy(host)
18
if os.path.exists(bkbfn):
19
if open(hacfg).read() == open(bkbfn).read():
20
# identical files, no need to update to targets
21
return False
22
# since it changed, make backup for the next time
23
os.system("cp -a haproxy.cfg %s" % bkbfn)
24
return True
25
26
27
def push_conf(mode):
28
assert mode in ['public', 'private']
29
30
if mode == "public":
31
# These are the web servers that are visible externally -- they also run haproxy
32
# and load balance between all web servers.
33
TARGETS = public_hosts
34
35
elif mode == "private":
36
TARGETS = [x for x in gen_conf.web_hosts() if x not in public_hosts]
37
38
# Now push out the haproxy script to the externally visible web servers
39
for host in TARGETS:
40
if (mode == "public" and create()) or (mode == "private" and create(host)):
41
os.system("scp %s %s:/tmp/" % (hacfg, host))
42
os.system("ssh %s 'sudo mv /tmp/%s /etc/haproxy/'" % (hacfg, host))
43
os.system("ssh %s 'sudo service haproxy reload'" % host)
44
45
if __name__ == "__main__":
46
push_conf('public')
47
push_conf('private')
48
49