Migration.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\task;
  3. use \Exception;
  4. use MGModule\DNSManager2\mgLibs\custom\dns\record\Record;
  5. use \MGModule\DNSManager2\mgLibs\custom\manager;
  6. use \MGModule\DNSManager2\mgLibs\custom\manager\DefaultNotifications;
  7. use \MGModule\DNSManager2\mgLibs\custom\manager\EmailNotificationHelper;
  8. use \MGModule\DNSManager2\mgLibs\custom\manager\NotificationHelper;
  9. use \MGModule\DNSManager2\mgLibs\custom\TaskManager;
  10. use \MGModule\DNSManager2\models\custom\globalsetting\GlobalSettingEnum;
  11. use \MGModule\DNSManager2\models\custom\server;
  12. use \MGModule\DNSManager2\models\custom\task;
  13. use \MGModule\DNSManager2\mgLibs\custom\manager\LogHelper;
  14. use MGModule\DNSManager2\models\custom\zone\Repository;
  15. use MGModule\DNSManager2\models\custom\zone\Zone;
  16. class Migration extends TaskAbstract
  17. {
  18. protected $taskTypeCode = TaskTypeCodesCodes::MIGRATION;
  19. protected $abort_after_repeats = array('main' => 0);
  20. public function mainDescription()
  21. {
  22. $server_from = new server\Server($this->getParams('from'));
  23. $server_to = new server\Server($this->getParams('to'));
  24. return 'Migration from ' . $server_from->name . ' to ' . $server_to->name;
  25. }
  26. public function main($params)
  27. {
  28. $cron_migrator_run_each = manager\GlobalSettingHelper::getSetting(GlobalSettingEnum::CRON_MIGRATION_RUN_EACH? : 5);
  29. if(!$this->isReadyToRun( $cron_migrator_run_each ))
  30. {
  31. return true;
  32. }
  33. LogHelper::addSuccessLog('Cron Migrator', 'Cron Migrator Started');
  34. if($this->getStatus() == task\TaskStatusEnum::START)
  35. {
  36. $this->addChild('fetchZonesList')->run();
  37. $this->setStatus(task\TaskStatusEnum::IN_PROGRESS);
  38. }
  39. elseif($this->getStatus() == task\TaskStatusEnum::IN_PROGRESS)
  40. {
  41. $zones_per_run = manager\GlobalSettingHelper::getSetting(GlobalSettingEnum::CRON_MIGRATION_ZONES_PER_RUN);
  42. $childs = $this->getXChilds($zones_per_run, 'migrate', task\TaskStatusEnum::START);
  43. if(count($childs) < $zones_per_run)
  44. {
  45. $childs += $this->getXChilds($zones_per_run - count($childs), 'migrate');
  46. }
  47. $success = array();
  48. $error = array();
  49. foreach($childs as $child)
  50. {
  51. $child->run();
  52. if($child->getStatus() === task\TaskStatusEnum::FINISHED)
  53. {
  54. $success[] = $child->getParams('domain');
  55. }
  56. else
  57. {
  58. $result = $child->getResults(true);
  59. $error[$child->getParams('domain')] = is_null($result)?'Unknown reason':$result->data['error'];
  60. }
  61. }
  62. $migrated = count($childs);
  63. if($migrated > 0)
  64. {
  65. $count_success = count($success);
  66. $count_error = count($error);
  67. EmailNotificationHelper::sendAdminNotification(
  68. DefaultNotifications::ADMIN_CRON_MIGRATOR_NOTIFICATION,
  69. array(
  70. 'zones_migrated_count' => $migrated,
  71. 'zones_migrated_successfully_count' => $count_success,
  72. 'zones_migrated_successfully' => $success,
  73. 'zones_migrated_unsuccessfully_count' => $count_error,
  74. 'zones_migrated_unsuccessfully' => $error,
  75. )
  76. );
  77. if($count_success > 0)
  78. {
  79. $this->setFinishedStatusOnCronRun('migrate', $zones_per_run);
  80. $zonesCountWord = $count_success == 1 ? 'zone' : 'zones';
  81. NotificationHelper::addInfoNotification("Cron Migrator - ".$count_success." ".$zonesCountWord." migrated");
  82. LogHelper::addSuccessLog('Cron Migrator', 'End Of Cron Migrator Run - '.$count_success.' '.$zonesCountWord.' migrated');
  83. }
  84. if((int)$count_error > 0)
  85. {
  86. $zonesCountWord = $count_error == 1 ? 'zone' : 'zones';
  87. NotificationHelper::addProblemNotification("Cron Migrator - ".$count_error." ".$zonesCountWord." not migrated");
  88. LogHelper::addSuccessLog('Cron Migrator', 'End Of Cron Migrator Run - '.$count_error.' '.$zonesCountWord.' not migrated');
  89. }
  90. }
  91. }
  92. else
  93. {
  94. $this->setStatus(task\TaskStatusEnum::FINISHED);
  95. LogHelper::addSuccessLog('Cron Migrator', 'End Of Cron Migrator Run');
  96. }
  97. }
  98. public function fetchZonesList($params)
  99. {
  100. if($this->isInCliMode())
  101. {
  102. LogHelper::addSuccessLog('Cron Migrator', 'Fetching Zones List For Server Id: '.$this->parent->getParams('from').' Started.');
  103. }
  104. $server_from = new server\Server($this->parent->getParams('from'));
  105. foreach($server_from->getModule()->getZones() as $domain => $ip)
  106. {
  107. $this->addResult(array(
  108. 'domain' => $domain,
  109. 'ip' => $ip,
  110. ));
  111. }
  112. if($this->isInCliMode())
  113. {
  114. LogHelper::addSuccessLog('Cron Migrator', 'Fetching Zones List For Server Id: '.$this->parent->getParams('from').' Started.');
  115. }
  116. }
  117. public function migrate($params)
  118. {
  119. try
  120. {
  121. $server_from = new server\Server($this->parent->getParams('from'));
  122. $server_to = new server\Server($this->parent->getParams('to'));
  123. $module_from = $server_from->getModule();
  124. $module_from->setDomain($params['domain']);
  125. $module_to = $server_to->getModule();
  126. $module_to->setDomain($params['domain']);
  127. if($module_to->isIPRequired())
  128. {
  129. $module_to->setIP($params['ip']);
  130. }
  131. if(!$module_to->zoneExists())
  132. {
  133. $module_to->activateZone();
  134. }
  135. //TODO: czy usuwać stare rekordy?
  136. /** @var Record $record */
  137. foreach($module_from->getRecords() as $record)
  138. {
  139. try
  140. {
  141. $record->nameToAbsolute($params['domain']);
  142. $module_to->addRecord($record);
  143. }
  144. catch(Exception $exc)
  145. {
  146. //TODO: dodać logi albo uwalić migracje
  147. LogHelper::addFailLog('Cron Migrator - Addiing New Record Failed', $exc->getMessage());
  148. }
  149. }
  150. /** @var Zone $zone */
  151. $zone = (new Repository())->byName($params['domain'])->one();
  152. if( $zone && $this->parent->getParams('move_zone') )
  153. {
  154. $zone->serverid = (int)$this->parent->getParams('to');
  155. $zone->save();
  156. }
  157. $this->setStatus(task\TaskStatusEnum::FINISHED);
  158. $result = TaskManager::getTaskResultByID($this->getParams('resultid'));
  159. $result->data['status'] = 'migrated';
  160. $result->save();
  161. LogHelper::addSuccessLog('Cron Migrator - Migration Zone', 'Zone Migrated - '.$params['domain']);
  162. }
  163. catch(Exception $exc)
  164. {
  165. $result = TaskManager::getTaskResultByID($this->getParams('resultid'));
  166. $result->data['status'] = $exc->getMessage();
  167. $result->save();
  168. LogHelper::addFailLog('Cron Migrator - Migration Failed', $exc->getMessage());
  169. }
  170. }
  171. }