PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/src/plugins/linux-vserver/web/linux-vserver-action.php

https://github.com/qyjohn/openqrm
PHP | 161 lines | 118 code | 21 blank | 22 comment | 6 complexity | 6ddc83d016552035c471cdacf0cd8b1b MD5 | raw file
  1. <?php
  2. $linux_vserver_command = $_REQUEST["linux_vserver_command"];
  3. $linux_vserver_id = $_REQUEST["linux_vserver_id"];
  4. ?>
  5. <html>
  6. <head>
  7. <title>openQRM Linux-VServer actions</title>
  8. <meta http-equiv="refresh" content="0; URL=linux-vserver-manager.php?currenttab=tab0&strMsg=Processing <?php echo $linux_vserver_command; ?>">
  9. </head>
  10. <body>
  11. <?php
  12. /*
  13. This file is part of openQRM.
  14. openQRM is free software: you can redistribute it and/or modify
  15. it under the terms of the GNU General Public License version 2
  16. as published by the Free Software Foundation.
  17. openQRM is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with openQRM. If not, see <http://www.gnu.org/licenses/>.
  23. Copyright 2009, Matthias Rechenburg <matt@openqrm.com>
  24. */
  25. $RootDir = $_SERVER["DOCUMENT_ROOT"].'/openqrm/base/';
  26. require_once "$RootDir/include/openqrm-database-functions.php";
  27. require_once "$RootDir/include/user.inc.php";
  28. require_once "$RootDir/include/openqrm-server-config.php";
  29. require_once "$RootDir/class/resource.class.php";
  30. require_once "$RootDir/class/appliance.class.php";
  31. require_once "$RootDir/class/kernel.class.php";
  32. require_once "$RootDir/class/event.class.php";
  33. require_once "$RootDir/class/openqrm_server.class.php";
  34. global $OPENQRM_SERVER_BASE_DIR;
  35. global $RESOURCE_INFO_TABLE;
  36. global $OPENQRM_EXEC_PORT;
  37. $openqrm_server = new openqrm_server();
  38. $OPENQRM_SERVER_IP_ADDRESS=$openqrm_server->get_ip_address();
  39. global $OPENQRM_SERVER_IP_ADDRESS;
  40. // place for the linux-vserver stat files
  41. $vserver_vm_dir = $_SERVER["DOCUMENT_ROOT"].'/openqrm/base/plugins/linux-vserver/linux-vserver-stat';
  42. $event = new event();
  43. // user/role authentication
  44. if ($OPENQRM_USER->role != "administrator") {
  45. $event->log("authorization", $_SERVER['REQUEST_TIME'], 1, "linux-vserver-action", "Un-Authorized access to linux-vserver-actions from $OPENQRM_USER->name", "", "", 0, 0, 0);
  46. exit();
  47. }
  48. $linux_vserver_name = $_REQUEST["linux_vserver_name"];
  49. $linux_vserver_mac = $_REQUEST["linux_vserver_mac"];
  50. $linux_vserver_ip = $_REQUEST["linux_vserver_ip"];
  51. $linux_vserver_fields = array();
  52. foreach ($_REQUEST as $key => $value) {
  53. if (strncmp($key, "linux_vserver_", 14) == 0) {
  54. $linux_vserver_fields[$key] = $value;
  55. }
  56. }
  57. unset($linux_vserver_fields["linux_vserver_command"]);
  58. $event->log("$linux_vserver_command", $_SERVER['REQUEST_TIME'], 5, "linux-vserver-action", "Processing linux-vserver command $linux_vserver_command", "", "", 0, 0, 0);
  59. switch ($linux_vserver_command) {
  60. case 'new':
  61. // send command to linux-vserver-host to create the new vm
  62. $linux_vserver_appliance = new appliance();
  63. $linux_vserver_appliance->get_instance_by_id($linux_vserver_id);
  64. $linux_vserver = new resource();
  65. $linux_vserver->get_instance_by_id($linux_vserver_appliance->resources);
  66. $resource_command="$OPENQRM_SERVER_BASE_DIR/openqrm/plugins/linux-vserver/bin/openqrm-linux-vserver create -n $linux_vserver_name -m $linux_vserver_mac -i $linux_vserver_ip";
  67. $linux_vserver->send_command($linux_vserver->ip, $resource_command);
  68. // add vm to openQRM
  69. $resource_new = new resource();
  70. $resource_id = openqrm_db_get_free_id('resource_id', $RESOURCE_INFO_TABLE);
  71. $resource_fields = array();
  72. $resource_fields["resource_id"]=$resource_id;
  73. $resource_fields["resource_mac"]=$linux_vserver_mac;
  74. $resource_fields["resource_ip"]=$linux_vserver_ip;
  75. $resource_fields["resource_capabilities"]="linux-vserver-vm";
  76. $resource_new->add($resource_fields);
  77. // assign to linux-vserver kernel
  78. $kernel_linux_vserver = new kernel();
  79. $kernel_linux_vserver->get_instance_by_id($linux_vserver->kernelid);
  80. $resource_new->assign($resource_id, $kernel_linux_vserver->id, $kernel_linux_vserver->name, 1, "idle");
  81. break;
  82. case 'start':
  83. $linux_vserver_appliance = new appliance();
  84. $linux_vserver_appliance->get_instance_by_id($linux_vserver_id);
  85. $linux_vserver = new resource();
  86. $linux_vserver->get_instance_by_id($linux_vserver_appliance->resources);
  87. $resource_command="$OPENQRM_SERVER_BASE_DIR/openqrm/plugins/linux-vserver/bin/openqrm-linux-vserver start -n $linux_vserver_name -u $OPENQRM_USER->name -p $OPENQRM_USER->password";
  88. $linux_vserver->send_command($linux_vserver->ip, $resource_command);
  89. break;
  90. case 'stop':
  91. $linux_vserver_appliance = new appliance();
  92. $linux_vserver_appliance->get_instance_by_id($linux_vserver_id);
  93. $linux_vserver = new resource();
  94. $linux_vserver->get_instance_by_id($linux_vserver_appliance->resources);
  95. $resource_command="$OPENQRM_SERVER_BASE_DIR/openqrm/plugins/linux-vserver/bin/openqrm-linux-vserver stop -n $linux_vserver_name -u $OPENQRM_USER->name -p $OPENQRM_USER->password";
  96. $linux_vserver->send_command($linux_vserver->ip, $resource_command);
  97. break;
  98. case 'reboot':
  99. $linux_vserver_appliance = new appliance();
  100. $linux_vserver_appliance->get_instance_by_id($linux_vserver_id);
  101. $linux_vserver = new resource();
  102. $linux_vserver->get_instance_by_id($linux_vserver_appliance->resources);
  103. $resource_command="$OPENQRM_SERVER_BASE_DIR/openqrm/plugins/linux-vserver/bin/openqrm-linux-vserver reboot -n $linux_vserver_name -u $OPENQRM_USER->name -p $OPENQRM_USER->password";
  104. $linux_vserver->send_command($linux_vserver->ip, $resource_command);
  105. break;
  106. case 'delete':
  107. $linux_vserver_appliance = new appliance();
  108. $linux_vserver_appliance->get_instance_by_id($linux_vserver_id);
  109. $linux_vserver = new resource();
  110. $linux_vserver->get_instance_by_id($linux_vserver_appliance->resources);
  111. $resource_command="$OPENQRM_SERVER_BASE_DIR/openqrm/plugins/linux-vserver/bin/openqrm-linux-vserver delete -n $linux_vserver_name -u $OPENQRM_USER->name -p $OPENQRM_USER->password";
  112. $linux_vserver->send_command($linux_vserver->ip, $resource_command);
  113. break;
  114. case 'get_linux_vserver':
  115. if (!file_exists($vserver_vm_dir)) {
  116. mkdir($vserver_vm_dir);
  117. }
  118. $filename = $vserver_vm_dir."/".$_POST['filename'];
  119. $filedata = base64_decode($_POST['filedata']);
  120. echo "<h1>$filename</h1>";
  121. $fout = fopen($filename,"wb");
  122. fwrite($fout, $filedata);
  123. fclose($fout);
  124. break;
  125. case 'refresh_vm_list':
  126. $linux_vserver = new resource();
  127. $linux_vserver->get_instance_by_id($linux_vserver_id);
  128. $resource_command="$OPENQRM_SERVER_BASE_DIR/openqrm/plugins/linux-vserver/bin/openqrm-linux-vserver post_vm_list -u $OPENQRM_USER->name -p $OPENQRM_USER->password";
  129. $linux_vserver->send_command($linux_vserver->ip, $resource_command);
  130. break;
  131. default:
  132. $event->log("$linux_vserver_command", $_SERVER['REQUEST_TIME'], 3, "linux-vserver-action", "No such event command ($linux_vserver_command)", "", "", 0, 0, 0);
  133. break;
  134. }
  135. ?>
  136. </body>