Search in sources :

Example 26 with ComputeServiceContext

use of org.jclouds.compute.ComputeServiceContext in project legacy-jclouds-examples by jclouds.

the class ServerMetadata method init.

private void init(String[] args) {
    // The provider configures jclouds To use the Rackspace Cloud (US)
    // To use the Rackspace Cloud (UK) set the provider to "rackspace-cloudservers-uk"
    String provider = "rackspace-cloudservers-us";
    String username = args[0];
    String apiKey = args[1];
    ComputeServiceContext context = ContextBuilder.newBuilder(provider).credentials(username, apiKey).buildView(ComputeServiceContext.class);
    compute = context.getComputeService();
    nova = context.unwrap();
    serverApi = nova.getApi().getServerApiForZone(Constants.ZONE);
}
Also used : ComputeServiceContext(org.jclouds.compute.ComputeServiceContext)

Example 27 with ComputeServiceContext

use of org.jclouds.compute.ComputeServiceContext in project legacy-jclouds-examples by jclouds.

the class CloudServersPublish method init.

private void init(List<String> args) {
    // The provider configures jclouds To use the Rackspace Cloud (US)
    // To use the Rackspace Cloud (UK) set the provider to "rackspace-cloudservers-uk"
    String provider = "rackspace-cloudservers-us";
    String username = args.get(0);
    String apiKey = args.get(1);
    numServers = args.size() == 3 ? Integer.valueOf(args.get(2)) : 1;
    Iterable<Module> modules = ImmutableSet.<Module>of(new SshjSshClientModule());
    // These properties control how often jclouds polls for a status udpate
    Properties overrides = new Properties();
    overrides.setProperty(ComputeServiceProperties.POLL_INITIAL_PERIOD, Constants.POLL_PERIOD_TWENTY_SECONDS);
    overrides.setProperty(ComputeServiceProperties.POLL_MAX_PERIOD, Constants.POLL_PERIOD_TWENTY_SECONDS);
    ComputeServiceContext context = ContextBuilder.newBuilder(provider).credentials(username, apiKey).overrides(overrides).modules(modules).buildView(ComputeServiceContext.class);
    compute = context.getComputeService();
}
Also used : SshjSshClientModule(org.jclouds.sshj.config.SshjSshClientModule) ComputeServiceContext(org.jclouds.compute.ComputeServiceContext) Module(com.google.inject.Module) SshjSshClientModule(org.jclouds.sshj.config.SshjSshClientModule) Properties(java.util.Properties) ComputeServiceProperties(org.jclouds.compute.config.ComputeServiceProperties)

Example 28 with ComputeServiceContext

use of org.jclouds.compute.ComputeServiceContext in project legacy-jclouds-examples by jclouds.

the class CreateServer method init.

private void init(String[] args) {
    // The provider configures jclouds To use the Rackspace Cloud (US)
    // To use the Rackspace Cloud (UK) set the provider to "rackspace-cloudservers-uk"
    String provider = "rackspace-cloudservers-us";
    String username = args[0];
    String apiKey = args[1];
    // These properties control how often jclouds polls for a status udpate
    Properties overrides = new Properties();
    overrides.setProperty(ComputeServiceProperties.POLL_INITIAL_PERIOD, Constants.POLL_PERIOD_TWENTY_SECONDS);
    overrides.setProperty(ComputeServiceProperties.POLL_MAX_PERIOD, Constants.POLL_PERIOD_TWENTY_SECONDS);
    ComputeServiceContext context = ContextBuilder.newBuilder(provider).credentials(username, apiKey).overrides(overrides).buildView(ComputeServiceContext.class);
    compute = context.getComputeService();
}
Also used : ComputeServiceContext(org.jclouds.compute.ComputeServiceContext) Properties(java.util.Properties) ComputeServiceProperties(org.jclouds.compute.config.ComputeServiceProperties)

Example 29 with ComputeServiceContext

use of org.jclouds.compute.ComputeServiceContext in project legacy-jclouds-examples by jclouds.

the class MainApp method main.

