use of org.jclouds.compute.domain.ComputeMetadata in project whirr by apache.
the class ByonClusterAction method doAction.
@Override
protected void doAction(Map<InstanceTemplate, ClusterActionEvent> eventMap) throws IOException, InterruptedException {
final Collection<Future<ExecResponse>> futures = Sets.newHashSet();
List<NodeMetadata> nodes = Lists.newArrayList();
List<NodeMetadata> usedNodes = Lists.newArrayList();
int numberAllocated = 0;
Set<Instance> allInstances = Sets.newLinkedHashSet();
for (Entry<InstanceTemplate, ClusterActionEvent> entry : eventMap.entrySet()) {
final ClusterSpec clusterSpec = entry.getValue().getClusterSpec();
final StatementBuilder statementBuilder = entry.getValue().getStatementBuilder();
if (statementBuilder.isEmpty()) {
// skip
continue;
}
final ComputeServiceContext computeServiceContext = getCompute().apply(clusterSpec);
final ComputeService computeService = computeServiceContext.getComputeService();
LoginCredentials credentials = LoginCredentials.builder().user(clusterSpec.getClusterUser()).privateKey(clusterSpec.getPrivateKey()).build();
final RunScriptOptions options = overrideLoginCredentials(credentials);
if (numberAllocated == 0) {
for (ComputeMetadata compute : computeService.listNodes()) {
if (!(compute instanceof NodeMetadata)) {
throw new IllegalArgumentException("Not an instance of NodeMetadata: " + compute);
}
nodes.add((NodeMetadata) compute);
}
}
int num = entry.getKey().getNumberOfInstances();
Predicate<NodeMetadata> unused = not(in(usedNodes));
// TODO: This seems very fragile and a bug. It is not required that someone passes a hardware id,
// so this is likely to break badly. Even if there was, why do we assume it is splittable?!
// this logic should be refactored or removed ASAP
Predicate<NodeMetadata> instancePredicate = Predicates.alwaysTrue();
if (entry.getKey().getTemplate() != null) {
String hardwareId = entry.getKey().getTemplate().getHardwareId();
if (hardwareId != null)
instancePredicate = new TagsPredicate(StringUtils.split(hardwareId));
}
List<NodeMetadata> templateNodes = Lists.newArrayList(filter(nodes, and(unused, instancePredicate)));
if (templateNodes.size() < num) {
LOG.warn("Not enough nodes available for template " + StringUtils.join(entry.getKey().getRoles(), "+"));
}
templateNodes = templateNodes.subList(0, num);
usedNodes.addAll(templateNodes);
numberAllocated = usedNodes.size();
Set<Instance> templateInstances = getInstances(credentials, entry.getKey().getRoles(), templateNodes);
allInstances.addAll(templateInstances);
for (final Instance instance : templateInstances) {
futures.add(runStatementOnInstanceInCluster(statementBuilder, instance, clusterSpec, options));
}
}
for (Future<ExecResponse> future : futures) {
try {
future.get();
} catch (ExecutionException e) {
throw new IOException(e.getCause());
}
}
if (action.equals(ClusterActionHandler.BOOTSTRAP_ACTION)) {
Cluster cluster = new Cluster(allInstances);
for (ClusterActionEvent event : eventMap.values()) {
event.setCluster(cluster);
}
}
}
use of org.jclouds.compute.domain.ComputeMetadata in project ignite by apache.
the class TcpDiscoveryCloudIpFinder method initComputeService.
/**
* Initializes Apache jclouds compute service.
*/
private void initComputeService() {
if (initGuard.compareAndSet(false, true))
try {
if (provider == null)
throw new IgniteSpiException("Cloud provider is not set.");
if (identity == null)
throw new IgniteSpiException("Cloud identity is not set.");
if (credential != null && credentialPath != null)
throw new IgniteSpiException("Both credential and credentialPath are set. Use only one method.");
if (credentialPath != null)
credential = getCredentialFromFile();
try {
ContextBuilder ctxBuilder = ContextBuilder.newBuilder(provider);
ctxBuilder.credentials(identity, credential);
Properties properties = new Properties();
properties.setProperty(Constants.PROPERTY_SO_TIMEOUT, JCLOUD_CONNECTION_TIMEOUT);
properties.setProperty(Constants.PROPERTY_CONNECTION_TIMEOUT, JCLOUD_CONNECTION_TIMEOUT);
if (!F.isEmpty(regions))
properties.setProperty(LocationConstants.PROPERTY_REGIONS, keysSetToStr(regions));
if (!F.isEmpty(zones))
properties.setProperty(LocationConstants.PROPERTY_ZONES, keysSetToStr(zones));
ctxBuilder.overrides(properties);
computeService = ctxBuilder.buildView(ComputeServiceContext.class).getComputeService();
if (!F.isEmpty(zones) || !F.isEmpty(regions)) {
nodesFilter = new Predicate<ComputeMetadata>() {
@Override
public boolean apply(ComputeMetadata computeMetadata) {
String region = null;
String zone = null;
Location location = computeMetadata.getLocation();
while (location != null) {
switch(location.getScope()) {
case ZONE:
zone = location.getId();
break;
case REGION:
region = location.getId();
break;
}
location = location.getParent();
}
if (regions != null && region != null && !regions.contains(region))
return false;
if (zones != null && zone != null && !zones.contains(zone))
return false;
return true;
}
};
}
} catch (Exception e) {
throw new IgniteSpiException("Failed to connect to the provider: " + provider, e);
}
} finally {
initLatch.countDown();
}
else {
try {
U.await(initLatch);
} catch (IgniteInterruptedCheckedException e) {
throw new IgniteSpiException("Thread has been interrupted.", e);
}
if (computeService == null)
throw new IgniteSpiException("Ip finder has not been initialized properly.");
}
}
use of org.jclouds.compute.domain.ComputeMetadata in project hive by apache.
the class CloudComputeService method createFilterPTestPredicate.
static Predicate<ComputeMetadata> createFilterPTestPredicate(final String groupName, final String groupTag) {
return new Predicate<ComputeMetadata>() {
@Override
public boolean apply(ComputeMetadata computeMetadata) {
NodeMetadata nodeMetadata = (NodeMetadata) computeMetadata;
return nodeMetadata.getStatus() == Status.RUNNING && isPTestHost(nodeMetadata);
}
private boolean isPTestHost(NodeMetadata node) {
String result = "false non-ptest host";
if (groupName.equalsIgnoreCase(node.getGroup())) {
result = "true due to group " + groupName;
return true;
}
if (Strings.nullToEmpty(node.getName()).startsWith(groupName)) {
result = "true due to name " + groupName;
return true;
}
if (node.getTags().contains(groupTag)) {
result = "true due to tag " + groupName;
return true;
}
LOG.debug("Found node: " + node + ", Result: " + result);
return false;
}
};
}
use of org.jclouds.compute.domain.ComputeMetadata in project acceptance-test-harness by jenkinsci.
the class JcloudsMachineProvider method getRunningInstances.
private Set<NodeMetadata> getRunningInstances() {
logger.info(String.format("Check if we already got running machines in the security group: %s... ", getGroupName()));
Set<? extends NodeMetadata> nodeMetadatas = computeService.listNodesDetailsMatching(new Predicate<ComputeMetadata>() {
@Override
public boolean apply(ComputeMetadata computeMetadata) {
return true;
}
});
Set<NodeMetadata> filteredNodes = new HashSet<>();
for (NodeMetadata nm : nodeMetadatas) {
if (getGroupName().equals(nm.getGroup()) && nm.getStatus() == NodeMetadata.Status.RUNNING) {
logger.info(String.format("Found running machine: %s", getGroupName()));
filteredNodes.add(nm);
}
}
return filteredNodes;
}
use of org.jclouds.compute.domain.ComputeMetadata in project SimianArmy by Netflix.
the class AWSClient method getJcloudsNode.
private NodeMetadata getJcloudsNode(ComputeService computeService, String jcloudsId) {
// Work around a jclouds bug / documentation issue...
// TODO: Figure out what's broken, and eliminate this function
// This should work (?):
// Set<NodeMetadata> nodes = computeService.listNodesByIds(Collections.singletonList(jcloudsId));
Set<NodeMetadata> nodes = Sets.newHashSet();
for (ComputeMetadata n : computeService.listNodes()) {
if (jcloudsId.equals(n.getId())) {
nodes.add((NodeMetadata) n);
}
}
if (nodes.isEmpty()) {
LOGGER.warn("Unable to find jclouds node: {}", jcloudsId);
for (ComputeMetadata n : computeService.listNodes()) {
LOGGER.info("Did find node: {}", n);
}
throw new IllegalStateException("Unable to find node using jclouds: " + jcloudsId);
}
NodeMetadata node = Iterables.getOnlyElement(nodes);
return node;
}
Aggregations