zen.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * The zen file of zanode module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
  6. * @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Qiyu Xie <xieqiyu@easycorp.ltd>
  8. * @package admin
  9. * @link https://www.zentao.net
  10. */
  11. class zanodeZen extends zanode
  12. {
  13. /**
  14. * 操作执行节点。
  15. * Handle node.
  16. *
  17. * @param int $nodeID
  18. * @param string $type boot|destroy|suspend|reboot|resume
  19. * @access protected
  20. * @return void
  21. */
  22. protected function handleNode($nodeID, $type)
  23. {
  24. $node = $this->zanode->getNodeByID($nodeID);
  25. if(in_array($node->status, array('restoring', 'creating_img', 'creating_snap')))
  26. {
  27. return $this->sendError(sprintf($this->lang->zanode->busy, $this->lang->zanode->statusList[$node->status]), true);
  28. }
  29. $url = 'http://' . $node->ip . ':' . $node->hzap . '/api/v1/kvm/' . $node->name . '/' . $type;
  30. $param = array('vmUniqueName' => $node->name);
  31. $result = commonModel::http($url, $param, array(), array("Authorization:$node->tokenSN"), 'data', 'POST', 10);
  32. $result = json_decode($result, true);
  33. if(empty($result)) return $this->sendError($this->lang->zanode->notFoundAgent, true);
  34. if($result['code'] != 'success') return $this->sendError(zget($this->lang->zanode->apiError, $result['code'], $result['msg']), true);
  35. if($type != 'reboot')
  36. {
  37. $status = $type == 'suspend' ? 'suspend' : 'running';
  38. if($type == 'destroy') $status = 'shutoff';
  39. $this->dao->update(TABLE_ZAHOST)->set('status')->eq($status)->where('id')->eq($nodeID)->exec();
  40. }
  41. $this->loadModel('action')->create('zanode', $nodeID, ucfirst($type));
  42. return $this->sendSuccess(array('message' => $this->lang->zanode->actionSuccess, 'load' => true));
  43. }
  44. /**
  45. * 处理创建 zanode 请求数据。
  46. * Processing request data for creating zanode.
  47. *
  48. * @access protected
  49. * @return object
  50. */
  51. protected function prepareCreateExtras()
  52. {
  53. if($this->post->hostType == 'physics') $this->config->zanode->create->requiredFields = $this->config->zanode->create->physicsRequiredFields;
  54. if($this->loadModel('zahost')->hiddenHost() || $this->post->hostType == 'physics')
  55. {
  56. $this->config->zanode->form->create['memory']['required'] = false;
  57. $this->config->zanode->form->create['diskSize']['required'] = false;
  58. }
  59. $data = form::data()
  60. ->setDefault('type', 'node')
  61. ->setDefault('createdDate', helper::now())
  62. ->setDefault('createdBy', $this->app->user->account)
  63. ->setDefault('status', 'running')
  64. ->setIF($this->post->hostType != 'physics', 'hostType', '')
  65. ->setIF($this->post->hostType == 'physics', 'parent', 0)
  66. ->setIF($this->post->hostType == 'physics', 'osName', $this->post->osNamePhysics)
  67. ->setIF($this->post->hostType == 'physics', 'secret', md5($this->post->name . time()))
  68. ->setIF($this->post->hostType == 'physics', 'status', 'offline')
  69. ->get();
  70. $checkResult = $this->zanode->checkFields4Create($data);
  71. if(!$checkResult) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  72. if($data->hostType != 'physics')
  73. {
  74. $result = $this->zanode->linkAgentService($data);
  75. if(!$result) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  76. $data->mac = $result->data->mac;
  77. $data->vnc = (int)$result->data->vnc;
  78. }
  79. return $data;
  80. }
  81. /**
  82. * 处理创建快照请求数据。
  83. * Processing request data for creating snapshot.
  84. *
  85. * @param int nodeID
  86. * @access protected
  87. * @return object
  88. */
  89. protected function prepareCreateSnapshotExtras($node)
  90. {
  91. $data = form::data()->get();
  92. if(is_numeric($data->name)) return $this->sendError(array('name' => sprintf($this->lang->error->code, $this->lang->zanode->name)));
  93. $snapshot = new stdClass();
  94. $snapshot->host = $node->id;
  95. $snapshot->name = $data->name;
  96. $snapshot->desc = $data->desc;
  97. $snapshot->status = 'creating';
  98. $snapshot->osName = $node->osName;
  99. $snapshot->memory = 0;
  100. $snapshot->disk = 0;
  101. $snapshot->fileSize = 0;
  102. $snapshot->from = 'snapshot';
  103. $snapshot->createdBy = $this->app->user->account;
  104. $snapshot->createdDate = helper::now();
  105. return $snapshot;
  106. }
  107. /**
  108. * 获取导出镜像状态。
  109. * Get task status by zagent api.
  110. *
  111. * @param object $node
  112. * @param int $taskID
  113. * @param string $type
  114. * @param string $status
  115. * @access protected
  116. * @return false|array
  117. */
  118. protected function getTaskStatus($node, $taskID = 0, $type = '', $status = '')
  119. {
  120. $agnetUrl = 'http://' . $node->ip . ':' . $node->hzap . '/api/v1/task/getStatus';
  121. $result = json_decode(commonModel::http($agnetUrl, array(), array(CURLOPT_CUSTOMREQUEST => 'POST'), array("Authorization:$node->tokenSN"), 'json', 'POST', 10));
  122. if(empty($result) || $result->code != 'success') return false;
  123. $data = $result->data;
  124. if(empty($data)) return array();
  125. if($status && !$taskID && isset($data->$status)) return $data->$status;
  126. if(!$taskID) return $data;
  127. foreach($data as $status => $tasks)
  128. {
  129. if(empty($tasks)) continue;
  130. foreach($tasks as $task)
  131. {
  132. if(!empty($tasks['inprogress']) && $task->task != $tasks['inprogress'][0]->task && $task->status == 'created') $task->status = 'pending';
  133. if($type == $task->type && $taskID == $task->task) return $task;
  134. }
  135. }
  136. return $result;
  137. }
  138. /**
  139. * 获取宿主机中zagent、nginx、websockify、novnc的运行及安装状态.
  140. * Get service status from host.
  141. *
  142. * @param object $node
  143. * @access protected
  144. * @return array
  145. */
  146. protected function getServiceStatus($node)
  147. {
  148. $result = json_decode(commonModel::http("http://{$node->ip}:{$node->zap}/api/v1/service/check", json_encode(array('services' => 'all')), array(), array("Authorization:$node->tokenSN"), 'data', 'POST', 10));
  149. if(empty($result->data->ztfStatus) || $result->code != 'success') return $this->lang->zanode->init->serviceStatus;
  150. return array('ZenAgent' => 'ready', 'ZTF' => $result->data->ztfStatus);
  151. }
  152. /**
  153. * 执行节点安装应用(支持ztf,zendata).
  154. * Install service to node by name.
  155. *
  156. * @param object $node
  157. * @param string $name
  158. * @access protected
  159. * @return array
  160. */
  161. protected function installService($node, $name)
  162. {
  163. $param = array(
  164. 'name' => strtolower($name),
  165. 'secret' => $node->secret,
  166. 'server' => getWebRoot(true),
  167. );
  168. $result = json_decode(commonModel::http("http://{$node->ip}:{$node->zap}/api/v1/service/setup", json_encode($param), array(), array("Authorization:$node->tokenSN")));
  169. if(empty($result->data) || $result->code != 'success') return $this->lang->zanode->init->serviceStatus;
  170. return array('ZenAgent' => 'ready', 'ZTF' => $result->data->ztfStatus,);
  171. }
  172. }