public static void main(String[] args) {
    if (args.length < PARAMETERS) {
        throw new IllegalArgumentException(INVALID_SYNTAX);
    }
    // Arguments
    String accessKeyId = args[0];
    String secretKey = args[1];
    ComputeServiceContext awsEC2Context = null;
    RestContext<CloudWatchClient, CloudWatchAsyncClient> cloudWatchContext = null;
    try {
        cloudWatchContext = ContextBuilder.newBuilder(new AWSCloudWatchProviderMetadata()).credentials(accessKeyId, secretKey).build();
        awsEC2Context = ContextBuilder.newBuilder(new AWSEC2ProviderMetadata()).credentials(accessKeyId, secretKey).build(ComputeServiceContext.class);
        // Get all nodes
        Set<? extends ComputeMetadata> allNodes = awsEC2Context.getComputeService().listNodes();
        for (ComputeMetadata node : allNodes) {
            String nodeId = node.getProviderId();
            String region = getRegion(node.getLocation());
            MetricClient metricClient = cloudWatchContext.getApi().getMetricClientForRegion(region);
            int metricsCount = getMetricsCountForInstance(cloudWatchContext.getApi(), region, nodeId);
            double[] cpuUtilization = getCPUUtilizationStatsForInstanceOverTheLast24Hours(metricClient, nodeId);
            String cpuUtilizationHeader = "  CPU utilization statistics: ";
            DecimalFormat df = new DecimalFormat("#.##");
            System.out.println(nodeId + " CloudWatch Metrics (Past 24 hours)");
            System.out.println("  Total metrics stored: " + metricsCount);
            if (cpuUtilization == null) {
                System.out.println(cpuUtilizationHeader + "Unable to compute as there are no CPU utilization " + "metrics stored.");
            } else {
                System.out.println(cpuUtilizationHeader + df.format(cpuUtilization[0]) + "% (avg), " + df.format(cpuUtilization[1]) + "% (max), " + df.format(cpuUtilization[2]) + "% (min)");
            }
        }
    } finally {
        if (awsEC2Context != null) {
            awsEC2Context.close();
        }
        if (cloudWatchContext != null) {
            cloudWatchContext.close();
        }
    }
}
Also used : AWSEC2ProviderMetadata(org.jclouds.aws.ec2.AWSEC2ProviderMetadata) CloudWatchAsyncClient(org.jclouds.cloudwatch.CloudWatchAsyncClient) DecimalFormat(java.text.DecimalFormat) ComputeServiceContext(org.jclouds.compute.ComputeServiceContext) ComputeMetadata(org.jclouds.compute.domain.ComputeMetadata) Datapoint(org.jclouds.cloudwatch.domain.Datapoint) AWSCloudWatchProviderMetadata(org.jclouds.aws.cloudwatch.AWSCloudWatchProviderMetadata) CloudWatchClient(org.jclouds.cloudwatch.CloudWatchClient) MetricClient(org.jclouds.cloudwatch.features.MetricClient)

Example 30 with ComputeServiceContext

use of org.jclouds.compute.ComputeServiceContext in project legacy-jclouds-examples by jclouds.

the class MainApp method initController.

private static MinecraftController initController(String provider, String identity, String credential, String group) {
    Properties properties = new Properties();
    properties.setProperty("minecraft.port", "25565");
    properties.setProperty("minecraft.group", group);
    properties.setProperty("minecraft.ms", "1024");
    properties.setProperty("minecraft.mx", "1024");
    properties.setProperty("minecraft.url", "https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar");
    if ("aws-ec2".equals(provider)) {
        // since minecraft download is in s3 on us-east, lowest latency is from
        // there
        properties.setProperty(PROPERTY_REGIONS, "us-east-1");
        properties.setProperty("jclouds.ec2.ami-query", "owner-id=137112412989;state=available;image-type=machine");
        properties.setProperty("jclouds.ec2.cc-ami-query", "");
    }
    // example of injecting a ssh implementation
    Iterable<Module> modules = ImmutableSet.<Module>of(new SshjSshClientModule(), new SLF4JLoggingModule(), new EnterpriseConfigurationModule(), // This is extended stuff you might inject!!
    new ConfigureMinecraftDaemon());
    ContextBuilder builder = ContextBuilder.newBuilder(provider).credentials(identity, credential).modules(modules).overrides(properties);
    System.out.printf(">> initializing %s%n", builder.getApiMetadata());
    ComputeServiceContext context = builder.buildView(ComputeServiceContext.class);
    context.utils().eventBus().register(ScriptLogger.INSTANCE);
    return context.utils().injector().getInstance(MinecraftController.class);
}
Also used : EnterpriseConfigurationModule(org.jclouds.enterprise.config.EnterpriseConfigurationModule) SLF4JLoggingModule(org.jclouds.logging.slf4j.config.SLF4JLoggingModule) SshjSshClientModule(org.jclouds.sshj.config.SshjSshClientModule) ContextBuilder(org.jclouds.ContextBuilder) ComputeServiceContext(org.jclouds.compute.ComputeServiceContext) Properties(java.util.Properties) SLF4JLoggingModule(org.jclouds.logging.slf4j.config.SLF4JLoggingModule) Module(com.google.inject.Module) SshjSshClientModule(org.jclouds.sshj.config.SshjSshClientModule) EnterpriseConfigurationModule(org.jclouds.enterprise.config.EnterpriseConfigurationModule)

Aggregations

ComputeServiceContext (org.jclouds.compute.ComputeServiceContext)31 ComputeService (org.jclouds.compute.ComputeService)13 Module (com.google.inject.Module)9 Template (org.jclouds.compute.domain.Template)9 TemplateBuilder (org.jclouds.compute.domain.TemplateBuilder)9 Properties (java.util.Properties)8 SshjSshClientModule (org.jclouds.sshj.config.SshjSshClientModule)7 ClusterSpec (org.apache.whirr.ClusterSpec)6 NodeMetadata (org.jclouds.compute.domain.NodeMetadata)6 IOException (java.io.IOException)5 Set (java.util.Set)5 SLF4JLoggingModule (org.jclouds.logging.slf4j.config.SLF4JLoggingModule)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 HashSet (java.util.HashSet)4 Stack (java.util.Stack)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 CompositeConfiguration (org.apache.commons.configuration.CompositeConfiguration)4 Configuration (org.apache.commons.configuration.Configuration)4 PropertiesConfiguration (org.apache.commons.configuration.PropertiesConfiguration)4 InstanceTemplate (org.apache.whirr.InstanceTemplate)4