Remove all integration specifications

This commit is contained in:
Théophile Diot 2024-08-30 10:29:52 +02:00
parent 582e314c03
commit 2c0a0d4b8f
No known key found for this signature in database
GPG key ID: FA995104A0BA376A
6 changed files with 60 additions and 136 deletions

View file

@ -20,7 +20,6 @@ for deps_path in [join(sep, "usr", "share", "bunkerweb", *paths) for paths in ((
from API import API # type: ignore
from ApiCaller import ApiCaller # type: ignore
from common_utils import get_integration # type: ignore
from logger import setup_logger # type: ignore
@ -76,7 +75,6 @@ class CLI(ApiCaller):
if tz:
self.__variables["TZ"] = tz
self.__integration = get_integration()
self.__use_redis = self.__get_variable("USE_REDIS", "no") == "yes"
self.__redis = None
if self.__use_redis:
@ -184,15 +182,6 @@ class CLI(ApiCaller):
self.__logger.error("USE_REDIS is set to yes but REDIS_HOST or REDIS_SENTINEL_HOSTS is not set, disabling redis")
self.__use_redis = False
if Path(sep, "usr", "sbin", "nginx").exists() and self.__integration != "Linux":
return super().__init__(
[
API(
f"http://127.0.0.1:{self.__get_variable('API_HTTP_PORT', '5000')}",
host=self.__get_variable("API_SERVER_NAME", "bwapi"),
)
]
)
super().__init__()
for db_instance in self.__db.get_instances():
self.apis.append(API(f"http://{db_instance['hostname']}:{db_instance['port']}", db_instance["server_name"]))

View file

@ -3,7 +3,6 @@
from io import BytesIO
from os import getenv, sep
from os.path import join
from subprocess import DEVNULL, STDOUT, run
from sys import exit as sys_exit, path as sys_path
from tarfile import open as tar_open
from traceback import format_exc
@ -13,7 +12,6 @@ for deps_path in [join(sep, "usr", "share", "bunkerweb", *paths) for paths in ((
sys_path.append(deps_path)
from Database import Database # type: ignore
from common_utils import get_integration # type: ignore
from logger import setup_logger # type: ignore
from API import API # type: ignore
@ -26,52 +24,43 @@ try:
LOGGER.info(f"Certificates renewal for {getenv('RENEWED_DOMAINS')} successful")
# Cluster case
if get_integration() in ("Docker", "Swarm", "Kubernetes", "Autoconf"):
# Create tarball of /var/cache/bunkerweb/letsencrypt
tgz = BytesIO()
# Create tarball of /var/cache/bunkerweb/letsencrypt
tgz = BytesIO()
with tar_open(mode="w:gz", fileobj=tgz, compresslevel=3) as tf:
tf.add(join(sep, "var", "cache", "bunkerweb", "letsencrypt", "etc"), arcname="etc")
tgz.seek(0, 0)
files = {"archive.tar.gz": tgz}
with tar_open(mode="w:gz", fileobj=tgz, compresslevel=3) as tf:
tf.add(join(sep, "var", "cache", "bunkerweb", "letsencrypt", "etc"), arcname="etc")
tgz.seek(0, 0)
files = {"archive.tar.gz": tgz}
db = Database(LOGGER, sqlalchemy_string=getenv("DATABASE_URI", None))
db = Database(LOGGER, sqlalchemy_string=getenv("DATABASE_URI", None))
instances = db.get_instances()
instances = db.get_instances()
for instance in instances:
endpoint = f"http://{instance['hostname']}:{instance['port']}"
host = instance["server_name"]
api = API(endpoint, host=host)
for instance in instances:
endpoint = f"http://{instance['hostname']}:{instance['port']}"
host = instance["server_name"]
api = API(endpoint, host=host)
sent, err, status, resp = api.request("POST", "/lets-encrypt/certificates", files=files)
sent, err, status, resp = api.request("POST", "/lets-encrypt/certificates", files=files)
if not sent:
status = 1
LOGGER.error(f"Can't send API request to {api.endpoint}/lets-encrypt/certificates : {err}")
elif status != 200:
status = 1
LOGGER.error(f"Error while sending API request to {api.endpoint}/lets-encrypt/certificates : status = {resp['status']}, msg = {resp['msg']}")
else:
LOGGER.info(
f"Successfully sent API request to {api.endpoint}/lets-encrypt/certificates",
)
sent, err, status, resp = api.request("POST", "/reload")
if not sent:
status = 1
LOGGER.error(f"Can't send API request to {api.endpoint}/lets-encrypt/certificates : {err}")
LOGGER.error(f"Can't send API request to {api.endpoint}/reload : {err}")
elif status != 200:
status = 1
LOGGER.error(f"Error while sending API request to {api.endpoint}/lets-encrypt/certificates : status = {resp['status']}, msg = {resp['msg']}")
LOGGER.error(f"Error while sending API request to {api.endpoint}/reload : status = {resp['status']}, msg = {resp['msg']}")
else:
LOGGER.info(
f"Successfully sent API request to {api.endpoint}/lets-encrypt/certificates",
)
sent, err, status, resp = api.request("POST", "/reload")
if not sent:
status = 1
LOGGER.error(f"Can't send API request to {api.endpoint}/reload : {err}")
elif status != 200:
status = 1
LOGGER.error(f"Error while sending API request to {api.endpoint}/reload : status = {resp['status']}, msg = {resp['msg']}")
else:
LOGGER.info(f"Successfully sent API request to {api.endpoint}/reload")
# Linux case
else:
if run([join(sep, "usr", "sbin", "nginx"), "-s", "reload"], stdin=DEVNULL, stderr=STDOUT, check=False).returncode != 0:
status = 1
LOGGER.error("Error while reloading nginx")
else:
LOGGER.info("Successfully reloaded nginx")
LOGGER.info(f"Successfully sent API request to {api.endpoint}/reload")
except:
status = 1
LOGGER.error(f"Exception while running certbot-deploy.py :\n{format_exc()}")

View file

@ -6,9 +6,7 @@ from os import R_OK, W_OK, X_OK, access, getenv, sep
from os.path import join
from pathlib import Path
from shutil import rmtree
from subprocess import DEVNULL, STDOUT, run
from sys import exit as sys_exit, path as sys_path
from time import sleep
from traceback import format_exc
from typing import Any, Dict
@ -16,7 +14,6 @@ for deps_path in [join(sep, "usr", "share", "bunkerweb", *paths) for paths in ((
if deps_path not in sys_path:
sys_path.append(deps_path)
from common_utils import get_integration # type: ignore
from logger import setup_logger # type: ignore
from Configurator import Configurator
from Templator import Templator
@ -67,8 +64,6 @@ if __name__ == "__main__":
logger.info(f"Output : {output_path}")
logger.info(f"Target : {target_path}")
integration = get_integration()
db = None
if DB_PATH.is_dir():
if DB_PATH.as_posix() not in sys_path:
@ -134,32 +129,6 @@ if __name__ == "__main__":
templator = Templator(str(templates_path), str(core_path), str(plugins_path), str(pro_plugins_path), str(output_path), str(target_path), config)
templator.render()
if integration not in ("Autoconf", "Swarm", "Kubernetes", "Docker") and not args.no_linux_reload:
retries = 0
while not Path(sep, "var", "run", "bunkerweb", "nginx.pid").exists():
if retries == 5:
logger.error(
"BunkerWeb's nginx didn't start in time.",
)
sys_exit(1)
logger.warning(
"Waiting for BunkerWeb's nginx to start, retrying in 5 seconds ...",
)
retries += 1
sleep(5)
proc = run(
[join(sep, "usr", "sbin", "nginx"), "-s", "reload"],
stdin=DEVNULL,
stderr=STDOUT,
)
if proc.returncode != 0:
status = 1
logger.error("Error while reloading nginx")
else:
logger.info("Successfully reloaded nginx")
except SystemExit as e:
raise e
except:

View file

@ -65,9 +65,6 @@ if __name__ == "__main__":
if args.init:
LOGGER.info(f"Detected {integration} integration")
if integration == "Linux" and not args.variables:
args.variables = join(sep, "etc", "bunkerweb", "variables.env")
external_plugins = args.plugins
pro_plugins = args.pro_plugins

View file

@ -18,7 +18,7 @@ from schedule import (
every as schedule_every,
jobs as schedule_jobs,
)
from subprocess import DEVNULL, PIPE, STDOUT, run
from subprocess import DEVNULL, STDOUT, run
from sys import path as sys_path
from threading import Lock, Semaphore, Thread
from traceback import format_exc
@ -37,7 +37,6 @@ class JobScheduler(ApiCaller):
self,
env: Optional[Dict[str, Any]] = None,
logger: Optional[Logger] = None,
integration: str = "Linux",
*,
db: Optional[Database] = None,
lock: Optional[Lock] = None,
@ -45,7 +44,6 @@ class JobScheduler(ApiCaller):
):
super().__init__(apis or [])
self.__logger = logger or setup_logger("Scheduler", getenv("CUSTOM_LOG_LEVEL", getenv("LOG_LEVEL", "INFO")))
self.__integration = integration
self.db = db or Database(self.__logger)
self.__env = env or {}
self.__env.update(environ)
@ -64,9 +62,6 @@ class JobScheduler(ApiCaller):
def env(self, env: Dict[str, Any]):
self.__env = env
def set_integration(self, integration: str):
self.__integration = integration
def update_jobs(self):
self.__jobs = self.__get_jobs()
@ -138,24 +133,12 @@ class JobScheduler(ApiCaller):
raise ValueError(f"can't convert string {every} to schedule")
def __reload(self) -> bool:
reload = True
if self.__integration not in ("Autoconf", "Swarm", "Kubernetes", "Docker"):
self.__logger.info("Reloading nginx ...")
proc = run([join(sep, "usr", "sbin", "nginx"), "-s", "reload"], stdin=DEVNULL, stderr=PIPE, env=self.__env, check=False)
reload = proc.returncode == 0
if reload:
self.__logger.info("Successfully reloaded nginx")
return True
self.__logger.error(
f"Error while reloading nginx - returncode: {proc.returncode} - error: {proc.stderr.decode() if proc.stderr else 'Missing stderr'}"
)
else:
self.__logger.info("Reloading nginx ...")
reload = self.send_to_apis("POST", "/reload")[0]
if reload:
self.__logger.info("Successfully reloaded nginx")
return True
self.__logger.error("Error while reloading nginx")
self.__logger.info("Reloading nginx ...")
reload = self.send_to_apis("POST", "/reload")[0]
if reload:
self.__logger.info("Successfully reloaded nginx")
return True
self.__logger.error("Error while reloading nginx")
return reload
def __job_wrapper(self, path: str, plugin: str, name: str, file: str) -> int:

View file

@ -12,7 +12,7 @@ from pathlib import Path
from shutil import copy, rmtree, copytree
from signal import SIGINT, SIGTERM, signal, SIGHUP
from stat import S_IEXEC
from subprocess import run as subprocess_run, DEVNULL, STDOUT, PIPE
from subprocess import run as subprocess_run, DEVNULL, STDOUT
from sys import path as sys_path
from tarfile import TarFile, open as tar_open
from threading import Event, Thread
@ -27,7 +27,7 @@ for deps_path in [join(sep, "usr", "share", "bunkerweb", *paths) for paths in ((
from dotenv import dotenv_values
from schedule import every as schedule_every, run_pending
from common_utils import bytes_hash, dict_to_frozenset, get_integration # type: ignore
from common_utils import bytes_hash, dict_to_frozenset # type: ignore
from logger import setup_logger # type: ignore
from Database import Database # type: ignore
from JobScheduler import JobScheduler
@ -140,8 +140,8 @@ def handle_reload(signum, frame):
LOGGER.error("Config saver failed, configuration will not work as expected...")
else:
LOGGER.warning("Ignored reload operation because scheduler is not running ...")
except:
LOGGER.error(f"Exception while reloading scheduler : {format_exc()}")
except BaseException as e:
LOGGER.error(f"Exception while reloading scheduler : {e}")
signal(SIGHUP, handle_reload)
@ -517,12 +517,11 @@ if __name__ == "__main__":
parser.add_argument("--variables", type=str, help="path to the file containing environment variables")
args = parser.parse_args()
INTEGRATION = get_integration()
tmp_variables_path = Path(args.variables or join(sep, "var", "tmp", "bunkerweb", "variables.env"))
nginx_variables_path = CONFIG_PATH.joinpath("variables.env")
dotenv_env = dotenv_values(str(tmp_variables_path))
SCHEDULER = JobScheduler(environ, LOGGER, INTEGRATION, db=Database(LOGGER, sqlalchemy_string=dotenv_env.get("DATABASE_URI", getenv("DATABASE_URI", None)))) # type: ignore
SCHEDULER = JobScheduler(environ, LOGGER, db=Database(LOGGER, sqlalchemy_string=dotenv_env.get("DATABASE_URI", getenv("DATABASE_URI", None)))) # type: ignore
JOB = Job(LOGGER, SCHEDULER.db)
@ -723,7 +722,8 @@ if __name__ == "__main__":
join(sep, "usr", "share", "bunkerweb", "gen", "save_config.py"),
"--settings",
join(sep, "usr", "share", "bunkerweb", "settings.json"),
],
]
+ (["--variables", str(tmp_variables_path)] if args.variables else []),
stdin=DEVNULL,
stderr=STDOUT,
check=False,
@ -795,41 +795,38 @@ if __name__ == "__main__":
if SCHEDULER.apis:
# send nginx configs
thread = Thread(target=send_nginx_configs)
thread.start()
threads.append(thread)
elif INTEGRATION != "Linux":
LOGGER.warning("No BunkerWeb instance found, skipping nginx configs sending ...")
threads.append(Thread(target=send_nginx_configs))
threads[-1].start()
try:
success = True
reachable = True
if SCHEDULER.apis:
# send cache
thread = Thread(target=send_nginx_cache)
thread.start()
threads.append(thread)
threads.append(Thread(target=send_nginx_cache))
threads[-1].start()
for thread in threads:
thread.join()
success, responses = SCHEDULER.send_to_apis("POST", "/reload", response=True)
if not success:
LOGGER.debug(f"Error while reloading bunkerweb: {responses}")
reachable = bool(responses)
elif INTEGRATION == "Linux":
# Reload nginx
LOGGER.info("Reloading nginx ...")
proc = subprocess_run(
[join(sep, "usr", "sbin", "nginx"), "-s", "reload"],
stdin=DEVNULL,
stderr=STDOUT,
env=env.copy(),
check=False,
stdout=PIPE,
)
success = proc.returncode == 0
LOGGER.debug(f"Error while reloading all bunkerweb instances: {responses}")
reachable = False
for db_instance in SCHEDULER.db.get_instances():
status = responses.get(db_instance["hostname"], {"status": "down"}).get("status", "down")
if status == "success":
reachable = True
ret = SCHEDULER.db.update_instance(db_instance["hostname"], "up" if status == "success" else "down")
if ret:
LOGGER.error(f"Couldn't update instance {db_instance['hostname']} status to down in the database: {ret}")
if db_instance["hostname"] in SCHEDULER.apis:
SCHEDULER.apis.remove(db_instance["hostname"])
else:
for thread in threads:
thread.join()
LOGGER.warning("No BunkerWeb instance found, skipping bunkerweb reload ...")
except BaseException as e:
LOGGER.error(f"Exception while reloading after running jobs once scheduling : {e}")