PageRenderTime 34ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/1.0RC2/www/instance_eip.php

http://scalr.googlecode.com/
PHP | 164 lines | 135 code | 20 blank | 9 comment | 26 complexity | 8597f13d659266ad79b4ec905cd3c87a MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?
  2. require_once('src/prepend.inc.php');
  3. $instance_info = $db->GetRow("SELECT * FROM farm_instances WHERE instance_id=?", array($req_iid));
  4. if ($_SESSION["uid"] != 0)
  5. $farminfo = $db->GetRow("SELECT * FROM farms WHERE id=? AND clientid=?", array($instance_info['farmid'], $_SESSION["uid"]));
  6. else
  7. $farminfo = $db->GetRow("SELECT * FROM farms WHERE id=?", array($instance_info['farmid']));
  8. if (!$farminfo)
  9. {
  10. $errmsg = "Farm not found";
  11. UI::Redirect("farms_view.php");
  12. }
  13. if ($_SESSION['uid'] == 0)
  14. {
  15. $clientinfo = $db->GetRow("SELECT * FROM clients WHERE id=?", array($farminfo['clientid']));
  16. // Decrypt client prvate key and certificate
  17. $private_key = $Crypto->Decrypt($clientinfo["aws_private_key_enc"], $cpwd);
  18. $certificate = $Crypto->Decrypt($clientinfo["aws_certificate_enc"], $cpwd);
  19. }
  20. else
  21. {
  22. $private_key = $_SESSION["aws_private_key"];
  23. $certificate = $_SESSION["aws_certificate"];
  24. }
  25. $AmazonEC2Client = new AmazonEC2($private_key, $certificate);
  26. if ($post_cancel)
  27. UI::Redirect("instances_view.php?farmid={$farminfo['id']}");
  28. if ($req_task == "unassign")
  29. {
  30. if ($_POST)
  31. {
  32. try
  33. {
  34. if (!$post_releaseaddress)
  35. $AmazonEC2Client->DisassociateAddress($instance_info['external_ip']);
  36. else
  37. $AmazonEC2Client->ReleaseAddress($instance_info['external_ip']);
  38. }
  39. catch(Exception $e)
  40. {
  41. $errmsg = "Cannot unassign elastic IP: {$e->getMessage()}";
  42. }
  43. if (!$errmsg)
  44. {
  45. $db->Execute("UPDATE farm_instances SET isipchanged='1', isactive='0', custom_elastic_ip=? WHERE id=?",
  46. array("", $instance_info['id'])
  47. );
  48. $okmsg = "Elastic IP is now unassigned from instance {$instance_info['instance_id']}. New IP will be assigned to it shortly.";
  49. UI::Redirect("instances_view.php?farmid={$farminfo['id']}");
  50. }
  51. }
  52. }
  53. elseif ($req_task == "assign")
  54. {
  55. if ($post_assigntype)
  56. {
  57. // Assign already allocated IP address
  58. if ($post_assigntype == 1)
  59. {
  60. $ip_address = $post_eip;
  61. }
  62. // Allocate and assign new address
  63. elseif ($post_assigntype == 2)
  64. {
  65. try
  66. {
  67. // Alocate new IP address
  68. $address = $AmazonEC2Client->AllocateAddress();
  69. }
  70. catch (Exception $e)
  71. {
  72. $errmsg = "Cannot allocate new elastic IP address: {$e->getMessage()}";
  73. }
  74. if ($address)
  75. {
  76. // wait few seconds before we can associate ip with instance.
  77. sleep(5);
  78. $ip_address = $address->publicIp;
  79. }
  80. }
  81. if ($ip_address)
  82. {
  83. try
  84. {
  85. $assign_retries = 1;
  86. while (true)
  87. {
  88. try
  89. {
  90. // Associate elastic ip address with instance
  91. $AmazonEC2Client->AssociateAddress($req_iid, $ip_address);
  92. $assigned_ip = $ip_address;
  93. }
  94. catch(Exception $e)
  95. {
  96. if (!stristr($e->getMessage(), "does not belong to you") || $assign_retries == 3)
  97. throw new Exception($e->getMessage());
  98. else
  99. {
  100. // Waiting...
  101. $Logger->debug("Waiting 2 seconds...");
  102. sleep(2);
  103. $assign_retries++;
  104. continue;
  105. }
  106. }
  107. break;
  108. }
  109. }
  110. catch(Exception $e)
  111. {
  112. $errmsg = "Cannot associate with instance specified Elastic IP: {$e->getMessage()}";
  113. }
  114. }
  115. if ($assigned_ip)
  116. {
  117. $db->Execute("UPDATE farm_instances SET external_ip=?, isipchanged='1', isactive='0', custom_elastic_ip=? WHERE id=?",
  118. array($assigned_ip, $assigned_ip, $instance_info['id'])
  119. );
  120. $okmsg = "Elastic IP successfully associated with instance";
  121. UI::Redirect("instances_view.php?farmid={$farminfo['id']}");
  122. }
  123. }
  124. // Get list of all elastic ips
  125. $result = $AmazonEC2Client->DescribeAddresses();
  126. $ips = $result->addressesSet->item;
  127. if ($ips instanceof stdClass)
  128. $ips = array($ips);
  129. foreach ($ips as $address)
  130. {
  131. // Exclude ips used by scalr
  132. if (!$address->instanceId)
  133. {
  134. $check = $db->GetOne("SELECT id FROM elastic_ips WHERE ipaddress=?", array($address->publicIp));
  135. if (!$check)
  136. $display["ips"][] = $address->publicIp;
  137. }
  138. }
  139. }
  140. $display["iid"] = $req_iid;
  141. $display["task"] = $req_task;
  142. $display["ipaddr"] = $instance_info['external_ip'];
  143. require_once ("src/append.inc.php");
  144. ?>