Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011, the original authors
   *
   * ====================================================================
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * ====================================================================
  */
 
 package org.jclouds.karaf.core;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 
A proxy implementations of the org.jclouds.compute.ComputeService which delegates calls to the underlying impl and notifies NodeListeners about node creation/destruction events.
 
 public class ComputeServiceEventProxy implements ComputeService {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(ComputeServiceEventProxy.class);
 
     private BundleContext bundleContext;
     private ComputeService computeService;
 
     private ServiceTracker listenerTracker;

    
Constructor

Parameters:
bundleContext
computeService
 
     public ComputeServiceEventProxy(BundleContext bundleContextComputeService computeService) {
         this. = bundleContext;
         this. = computeService;
         this. = new ServiceTracker(bundleContextNodeListener.class.getName(), null);
         this..open();
     }
 
     private List<? extends NodeListenergetNodeListeners() {
         List<NodeListenerlisteners = new LinkedList<NodeListener>();
         try {
             listeners.addAll(Arrays.asList((NodeListener[]) .getServices()));
         } catch (Exception ex) {
             .warn("Could not lookup node listeners. Listeners will not receive the last event.");
         }
         return listeners;
     }

    

Returns:
a reference to the context that created this ComputeService.
 
     @Override
     public ComputeServiceContext getContext() {
         return .getContext();
     }

    
Makes a new template builder for this service
 
    @Override
    public TemplateBuilder templateBuilder() {
        return .templateBuilder();
    }

    
Makes a new set of options for running nodes
    @Override
    public TemplateOptions templateOptions() {
        return .templateOptions();
    }

    
The list hardware profiles command shows you the options including virtual cpu count, memory, and disks. cpu count is not a portable quantity across clouds, as they are measured differently. However, it is a good indicator of relative speed within a cloud. memory is measured in megabytes and disks in gigabytes.

Returns:
a map of hardware profiles by ID, conceding that in some clouds the "id" is not used.
    @Override
    public Set<? extends HardwarelistHardwareProfiles() {
        return .listHardwareProfiles();
    }

    
Images define the operating system and metadata related to a node. In some clouds, Images are bound to a specific region, and their identifiers are different across these regions. For this reason, you should consider matching image requirements like operating system family with TemplateBuilder as opposed to choosing an image explicitly. The getImages() command returns a map of images by id.
    @Override
    public Set<? extends ImagelistImages() {
        return .listImages();
    }

    
all nodes available to the current user by id. If possible, the returned set will include org.jclouds.compute.domain.NodeMetadata objects.
    @Override
    public Set<? extends ComputeMetadatalistNodes() {
        return .listNodes();
    }

    
The list locations command returns all the valid locations for nodes. A location has a scope, which is typically region or zone. A region is a general area, like eu-west, where a zone is similar to a datacenter. If a location has a parent, that implies it is within that location. For example a location can be a rack, whose parent is likely to be a zone.
    @Override
    public Set<? extends LocationlistAssignableLocations() {
        return .listAssignableLocations();
    }

    
The compute api treats nodes as a group based on the name you specify. Using this group, you can choose to operate one or many nodes as a logical unit without regard to the implementation details of the cloud.

The set that is returned will include credentials you can use to ssh into the nodes. The "key" part of the credentials is either a password or a private key. You have to inspect the value to determine this.

 if (node.getCredentials().key.startsWith("-----BEGIN RSA PRIVATE KEY-----"))
    // it is a private key, not a password.
 

Note. if all you want to do is execute a script at bootup, you should consider use of the runscript option.

If resources such as security groups are needed, they will be reused or created for you. Inbound port 22 will always be opened up.

Parameters:
group - common identifier to group nodes by, cannot contain hyphens
count - how many to fire up.
template - how to configure the nodes
Returns:
all of the nodes the api was able to launch in a running state.
Throws:
org.jclouds.compute.RunNodesException when there's a problem applying options to nodes. Note that successful and failed nodes are a part of this exception, so be sure to inspect this carefully.
    @Override
    public Set<? extends NodeMetadatacreateNodesInGroup(String groupint countTemplate templatethrows RunNodesException {
        Set<? extends NodeMetadatametadata = .createNodesInGroup(groupcounttemplate);
        try {
            for (NodeListener listener : getNodeListeners()) {
                for (NodeMetadata node : metadata) {
                    listener.nodeCreated(node);
                }
            }
        } catch (Exception ex) {
            .error("Error while notifying node listeners."ex);
        }
        return metadata;
    }

    
Like org.jclouds.compute.ComputeService.createNodesInGroup(java.lang.String,int,org.jclouds.compute.domain.Template), except that the template is default, equivalent to templateBuilder().any().options(templateOptions).
    @Override
    public Set<? extends NodeMetadatacreateNodesInGroup(String groupint countTemplateOptions templateOptionsthrows RunNodesException {
        Set<? extends NodeMetadatametadata = .createNodesInGroup(groupcounttemplateOptions);
        try {
            for (NodeListener listener : getNodeListeners()) {
                for (NodeMetadata node : metadata) {
                    listener.nodeCreated(node);
                }
            }
        } catch (Exception ex) {
            .error("Error while notifying node listeners."ex);
        }
        return metadata;
    }

    
    @Override
    public Set<? extends NodeMetadatacreateNodesInGroup(String groupint countthrows RunNodesException {
        Set<? extends NodeMetadatametadata = .createNodesInGroup(groupcount);
        try {
            for (NodeListener listener : getNodeListeners()) {
                for (NodeMetadata node : metadata) {
                    listener.nodeCreated(node);
                }
            }
        } catch (Exception ex) {
            .error("Error while notifying node listeners."ex);
        }
        return metadata;
    }

    
resume the node from suspended state, given its id.

note

affected nodes may not resume with the same IP address(es)

    @Override
    public void resumeNode(String id) {
        .resumeNode(id);
    }

    
nodes matching the filter are treated as a logical set. Using the resume command, you can save time by resumeing the nodes in parallel.

note

affected nodes may not resume with the same IP address(es)

Throws:
java.lang.UnsupportedOperationException if the underlying provider doesn't support suspend/resume
java.util.NoSuchElementException if no nodes matched the predicate specified
    @Override
    public void resumeNodesMatching(Predicate<NodeMetadatafilter) {
        .resumeNodesMatching(filter);
    }

    
suspend the node, given its id. This will result in suspended state.

note

affected nodes may not resume with the same IP address(es)

Throws:
java.lang.UnsupportedOperationException if the underlying provider doesn't support suspend/resume
    @Override
    public void suspendNode(String id) {
        .suspendNode(id);
    }

    
nodes matching the filter are treated as a logical set. Using the suspend command, you can save time by suspending the nodes in parallel.

note

affected nodes may not resume with the same IP address(es)

Throws:
java.lang.UnsupportedOperationException if the underlying provider doesn't support suspend/resume
java.util.NoSuchElementException if no nodes matched the predicate specified
    @Override
    public void suspendNodesMatching(Predicate<NodeMetadatafilter) {
        .suspendNodesMatching(filter);
    }

    
destroy the node, given its id. If it is the only node in a tag set, the dependent resources will also be destroyed.
    @Override
    public void destroyNode(String id) {
        NodeMetadata node = null;
        try {
            node = .getNodeMetadata(id);
        } catch (Exception ex) {
            .error("Error while retrieving node metadata."ex);
        }
        .destroyNode(id);
        try {
            for (NodeListener listener : getNodeListeners()) {
                listener.nodeDestroyed(node);
            }
        } catch (Exception ex) {
            .error("Error while notifying node listeners."ex);
        }
    }

    
nodes matching the filter are treated as a logical set. Using the delete command, you can save time by removing the nodes in parallel. When the last node in a set is destroyed, any indirect resources it uses, such as keypairs, are also destroyed.

Returns:
list of nodes destroyed
    @Override
    public Set<? extends NodeMetadatadestroyNodesMatching(Predicate<NodeMetadatafilter) {
        Set<? extends NodeMetadatametadata = .destroyNodesMatching(filter);
        try {
            for (NodeListener listener : getNodeListeners()) {
                for (NodeMetadata node : metadata) {
                    listener.nodeCreated(node);
                }
            }
        } catch (Exception ex) {
            .error("Error while notifying node listeners."ex);
        }
        return metadata;
    }

    
reboot the node, given its id.
    @Override
    public void rebootNode(String id) {
        .rebootNode(id);
    }

    
nodes matching the filter are treated as a logical set. Using this command, you can save time by rebooting the nodes in parallel.

Throws:
java.util.NoSuchElementException if no nodes matched the predicate specified
    @Override
    public void rebootNodesMatching(Predicate<NodeMetadatafilter) {
        .rebootNodesMatching(filter);
    }

    
Find a node by its id.
    @Override
    public NodeMetadata getNodeMetadata(String id) {
        return .getNodeMetadata(id);
    }

    
get all nodes including details such as image and ip addresses even if it incurs extra requests to the service.

Parameters:
filter how to select the nodes you are interested in details on.
    @Override
    public Set<? extends NodeMetadatalistNodesDetailsMatching(Predicate<ComputeMetadatafilter) {
        return .listNodesDetailsMatching(filter);
    }

    
    @Override
    public Map<? extends NodeMetadataExecResponserunScriptOnNodesMatching(Predicate<NodeMetadatafilterString runScriptthrows RunScriptOnNodesException {
        return .runScriptOnNodesMatching(filterrunScript);
    }

    
    @Override
    public Map<? extends NodeMetadataExecResponserunScriptOnNodesMatching(Predicate<NodeMetadatafilterStatement runScriptthrows RunScriptOnNodesException {
        return .runScriptOnNodesMatching(filterrunScript);
    }

    
    @Override
    public Map<? extends NodeMetadataExecResponserunScriptOnNodesMatching(Predicate<NodeMetadatafilterString runScriptRunScriptOptions optionsthrows RunScriptOnNodesException {
        return .runScriptOnNodesMatching(filterrunScriptoptions);
    }

    
Run the script on all nodes with the specific predicate.

Parameters:
filter Predicate-based filter to define on which nodes the script is to be executed
runScript statement containing the script to run
options nullable options to how to run the script, whether to override credentials
Returns:
map with node identifiers and corresponding responses
Throws:
java.util.NoSuchElementException if no nodes matched the predicate specified
org.jclouds.compute.RunScriptOnNodesException if anything goes wrong during script execution
See also:
org.jclouds.compute.predicates.NodePredicates.runningInGroup(java.lang.String)
org.jclouds.scriptbuilder.domain.Statements
    @Override
    public Map<? extends NodeMetadataExecResponserunScriptOnNodesMatching(Predicate<NodeMetadatafilterStatement runScriptRunScriptOptions optionsthrows RunScriptOnNodesException {
        return .runScriptOnNodesMatching(filterrunScriptoptions);
    }

    
Run the script on a specific node

Parameters:
id node the script is to be executed on
runScript statement containing the script to run
options nullable options to how to run the script, whether to override credentials
Returns:
map with node identifiers and corresponding responses
Throws:
java.util.NoSuchElementException if the node is not found
java.lang.IllegalStateException if the node is not in running state
org.jclouds.compute.callables.ScriptStillRunningException if the script was still running after org.jclouds.compute.reference.ComputeServiceConstants.Timeouts.scriptComplete
See also:
org.jclouds.compute.predicates.NodePredicates.runningInGroup(java.lang.String)
org.jclouds.scriptbuilder.domain.Statements
    @Override
    public ExecResponse runScriptOnNode(String idStatement runScriptRunScriptOptions options) {
        return .runScriptOnNode(idrunScriptoptions);
    }

    
Run the script on a specific node in the background, typically as nohup

Parameters:
id node the script is to be executed on
runScript statement containing the script to run
options nullable options to how to run the script, whether to override credentials
Returns:
map with node identifiers and corresponding responses
Throws:
java.util.NoSuchElementException if the node is not found
java.lang.IllegalStateException if the node is not in running state
See also:
org.jclouds.compute.predicates.NodePredicates.runningInGroup(java.lang.String)
org.jclouds.scriptbuilder.domain.Statements
    @Override
    @Beta
    public ListenableFuture<ExecResponsesubmitScriptOnNode(String idStatement runScriptRunScriptOptions options) {
        return .submitScriptOnNode(idrunScriptoptions);
    }

    
    @Override
    public ExecResponse runScriptOnNode(String idStatement runScript) {
        return .runScriptOnNode(idrunScript);
    }

    
    @Override
    public ExecResponse runScriptOnNode(String idString runScriptRunScriptOptions options) {
        return .runScriptOnNode(idrunScriptoptions);
    }

    
    @Override
    public ExecResponse runScriptOnNode(String idString runScript) {
        return .runScriptOnNode(idrunScript);
    }

    
Find an image by its id.

note

This is an uncached call to the backend service

    @Override
    public Image getImage(String id) {
       return .getImage(id);
    }

    
Returns the org.jclouds.compute.extensions.ImageExtension for this provider if it implements it.

    @Override
        return .getImageExtension();
    }
New to GrepCode? Check out our FAQ X