use of io.fabric8.karaf.checks.Check in project fabric8 by jboss-fuse.
the class ProfileDownloadArtifactsAction method doExecute.
@Override
protected Object doExecute() throws Exception {
Version ver = version != null ? profileService.getVersion(version) : fabricService.getDefaultVersion();
if (ver == null) {
if (version != null) {
System.out.println("version " + version + " does not exist!");
} else {
System.out.println("No default version available!");
}
return null;
}
if (target == null) {
String karafBase = System.getProperty("karaf.base", ".");
target = new File(karafBase, ProfileDownloadArtifactsAction.DEFAULT_TARGET);
}
target.mkdirs();
if (!target.exists()) {
System.out.println("Could not create the target directory " + target);
return null;
}
if (!target.isDirectory()) {
System.out.println("Target is not a directory " + target);
return null;
}
if (executorService == null) {
if (threadPoolSize > 1) {
executorService = Executors.newScheduledThreadPool(threadPoolSize);
} else {
executorService = Executors.newSingleThreadScheduledExecutor();
}
}
ProfileDownloader downloader = new ProfileDownloader(fabricService, target, force, executorService);
downloader.setStopOnFailure(stopOnFailure);
// we do not want to download the files from within the profile itself, only the dependencies
downloader.setDownloadFilesFromProfile(false);
if (verbose) {
downloader.setListener(new ProgressIndicator());
}
if (profile != null) {
Profile profileObject = null;
if (ver.hasProfile(profile)) {
profileObject = ver.getRequiredProfile(profile);
}
if (profileObject == null) {
System.out.println("Source profile " + profile + " not found.");
return null;
}
downloader.downloadProfile(profileObject);
} else {
downloader.downloadVersion(ver);
}
List<String> failedProfileIDs = downloader.getFailedProfileIDs();
System.out.println("Downloaded " + downloader.getProcessedFileCount() + " file(s) to " + target);
if (failedProfileIDs.size() > 0) {
System.out.println("Failed to download these profiles: " + failedProfileIDs + ". Check the logs for details");
}
return null;
}
use of io.fabric8.karaf.checks.Check in project fabric8 by jboss-fuse.
the class ContainerRollbackAction method doExecute.
@Override
protected Object doExecute() throws Exception {
FabricValidations.validateContainerNames(containerIds);
// check and validate version
Version version = profileService.getRequiredVersion(this.version);
if (containerIds == null || containerIds.isEmpty()) {
if (all) {
containerIds = new ArrayList<String>();
for (Container container : fabricService.getContainers()) {
containerIds.add(container.getId());
}
} else {
containerIds = Arrays.asList(fabricService.getCurrentContainer().getId());
}
} else {
if (all) {
throw new IllegalArgumentException("Can not use --all with a list of containers simultaneously");
}
}
List<Container> toRollback = new ArrayList<Container>();
List<Container> same = new ArrayList<Container>();
for (String containerName : containerIds) {
Container container = FabricCommand.getContainer(fabricService, containerName);
// check first that all can rollback
int num = ContainerUpgradeSupport.canRollback(version, container);
if (num < 0) {
throw new IllegalArgumentException("Container " + container.getId() + " has already lower version " + container.getVersion().getId() + " than the requested version " + version.getId() + " to rollback.");
} else if (num == 0) {
// same version
same.add(container);
} else {
// needs rollback
toRollback.add(container);
}
}
// report same version
for (Container container : same) {
System.out.println("Container " + container.getId() + " is already version " + version.getId());
}
// report and do rollbacks
for (Container container : toRollback) {
Version oldVersion = container.getVersion();
// rollback version first
container.setVersion(version);
log.info("Rolled back container {} from {} to {}", new Object[] { container.getId(), oldVersion.getId(), version.getId() });
System.out.println("Rolled back container " + container.getId() + " from version " + oldVersion.getId() + " to " + version.getId());
}
if (all) {
fabricService.setDefaultVersionId(version.getId());
System.out.println("Changed default version to " + version.getId());
}
return null;
}
use of io.fabric8.karaf.checks.Check in project fabric8 by jboss-fuse.
the class FabricGitSummaryAction method afterEachContainer.
@Override
protected void afterEachContainer(Collection<String> names) {
System.out.printf("Scheduled git-summary command to %d containers. Awaiting response(s).\n", names.size());
final CountDownLatch latch = new CountDownLatch(requests.size() + (masterRequest == null ? 0 : 1));
Thread waiter = null;
try {
waiter = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
String currentContainer = null;
try {
// master request
if (masterRequest != null && masterResult == null) {
currentContainer = master;
List<JMXResult> results = fetchResponses(master);
for (JMXResult result : results) {
if (result.getCorrelationId().equals(masterRequest.getId())) {
masterResult = result;
latch.countDown();
break;
}
}
}
// requests for containers
for (Map.Entry<String, JMXRequest> req : requests.entrySet()) {
currentContainer = req.getKey();
List<JMXResult> containerResults = fetchResponses(currentContainer);
for (JMXResult result : containerResults) {
if (result.getCorrelationId().equals(req.getValue().getId())) {
results.put(currentContainer, result);
latch.countDown();
break;
}
}
}
if ((masterRequest == null || masterResult != null) && results.size() == requests.size()) {
break;
} else {
// active waiting - so no need for ZK watchers, etc...
Thread.sleep(1000);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
} catch (Exception e) {
System.err.println("Problem occurred while fetching response from " + currentContainer + " container: " + e.getMessage());
// active waiting - so no need for ZK watchers, etc...
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
}
}
}
});
waiter.start();
boolean finished = latch.await(timeout, TimeUnit.MILLISECONDS);
if (!finished) {
waiter.interrupt();
System.out.println("Timeout waiting for git-summary response");
return;
}
// before printing summary, let's check if there are out of sync containers
Map<String, String> centralGitRepo = new HashMap<>();
Set<String> outOfSyncContainers = new TreeSet<>();
if (masterResult != null) {
for (GitVersion version : ((GitVersions) masterResult.getResponse()).getVersions()) {
centralGitRepo.put(version.getVersion(), version.getSha1());
}
for (String containerName : results.keySet()) {
List<GitVersion> localRepo = ((GitVersions) results.get(containerName).getResponse()).getVersions();
for (GitVersion version : localRepo) {
String ref = centralGitRepo.get(version.getVersion());
if (ref == null) {
// local container knows about version, which is not tracked in central repo
outOfSyncContainers.add(containerName);
} else if (!ref.equals(version.getSha1())) {
// version not in sync
outOfSyncContainers.add(containerName);
}
}
if (localRepo.size() != centralGitRepo.size()) {
// extra or not-in-sync versions handled, so this must mean some central version is not
// available locally
outOfSyncContainers.add(containerName);
}
}
if (outOfSyncContainers.size() > 0) {
System.out.println();
System.out.println("Containers that require synchronization: " + outOfSyncContainers);
System.out.println("Please use \"fabric:git-synchronize\" command");
}
} else {
System.out.println();
System.out.println("Can't determine synchronization status of containers - no master Git repository detected");
}
// now summary time
if (masterResult != null) {
System.out.println();
printVersions("=== Summary for master Git repository (container: " + master + ") ===", ((GitVersions) masterResult.getResponse()).getVersions());
}
System.out.println();
for (String containerName : results.keySet()) {
printVersions("=== Summary for local Git repository (container: " + containerName + ") ===", ((GitVersions) results.get(containerName).getResponse()).getVersions());
System.out.println();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
waiter.interrupt();
System.out.println("Interrupted waiting for git-summary response");
}
}
use of io.fabric8.karaf.checks.Check in project fabric8 by jboss-fuse.
the class ContainerUpgradeAction method doExecute.
@Override
protected Object doExecute() throws Exception {
FabricValidations.validateContainerNames(containerIds);
// check and validate version
Version version = profileService.getRequiredVersion(this.version);
if (containerIds == null || containerIds.isEmpty()) {
if (all) {
containerIds = new ArrayList<String>();
for (Container container : fabricService.getContainers()) {
containerIds.add(container.getId());
}
} else {
containerIds = Arrays.asList(fabricService.getCurrentContainer().getId());
}
} else {
if (all) {
throw new IllegalArgumentException("Can not use --all with a list of containers simultaneously");
}
}
List<Container> toUpgrade = new ArrayList<Container>();
List<Container> same = new ArrayList<Container>();
for (String containerName : containerIds) {
Container container = FabricCommand.getContainer(fabricService, containerName);
// check first that all can upgrade
int num = ContainerUpgradeSupport.canUpgrade(version, container);
if (num < 0) {
throw new IllegalArgumentException("Container " + container.getId() + " already has a higher version " + container.getVersion() + " than the requested version " + version.getId() + ".");
} else if (num == 0) {
// same version
same.add(container);
} else {
// needs upgrade
toUpgrade.add(container);
}
}
// report same version
for (Container container : same) {
System.out.println("Container " + container.getId() + " is already at version " + version.getId());
}
// report and do upgrades
for (Container container : toUpgrade) {
Version oldVersion = container.getVersion();
// upgrade version first
container.setVersion(version);
// get the profile for version 1.1
log.info("Upgraded container {} from {} to {}", new Object[] { container.getId(), oldVersion.getId(), version.getId() });
System.out.println("Upgraded container " + container.getId() + " from version " + oldVersion.getId() + " to " + version.getId());
}
if (all) {
fabricService.setDefaultVersionId(version.getId());
System.out.println("Changed default version to " + version.getId());
}
return null;
}
use of io.fabric8.karaf.checks.Check in project fabric8 by jboss-fuse.
the class CachingGitDataStoreTest method testDataStore.
@Test
public void testDataStore() throws Exception {
// dataStore.getDefaultVersion();
String defaultVersion = null;
assertEquals("defaultVersion", "1.0", defaultVersion);
// now lets import some data - using the old non-git file layout...
String importPath = basedir + "/../fabric8-karaf/src/main/resources/distro/fabric";
if (useOldImportFormat) {
assertFolderExists(importPath);
try {
dataStore.importFromFileSystem(importPath);
} catch (Exception e) {
// Ignore exception about missing url handlers. Not needed for this test anyway.
}
assertHasVersion(defaultVersion);
} else {
String prefix = importPath + "/fabric";
String profileImport = prefix + "/configs/versions/1.0/profiles";
assertFolderExists(profileImport);
// dataStore.importFromFileSystem(new File(profileImport), "fabric",
// "1.0", true);
assertHasVersion(defaultVersion);
}
remote.checkout().setName("1.0").call();
String importedProfile = "example-dozer";
String profile = importedProfile;
assertProfileExists(defaultVersion, profile);
// assertFolderExists("Should have imported an mq/ReadMe.md file!",
// getLocalGitFile("fabric/profiles/mq/ReadMe.md"));
String version = "1.1";
assertCreateVersion("1.0", version);
assertProfileConfiguration(version, importedProfile, Constants.AGENT_PID, "attribute.parents", "feature-camel");
assertProfileTextFileConfigurationContains(version, "example-camel-mq", "camel.xml", "http://camel.apache.org/schema/blueprint");
/*
* List<String> fileNames = dataStore.getConfigurationFileNames(version,
* "example-camel-mq"); assertNotNull("Should have some file names",
* fileNames); assertTrue("Should have some file names",
* fileNames.size() > 0); assertTrue("Should contain 'came",
* fileNames.size() > 0);
* assertCollectionContains("configurationFileNames", fileNames,
* "camel.xml");
*/
// lets test the profile attributes
// dataStore.getProfileAttributes(version, importedProfile);
Map<String, String> profileAttributes = Collections.emptyMap();
String parent = profileAttributes.get("parents");
assertEquals(importedProfile + ".profileAttributes[parent]", "feature-camel", parent);
System.out.println("Profile attributes: " + profileAttributes);
String profileAttributeKey = "myKey";
String expectedProfileAttributeValue = "myValue";
// dataStore.setProfileAttribute(version, importedProfile, profileAttributeKey, expectedProfileAttributeValue);
// profileAttributes = dataStore.getProfileAttributes(version, importedProfile);
System.out.println("Profile attributes: " + profileAttributes);
assertMapContains("Profile attribute[" + profileAttributeKey + "]", profileAttributes, profileAttributeKey, expectedProfileAttributeValue);
String hawtioRepoKey = "repository.hawtio";
// dataStore.getConfiguration(version, "hawtio", Constants.AGENT_PID);
Map<String, String> hawtioAttrbutes = Collections.emptyMap();
String currentHawtRepo = hawtioAttrbutes.get(hawtioRepoKey);
System.out.println("Current repository.hawtio: " + currentHawtRepo);
// now lets write via the hawtio API
FabricGitFacade hawtio = new FabricGitFacade();
hawtio.bindGitDataStoreForTesting(dataStore);
hawtio.activateForTesting();
/* String hawtioPropertyFile = "/fabric/profiles/" + dataStore.convertProfileIdToDirectory("hawtio") + "/" + Constants.AGENT_PID + ".properties";
hawtio.write(version, hawtioPropertyFile, "My commit message", "me", "me@apache.org", "# new file\n" + hawtioRepoKey + " = "
+ "mvn\\:io.hawt/hawtio-karaf/myNewVersion/xml/features" + "\n");
*/
// dataStore.getConfiguration(version, "hawtio", Constants.AGENT_PID);
hawtioAttrbutes = Collections.emptyMap();
String actual = hawtioAttrbutes.get(hawtioRepoKey);
assertEquals("should have found the updated hawtio repo key", "mvn:io.hawt/hawtio-karaf/myNewVersion/xml/features", actual);
// lets check that the file configurations recurses into folders
// Map<String, byte[]> tomcatFileConfigurations = dataStore.getFileConfigurations("1.0", "controller-tomcat");
// assertHasFileConfiguration(tomcatFileConfigurations, "tomcat/conf/server.xml.mvel");
/*
Collection<String> schemas = dataStore.listFiles("1.0", Arrays.asList("example-dozer"), "schemas");
assertNotNull(schemas);
assertContainerEquals("schemas for example-dozer", Arrays.asList("invoice.xsd"), new ArrayList<String>(schemas));
*/
// check we don't accidentally create a profile
String profileNotCreated = "shouldNotBeCreated";
// assertEquals("Should not create profile: " + profileNotCreated, null, dataStore.getProfile(version, profileNotCreated, false));
assertProfileNotExists(defaultVersion, profileNotCreated);
// assertFolderNotExists(getLocalGitFile("fabric/profiles/" +
// dataStore.convertProfileIdToDirectory(profileNotCreated)));
// now lets create some profiles in this new version
String newProfile = "myNewProfile";
// dataStore.createProfile(version, newProfile);
assertProfileExists(version, newProfile);
// lazy create a profile
String anotherNewProfile = "anotherNewProfile";
// dataStore.getProfile(version, anotherNewProfile, true);
assertProfileExists(version, anotherNewProfile);
version = "1.2";
assertCreateVersion("1.1", version);
// check this version has the profile too
assertProfileExists(version, newProfile);
assertProfileExists(version, profile);
// now lets delete a profile
dataStore.deleteProfile(version, newProfile);
assertProfileNotExists(version, newProfile);
// lets check the remote repo
remote.checkout().setName("1.1").call();
assertProfileExists("1.1", profile);
assertProfileExists("1.1", newProfile);
// assertFolderExists(getRemoteGitFile("fabric/profiles/" +
// dataStore.convertProfileIdToDirectory(profile)));
// assertFolderExists(getRemoteGitFile("fabric/profiles/" +
// dataStore.convertProfileIdToDirectory(newProfile)));
remote.checkout().setName("1.2").call();
assertProfileExists("1.2", profile);
assertProfileNotExists("1.2", newProfile);
// assertFolderExists(getRemoteGitFile("fabric/profiles/" +
// dataStore.convertProfileIdToDirectory(profile)));
// assertFolderNotExists(getRemoteGitFile("fabric/profiles/" +
// dataStore.convertProfileIdToDirectory(newProfile)));
remote.checkout().setName("1.0").call();
// assertFolderExists(getRemoteGitFile("fabric/profiles/" +
// dataStore.convertProfileIdToDirectory(profile)));
// assertFolderNotExists(getRemoteGitFile("fabric/profiles/" +
// dataStore.convertProfileIdToDirectory(newProfile)));
// delete version 1.2
assertHasVersion("1.1");
assertHasVersion("1.2");
// dataStore.removeVersion("1.2");
assertHasVersion("1.1");
assertHasNotVersion("1.2");
Collection<String> remoteBranches = RepositoryUtils.getBranches(remote.getRepository());
System.out.println("Remote branches after delete: " + remoteBranches);
}
Aggregations