PageRenderTime 57ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/nocprovisioning/include/citrix.php

https://gitlab.com/billyprice1/whmcs-customizations
PHP | 313 lines | 240 code | 71 blank | 2 comment | 45 complexity | ede915e3bb7bfce2d87a4cfa467da628 MD5 | raw file
  1. <?PHP
  2. function nocps_init() {
  3. $nopcs_server = "NOCPS.SERVER.IP.ADDRESS";
  4. $nocps_user = "admin_username";
  5. $nocps_pass = "admin_password";
  6. return new nocps_api("$nocps_server","$nocps_user","$nocps_pass");
  7. }
  8. function nocps_get_hosts($nocps_api) {
  9. static $all_hosts = Array();
  10. if (!( $all_hosts[0] and is_array($all_hosts[0]))) {
  11. foreach (nocps_get_subnets($nocps_api) as $index => $data ) {
  12. $result = $nocps_api->getHosts($data["subnet"],0,1000);
  13. $all_hosts = array_merge ( $all_hosts, $result["data"]);
  14. }
  15. }
  16. lg_debug(print_r($all_hosts,true));
  17. return $all_hosts;
  18. }
  19. function nocps_get_host_ip_by_name($nocps_api,$hostname) {
  20. foreach ( nocps_get_hosts($nocps_api) as $index => $data ) {
  21. if ( $data["hostname"] == $hostname ) {
  22. return $data["ip"];
  23. }
  24. }
  25. }
  26. function nocps_get_host_mac_by_name($nocps_api,$hostname) {
  27. foreach ( nocps_get_hosts($nocps_api) as $index => $data ) {
  28. if ( $data["hostname"] == $hostname ) {
  29. return $data["mac"];
  30. }
  31. }
  32. }
  33. function nocps_get_citrix_servers($nocps_api) {
  34. static $servers = Array();
  35. if ( ! (is_array($servers) and $servers[0]["id"] ) ) {
  36. $result = $nocps_api->getDevices(0,1000);
  37. if (is_array($result) and $result["success"]=="1" and $result["total"] >= 1) {
  38. foreach ( $result["data"] as $index => $data) {
  39. if ($data["type"] == "xenserver") {
  40. lg_debug("Got new XenServer from NOC-PS: ".$data["name"]);
  41. array_push($servers,
  42. Array(
  43. "servername" => $data["name"],
  44. "address" => $data["ip"],
  45. "id" => $data["id"] )
  46. );
  47. }
  48. }
  49. }
  50. }
  51. $server_count = count($servers);
  52. lg_debug("Got $server_count XenServers from NOC-PS");
  53. return $servers;
  54. }
  55. function nocps_get_free_ram($nocps_api,$server_id,$server_name,$server_address) {
  56. $info = get_citrix_info($nocps_api,$server_id,$server_address);
  57. lg_debug("XenServer $server_name has ".$info["free_ram_mb"]." MB free RAM");
  58. return $info["free_ram_mb"];
  59. }
  60. function nocps_get_free_disk_space($nocps_api,$server_id,$server_name,$server_address) {
  61. $info = get_citrix_info($nocps_api,$server_id,$server_address);
  62. lg_debug("XenServer $server_name has ".$info["free_diskspace_mb"]." MB free Diskspace");
  63. return $info["free_diskspace_mb"];
  64. }
  65. function nocps_get_core_count($nocps_api,$server_id,$server_name,$server_address) {
  66. $info = get_citrix_info($nocps_api,$server_id,$server_address);
  67. lg_debug("XenServer $server_name has ".$info["total_cores"]." CPU Cores");
  68. return $info["total_cores"];
  69. }
  70. function get_citrix_info ($nocps_api,$server_id,$server_address) {
  71. $ssh_key_file = "/var/www/.ssh/id_citrix_info";
  72. static $citrix_info = Array ();
  73. if ( ! ( is_array($citrix_info) and count($citrix_info) > 0 and $citrix_info["free_ram_mb"])) {
  74. lg_debug2("SSH-Connection to $server_address to get free ram / disksize");
  75. $ssh_options = "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no";
  76. $ssh_command = "/usr/bin/ssh 2>&1 $ssh_options -i $ssh_key_file -l citrix_info $server_address";
  77. lg_debug("$ssh_command");
  78. $ssh_output =
  79. popen("$ssh_command","r");
  80. foreach (error_get_last() as $index => $line) { lg_err("$line"); }
  81. do {
  82. $line = fgets($ssh_output);
  83. lg_debug2($line);
  84. $values = explode ( " ", $line);
  85. if ( $values[0] == "free_memory" ) $citrix_info["free_ram_mb"] = chop($values[1]);
  86. if ( $values[0] == "free_disk_space" ) $citrix_info["free_diskspace_mb"] = chop($values[1]);
  87. if ( $values[0] == "total_cores" ) $citrix_info["total_cores"] = chop($values[1]);
  88. } while ( $line );
  89. pclose($ssh_output);
  90. }
  91. return $citrix_info;
  92. }
  93. function nocps_define_citrix_vm($nocps_api,$vm_name,$vm_description,$ram_amount,$disk_size,$vcore_count) {
  94. # ram_mount = needed ram in MB
  95. # disk_size = needed disk_size in MB
  96. $servers = nocps_get_citrix_servers($nocps_api);
  97. $target_server = "";
  98. foreach ( $servers as $index => $data ) {
  99. $free_ram = nocps_get_free_ram ($nocps_api,$data["id"],$data["servername"],$data["address"]);
  100. $free_disk_space = nocps_get_free_disk_space($nocps_api,$data["id"],$data["servername"],$data["address"]);
  101. $total_cores = nocps_get_core_count ($nocps_api,$data["id"],$data["servername"],$data["address"]);
  102. lg_debug("free_ram of server: $free_ram reqd ram: $ram_amount");
  103. lg_debug("free_disk space of server: $free_disk_space reqd disk space: $disk_size");
  104. if($free_ram >= $ram_amount and $free_disk_space >= $disk_size and $total_cores >= $vcore_count) {
  105. lg_info("Got Server ".$data["servername"]." for new VM $vm_name with $ram_amount MB RAM, $disk_size MB Disk size and $vcore_count VCores");
  106. $target_server=$data;
  107. break;
  108. }
  109. }
  110. if ($target_server) {
  111. $ip = nocps_get_free_ip_address($nocps_api);
  112. if ($ip[0] == "No IPs avalable") {
  113. lg_err("No IPs available in NOC-PS");
  114. return "No IPs available";
  115. } else {
  116. $subnet = $ip[1];
  117. $ip = $ip[0];
  118. lg_info("Got new IP address $ip");
  119. lg_debug("Creating new VM");
  120. lg_debug2("Subnet: $subnet");
  121. lg_debug2("ip: $ip");
  122. lg_debug2("vm_name: $vm_name");
  123. lg_debug2("vm_description: $vm_description");
  124. lg_debug2("module: ".$target_server["id"]);
  125. lg_debug2("memory: $ram_amount");
  126. lg_debug2("disk_size: $disk_size");
  127. lg_debug2("network: ".nocps_get_citrix_first_nic($nocps_api,$target_server["id"]));
  128. try {
  129. $result = $nocps_api->addVM(Array(
  130. "subnet" => $subnet,
  131. "ip" => $ip,
  132. "numips" => 1,
  133. "hostname" => $vm_name,
  134. "description" => $vm_description,
  135. "module" => $target_server["id"],
  136. "memory" => $ram_amount,
  137. "disk" => $disk_size,
  138. "diskstore" => "Local storage",
  139. "network" => nocps_get_citrix_first_nic($nocps_api,$target_server["id"]),
  140. ));
  141. }
  142. catch (exception $e) {
  143. lg_err("Error from XenServer while creating new VM, Please check xensource.log");
  144. return "Error: from XenServer while Creating VM";
  145. }
  146. }
  147. if( is_array($result) and $result["success"] == "1") {
  148. $mac = $result["mac"];
  149. lg_info("VM-Setup Phase 1 - successful");
  150. if ($vcore_count > 1 ) { citrix_set_vcore_count($target_server["servername"],$vm_name, $vcore_count); }
  151. return "NEWIP $ip";
  152. } else {
  153. lg_err("VM-Setup failed, unknown error");
  154. lg_err("success: ".$result["success"]);
  155. $lines = print_r($result,true);
  156. if(is_array($lines)) { foreach($lines as $ind => $text) { lg_info($ind." ".$text); } }
  157. return "Error: VM Setup Failed, unknown Error";
  158. }
  159. } else {
  160. lg_err("VM-Setup failed, No Server with enough resources for this VM available");
  161. return "Error: No Server has enough resources for this VM";
  162. }
  163. }
  164. function nocps_get_server_id ( $nocps_api, $server_name ) {
  165. $servers = nocps_get_citrix_servers ( $nocps_api );
  166. foreach ( $servers as $key => $data ) {
  167. if ( $data["servername"] == "$server_name" ) {
  168. lg_debug2("Server ID for server $server_name is ".$data["id"]);
  169. return $data["id"];
  170. }
  171. }
  172. }
  173. function nocps_get_datastores ( $nocps_api, $server_name ) {
  174. $result = $nocps_api->getDatastores(nocps_get_server_id($nocps_api,$server_name));
  175. }
  176. function nocps_get_subnets ( $nocps_api ) {
  177. $result = $nocps_api->getSubnets(0, 1000);
  178. return $result['data'];
  179. }
  180. function nocps_get_free_ip_address ( $nocps_api) {
  181. foreach (nocps_get_subnets($nocps_api) as $index => $data ) {
  182. $result = $nocps_api->getFirstAvailableIP($data["subnet"]);
  183. if (preg_match("/^([0-9]{1,3}.){3}[0-9]{1,3}$/",$result)) return Array($result,$data["subnet"]);
  184. }
  185. return Array("No IPs available");
  186. }
  187. function nocps_get_citrix_first_nic($nocps_api,$xenserver_id) {
  188. $result = $nocps_api->getNetworks($xenserver_id);
  189. if (is_array($result) and $result["success"] == 1) {
  190. return $result["data"][0]["id"];
  191. }
  192. }
  193. function nocps_get_xenserver_for_vm ( $nocps_api, $vmname, $vmip ) {
  194. lg_debug("wanted vm-name: $vmname vm-ip: $vmip");
  195. $citrix_servers = nocps_get_citrix_servers ( $nocps_api);
  196. lg_debug2(print_r($citrix_servers,true));
  197. foreach ($citrix_servers as $index => $data) {
  198. $vms = $nocps_api->searchHosts(Array("device" => $data["id"]));
  199. lg_debug2(print_r($vms,true));
  200. foreach ($vms["data"] as $vm_index => $vm_data ) {
  201. lg_debug("checking ip ".$vm_data["ip"]);
  202. if ( $vm_data["ip"] == "$vmip" ) {
  203. lg_debug("Found VM $vmip on Xen-Server ".$data["servername"]);
  204. return $data["servername"];
  205. }
  206. }
  207. }
  208. }
  209. function citrix_get_vm_vnc_port($xenserver_name, $vm_name) {
  210. $ssh_key_file = "/var/www/.ssh/id_citrix_vminfo";
  211. lg_debug("SSH-Connection to $xenserver_name to get vnc port of vm $vm_name");
  212. $ssh_options = "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no";
  213. $ssh_command = "/usr/bin/ssh 2>&1 $ssh_options -i $ssh_key_file -l citrix_info $xenserver_name /usr/bin/sudo /usr/local/bin/citrix_get_vnc_port $vm_name";
  214. lg_debug("$ssh_command");
  215. $ssh_output = popen("$ssh_command","r");
  216. do {
  217. $line = fgets($ssh_output);
  218. lg_debug2($line);
  219. $values = explode ( " ", $line);
  220. if ( $values[0] == "vnc_port" ) {
  221. $vnc_port = chop($values[1]);
  222. lg_debug("got vnc port $vnc_port");
  223. }
  224. } while ( $line );
  225. pclose($ssh_output);
  226. return $vnc_port;
  227. }
  228. function citrix_set_vcore_count ($xen_server, $vm_name, $vcore_count) {
  229. $ssh_key_file = "/var/www/.ssh/id_citrix_vminfo";
  230. lg_debug("SSH-Connection to $xen_server to set vcores of $vm_name to $vcore_count");
  231. $ssh_options = "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no";
  232. $ssh_command = "/usr/bin/ssh 2>&1 $ssh_options -i $ssh_key_file -l citrix_info $xen_server /usr/bin/sudo /usr/local/bin/citrix_set_vcores $vm_name $vcore_count";
  233. lg_debug("$ssh_command");
  234. $ssh_output = popen("$ssh_command","r");
  235. do {
  236. $line = fgets($ssh_output);
  237. lg_debug2($line);
  238. } while ( $line );
  239. pclose($ssh_output);
  240. return $vnc_port;
  241. }
  242. ?>