Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.google.common.collect.ImmutableSet;
- import com.google.common.collect.Iterables;
- import com.google.inject.Module;
- import org.jclouds.ContextBuilder;
- import org.jclouds.aws.ec2.compute.AWSEC2TemplateOptions;
- import org.jclouds.compute.ComputeServiceContext;
- import org.jclouds.compute.domain.Image;
- import org.jclouds.compute.domain.NodeMetadata;
- import org.jclouds.compute.domain.OsFamily;
- import org.jclouds.compute.domain.Template;
- import org.jclouds.domain.Location;
- import org.jclouds.location.reference.LocationConstants;
- import org.jclouds.logging.log4j.config.Log4JLoggingModule;
- import org.jclouds.sshj.config.SshjSshClientModule;
- import java.util.Properties;
- import java.util.Set;
- public class MainApp {
- public static void main(String[] args){
- String accesskeyid="xxxx";
- String secretkey="xxxxxx";
- Properties props = new Properties();
- props.setProperty(LocationConstants.PROPERTY_REGIONS, "us-east-1");
- // get a context with ec2 that offers the portable ComputeService API
- ComputeServiceContext context = ContextBuilder.newBuilder("aws-ec2")
- .credentials(accesskeyid, secretkey).overrides(props)
- .modules(ImmutableSet.<Module> of(new Log4JLoggingModule(),
- new SshjSshClientModule()))
- .buildView(ComputeServiceContext.class);
- // here's an example of the portable api
- Set<? extends Location> locations =
- context.getComputeService().listAssignableLocations();
- Set<? extends Image> images = context.getComputeService().listImages();
- System.out.println(images.size());
- // pick the highest version of the RightScale CentOS template
- // Template template = context.getComputeService().templateBuilder().osFamily(OsFamily.CENTOS).build();
- //
- //// specify your own groups which already have the correct rules applied
- // template.getOptions().as(AWSEC2TemplateOptions.class).securityGroups(group1);
- //
- //// specify your own keypair for use in creating nodes
- // template.getOptions().as(AWSEC2TemplateOptions.class).keyPair(keyPair);
- //
- //// run a couple nodes accessible via group
- // Set<? extends NodeMetadata> nodes = context.getComputeService().createNodesInGroup("webserver", 2, template);
- //
- //// when you need access to very ec2-specific features, use the provider-specific context
- // AWSEC2Client ec2Client = AWSEC2Client.class.cast(context.getProviderSpecificContext().getApi());
- //
- //// ex. to get an ip and associate it with a node
- // NodeMetadata node = Iterables.get(nodes, 0);
- // String ip = ec2Client.getElasticIPAddressServices().allocateAddressInRegion(node.getLocation().getId());
- // ec2Client.getElasticIPAddressServices().associateAddressInRegion(node.getLocation().getId(),ip, node.getProviderId());
- context.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement