use of org.jboss.fuse.patch.management.Pending in project fuse-karaf by jboss-fuse.
the class GitPatchManagementServiceImpl method checkPendingPatches.
@Override
public void checkPendingPatches() {
File[] pendingPatches = patchesDir.listFiles(pathname -> pathname.exists() && pathname.getName().endsWith(".pending"));
if (pendingPatches == null || pendingPatches.length == 0) {
return;
}
final String dataCache = systemContext.getProperty("org.osgi.framework.storage");
for (File pending : pendingPatches) {
try {
Pending what = Pending.valueOf(FileUtils.readFileToString(pending, "UTF-8"));
final String prefix = what == Pending.ROLLUP_INSTALLATION ? "install" : "rollback";
String name = pending.getName().replaceFirst("\\.pending$", "");
if (isStandaloneChild()) {
if (name.endsWith("." + System.getProperty("karaf.name") + ".patch")) {
name = name.replaceFirst("\\." + System.getProperty("karaf.name"), "");
} else {
continue;
}
}
File patchFile = new File(pending.getParentFile(), name);
if (!patchFile.isFile()) {
Activator.log(LogService.LOG_INFO, "Ignoring patch result file: " + patchFile.getName());
continue;
}
PatchData patchData = PatchData.load(new FileInputStream(patchFile));
Patch patch = loadPatch(new PatchDetailsRequest(patchData.getId()));
String dataFilesName = patchData.getId() + ".datafiles";
if (isStandaloneChild()) {
dataFilesName = patchData.getId() + "." + System.getProperty("karaf.name") + ".datafiles";
}
final File dataFilesBackupDir = new File(pending.getParentFile(), dataFilesName);
final Properties backupProperties = new Properties();
FileInputStream inStream = new FileInputStream(new File(dataFilesBackupDir, "backup-" + prefix + ".properties"));
backupProperties.load(inStream);
Utils.closeQuietly(inStream);
// maybe one of those bundles has data directory to restore?
for (Bundle b : systemContext.getBundles()) {
if (b.getSymbolicName() != null) {
String key = String.format("%s$$%s", stripSymbolicName(b.getSymbolicName()), b.getVersion().toString());
if (backupProperties.containsKey(key)) {
String backupDirName = backupProperties.getProperty(key);
File backupDir = new File(dataFilesBackupDir, prefix + "/" + backupDirName + "/data");
restoreDataDirectory(dataCache, b, backupDir);
// we no longer want to restore this dir
backupProperties.remove(key);
}
}
}
// 2. We can however have more bundle data backups - we'll restore them after each bundle
// is INSTALLED and we'll use listener for this
BundleListener bundleListener = new SynchronousBundleListener() {
@Override
public void bundleChanged(BundleEvent event) {
Bundle b = event.getBundle();
if (event.getType() == BundleEvent.INSTALLED && b.getSymbolicName() != null) {
String key = String.format("%s$$%s", stripSymbolicName(b.getSymbolicName()), b.getVersion().toString());
if (backupProperties.containsKey(key)) {
String backupDirName = backupProperties.getProperty(key);
File backupDir = new File(dataFilesBackupDir, prefix + "/" + backupDirName + "/data");
restoreDataDirectory(dataCache, b, backupDir);
}
}
}
};
systemContext.addBundleListener(bundleListener);
pendingPatchesListeners.put(patchData.getId(), bundleListener);
} catch (Exception e) {
Activator.log(LogService.LOG_ERROR, null, e.getMessage(), e, true);
}
}
}
use of org.jboss.fuse.patch.management.Pending in project fuse-karaf by jboss-fuse.
the class PatchServiceImpl method resumePendingPatchTasks.
/**
* Upon startup (activation), we check if there are any *.patch.pending files. if yes, we're finishing the
* installation
*/
private void resumePendingPatchTasks() {
LOG.info("Performing \"resume pending patch tasks\"");
try {
File[] pendingPatches = patchDir.listFiles(pathname -> pathname.exists() && pathname.getName().endsWith(".pending"));
if (pendingPatches == null || pendingPatches.length == 0) {
return;
}
for (File pending : pendingPatches) {
Pending what = Pending.valueOf(FileUtils.readFileToString(pending, "UTF-8"));
String name = pending.getName().replaceFirst("\\.pending$", "");
if (patchManagement.isStandaloneChild()) {
if (name.endsWith("." + System.getProperty("karaf.name") + ".patch")) {
name = name.replaceFirst("\\." + System.getProperty("karaf.name"), "");
} else {
continue;
}
}
File patchFile = new File(pending.getParentFile(), name);
if (!patchFile.isFile()) {
System.out.println("Ignoring patch result file: " + patchFile.getName());
continue;
}
PatchData patchData = PatchData.load(new FileInputStream(patchFile));
Patch patch = patchManagement.loadPatch(new PatchDetailsRequest(patchData.getId()));
System.out.printf("Resume %s of %spatch \"%s\"%n", what == Pending.ROLLUP_INSTALLATION ? "installation" : "rollback", patch.getPatchData().isRollupPatch() ? "rollup " : "", patch.getPatchData().getId());
PatchResult result = patch.getResult();
if (patchManagement.isStandaloneChild()) {
result = result.getChildPatches().get(System.getProperty("karaf.name"));
if (result == null) {
System.out.println("Ignoring patch result file: " + patchFile.getName());
continue;
}
}
// feature time
Set<String> newRepositories = new LinkedHashSet<>();
Set<String> features = new LinkedHashSet<>();
for (FeatureUpdate featureUpdate : result.getFeatureUpdates()) {
if (featureUpdate.getName() == null && featureUpdate.getPreviousRepository() != null) {
// feature was not shipped by patch
newRepositories.add(featureUpdate.getPreviousRepository());
} else if (featureUpdate.getNewRepository() == null) {
// feature was not changed by patch
newRepositories.add(featureUpdate.getPreviousRepository());
features.add(String.format("%s|%s", featureUpdate.getName(), featureUpdate.getPreviousVersion()));
} else {
// feature was shipped by patch
if (what == Pending.ROLLUP_INSTALLATION) {
newRepositories.add(featureUpdate.getNewRepository());
features.add(String.format("%s|%s", featureUpdate.getName(), featureUpdate.getNewVersion()));
} else {
newRepositories.add(featureUpdate.getPreviousRepository());
features.add(String.format("%s|%s", featureUpdate.getName(), featureUpdate.getPreviousVersion()));
}
}
}
System.out.println("Restoring feature repositories");
for (String repo : newRepositories) {
try {
URI repositoryUri = URI.create(repo);
if (featuresService.getRepository(repositoryUri) == null) {
System.out.println("Restoring feature repository: " + repo);
featuresService.addRepository(repositoryUri);
}
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace(System.err);
System.err.flush();
}
}
Set<String> installedFeatures = null;
try {
installedFeatures = Arrays.stream(featuresService.listInstalledFeatures()).map(f -> String.format("%s|%s", f.getName(), f.getVersion())).collect(Collectors.toSet());
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace(System.err);
System.err.flush();
}
EnumSet<FeaturesService.Option> options = EnumSet.noneOf(FeaturesService.Option.class);
Set<String> toInstall = new LinkedHashSet<>();
System.out.println("Restoring features");
for (String f : features) {
if (installedFeatures == null || !installedFeatures.contains(f)) {
String[] fv = f.split("\\|");
String fid = String.format("%s/%s", fv[0], fv[1]);
System.out.printf("Restoring feature %s%n", fid);
toInstall.add(fid);
}
}
try {
if (!toInstall.isEmpty()) {
featuresService.installFeatures(toInstall, options);
}
System.out.println("Refreshing features service");
featuresService.refreshFeatures(options);
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace(System.err);
System.err.flush();
}
for (BundleUpdate update : result.getBundleUpdates()) {
if (!update.isIndependent()) {
continue;
}
String location = null;
if (update.getNewVersion() == null) {
System.out.printf("Restoring bundle %s from %s%n", update.getSymbolicName(), update.getPreviousLocation());
location = update.getPreviousLocation();
} else {
if (what == Pending.ROLLUP_INSTALLATION) {
System.out.printf("Updating bundle %s from %s%n", update.getSymbolicName(), update.getNewLocation());
location = update.getNewLocation();
} else {
System.out.printf("Downgrading bundle %s from %s%n", update.getSymbolicName(), update.getPreviousLocation());
location = update.getPreviousLocation();
}
}
try {
Bundle b = bundleContext.installBundle(location);
if (update.getStartLevel() > -1) {
b.adapt(BundleStartLevel.class).setStartLevel(update.getStartLevel());
}
switch(update.getState()) {
// ?
case Bundle.UNINSTALLED:
case Bundle.INSTALLED:
case Bundle.STARTING:
case Bundle.STOPPING:
break;
case Bundle.RESOLVED:
// ?bundleContext.getBundle(0L).adapt(org.osgi.framework.wiring.FrameworkWiring.class).resolveBundles(...);
break;
case Bundle.ACTIVE:
b.start();
break;
}
} catch (BundleException e) {
System.err.println(" - " + e.getMessage());
// e.printStackTrace(System.err);
System.err.flush();
}
}
pending.delete();
System.out.printf("%spatch \"%s\" %s successfully%n", patch.getPatchData().isRollupPatch() ? "Rollup " : "", patchData.getId(), what == Pending.ROLLUP_INSTALLATION ? "installed" : "rolled back");
if (what == Pending.ROLLUP_INSTALLATION) {
System.out.printf("Summary of patch %s:%n", patch.getPatchData().getId());
PatchReport report = patch.getResult().getReport();
System.out.printf(" - Bundles updated: %d%n", report.getUpdatedBundles());
System.out.printf(" - Features updated: %d%n", report.getUpdatedFeatures());
System.out.printf(" - Features overriden: %d%n", report.getOverridenFeatures());
System.out.printf("Detailed report: %s%n", new File(patch.getPatchData().getPatchLocation(), patch.getPatchData().getId() + ".patch.result.html").getCanonicalPath());
System.out.flush();
}
if (what == Pending.ROLLUP_ROLLBACK) {
List<String> bases = patch.getResult().getKarafBases();
bases.removeIf(s -> s.startsWith(System.getProperty("karaf.name")));
result.setPending(null);
patch.getResult().store();
if (patch.getResult().getKarafBases().size() == 0) {
File file = new File(patchDir, patchData.getId() + ".patch.result");
file.delete();
}
if (patchManagement.isStandaloneChild()) {
File file = new File(patchDir, patchData.getId() + "." + System.getProperty("karaf.name") + ".patch.result");
if (file.isFile()) {
file.delete();
}
}
}
}
} catch (IOException e) {
LOG.error("Error resuming a patch: " + e.getMessage(), e);
}
}
Aggregations