synchro_exec.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Import web service
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. //
  25. // Known limitations
  26. // - reconciliation is made on the first column
  27. //
  28. // Known issues
  29. // - ALMOST impossible to troubleshoot when an externl key has a wrong value
  30. // - no character escaping in the xml output (yes !?!?!)
  31. // - not outputing xml when a wrong input is given (class, attribute names)
  32. //
  33. require_once('../approot.inc.php');
  34. require_once(APPROOT.'/application/application.inc.php');
  35. require_once(APPROOT.'/application/webpage.class.inc.php');
  36. require_once(APPROOT.'/application/csvpage.class.inc.php');
  37. require_once(APPROOT.'/application/clipage.class.inc.php');
  38. require_once(APPROOT.'/application/startup.inc.php');
  39. function UsageAndExit($oP)
  40. {
  41. global $aPageParams;
  42. $bModeCLI = utils::IsModeCLI();
  43. if ($bModeCLI)
  44. {
  45. $oP->p("USAGE:\n");
  46. $oP->p("php -q synchro_exec.php --auth_user=<login> --auth_pwd=<password> --data_sources=<comma_separated_list_of_data_sources>\n");
  47. }
  48. else
  49. {
  50. $oP->p("The parameter 'data_sources' is mandatory, and must contain a comma separated list of data sources\n");
  51. }
  52. $oP->output();
  53. exit -2;
  54. }
  55. function ReadMandatoryParam($oP, $sParam)
  56. {
  57. $sValue = utils::ReadParam($sParam, null, true /* Allow CLI */);
  58. if (is_null($sValue))
  59. {
  60. $oP->p("ERROR: Missing argument '$sParam'\n");
  61. UsageAndExit($oP);
  62. }
  63. return trim($sValue);
  64. }
  65. /////////////////////////////////
  66. // Main program
  67. if (utils::IsModeCLI())
  68. {
  69. $oP = new CLIPage(Dict::S("TitleSynchroExecution"));
  70. // Next steps:
  71. // specific arguments: 'csvfile'
  72. //
  73. $sAuthUser = ReadMandatoryParam($oP, 'auth_user');
  74. $sAuthPwd = ReadMandatoryParam($oP, 'auth_pwd');
  75. $sDataSourcesList = ReadMandatoryParam($oP, 'data_sources');
  76. if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd))
  77. {
  78. UserRights::Login($sAuthUser); // Login & set the user's language
  79. }
  80. else
  81. {
  82. $oP->p("Access restricted or wrong credentials ('$sAuthUser')");
  83. $oP->output();
  84. exit -1;
  85. }
  86. }
  87. else
  88. {
  89. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  90. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  91. $oP = new WebPage(Dict::S("TitleSynchroExecution"));
  92. $sDataSourcesList = utils::ReadParam('data_sources', null, true);
  93. if ($sDataSourcesList == null)
  94. {
  95. UsageAndExit($oP);
  96. }
  97. }
  98. $bSimulate = (utils::ReadParam('simulate', '0', true) == '1');
  99. foreach(explode(',', $sDataSourcesList) as $iSDS)
  100. {
  101. $oSynchroDataSource = MetaModel::GetObject('SynchroDataSource', $iSDS, false);
  102. if ($oSynchroDataSource == null)
  103. {
  104. $oP->p("The data source (id=$iSDS) does not exist. Exiting...");
  105. $oP->output();
  106. exit -3;
  107. }
  108. else
  109. {
  110. if ($bSimulate)
  111. {
  112. CMDBSource::Query('START TRANSACTION');
  113. }
  114. try
  115. {
  116. $oStatLog = $oSynchroDataSource->Synchronize(null);
  117. if ($bSimulate)
  118. {
  119. CMDBSource::Query('ROLLBACK');
  120. }
  121. foreach ($oStatLog->GetTraces() as $sMessage)
  122. {
  123. $oP->p('#'.$sMessage);
  124. }
  125. if ($oStatLog->Get('status') == 'error')
  126. {
  127. $oP->p("ERROR: ".$oStatLog->Get('last_error'));
  128. }
  129. $oP->p("Replicas: ".$oStatLog->Get('stats_nb_replica_total'));
  130. $oP->p("Replicas touched since last synchro: ".$oStatLog->Get('stats_nb_replica_seen'));
  131. $oP->p("Objects deleted: ".$oStatLog->Get('stats_nb_obj_deleted'));
  132. $oP->p("Objects deletion errors: ".$oStatLog->Get('stats_nb_obj_deleted_errors'));
  133. $oP->p("Objects obsoleted: ".$oStatLog->Get('stats_nb_obj_obsoleted'));
  134. $oP->p("Objects obsolescence errors: ".$oStatLog->Get('stats_nb_obj_obsoleted_errors'));
  135. $oP->p("Objects created: ".$oStatLog->Get('stats_nb_obj_created'));
  136. $oP->p("Objects creation errors: ".$oStatLog->Get('stats_nb_obj_created_errors'));
  137. $oP->p("Objects updated: ".$oStatLog->Get('stats_nb_obj_updated'));
  138. $oP->p("Objects update errors: ".$oStatLog->Get('stats_nb_obj_updated_errors'));
  139. $oP->p("Objects reconciled (updated): ".$oStatLog->Get('stats_nb_obj_new_updated'));
  140. $oP->p("Objects reconciled (unchanged): ".$oStatLog->Get('stats_nb_obj_new_unchanged'));
  141. $oP->p("Objects reconciliation errors: ".$oStatLog->Get('stats_nb_replica_reconciled_errors'));
  142. $oP->p("Replica disappeared, no action taken: ".$oStatLog->Get('stats_nb_replica_disappeared_no_action'));
  143. }
  144. catch(Exception $e)
  145. {
  146. $oP->add($e->getMessage());
  147. if ($bSimulate)
  148. {
  149. CMDBSource::Query('ROLLBACK');
  150. }
  151. }
  152. }
  153. }
  154. $oP->output();
  155. ?>