control.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /**
  3. * The control file of zahost 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 Jianhua Wang <wangjianhua@easycorp.ltd>
  8. * @package zahost
  9. * @version $Id$
  10. * @link https://www.zentao.net
  11. */
  12. class zahost extends control
  13. {
  14. /**
  15. * 展示宿主机列表。
  16. * View host list.
  17. *
  18. * @param string $browseType
  19. * @param int $param
  20. * @param string $orderBy
  21. * @param int $recTotal
  22. * @param int $recPerPage
  23. * @param int $pageID
  24. * @access public
  25. * @return void
  26. */
  27. public function browse($browseType = 'all', $param = 0, $orderBy = 'id_desc', $recTotal = 0, $recPerPage = 20, $pageID = 1)
  28. {
  29. $this->app->session->set('zahostList', $this->app->getURI(true));
  30. /* 构建搜索表单。*/
  31. /* Build the search form. */
  32. $param = (int)$param;
  33. $actionURL = $this->createLink('zahost', 'browse', "browseType=bySearch&param=myQueryID");
  34. $this->config->zahost->search['actionURL'] = $actionURL;
  35. $this->config->zahost->search['queryID'] = $param;
  36. $this->config->zahost->search['onMenuBar'] = 'no';
  37. $this->loadModel('search')->setSearchParams($this->config->zahost->search);
  38. $browseType = strtolower($browseType);
  39. $this->app->loadClass('pager', true);
  40. $pager = pager::init($recTotal, $recPerPage, $pageID);
  41. $this->view->title = $this->lang->zahost->common;
  42. $this->view->hostList = $this->zahost->getList($browseType, $param, $orderBy, $pager);
  43. $this->view->nodeList = $this->zahost->getNodeGroupHost();
  44. $this->view->users = $this->loadModel('user')->getPairs('noletter,noempty,noclosed');
  45. $this->view->pager = $pager;
  46. $this->view->param = $param;
  47. $this->view->orderBy = $orderBy;
  48. $this->view->browseType = $browseType;
  49. $this->display();
  50. }
  51. /**
  52. * 展示宿主机详情。
  53. * View host.
  54. *
  55. * @param int $id
  56. * @access public
  57. * @return void
  58. */
  59. public function view($id)
  60. {
  61. $zahost = $this->zahost->getById($id);
  62. $this->view->title = $this->lang->zahost->view;
  63. $this->view->zahost = $zahost;
  64. $this->view->nodeList = $this->loadModel('zanode')->getListByHost($id);
  65. $this->view->initBash = sprintf($this->config->zahost->initBash, $zahost->secret, getWebRoot(true));
  66. $this->view->actions = $this->loadModel('action')->getList('zahost', $id);
  67. $this->display();
  68. }
  69. /**
  70. * 创建宿主机。
  71. * Create host.
  72. *
  73. * @access public
  74. * @return void
  75. */
  76. public function create()
  77. {
  78. if($_POST)
  79. {
  80. $hostInfo = form::data($this->config->zahost->form->create)
  81. ->add('createdBy', isset($this->app->user->account) ? $this->app->user->account : '')
  82. ->get();
  83. $hostInfo->secret = md5($hostInfo->name . time());
  84. $hostID = $this->zahost->create($hostInfo);
  85. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  86. if($hostID === false) return $this->send(array('result' => 'fail', 'message' => array('extranet' => array($this->lang->zahost->netError))));
  87. if(isInModal()) return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'closeModal' => true, 'callback' => 'parent.loadHosts()'));
  88. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => $this->createLink('zahost', 'view', "hostID=$hostID")));
  89. }
  90. $this->view->title = $this->lang->zahost->create;
  91. $this->display();
  92. }
  93. /**
  94. * 编辑宿主机。
  95. * Edit host.
  96. *
  97. * @param int $hostID
  98. * @access public
  99. * @return void
  100. */
  101. public function edit($hostID)
  102. {
  103. if($_POST)
  104. {
  105. $hostInfo = form::data($this->config->zahost->form->edit)
  106. ->add('id', $hostID)
  107. ->add('editedBy', isset($this->app->user->account) ? $this->app->user->account : '')
  108. ->get();
  109. $changes = $this->zahost->update($hostInfo);
  110. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  111. if($changes === false) return $this->send(array('result' => 'fail', 'message' => array('extranet' => array($this->lang->zahost->netError))));
  112. if(!empty($changes))
  113. {
  114. $actionID = $this->loadModel('action')->create('zahost', $hostID, 'Edited');
  115. $this->action->logHistory($actionID, $changes);
  116. }
  117. if(isInModal()) return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'closeModal' => true, 'load' => true));
  118. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => inlink('browse')));
  119. }
  120. $this->view->title = $this->lang->zahost->editAction;
  121. $this->view->host = $this->zahost->getById($hostID);
  122. $this->display();
  123. }
  124. /**
  125. * 删除宿主机。
  126. * Delete host.
  127. *
  128. * @param int $hostID
  129. * @access public
  130. * @return void
  131. */
  132. public function delete($hostID)
  133. {
  134. $this->zahost->delete(TABLE_ZAHOST, $hostID);
  135. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  136. if(isInModal()) return $this->send(array('result' => 'success', 'load' => true));
  137. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => $this->createLink('zahost', 'browse')));
  138. }
  139. /**
  140. * 显示镜像列表。
  141. * Show image list page.
  142. *
  143. * @param int $hostID
  144. * @param string $browseType
  145. * @param int $param
  146. * @param string $orderBy
  147. * @param int $recTotal
  148. * @param int $recPerPage
  149. * @param int $pageID
  150. * @access public
  151. * @return void
  152. */
  153. public function browseImage($hostID, $orderBy = 'id', $recTotal = 0, $recPerPage = 20, $pageID = 1)
  154. {
  155. $this->session->set('imageList', $this->app->getURI(true));
  156. $this->app->loadClass('pager', $static = true);
  157. $pager = pager::init($recTotal, $recPerPage, $pageID);
  158. $this->view->title = $this->lang->zahost->image->browseImage;
  159. $this->view->hostID = $hostID;
  160. $this->view->imageList = $this->zahost->getImageList($hostID, $orderBy, $pager);
  161. $this->view->pager = $pager;
  162. $this->view->orderBy = $orderBy;
  163. $this->display();
  164. }
  165. /**
  166. * 下载镜像。
  167. * Sent download image request to Host.
  168. *
  169. * @param int $hostID
  170. * @param int $imageID
  171. * @access public
  172. * @return object
  173. */
  174. public function downloadImage($hostID, $imageID)
  175. {
  176. $image = $this->zahost->getImageByID($imageID);
  177. $this->zahost->downloadImage($image);
  178. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => $this->lang->zahost->image->downloadImageFail));
  179. if(helper::isAjaxRequest()) return $this->send(array('result' => 'success', 'load' => 'modal'));
  180. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => $this->createLink('zahost', 'browseImage', array("hostID" => $hostID))));
  181. }
  182. /**
  183. * 查询镜像下载进度。
  184. * Query downloading progress of images of host.
  185. *
  186. * @param int $hostID
  187. * @access public
  188. * @return void
  189. */
  190. public function ajaxImageDownloadProgress($hostID)
  191. {
  192. $statusList = array();
  193. $imageList = $this->zahost->getImageList($hostID);
  194. foreach($imageList as $image)
  195. {
  196. $this->zahost->queryDownloadImageStatus($image);
  197. $statusName = zget($this->lang->zahost->image->statusList, $image->status, '');
  198. $progress = '';
  199. if($image->status == 'inprogress') $progress = $image->rate * 100 . '%';
  200. if($image->status == 'completed') $progress = '100%';
  201. $statusList[$image->id] = array('statusCode' => $image->status, 'status' => $statusName, 'progress' => $progress, 'path' => $image->path);
  202. }
  203. return $this->send(array('result' => 'success', 'message' => '', 'data' => $statusList));
  204. }
  205. /**
  206. * 取消镜像下载。
  207. * Sent cancel download image request to Host.
  208. *
  209. * @param int $imageID
  210. * @access public
  211. * @return object
  212. */
  213. public function cancelDownload($imageID)
  214. {
  215. $image = $this->zahost->getImageByID($imageID);
  216. $this->zahost->cancelDownload($image);
  217. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => $this->lang->zahost->image->downloadImageFail));
  218. if(helper::isAjaxRequest()) return $this->send(array('result' => 'success', 'load' => 'modal'));
  219. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => $this->createLink('zahost', 'browseImage', array("hostID" => $image->host))));
  220. }
  221. /**
  222. * Ajax 方式获取服务状态。
  223. * Check service status by ajax.
  224. *
  225. * @param int $hostID
  226. * @access public
  227. * @return void
  228. */
  229. public function ajaxGetServiceStatus($hostID)
  230. {
  231. $host = $this->zahost->getByID($hostID);
  232. $serviceStatus = $this->zahostZen->getServiceStatus($host);
  233. return $this->send(array('result' => 'success', 'message' => '', 'data' => $serviceStatus));
  234. }
  235. /**
  236. * Ajax 方式获取镜像列表。
  237. * Ajax:get hosts.
  238. *
  239. * @access public
  240. * @return void
  241. */
  242. public function ajaxGetHosts()
  243. {
  244. $hostList = $this->zahost->getPairs();
  245. $options = array();
  246. foreach($hostList as $key => $host) $options[] = array('text' => $host, 'value' => $key);
  247. return print(json_encode($options));
  248. }
  249. }