hostheartbeat.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * The host entry point of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  6. * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Yuchun Li <liyuchun@easycorp.ltd>
  8. * @package entries
  9. * @version 1
  10. * @link https://www.zentao.net
  11. */
  12. class hostHeartbeatEntry extends baseEntry
  13. {
  14. /**
  15. * Listen host heartbeat.
  16. *
  17. * @param int|string $userID
  18. * @access public
  19. * @return void
  20. */
  21. public function post()
  22. {
  23. /* Check authorize. */
  24. $header = getallheaders();
  25. $token = isset($header['Authorization']) ? substr($header['Authorization'], 7) : '';
  26. $secret = isset($this->requestBody->secret) ? $this->requestBody->secret : '';
  27. if(!$secret and !$token) return $this->sendError(401, 'Unauthorized');
  28. /* Check param. */
  29. $status = $this->requestBody->status;
  30. $vms = !empty($this->requestBody->Vms) ? $this->requestBody->Vms : array();
  31. $zap = !empty($this->requestBody->port) ? $this->requestBody->port : 0;
  32. $now = helper::now();
  33. if(!$status) return $this->sendError(400, 'Params error.');
  34. $conditionField = $secret ? 'secret' : 'tokenSN';
  35. $conditionValue = $secret ? $secret : $token;
  36. $this->dao = $this->loadModel('common')->dao;
  37. $hostInfo = $this->dao->select('id,tokenSN,hostType,type')->from(TABLE_ZAHOST)
  38. ->beginIF($secret)->where('secret')->eq($secret)->fi()
  39. ->beginIF(!$secret)->where('tokenSN')->eq($token)
  40. ->andWhere('tokenTime')->gt($now)->fi()
  41. ->fetch();
  42. if(empty($hostInfo->id))
  43. {
  44. if(empty($token)) return $this->sendError(400, 'Secret error.');
  45. $hostInfo = $this->dao->select('id,tokenSN,hostType,type')->from(TABLE_ZAHOST)
  46. ->where('oldTokenSN')->eq($token)
  47. ->andWhere('tokenTime')->gt(date(DT_DATETIME1, strtotime($now) - 30))->fi()
  48. ->fetch();
  49. if(empty($hostInfo->id)) return $this->sendError(400, 'Secret error.');
  50. }
  51. $isPhysicsNode = $hostInfo->type == 'node' && $hostInfo->hostType == 'physics' ? true : false;
  52. $host = new stdclass();
  53. $host->status = $status;
  54. if($secret)
  55. {
  56. $host->tokenSN = md5($secret . $now);
  57. $host->tokenTime = date('Y-m-d H:i:s', time() + 7200);
  58. $host->oldTokenSN = $hostInfo->tokenSN;
  59. }
  60. $host->heartbeat = $now;
  61. $host->zap = $zap;
  62. $this->dao->update(TABLE_ZAHOST)->data($host)->where($conditionField)->eq($conditionValue)->exec();
  63. if($vms)
  64. {
  65. foreach($vms as $vm)
  66. {
  67. $vmData = array(
  68. 'vnc' => $vm->vncPortOnHost,
  69. 'zap' => $vm->agentPortOnHost,
  70. 'ztf' => $vm->ztfPortOnHost,
  71. 'zd' => $vm->zdPortOnHost,
  72. 'ssh' => $vm->sshPortOnHost,
  73. 'status' => $vm->status,
  74. 'extranet' => $vm->ip,
  75. 'heartbeat' => helper::now()
  76. );
  77. if(!$vm->sshPortOnHost) unset($vmData['ssh']);
  78. if($isPhysicsNode)
  79. {
  80. unset($vmData['extranet']);
  81. $this->dao->update(TABLE_ZAHOST)->data($vmData)->where('id')->eq($hostInfo->id)->exec();
  82. }
  83. else
  84. {
  85. $node = $this->loadModel('zanode')->getNodeByMac($vm->macAddress);
  86. if(empty($node)) continue;
  87. if(in_array($node->status, array('restoring', 'creating_img', 'creating_snap')))
  88. $vmData['status'] = $vm->status = $node->status;
  89. $this->dao->update(TABLE_ZAHOST)->data($vmData)->where('mac')->eq($vm->macAddress)->exec();
  90. }
  91. if($vm->status == 'running' && !$isPhysicsNode)
  92. {
  93. if(!empty($node))
  94. {
  95. $snaps = $this->loadModel('zanode')->getSnapshotList($node->id);
  96. if(empty($snaps))
  97. {
  98. if($vm->status == 'running') $this->loadModel('zanode')->createDefaultSnapshot($node->id);
  99. }
  100. }
  101. }
  102. }
  103. }
  104. if(!$secret) return $this->sendSuccess(200, 'success');
  105. $host->tokenTimeUnix = strtotime($host->tokenTime);
  106. unset($host->status);
  107. unset($host->tokenTime);
  108. unset($host->oldTokenSN);
  109. return $this->send(200, $host);
  110. }
  111. }