road to swarm - still some mess to fix

This commit is contained in:
bunkerity
2021-03-16 17:56:24 +01:00
parent b8027d2bac
commit ceed904882
16 changed files with 188 additions and 42 deletions

View File

@@ -11,6 +11,9 @@ class AutoConf :
self.__sites = {}
self.__config = Config(self.__swarm, api)
def reload(self) :
return self.__config.reload(self.instances)
def pre_process(self, objs) :
for instance in objs :
(id, name, labels) = self.__get_infos(instance)

View File

@@ -92,8 +92,9 @@ class Config :
# Include the server conf
utils.replace_in_file("/etc/nginx/nginx.conf", "}", "include /etc/nginx/" + vars["SERVER_NAME"] + "/server.conf;\n}")
return self.__reload(instances)
return self.reload(instances)
except Exception as e :
traceback.print_exc()
utils.log("[!] Error while activating config : " + str(e))
return False
@@ -107,9 +108,10 @@ class Config :
# Remove the include
utils.replace_in_file("/etc/nginx/nginx.conf", "include /etc/nginx/" + vars["SERVER_NAME"] + "/server.conf;\n", "")
return self.__reload(instances)
return self.reload(instances)
except Exception as e :
traceback.print_exc()
utils.log("[!] Error while deactivating config : " + str(e))
return False
@@ -127,13 +129,13 @@ class Config :
utils.log("[!] Error while deactivating config : " + str(e))
return False
def __reload(self, instances) :
return self.__api(instances, "/reload")
def reload(self, instances) :
return self.__api_call(instances, "/reload")
def __status(self, instances) :
return self.__api(instances, "/status")
return self.__api_call(instances, "/status")
def __api(self, instances, path) :
def __api_call(self, instances, path) :
ret = True
for instance_id, instance in instances.items() :
# Reload the instance object just in case
@@ -146,7 +148,11 @@ class Config :
nodeID = task["NodeID"]
taskID = task["ID"]
fqdn = name + "." + nodeID + "." + taskID
req = requests.post("http://" + fqdn + ":8080" + self.__api + path)
req = False
try :
req = requests.post("http://" + fqdn + ":8080" + self.__api + path)
except :
pass
if req and req.status_code == 200 :
utils.log("[*] Sent reload order to instance " + fqdn + " (service.node.task)")
else :

23
autoconf/ReloadServer.py Normal file
View File

@@ -0,0 +1,23 @@
import socketserver, threading
class ReloadServerHandler(socketserver.BaseRequestHandler):
def handle(self) :
data = self.request.recv(512)
if not data :
return
with self.server.lock :
ret = self.server.autoconf.reload()
if ret :
self.request.sendall("ok")
else :
self.request.sendall("ko")
def run_reload_server(autoconf, lock) :
server = socketserver.UnixStreamServer("/tmp/autoconf.pid", ReloadServerHandler)
server.autoconf = autoconf
server.lock = lock
thread = threading.Thread(target=server.serve_forever)
thread.daemon = True
thread.start()
return (server, thread)

View File

@@ -1,8 +1,9 @@
#!/usr/bin/python3
from AutoConf import AutoConf
from ReloadServer import run_reload_server
import utils
import docker, os, stat, sys
import docker, os, stat, sys, select, threading
# Connect to the endpoint
endpoint = "/var/run/docker.sock"
@@ -23,6 +24,9 @@ api = ""
if swarm :
api = os.getenv("API_URI")
autoconf = AutoConf(swarm, api)
lock = threading.Lock()
if swarm :
(server, thread) = run_reload_server(autoconf, lock)
# Get all bunkerized-nginx instances and web services created before
try :
@@ -35,7 +39,8 @@ except docker.errors.APIError as e :
sys.exit(3)
# Process them before events
autoconf.pre_process(before)
with lock :
autoconf.pre_process(before)
# Process events received from Docker
try :
@@ -55,7 +60,8 @@ try :
continue
# Process the event
autoconf.process(server, event["Action"])
with lock :
autoconf.process(server, event["Action"])
except docker.errors.APIError as e :
utils.log("[!] Docker API error " + str(e))

19
autoconf/reload.py Normal file
View File

@@ -0,0 +1,19 @@
#!/usr/bin/python3
import sys, socket, os
if not os.path.exists("/tmp/autoconf.sock") :
sys.exit(1)
try :
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
client.connect("/tmp/autoconf.sock")
client.send("reload".encode("utf-8"))
data = client.recv(512)
client.close()
if not data or data.decode("utf-8") != "ok" :
sys.exit(3)
except Exception as e :
sys.exit(2)
sys.exit(0)