$pTo d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl m Z ddlmZ ddl mZ ddlZddlmZ ddlmZ dd lmZ dd lmZ ddlmZ ddlmZ dd lmZ ddlm Z ddl!m"Z"m#Z#m$Z$ ddl%m&Z& e e' Z(dZ)ej* de+de,fd Z-dedee+ fdZ. e/ fdee+ de/e/e+e+f df fdZ0d Z1deddZ2e'dk r e2 dS dS ) zbThis module import peewee_migrate and apply migrations, for Imunify-AV it's entrypoint for service N)Iterable) getLogger)migrator)SqliteExtDatabase)app) configure)Core)Model)Router)systemd_notifier)db) tls_check)write_pid_fileIM360_RESIDENT_PID_PATHcleanup_pid_file)recreate_schema_modelsz/usr/bin/imunify-residentlog_msgreraisec # v K dV dS # t $ r$ t | | |r Y dS w xY w)z Logs error in case of exception. Depending on `reraise`: - re-raise exception and don't include exception info in the log operation - do not re-raise exception and include exception info in the log operation N)exc_info) Exceptionloggererror)r r s L/opt/imunify360/venv/lib/python3.11/site-packages/defence360agent/migrate.pyexc_handlerr ) se W7{333 s *88r migrations_dirsc ~ t | |t }t t _ | dS )z4Apply migrations: restructure db, config files, etc.)r r N)r r r LOGGERrun)r r routers r apply_migrationsr! : s; ' F HO JJLLLLL attached_dbs.c t j t j t j g }|D ]1\ }}t j d||f | | 2 t d t j t j j t j d 5 t dd 5 t! t | ddd n# 1 swxY w Y ddd n# 1 swxY w Y t d t j d 5 t d d 5 t# t | t dd 5 t! t | ddd n# 1 swxY w Y ddd n# 1 swxY w Y ddd n# 1 swxY w Y t j dS # t j w xY w)a> Apply migrations and recreate attached databases. The workflow: 1. Apply migrations 2. Regardless whether the migrations were applied - recreate attached databases 3. If the recreation of the attached databases was successful - apply migrations again - this is done to verify that migrations will successfully apply in future for the recreated databases - the recreation + the migrations in this step are within the same transaction, so databases will only be recreated if the migrations can applied after the recreation. z ATTACH ? AS ?zApplying database migrations... EXCLUSIVEzError applying migrationsF)r Nz Recreating attached databases...z#Error recreating attached databasesTz=Error applying migrations after recreating attached databases)r resetdb_instanceinitr PATHexecute_sqlappendr infor notify AgentState MIGRATINGatomicr r! r close)r r# attached_schemasdb_pathschema_names r prepare_databasesr5 H s2 " OUZ , - -';1GHHH,,,,5666 0 ; EFFF , , ; ;k'/ / / ; ; [/::: ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; 6777 , , ? ?k14/ / / ? ? #;0@AAA ? ? !o>>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?" s )AG' :DC."D.C2 2D5C2 6D9G' D G' D 1G' >G'F07F F0FF0 F!F0$G0F4 4G7F4 8G;G' GG' GG' 'G<c t d| t dt j t j t j t d t j d d S )Nz$Received signal %s in signal_handlerz0waiting %d seconds so that migrations can finishExitingr ) r warningr %SIGNAL_HANDLER_MIGRATION_TIMEOUT_SECStimesleepr, sysexit)sig_s r signal_handlerr@ ~ sn NN93??? NN:2 Jt9::: KK HQKKKKKr" defence360agent) start_pkgr c t j t j t j fD ]}t j |t | dk rt t t j t j | t j j t j t" t$ j t$ j f }| | t/ j t. j j t d t/ j t. j j | dk rqt j d t d t j t@ t@ gtB j" dd z dS t j tB j# tB j# d d $ | gtB j" dd z dS # tJ $ r! | dk rtM t Y dS Y dS w xY w)zoEntry point for Imunify-AV service. Apply migrations, and then replace process with {start_pkg}.run module.zim360.run_resident)targetargszStarting main process...T)exist_okzRun imunify-resident service Nz-mz{})'signalSIGINTSIGTERMSIGHUPr@ r r osumaskr FILE_UMASKrA internalsr reconfigure threadingThreadr5 r MIGRATIONS_DIRSMIGRATIONS_ATTACHED_DBSstartjoinr r- r. READYr, STARTINGGO_FLAG_FILEtouchexecvGO_SERVICE_NAMEr<