mirror of
https://github.com/bunkerity/bunkerized-nginx
synced 2023-12-13 21:30:18 +01:00
Change the way linux starts and the scheduler
This commit is contained in:
parent
95d4f0f87c
commit
f7d986d6a5
3 changed files with 90 additions and 98 deletions
|
@ -151,9 +151,6 @@ class Database:
|
||||||
)
|
)
|
||||||
self.suffix_rx = re_compile(r"_\d+$")
|
self.suffix_rx = re_compile(r"_\d+$")
|
||||||
|
|
||||||
def get_database_uri(self) -> str:
|
|
||||||
return self.database_uri
|
|
||||||
|
|
||||||
def __del__(self) -> None:
|
def __del__(self) -> None:
|
||||||
"""Close the database"""
|
"""Close the database"""
|
||||||
if self.__sql_session:
|
if self.__sql_session:
|
||||||
|
|
|
@ -85,7 +85,7 @@ function start() {
|
||||||
|
|
||||||
# Create dummy variables.env
|
# Create dummy variables.env
|
||||||
if [ ! -f /etc/bunkerweb/variables.env ]; then
|
if [ ! -f /etc/bunkerweb/variables.env ]; then
|
||||||
sudo -E -u nginx -g nginx /bin/bash -c "echo -ne '# remove IS_LOADING=yes when your config is ready\nIS_LOADING=yes\nHTTP_PORT=80\nHTTPS_PORT=443\nAPI_LISTEN_IP=127.0.0.1\nSERVER_NAME=\n' > /etc/bunkerweb/variables.env"
|
sudo -E -u nginx -g nginx /bin/bash -c "echo -ne '# remove IS_LOADING=yes when your config is ready\nIS_LOADING=yes\nUSE_BUNKERNET=no\nHTTP_PORT=80\nHTTPS_PORT=443\nAPI_LISTEN_IP=127.0.0.1\nSERVER_NAME=\n' > /etc/bunkerweb/variables.env"
|
||||||
log "SYSTEMCTL" "ℹ️" "Created dummy variables.env file"
|
log "SYSTEMCTL" "ℹ️" "Created dummy variables.env file"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -134,19 +134,6 @@ function start() {
|
||||||
fi
|
fi
|
||||||
log "SYSTEMCTL" "ℹ️" "nginx started ..."
|
log "SYSTEMCTL" "ℹ️" "nginx started ..."
|
||||||
|
|
||||||
# Update database
|
|
||||||
log "SYSTEMCTL" "ℹ️" "Updating database ..."
|
|
||||||
if [ ! -f /var/lib/bunkerweb/db.sqlite3 ]; then
|
|
||||||
sudo -E -u nginx -g nginx /bin/bash -c "PYTHONPATH=/usr/share/bunkerweb/deps/python/ /usr/share/bunkerweb/gen/save_config.py --variables /etc/bunkerweb/variables.env --init"
|
|
||||||
else
|
|
||||||
sudo -E -u nginx -g nginx /bin/bash -c "PYTHONPATH=/usr/share/bunkerweb/deps/python/ /usr/share/bunkerweb/gen/save_config.py --variables /etc/bunkerweb/variables.env"
|
|
||||||
fi
|
|
||||||
if [ $? -ne 0 ] ; then
|
|
||||||
log "SYSTEMCTL" "❌" "save_config failed"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
log "SYSTEMCTL" "ℹ️" "Database updated ..."
|
|
||||||
|
|
||||||
# Execute scheduler
|
# Execute scheduler
|
||||||
log "SYSTEMCTL" "ℹ️ " "Executing scheduler ..."
|
log "SYSTEMCTL" "ℹ️ " "Executing scheduler ..."
|
||||||
sudo -E -u nginx -g nginx /bin/bash -c "PYTHONPATH=/usr/share/bunkerweb/deps/python/ /usr/share/bunkerweb/scheduler/main.py --variables /etc/bunkerweb/variables.env"
|
sudo -E -u nginx -g nginx /bin/bash -c "PYTHONPATH=/usr/share/bunkerweb/deps/python/ /usr/share/bunkerweb/scheduler/main.py --variables /etc/bunkerweb/variables.env"
|
||||||
|
|
|
@ -192,99 +192,102 @@ if __name__ == "__main__":
|
||||||
help="path to the file containing environment variables",
|
help="path to the file containing environment variables",
|
||||||
)
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
integration_path = Path(sep, "usr", "share", "bunkerweb", "INTEGRATION")
|
||||||
|
os_release_path = Path(sep, "etc", "os-release")
|
||||||
|
if getenv("KUBERNETES_MODE", "no").lower() == "yes":
|
||||||
|
INTEGRATION = "Kubernetes"
|
||||||
|
elif getenv("SWARM_MODE", "no").lower() == "yes":
|
||||||
|
INTEGRATION = "Swarm"
|
||||||
|
elif getenv("AUTOCONF_MODE", "no").lower() == "yes":
|
||||||
|
INTEGRATION = "Autoconf"
|
||||||
|
elif integration_path.is_file():
|
||||||
|
INTEGRATION = integration_path.read_text(encoding="utf-8").strip()
|
||||||
|
elif os_release_path.is_file() and "Alpine" in os_release_path.read_text(
|
||||||
|
encoding="utf-8"
|
||||||
|
):
|
||||||
|
INTEGRATION = "Docker"
|
||||||
|
|
||||||
|
del integration_path, os_release_path
|
||||||
|
|
||||||
tmp_variables_path = (
|
tmp_variables_path = (
|
||||||
normpath(args.variables)
|
normpath(args.variables)
|
||||||
if args.variables
|
if args.variables
|
||||||
else join(sep, "var", "tmp", "bunkerweb", "variables.env")
|
else join(sep, "var", "tmp", "bunkerweb", "variables.env")
|
||||||
)
|
)
|
||||||
tmp_variables_path = Path(tmp_variables_path)
|
tmp_variables_path = Path(tmp_variables_path)
|
||||||
|
nginx_variables_path = Path(sep, "etc", "nginx", "variables.env")
|
||||||
dotenv_env = dotenv_values(str(tmp_variables_path))
|
dotenv_env = dotenv_values(str(tmp_variables_path))
|
||||||
|
|
||||||
# Instantiate scheduler
|
db = Database(
|
||||||
SCHEDULER = JobScheduler(environ.copy(), logger)
|
logger,
|
||||||
|
sqlalchemy_string=dotenv_env.get(
|
||||||
|
"DATABASE_URI", getenv("DATABASE_URI", None)
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
logger.info("Scheduler started ...")
|
if INTEGRATION in (
|
||||||
|
"Swarm",
|
||||||
# Checking if the argument variables is true.
|
"Kubernetes",
|
||||||
if args.variables:
|
"Autoconf",
|
||||||
logger.info(f"Variables : {tmp_variables_path}")
|
):
|
||||||
|
while not db.is_autoconf_loaded():
|
||||||
# Read env file
|
|
||||||
env = dotenv_env.copy()
|
|
||||||
|
|
||||||
db = Database(
|
|
||||||
logger,
|
|
||||||
sqlalchemy_string=env.get("DATABASE_URI", getenv("DATABASE_URI", None)),
|
|
||||||
)
|
|
||||||
|
|
||||||
while not db.is_initialized():
|
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"Database is not initialized, retrying in 5s ...",
|
"Autoconf is not loaded yet in the database, retrying in 5s ...",
|
||||||
)
|
)
|
||||||
sleep(5)
|
sleep(5)
|
||||||
|
elif (
|
||||||
|
not tmp_variables_path.exists()
|
||||||
|
or not nginx_variables_path.exists()
|
||||||
|
or (
|
||||||
|
tmp_variables_path.read_text(encoding="utf-8")
|
||||||
|
!= nginx_variables_path.read_text(encoding="utf-8")
|
||||||
|
)
|
||||||
|
or db.is_initialized()
|
||||||
|
and db.get_config() != dotenv_env
|
||||||
|
):
|
||||||
|
# run the config saver
|
||||||
|
proc = subprocess_run(
|
||||||
|
[
|
||||||
|
"python3",
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
if proc.returncode != 0:
|
||||||
|
logger.error(
|
||||||
|
"Config saver failed, configuration will not work as expected...",
|
||||||
|
)
|
||||||
|
|
||||||
db_configs = db.get_custom_configs()
|
while not db.is_initialized():
|
||||||
else:
|
logger.warning(
|
||||||
# Read from database
|
"Database is not initialized, retrying in 5s ...",
|
||||||
INTEGRATION = "Docker"
|
)
|
||||||
integration_path = Path(sep, "usr", "share", "bunkerweb", "INTEGRATION")
|
sleep(5)
|
||||||
if integration_path.is_file():
|
|
||||||
INTEGRATION = integration_path.read_text(encoding="utf-8").strip()
|
|
||||||
|
|
||||||
del integration_path
|
env = db.get_config()
|
||||||
|
while not db.is_first_config_saved() or not env:
|
||||||
|
logger.warning(
|
||||||
|
"Database doesn't have any config saved yet, retrying in 5s ...",
|
||||||
|
)
|
||||||
|
sleep(5)
|
||||||
|
env = db.get_config()
|
||||||
|
|
||||||
SCHEDULER.set_integration(INTEGRATION)
|
env["DATABASE_URI"] = db.database_uri
|
||||||
|
|
||||||
|
# Instantiate scheduler
|
||||||
|
SCHEDULER = JobScheduler(env.copy() | environ.copy(), logger, INTEGRATION)
|
||||||
|
|
||||||
|
if INTEGRATION in ("Swarm", "Kubernetes", "Autoconf", "Docker"):
|
||||||
# Automatically setup the scheduler apis
|
# Automatically setup the scheduler apis
|
||||||
SCHEDULER.auto_setup()
|
SCHEDULER.auto_setup()
|
||||||
|
|
||||||
db = Database(
|
logger.info("Scheduler started ...")
|
||||||
logger,
|
|
||||||
sqlalchemy_string=getenv("DATABASE_URI", None),
|
|
||||||
)
|
|
||||||
|
|
||||||
if INTEGRATION in (
|
|
||||||
"Swarm",
|
|
||||||
"Kubernetes",
|
|
||||||
"Autoconf",
|
|
||||||
):
|
|
||||||
while not db.is_autoconf_loaded():
|
|
||||||
logger.warning(
|
|
||||||
"Autoconf is not loaded yet in the database, retrying in 5s ...",
|
|
||||||
)
|
|
||||||
sleep(5)
|
|
||||||
elif not tmp_variables_path.is_file() or db.get_config() != dotenv_env:
|
|
||||||
# run the config saver
|
|
||||||
proc = subprocess_run(
|
|
||||||
[
|
|
||||||
"python",
|
|
||||||
join(sep, "usr", "share", "bunkerweb", "gen", "save_config.py"),
|
|
||||||
"--settings",
|
|
||||||
join(sep, "usr", "share", "bunkerweb", "settings.json"),
|
|
||||||
],
|
|
||||||
stdin=DEVNULL,
|
|
||||||
stderr=STDOUT,
|
|
||||||
check=False,
|
|
||||||
)
|
|
||||||
if proc.returncode != 0:
|
|
||||||
logger.error(
|
|
||||||
"Config saver failed, configuration will not work as expected...",
|
|
||||||
)
|
|
||||||
|
|
||||||
while not db.is_initialized():
|
|
||||||
logger.warning(
|
|
||||||
"Database is not initialized, retrying in 5s ...",
|
|
||||||
)
|
|
||||||
sleep(5)
|
|
||||||
|
|
||||||
env = db.get_config()
|
|
||||||
while not db.is_first_config_saved() or not env:
|
|
||||||
logger.warning(
|
|
||||||
"Database doesn't have any config saved yet, retrying in 5s ...",
|
|
||||||
)
|
|
||||||
sleep(5)
|
|
||||||
env = db.get_config()
|
|
||||||
|
|
||||||
env["DATABASE_URI"] = db.get_database_uri()
|
|
||||||
|
|
||||||
# Checking if any custom config has been created by the user
|
# Checking if any custom config has been created by the user
|
||||||
custom_configs = []
|
custom_configs = []
|
||||||
|
@ -367,13 +370,21 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
logger.info("Executing scheduler ...")
|
logger.info("Executing scheduler ...")
|
||||||
|
|
||||||
GENERATE = not tmp_variables_path.exists() or env != dotenv_env
|
GENERATE = (
|
||||||
|
env != dotenv_env
|
||||||
|
or not tmp_variables_path.exists()
|
||||||
|
or not nginx_variables_path.exists()
|
||||||
|
or (
|
||||||
|
tmp_variables_path.read_text(encoding="utf-8")
|
||||||
|
!= nginx_variables_path.read_text(encoding="utf-8")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
del dotenv_env
|
del dotenv_env
|
||||||
|
|
||||||
if not GENERATE:
|
if not GENERATE:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"Looks like BunkerWeb configuration is already generated, will not GENERATE it again ..."
|
"Looks like BunkerWeb configuration is already generated, will not generate it again ..."
|
||||||
)
|
)
|
||||||
|
|
||||||
FIRST_RUN = True
|
FIRST_RUN = True
|
||||||
|
@ -464,10 +475,7 @@ if __name__ == "__main__":
|
||||||
"Config generator failed, configuration will not work as expected...",
|
"Config generator failed, configuration will not work as expected...",
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
copy(
|
copy(str(nginx_variables_path), str(tmp_variables_path))
|
||||||
join(sep, "etc", "nginx", "variables.env"),
|
|
||||||
str(tmp_variables_path),
|
|
||||||
)
|
|
||||||
|
|
||||||
if SCHEDULER.apis:
|
if SCHEDULER.apis:
|
||||||
# send nginx configs
|
# send nginx configs
|
||||||
|
|
Loading…
Reference in a new issue