import os
os.chdir(os.path.split(os.path.realpath(__file__))[0])
import gen_conf
public_hosts = ["web%s" % n for n in range(3)]
hacfg = "haproxy.cfg"
os.system("mkdir -p bkb")
def create(host = ''):
bkbfn = "bkb/haproxy-%s.cfg" % host
gen_conf.gen_haproxy(host)
if os.path.exists(bkbfn):
if open(hacfg).read() == open(bkbfn).read():
return False
os.system("cp -a haproxy.cfg %s" % bkbfn)
return True
def push_conf(mode):
assert mode in ['public', 'private']
if mode == "public":
TARGETS = public_hosts
elif mode == "private":
TARGETS = [x for x in gen_conf.web_hosts() if x not in public_hosts]
for host in TARGETS:
if (mode == "public" and create()) or (mode == "private" and create(host)):
os.system("scp %s %s:/tmp/" % (hacfg, host))
os.system("ssh %s 'sudo mv /tmp/%s /etc/haproxy/'" % (hacfg, host))
os.system("ssh %s 'sudo service haproxy reload'" % host)
if __name__ == "__main__":
push_conf('public')
push_conf('private')