Unverified Commit 232494bd authored by Jennifer Graul's avatar Jennifer Graul
Browse files

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.
parent 4028c38d
Loading
Loading
Loading
Loading
+15 −8
Original line number Diff line number Diff line
@@ -603,9 +603,10 @@ in
        ''
      )
      + ''
        # clear cache after update (before migrations)
        OLD_VERSION=$(cat ${cfg.dataDir}/version)
        if [[ $OLD_VERSION != "${package.version}" ]]; then
        # clear cache if package has changed (cache may contain cached paths
        # to the old package)
        OLD_PACKAGE=$(cat ${cfg.dataDir}/package)
        if [[ $OLD_PACKAGE != "${package}" ]]; then
          rm -r ${cfg.dataDir}/cache/*
        fi

@@ -616,16 +617,21 @@ in
          echo "${lib.boolToString cfg.enableOneMinutePolling}" > ${cfg.dataDir}/one_minute_enabled
        fi

        # migrate db
        # migrate db if package version has changed
        # not necessary for every package change
        OLD_VERSION=$(cat ${cfg.dataDir}/version)
        if [[ $OLD_VERSION != "${package.version}" ]]; then
          ${artisanWrapper}/bin/librenms-artisan migrate --force --no-interaction
          echo "${package.version}" > ${cfg.dataDir}/version
        fi

        # regenerate cache after migrations after update
        if [[ $OLD_VERSION != "${package.version}" ]]; then
        # regenerate cache if package has changed
        if [[ $OLD_PACKAGE != "${package}" ]]; then
          ${artisanWrapper}/bin/librenms-artisan view:clear
          ${artisanWrapper}/bin/librenms-artisan optimize:clear
          ${artisanWrapper}/bin/librenms-artisan view:cache
          ${artisanWrapper}/bin/librenms-artisan optimize
          echo "${package.version}" > ${cfg.dataDir}/version
          echo "${package}" > ${cfg.dataDir}/package
        fi
      '';
    };
@@ -693,6 +699,7 @@ in
      "d ${cfg.dataDir}                              0750 ${cfg.user} ${cfg.group} - -"
      "f ${cfg.dataDir}/.env                         0600 ${cfg.user} ${cfg.group} - -"
      "f ${cfg.dataDir}/version                      0600 ${cfg.user} ${cfg.group} - -"
      "f ${cfg.dataDir}/package                      0600 ${cfg.user} ${cfg.group} - -"
      "f ${cfg.dataDir}/one_minute_enabled           0600 ${cfg.user} ${cfg.group} - -"
      "f ${cfg.dataDir}/config.json                  0600 ${cfg.user} ${cfg.group} - -"
      "d ${cfg.dataDir}/storage                      0700 ${cfg.user} ${cfg.group} - -"