Example usage for com.amazonaws.services.elasticmapreduce.model InstanceState RUNNING

List of usage examples for com.amazonaws.services.elasticmapreduce.model InstanceState RUNNING

Introduction

In this page you can find the example usage for com.amazonaws.services.elasticmapreduce.model InstanceState RUNNING.

Prototype

InstanceState RUNNING

To view the source code for com.amazonaws.services.elasticmapreduce.model InstanceState RUNNING.

Click Source Link

Usage

From source file:org.excalibur.service.aws.ConfigurationService.java

License:Open Source License

public static void main(String[] args) throws JSchException, IOException {
    ConfigurationService service = new ConfigurationService();
    //Instance instance = service.filterInstances(InstanceStateType.RUNNING, service.getInstances()).get(0);

    Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");

    JSch shell = new JSch();
    shell.addIdentity(System.getProperty("user.home") + "/.ec2/leite.pem");
    //        Session session = shell.getSession("ubuntu", instance.getPublicDnsName());
    //        session.setConfig(config);
    //        session.connect();
    ///*  w  w  w .j av a  2  s  . co  m*/
    //        Channel channel = session.openChannel("shell");
    //        channel.setInputStream(System.in);
    //        channel.setOutputStream(System.out);
    //        channel.connect();

    RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withInstanceType("t1.micro")
            .withImageId("ami-832b72ea").withMinCount(18).withMaxCount(18).withSecurityGroupIds("sg-e352488a")
            .withKeyName("leite");

    System.out.println(new Date());
    RunInstancesResult runInstances = service.ec2_.runInstances(runInstancesRequest);
    List<Instance> instances = runInstances.getReservation().getInstances();

    for (Instance inst : instances) {
        System.out.printf("id:%s, dns:%s lauch time:%s, state:%s\n", inst.getInstanceId(),
                inst.getPublicDnsName(), inst.getLaunchTime(), inst.getState().getName());

        Session session = shell.getSession("ubuntu", inst.getPublicDnsName());
        session.setConfig(config);

        if (InstanceState.RUNNING.equals(inst.getState())) {
            session.connect();
            ChannelExec channel = (ChannelExec) session.openChannel("exec");
            channel.connect();

            channel = (ChannelExec) session.openChannel("exec");
            BufferedReader br = new BufferedReader(new InputStreamReader(channel.getInputStream()));
            channel.setCommand("uname -a && date && uptime && who");
            channel.connect();

            while (true) {
                String line = br.readLine();
                if (line == null) {
                    br.close();
                    break;
                }
                System.out.println(line);
            }
        }
    }

    System.out.println(instances.size());
}