Skip to content

Commit 232494b

Browse files
committed
nixos/librenms: regenerate caches after every package change
The LibreNMS cache may contain paths to the old package and may break when the old package is removed. So it is not enough to clear the cache only on version updates, as the package will also change when build inputs change. This commit updates the setup script to regenerate the cache on every package change. In addition, it now only performs migrations when the package version has changed, since the migrations only change on version updates and don't need to be applied on every package change.
1 parent 4028c38 commit 232494b

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

nixos/modules/services/monitoring/librenms.nix

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -603,9 +603,10 @@ in
603603
''
604604
)
605605
+ ''
606-
# clear cache after update (before migrations)
607-
OLD_VERSION=$(cat ${cfg.dataDir}/version)
608-
if [[ $OLD_VERSION != "${package.version}" ]]; then
606+
# clear cache if package has changed (cache may contain cached paths
607+
# to the old package)
608+
OLD_PACKAGE=$(cat ${cfg.dataDir}/package)
609+
if [[ $OLD_PACKAGE != "${package}" ]]; then
609610
rm -r ${cfg.dataDir}/cache/*
610611
fi
611612
@@ -616,16 +617,21 @@ in
616617
echo "${lib.boolToString cfg.enableOneMinutePolling}" > ${cfg.dataDir}/one_minute_enabled
617618
fi
618619
619-
# migrate db
620-
${artisanWrapper}/bin/librenms-artisan migrate --force --no-interaction
621-
622-
# regenerate cache after migrations after update
620+
# migrate db if package version has changed
621+
# not necessary for every package change
622+
OLD_VERSION=$(cat ${cfg.dataDir}/version)
623623
if [[ $OLD_VERSION != "${package.version}" ]]; then
624+
${artisanWrapper}/bin/librenms-artisan migrate --force --no-interaction
625+
echo "${package.version}" > ${cfg.dataDir}/version
626+
fi
627+
628+
# regenerate cache if package has changed
629+
if [[ $OLD_PACKAGE != "${package}" ]]; then
624630
${artisanWrapper}/bin/librenms-artisan view:clear
625631
${artisanWrapper}/bin/librenms-artisan optimize:clear
626632
${artisanWrapper}/bin/librenms-artisan view:cache
627633
${artisanWrapper}/bin/librenms-artisan optimize
628-
echo "${package.version}" > ${cfg.dataDir}/version
634+
echo "${package}" > ${cfg.dataDir}/package
629635
fi
630636
'';
631637
};
@@ -693,6 +699,7 @@ in
693699
"d ${cfg.dataDir} 0750 ${cfg.user} ${cfg.group} - -"
694700
"f ${cfg.dataDir}/.env 0600 ${cfg.user} ${cfg.group} - -"
695701
"f ${cfg.dataDir}/version 0600 ${cfg.user} ${cfg.group} - -"
702+
"f ${cfg.dataDir}/package 0600 ${cfg.user} ${cfg.group} - -"
696703
"f ${cfg.dataDir}/one_minute_enabled 0600 ${cfg.user} ${cfg.group} - -"
697704
"f ${cfg.dataDir}/config.json 0600 ${cfg.user} ${cfg.group} - -"
698705
"d ${cfg.dataDir}/storage 0700 ${cfg.user} ${cfg.group} - -"

0 commit comments

Comments
 (0)