bundle fix-applier groovy status Jahia 7.3

How to preserve the bundle status when upgrading Jahia

Question

When upgrading Jahia to a version prior to 7.3.1.1, some modules may not start automatically.

 

Answer

The persistent state should be flagged on each bundle as Bundle.STARTING, to ensure that all modules with the state STARTED restart automatically after applying a fix applier to upgrade Jahia.

However, the fix applier for version < 7.3.1.1 did not save this persistent state correctly. 

To prevent this you could run this groovy script from the tools Groovy Console:

import org.apache.commons.io.FileUtils
import org.apache.commons.io.filefilter.IOFileFilter
import org.apache.commons.io.filefilter.TrueFileFilter
import org.jahia.osgi.BundleState
import org.jahia.services.SpringContextSingleton
import org.jahia.services.modulemanager.ModuleManager
import org.jahia.settings.SettingsBean

// https://jira.jahia.org/browse/QA-11200

boolean fixIssues = false


String varFolder = SettingsBean.getInstance().getJahiaVarDiskPath()
File bundlesDeployed = new File(new File(varFolder), "bundles-deployed")
IOFileFilter ioff = new IOFileFilter() {
    @Override
    boolean accept(File file) {
        return "bundle.info".equals(file.getName());
    }

    @Override
    boolean accept(File dir, String name) {
        return true
    }
}

final ModuleManager moduleManager = (ModuleManager) SpringContextSingleton.getBean("ModuleManager");
for (File file : FileUtils.listFiles(bundlesDeployed, ioff, TrueFileFilter.INSTANCE)) {
    List<String> lines = FileUtils.readLines(file)
    String bundleID = lines.get(0)
    String bundleKey = lines.get(1)
    int persistentState = lines.get(2) as int

    try {
        BundleState localState = moduleManager.getLocalState(bundleKey)
        int localStateInt = localState.toInt()
        if (localStateInt != persistentState && localStateInt + persistentState != 6) {
            // if localStateInt + persistentState == 6 , then one state is installed, the other one resolved, let's ignore
            log.warn(String.format("Local state (%s=%s) inconsistent with the persistent state (%s) for the bundle %s (bundle ID %s)", localState, localStateInt, persistentState, bundleKey, bundleID))
            if (fixIssues && localStateInt == 32 && persistentState == 8) {
                moduleManager.start(bundleKey, null)
            }
        }
    } catch (org.jahia.services.modulemanager.ModuleNotFoundException ignored) {
    }
}

This script will list the inconsistent and if you set the fixIssues value to true, then it will change the value so all started bundles should restart after applying a patch. 

This script should be executed before applying a Jahia upgrade.