bulkdestroynodes.php 4.7 KB

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