bulkdestroynodes.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. exit;
  3. /**
  4. * This script is to be used from PHP command line and will create a set
  5. * of Virtual VChamilo automatically from a CSV nodelist description.
  6. * The standard structure of the nodelist is given by the nodelist-dest.csv file.
  7. */
  8. global $debuglevel;
  9. global $debugdisplay;
  10. $debuglevel = 4;
  11. $debugdisplay = 4;
  12. define('CLI_SCRIPT', true);
  13. define('CHAMILO_INTERNAL', true);
  14. // this will only run on master chamilo
  15. echo "Starting tool\n";
  16. echo "Chamilo Bulk Nodes Creation v.1.0\n";
  17. echo "=================================\n";
  18. require_once('../../../main/inc/global.inc.php');
  19. require_once('clilib.php'); // cli only functions
  20. // Ensure errors are well explained
  21. ini_set('debug_display', 1);
  22. ini_set('debug_level', E_ALL);
  23. // Now get cli options.
  24. list($options, $unrecognized) = cli_get_params(
  25. array(
  26. 'interactive' => false,
  27. 'help' => false,
  28. 'config' => false,
  29. 'nodes' => '',
  30. 'lint' => false
  31. ),
  32. array(
  33. 'h' => 'help',
  34. 'c' => 'config',
  35. 'n' => 'nodes',
  36. 'i' => 'interactive',
  37. 'l' => 'lint'
  38. )
  39. );
  40. $interactive = !empty($options['interactive']);
  41. if ($unrecognized) {
  42. $unrecognized = implode("\n ", $unrecognized);
  43. cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  44. }
  45. if ($options['help']) {
  46. $help =
  47. "Command line VMoodle Generator.
  48. Please note you must execute this script with the same uid as apache!
  49. Options:
  50. --interactive No interactive questions or confirmations
  51. -h, --help Print out this help
  52. -c, --config Define an external config file
  53. -n, --nodes A node descriptor CSV file
  54. -l, --lint Decodes node file and give a report on nodes to be created.
  55. Example:
  56. \$sudo -u www-data /usr/bin/php /var/www/chamilo19/plugin/vchamilo/cli/bulkdestroynodes.php --nodes=<node-file-path>
  57. "; //TODO: localize - to be translated later when everything is finished
  58. echo $help;
  59. die;
  60. }
  61. // Get all options from config file.
  62. if (!empty($options['config'])) {
  63. echo "Loading config : ".$options['config'];
  64. if (!file_exists($options['config'])) {
  65. cli_error(get_string('confignotfound', 'block_vmoodle'));
  66. }
  67. $content = file($options['config']);
  68. foreach ($content as $l) {
  69. if (preg_match('/^\s+$/', $l)) {
  70. continue; // Empty lines.
  71. }
  72. if (preg_match('/^[#\/!;]/', $l)) {
  73. continue; // Comments (any form).
  74. }
  75. if (preg_match('/^(.*?)=(.*)$/', $l, $matches)) {
  76. if (in_array($matches[1], $expectedoptions)) {
  77. $options[trim($matches[1])] = trim($matches[2]);
  78. }
  79. }
  80. }
  81. }
  82. require_once($_configuration['root_sys'].'local/classes/database.class.php'); // cli only functions
  83. if ($options['verbose']) {
  84. echo "loaded dbclass\n";
  85. }
  86. require_once($_configuration['root_sys'].'local/classes/textlib.class.php'); // cli only functions
  87. if ($options['verbose']) {
  88. echo "loaded textlib\n";
  89. }
  90. require_once($_configuration['root_sys'].'local/classes/mootochamlib.php'); // moodle like API
  91. if ($options['verbose']) {
  92. echo "loaded moodle wrapping\n";
  93. }
  94. require_once($_configuration['root_sys'].'/plugin/vchamilo/lib/vchamilo_plugin.class.php');
  95. if ($options['verbose']) {
  96. echo "loaded vchamilo plugin\n";
  97. }
  98. global $DB;
  99. if ($options['verbose']) {
  100. echo "building database manager\n";
  101. }
  102. $DB = new DatabaseManager();
  103. if ($options['verbose']) {
  104. echo "building plugin vchamilo\n";
  105. }
  106. $plugin = VChamiloPlugin::create();
  107. if (empty($options['nodes'])) {
  108. cli_error(get_string('climissingnodes', 'block_vmoodle'));
  109. }
  110. if ($options['verbose']) {
  111. echo "parsing nodelist\n";
  112. }
  113. $nodes = vchamilo_parse_csv_nodelist($options['nodes'], $plugin);
  114. if ($options['lint']) {
  115. ctrace("Lint mode:\n");
  116. print_object($nodes);
  117. die;
  118. }
  119. if (empty($nodes)) {
  120. cli_error(get_string('cliemptynodelist', 'block_vmoodle'));
  121. }
  122. ctrace('Starting CLI processing');
  123. foreach ($nodes as $n) {
  124. ctrace('Destroying node :'.$n->vhostname);
  125. if (!$DB->get_record('vchamilo', array('root_web' => $n->root_web))) {
  126. ctrace('Node does not exist. Skipping');
  127. continue;
  128. }
  129. /*
  130. * This launches automatically all steps of the controller.management.php script several times
  131. * with the "doadd" action and progressing in steps.
  132. */
  133. $action = "fulldeleteinstances";
  134. $automation = true;
  135. $return = include($_configuration['root_sys'].'/plugin/vchamilo/views/manage.controller.php');
  136. if ($interactive) {
  137. $input = readline("Continue (y/n|r) ?\n");
  138. if ($input == 'r' || $input == 'R') {
  139. $vmoodlestep--;
  140. } elseif ($input == 'n' || $input == 'N') {
  141. echo "finishing\n";
  142. exit(0);
  143. }
  144. }
  145. }
  146. exit (0);