private static void autoScale(OnDemandAWS bob2, double current) { Boolean isTerminated = bob2.getIsTerminated(false); if (bob2.getIsTerminated(false) && current >= upperT) { bob2.createInstance(); System.out.println("Autoscale machine created."); } else if (!isTerminated && current < lowerT) { bob2.shutDownOnDemandAWS(); System.out.println("Autoscale machine terminated."); } }
// Terminates a machine and takes a snapshot private static void terminateVM(OnDemandAWS machine) { if (machine.getIsTerminated(true)) return; // Try to shut down the machine System.out.println("Terminating machine..."); System.out.println("Detach EBS"); machine.detachEBS(); System.out.println("Take Snapshot"); machine.saveSnapShot(); // wait for the snapshot to be created and then shutdown the machine do { sleep(10); } while (!machine.getSnapShotState().equalsIgnoreCase("available")); System.out.println("Snapshot created"); System.out.println("Terminated"); machine.shutDownOnDemandAWS(); }
public static void main(String[] args) throws Exception { AWSCredentials credentials = new PropertiesCredentials(Admin.class.getResourceAsStream("AwsCredentials.properties")); AmazonEC2 ec2 = new AmazonEC2Client(credentials); AmazonS3Client s3 = new AmazonS3Client(credentials); // AmazonAutoScalingClient autoScale = new AmazonAutoScalingClient(credentials); AmazonCloudWatchClient cloudWatch = new AmazonCloudWatchClient(credentials); String securityGroup = "WorkSecurity"; String keyName = "my_key2"; String zone = "us-east-1a"; String imageId = "ami-76f0061f"; String bucketName = "workcluster789"; // pblcluster workcluster789"; createSecurityGroup(ec2, securityGroup); createKey(keyName, ec2); createBucket(s3, bucketName, zone); createFileS3(s3, bucketName); // setupAutoScale(autoScale, cloudWatch, keyName, zone, securityGroup, imageId); // setupPolicy(autoScale, cloudWatch); OnDemandAWS bob = new OnDemandAWS(keyName, securityGroup, zone, imageId, "bob-PC"); bob.createEBS(10); OnDemandAWS alice = new OnDemandAWS(keyName, securityGroup, zone, imageId, "alice-PC"); alice.createEBS(10); // For Auto Scaling OnDemandAWS bob2 = new OnDemandAWS(keyName, securityGroup, zone, imageId, "bob-PC-2"); /*bob.createInstance(); alice.createInstance(); Thread.sleep(2*60*1000); //Before increasing CPU ssh needs to be initialized. This takes a around 2 minutes after the cpu starts increaseCPU(bob, keyName); increaseCPU(alice, keyName); int count = 0; while(true){ System.out.println("bob cpu = " + getCPUUsage(cloudWatch, bob.instanceId)); System.out.println("alice cpu = " + getCPUUsage(cloudWatch, alice.instanceId)); Thread.sleep(60*1000); count++; if (count>3){ stopCPU(bob, keyName); stopCPU(alice, keyName); } }*/ List<OnDemandAWS> machines = Arrays.asList(bob, alice); int days = 1; int numberOfChecks = 0; while (true) { if (isStartOfDay(numberOfChecks)) { numberOfChecks++; System.out.println("DAY: " + days); // Create all instances and start them up; for (OnDemandAWS vm : machines) createAndStartUpVM(vm, bucketName); if (days <= 1) { Thread.sleep(2 * 60 * 1000); // Before increasing CPU ssh needs to be initialized. This takes a around 2 minutes after // the cpu starts System.out.println("Increase CPU"); increaseCPU(bob, keyName); increaseCPU(alice, keyName); } System.out.println("All machines are created."); // Sleep for 30sec before pinging CloudWatch for the first time sleep(pingCW); continue; } else if (isEndOfDay(numberOfChecks)) { numberOfChecks = 0; days++; // Terminate all instances for (OnDemandAWS vm : machines) terminateVM(vm); if (!bob2.getIsTerminated(false)) bob2.shutDownOnDemandAWS(); // We have reached maximum number of days if (days > maxDays) break; System.out.println("All machines are terminated. Now SLEEP."); // Sleep for the night - 2min sleep(nightDuration); continue; } // See which machine is used a lot and try to auto-scale and which one is not used and kill // it. numberOfChecks++; if ((days == 1) && (numberOfChecks > 3)) { stopCPU(bob, keyName); stopCPU(alice, keyName); } // Terminate idle machines for (OnDemandAWS vm : machines) if (isIdle(cloudWatch, vm, cpuIdle)) { terminateVM(vm); System.out.println(vm.machineName + " is IDLE and terminated"); } // Auto-scale code autoScale(bob2, getCPUUsage(cloudWatch, bob.instanceId)); System.out.println("Hour: " + numberOfChecks); // Sleep for 30sec before pinging CloudWatch again sleep(pingCW); } // Shutdown clients ec2.shutdown(); s3.shutdown(); cloudWatch.shutdown(); System.out.println("EXIT Program"); }