bunkerized-nginx/autoconf/app.py

72 lines
2.1 KiB
Python
Raw Normal View History

2020-12-08 23:27:23 +01:00
#!/usr/bin/python3
2021-03-09 17:33:22 +01:00
from AutoConf import AutoConf
2021-03-16 17:56:24 +01:00
from ReloadServer import run_reload_server
2021-03-12 17:31:26 +01:00
import utils
2021-03-16 17:56:24 +01:00
import docker, os, stat, sys, select, threading
2020-12-08 23:27:23 +01:00
# Connect to the endpoint
endpoint = "/var/run/docker.sock"
if not os.path.exists(endpoint) or not stat.S_ISSOCK(os.stat(endpoint).st_mode) :
2020-12-09 12:00:54 +01:00
utils.log("[!] /var/run/docker.sock not found (is it mounted ?)")
2020-12-08 23:27:23 +01:00
sys.exit(1)
try :
client = docker.DockerClient(base_url='unix:///var/run/docker.sock')
except Exception as e :
2020-12-09 12:00:54 +01:00
utils.log("[!] Can't instantiate DockerClient : " + str(e))
2020-12-08 23:27:23 +01:00
sys.exit(2)
2021-03-09 17:33:22 +01:00
# Check if we are in Swarm mode
swarm = os.getenv("SWARM_MODE") == "yes"
# Our object to process events
api = ""
if swarm :
api = os.getenv("API_URI")
autoconf = AutoConf(swarm, api)
2021-03-16 17:56:24 +01:00
lock = threading.Lock()
if swarm :
(server, thread) = run_reload_server(autoconf, lock)
2021-03-09 17:33:22 +01:00
2020-12-09 12:00:54 +01:00
# Get all bunkerized-nginx instances and web services created before
2020-12-08 23:27:23 +01:00
try :
2021-03-09 17:33:22 +01:00
if swarm :
before = client.services.list(filters={"label" : "bunkerized-nginx.AUTOCONF"}) + client.services.list(filters={"label" : "bunkerized-nginx.SERVER_NAME"})
else :
before = client.containers.list(all=True, filters={"label" : "bunkerized-nginx.AUTOCONF"}) + client.containers.list(filters={"label" : "bunkerized-nginx.SERVER_NAME"})
2020-12-08 23:27:23 +01:00
except docker.errors.APIError as e :
2020-12-09 12:00:54 +01:00
utils.log("[!] Docker API error " + str(e))
2020-12-08 23:27:23 +01:00
sys.exit(3)
2021-03-09 17:33:22 +01:00
# Process them before events
2021-06-07 09:48:50 +02:00
autoconf.pre_process(before)
2020-12-08 23:27:23 +01:00
# Process events received from Docker
try :
utils.log("[*] Listening for Docker events ...")
2020-12-08 23:27:23 +01:00
for event in client.events(decode=True) :
2020-12-09 12:00:54 +01:00
2021-03-09 17:33:22 +01:00
# Process only container/service events
if (swarm and event["Type"] != "service") or (not swarm and event["Type"] != "container") :
2020-12-09 12:00:54 +01:00
continue
2021-03-09 17:33:22 +01:00
# Get Container/Service object
2020-12-09 12:00:54 +01:00
try :
2021-03-09 17:33:22 +01:00
if swarm :
id = service_id=event["Actor"]["ID"]
server = client.services.get(service_id=id)
2021-03-09 17:33:22 +01:00
else :
id = event["id"]
server = client.containers.get(id)
2020-12-09 12:00:54 +01:00
except docker.errors.NotFound as e :
server = autoconf.get_server(id)
if not server :
continue
2020-12-09 12:00:54 +01:00
# Process the event
2021-06-07 09:48:50 +02:00
autoconf.process(server, event["Action"])
2020-12-09 12:00:54 +01:00
except docker.errors.APIError as e :
utils.log("[!] Docker API error " + str(e))
sys.exit(4)