use of com.openshift.client.IOpenShiftSSHKey in project fabric8 by jboss-fuse.
the class OpenShiftDeployAgent method onConfigurationChanged.
protected void onConfigurationChanged() {
LOGGER.info("Configuration has changed; so checking the Fabric managed Java cartridges on OpenShift are up to date");
Container[] containers = fabricService.get().getContainers();
for (Container container : containers) {
Profile effectiveProfile = Profiles.getEffectiveProfile(fabricService.get(), container.getOverlayProfile());
Map<String, String> openshiftConfiguration = effectiveProfile.getConfiguration(OpenShiftConstants.OPENSHIFT_PID);
if (openshiftConfiguration != null) {
DeploymentUpdater deployTask = null;
try {
deployTask = createDeployTask(container, openshiftConfiguration);
} catch (MalformedURLException e) {
LOGGER.error("Failed to create DeploymentUpdater. " + e, e);
}
if (deployTask != null && OpenShiftUtils.isFabricManaged(openshiftConfiguration)) {
String containerId = container.getId();
IOpenShiftConnection connection = OpenShiftUtils.createConnection(container);
CreateOpenshiftContainerOptions options = OpenShiftUtils.getCreateOptions(container);
if (connection == null || options == null) {
LOGGER.warn("Ignoring container which has no openshift connection or options. connection: " + connection + " options: " + options);
} else {
try {
IApplication application = OpenShiftUtils.getApplication(container, connection);
if (application != null) {
final String gitUrl = application.getGitUrl();
if (gitUrl != null) {
LOGGER.info("Git URL is " + gitUrl);
final CartridgeGitRepository repo = new CartridgeGitRepository(containerId);
final List<IOpenShiftSSHKey> sshkeys = application.getDomain().getUser().getSSHKeys();
final CredentialsProvider credentials = new CredentialsProvider() {
@Override
public boolean supports(CredentialItem... items) {
return true;
}
@Override
public boolean isInteractive() {
return true;
}
@Override
public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem {
LOGGER.info("Credential request " + uri + " items " + Arrays.asList(items));
int i = -1;
for (CredentialItem item : items) {
if (item instanceof CredentialItem.StringType) {
CredentialItem.StringType stringType = (CredentialItem.StringType) item;
int idx = ++i < sshkeys.size() ? i : 0;
if (idx < sshkeys.size()) {
IOpenShiftSSHKey sshKey = sshkeys.get(idx);
String passphrase = sshKey.getPublicKey();
LOGGER.info("For item " + item + " index " + i + " using passphrase: " + passphrase);
stringType.setValue(passphrase);
} else {
LOGGER.warn("No ssh keys we can pass into git!");
}
continue;
} else {
LOGGER.warn("Unknown CredentialItem " + item);
}
}
return true;
}
};
final DeploymentUpdater finalDeployTask = deployTask;
SshSessionFactoryUtils.useOpenShiftSessionFactory(new Callable<Object>() {
@Override
public Object call() throws Exception {
repo.cloneOrPull(gitUrl, credentials);
finalDeployTask.updateDeployment(repo.getGit(), repo.getLocalRepo(), credentials);
return null;
}
});
}
}
} catch (Exception e) {
LOGGER.error("Failed to update container " + containerId + ". Reason: " + e, e);
} finally {
OpenShiftUtils.close(connection);
}
}
}
}
}
}
Aggregations