*vh dZddlZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddl mZddlZddlmZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlm Z ddl!m"Z"m#Z#m$Z$ddl%m&Z&e e'Z(dZ)ej*de+de,fdZ-dedee+fdZ.e/fdee+de/e/e+e+fdffdZ0dZ1deddZ2e'dkr e2dSdS) zbThis module import peewee_migrate and apply migrations, for Imunify-AV it's entrypoint for serviceN)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#vK dVdS#t$r$t|| |rYdSwxYw)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)rrs L/opt/imunify360/venv/lib/python3.11/site-packages/defence360agent/migrate.py exc_handlerr)se   W7{ 333      s  *88r migrations_dirsc~t||t}tt_|dS)z4Apply migrations: restructure db, config files, etc.)rrN)r rrLOGGERrun)r rrouters rapply_migrationsr!:s; 'FHO JJLLLLL attached_dbs.ctjtjtjg}|D]1\}}tjd||f||2 t dtj tj j tjd5tdd5t!t|dddn #1swxYwYdddn #1swxYwYt dtjd5td d 5t#t|td d 5t!t|dddn #1swxYwYdddn #1swxYwYdddn #1swxYwYtjdS#tjwxYw) 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)rNz Recreating attached databases...z#Error recreating attached databasesTz=Error applying migrations after recreating attached databases)rreset db_instanceinitr PATH execute_sqlappendrinfor notify AgentState MIGRATINGatomicrr!rclose)rr#attached_schemasdb_path schema_names rprepare_databasesr5Hs2"OUZ    ,--';1GHHH ,,,, 5666 0 ; EFFF   , , ; ;k '/ / /  ; ; [/ : : : ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;  6777   , , ? ?k 14/ / / ? ? #;0@ A A A ? ? !o>>>  ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"  s)AG':D C." D.C2 2D5C2 6D9 G'D  G' D  1G'>G'F07F F0F F0 F !F0$ G0F4 4G7F4 8G; G'G  G'G G''G<ctd|tdtjt jtjtdtjddS)Nz$Received signal %s in signal_handlerz0waiting %d seconds so that migrations can finishExitingr) rwarningr %SIGNAL_HANDLER_MIGRATION_TIMEOUT_SECStimesleepr,sysexit)sig_s rsignal_handlerr@~sn NN93??? NN: 2 Jt9::: KK HQKKKKKr"defence360agent) start_pkgrctjtjtjfD]}tj|t |dkrt t tjtj |tj j tjt"t$jt$jf}||t/jt.jjtdt/jt.jj|dkrqtjdtdtjt@t@gtBj"ddzdStjtBj#tBj#d d $|gtBj"ddzdS#tJ$r!|dkrtMt YdSYdSwxYw) 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 serviceNz-mz{})'signalSIGINTSIGTERMSIGHUPr@rrosumaskr FILE_UMASKrA internalsr reconfigure threadingThreadr5rMIGRATIONS_DIRSMIGRATIONS_ATTACHED_DBSstartjoinr r-r.READYr,STARTING GO_FLAG_FILEtouchexecvGO_SERVICE_NAMEr<argv executableformatrr)rBrr>migration_threads rrrs* v~v}=++ c>****"6 , , , 2 3 3 3 !!! !(44666$+$%s'BC        0 ; ABBB ./// 0 ; DEEE , , ,   # #T # 2 2 2 KK6 7 7 7 H#(122,      Ht{{9'='=>!""M      666 , , , 4 5 5 5 5 5 5 - , ,6sFH-AH--$II__main__)3__doc__ contextlibrLr<rHrQr:collections.abcrloggingrpeewee_migraterplayhouse.sqlite_extr defence360agent.internals.loggerrAdefence360agent.applicationr$defence360agent.application.settingsr defence360agent.contracts.configr r defence360agent.routerr defence360agent.subsysr defence360agent.model.instancer r'defence360agent.modelrdefence360agent.utilsrrrdefence360agent.utils.check_dbr__name__rr\contextmanagerstrboolrr!tupler5r@rr"rrxs  $$$$$$######222222''''++++++::::::111111222222))))))333333<<<<<<++++++  8  -   t     * Xc]     1622c]2c3h,-2222l')(6(6(6(6(6V zCEEEEEr"