public function indexAction() { // This endpoint will send stop signal to all ec2 instances $Ec2Client = new Ec2Client(['version' => 'latest', 'region' => 'us-east-1']); // Find the instance ids $reservations = $Ec2Client->DescribeInstances()['Reservations']; if (!$reservations) { return new Response('Sorry didnt find any instance by tag Type: "Autoscale"'); } else { $inst_list = []; } foreach ($reservations as $reservation) { foreach ($reservation['Instances'] as $instance) { array_push($inst_list, $instance['InstanceId']); } } $term_resp = $Ec2Client->terminateInstances(array('DryRun' => true, 'InstanceIds' => $inst_list, 'Force' => true)); return new Response('Terminating the following instances:' . '<pre>' . print_r($inst_list, true) . '</pre>' . 'Got the following respose:' . '<pre>' . print_r($term_resp, true) . '</pre>' . '<hr />' . 'Note: The terminate call is set in dry-run mode,' . 'Comment it from the code to actually terminate all instances'); }
public function indexAction() { //Get the internal IP of the both the node pg1 and pg2 $Ec2Client = new Ec2Client(['version' => 'latest', 'region' => 'us-east-1']); // name => instanceid $nodes = ['pg1', 'pg2']; // db => port $dbs = array('dbam' => '5432', 'dbnz' => '5433'); // status $status = []; // Iterate through each node foreach ($nodes as $node) { $status[$node] = []; // Get node IP $ip = $Ec2Client->DescribeInstances(array('Filters' => array(array('Name' => 'tag:Name', 'Values' => array($node)))))['Reservations']['0']['Instances']['0']['PrivateIpAddress']; // Iterate through each db foreach ($dbs as $db => $dbport) { $status[$node][$db] = ''; $dbconn = pg_connect("host={$ip} port={$dbport} user=monuser dbname=postgres connect_timeout=5"); if (!$dbconn) { $status[$node][$db] = 'DOWN'; } else { $res = pg_query($dbconn, 'select pg_is_in_recovery()'); $stat = pg_fetch_assoc($res); if ($stat['pg_is_in_recovery'] == 't') { $status[$node][$db] = 'SLAVE'; } elseif ($stat['pg_is_in_recovery'] == 'f') { $status[$node][$db] = 'MASTER'; } else { $status[$node][$db] = 'UNKNOWN'; } } } } // Should have done this is using twig but short on time :( return new Response('Following is the status of the cluster' . '<pre>' . print_r($status, true) . '</pre>' . '<hr />' . '<a href=/takedown/pg1>Click here to stop PG1</a>' . '  ' . '<a href=/takedown/pg2>Click here to stop PG2</a>' . '<br />' . 'Be carefull, it will blindly stop the instance'); }