various bug fixes related to Swarm

This commit is contained in:
bunkerity
2021-06-06 23:22:33 +02:00
parent 678ad70b01
commit fcc6b3b5e4
17 changed files with 162 additions and 97 deletions

View File

@@ -1,4 +1,3 @@
# TODO : hard tests, jobs if swarm mode, check state when generating env, ...
from Config import Config
import utils
import os
@@ -94,13 +93,9 @@ class AutoConf :
if self.__swarm and len(self.__instances) == 1 :
if self.__config.generate(self.__env) :
utils.log("[*] Initial config succeeded")
with open("/etc/nginx/autoconf", "w") as f :
f.write("ok")
if not self.__config.swarm_wait(self.__instances) :
utils.log("[!] Removing bunkerized-nginx instances from list")
del self.__instances[id]
os.remove("/etc/nginx/autoconf")
else :
utils.log("[!] Initial config failed")
utils.log("[*] bunkerized-nginx instance created : " + name + " / " + id)
@@ -118,11 +113,6 @@ class AutoConf :
elif event == "destroy" or event == "remove" :
del self.__instances[id]
self.__gen_env()
if self.__swarm and len(self.__instances) == 0 :
with open("/etc/crontabs/nginx", "w") as f :
f.write("")
if os.path.exists("/etc/nginx/autoconf") :
os.remove("/etc/nginx/autoconf")
utils.log("[*] bunkerized-nginx instance removed : " + name + " / " + id)
def __process_server(self, instance, event, id, name, labels) :

View File

@@ -9,8 +9,26 @@ class Config :
self.__swarm = swarm
self.__api = api
def __jobs(self) :
utils.log("[*] Starting jobs ...")
proc = subprocess.run(["/bin/su", "-c", "/opt/entrypoint/jobs.sh", "nginx"], capture_output=True)
stdout = proc.stdout.decode("ascii")
stderr = proc.stderr.decode("ascii")
if stdout != "" :
utils.log("[*] Jobs stdout :")
utils.log(stdout)
if stderr != "" :
utils.log("[!] Jobs stderr :")
utils.log(stderr)
if proc.returncode != 0 :
utils.log("[!] Jobs error : return code != 0")
return False
return True
def swarm_wait(self, instances) :
try :
with open("/etc/nginx/autoconf", "w") as f :
f.write("ok")
utils.log("[*] Waiting for bunkerized-nginx tasks ...")
i = 1
started = False
@@ -23,16 +41,7 @@ class Config :
utils.log("[!] Waiting " + str(i) + " seconds before retrying to contact bunkerized-nginx tasks")
if started :
utils.log("[*] bunkerized-nginx tasks started")
proc = subprocess.run(["/bin/su", "-c", "/opt/entrypoint/jobs.sh", "nginx"], capture_output=True)
stdout = proc.stdout.decode("ascii")
stderr = proc.stderr.decode("ascii")
if stdout != "" :
for line in stdout.split("\n") :
utils.log("[*] Jobs output : " + stdout)
if stderr != "" :
for line in stderr.split("\n") :
utils.log("[!] Jobs error : " + stderr)
return proc.returncode == 0
return True
else :
utils.log("[!] bunkerized-nginx tasks are not started")
except Exception as e :
@@ -52,15 +61,17 @@ class Config :
# Print stdout/stderr
stdout = proc.stdout.decode("ascii")
stderr = proc.stderr.decode("ascii")
if stdout != "":
for line in stdout.split("\n") :
utils.log("[*] Generator output : " + stdout)
if stdout != "" :
utils.log("[*] Generator output :")
utils.log(stdout)
if stderr != "" :
for line in stderr.split("\n") :
utils.log("[*] Generator error : " + stderr)
utils.log("[*] Generator error :")
utils.log(error)
# We're done
if proc.returncode == 0 :
if self.__swarm :
return self.__jobs()
return True
utils.log("[!] Error while generating site config for " + env["SERVER_NAME"] + " : return code = " + str(proc.returncode))

View File

@@ -9,7 +9,6 @@ if [ "$?" -ne 0 ] ; then
fi
if [ "$SWARM_MODE" = "yes" ] ; then
cp -r /opt/confs/nginx/* /etc/nginx
chown -R root:nginx /etc/nginx
chmod -R 770 /etc/nginx
fi