From 38b5b41a6498596a7dd04bb4480887f0a3b10d44 Mon Sep 17 00:00:00 2001 From: wangjianhua Date: Fri, 11 Nov 2022 08:58:23 +0800 Subject: [PATCH] * Add image list page of zahost module. --- config/zentaopms.php | 1 + module/zahost/control.php | 63 ++++++++++++++++-- module/zahost/lang/zh-cn.php | 26 ++++++++ module/zahost/model.php | 85 ++++++++++++++++++------- module/zahost/view/browseimage.html.php | 82 ++++++++++++++++++++++++ module/zahost/view/createimage.html.php | 79 +++++++++++++++++++++++ module/zahost/view/editimage.html.php | 80 +++++++++++++++++++++++ 7 files changed, 390 insertions(+), 26 deletions(-) create mode 100755 module/zahost/view/browseimage.html.php create mode 100644 module/zahost/view/createimage.html.php create mode 100644 module/zahost/view/editimage.html.php diff --git a/config/zentaopms.php b/config/zentaopms.php index e542fd9dd3..4f694bbca5 100644 --- a/config/zentaopms.php +++ b/config/zentaopms.php @@ -254,6 +254,7 @@ define('TABLE_TESTRUN', '`' . $config->db->prefix . 'testrun`'); define('TABLE_TESTRESULT', '`' . $config->db->prefix . 'testresult`'); define('TABLE_USERTPL', '`' . $config->db->prefix . 'usertpl`'); define('TABLE_ZAHOST', '`' . $config->db->prefix . 'host`'); +define('TABLE_IMAGE', '`' . $config->db->prefix . 'image`'); if(!defined('TABLE_ASSET')) define('TABLE_ASSET', '`' . $config->db->prefix . 'asset`'); if(!defined('TABLE_VMTEMPLATE')) define('TABLE_VMTEMPLATE', '`' . $config->db->prefix . 'vmtemplate`'); diff --git a/module/zahost/control.php b/module/zahost/control.php index def6586261..cc9f562933 100644 --- a/module/zahost/control.php +++ b/module/zahost/control.php @@ -143,6 +143,63 @@ class zahost extends control return print(js::locate($this->createLink('zahost', 'browse'), 'parent')); } + public function browseImage($hostID, $browseType = 'all', $param = 0, $orderBy = 'id', $recTotal = 0, $recPerPage = 20, $pageID = 1) + { + $this->app->session->set('imageList', $this->app->getURI(true)); + $this->app->loadClass('pager', $static = true); + $pager = pager::init($recTotal, $recPerPage, $pageID); + + $imageList = $this->zahost->getImageList($hostID, $browseType, $param, $orderBy, $pager); + + $this->view->title = $this->lang->zahost->image->list; + $this->view->hostID = $hostID; + $this->view->imageList = $imageList; + $this->view->pager = $pager; + $this->view->param = $param; + $this->view->orderBy = $orderBy; + $this->view->browseType = $browseType; + + $this->display(); + } + + /** + * Create template. + * + * @access public + * @return void + */ + public function createImage($hostID) + { + $host = $this->zahost->getById($hostID); + if($_POST) + { + $this->zahost->createImage(); + if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError())); + + return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => inLink('browseTemplate', "id={$hostID}"))); + } + + $this->view->imageOptions = array('' => $this->lang->zahost->notice->loading); + $this->view->host = $host; + $this->display(); + } + + /** + * Sent download image request to Host. + * + * @param int $imageID + * @access public + * @return object + */ + public function ajaxDownloadImage($imageID) + { + $image = $this->zahost->getImageByID($imageID); + + $this->zahost->downloadImage($image); + + return $this->send(array('result' => 'success', 'message' => $this->lang->zahost->image->startDowload))); + } + /** * Create template. * @@ -302,11 +359,9 @@ class zahost extends control * @access public * @return void */ - public function ajaxImageList($hostID, $templateID = 0) + public function ajaxImageList() { - $host = $this->zahost->getById($hostID); - $imageList = $this->zahost->getImageList($host, $templateID); - + $imageList = array('1' => 'Ubuntu20'); if($imageList) return $this->send(array('result' => 'success', 'message' => '', 'data' => $imageList)); return $this->send(array('result' => 'fail', 'message' => array('imageName' => array($this->lang->zahost->notice->noImage)))); diff --git a/module/zahost/lang/zh-cn.php b/module/zahost/lang/zh-cn.php index a0c0e9110d..18434030b5 100644 --- a/module/zahost/lang/zh-cn.php +++ b/module/zahost/lang/zh-cn.php @@ -39,6 +39,32 @@ $lang->zahost->osVersion = '操作系统版本号'; $lang->zahost->osLang = '系统语言'; $lang->zahost->imageName = '镜像文件'; +$lang->zahost->image = new stdclass; +$lang->zahost->image->list = '镜像列表'; +$lang->zahost->image->browseImage = '镜像列表'; +$lang->zahost->image->createImage = '创建镜像'; +$lang->zahost->image->choseImage = '选择镜像'; +$lang->zahost->image->downloadImage = '下载镜像'; +$lang->zahost->image->startDowload = '开始下载'; + +$lang->zahost->image->name = '名称'; +$lang->zahost->image->common = '镜像'; +$lang->zahost->image->memory = $lang->zahost->memory; +$lang->zahost->image->disk = $lang->zahost->diskSize; +$lang->zahost->image->osType = $lang->zahost->osType; +$lang->zahost->image->osCategory = $lang->zahost->osCategory; +$lang->zahost->image->osVersion = $lang->zahost->osVersion; +$lang->zahost->image->osLang = $lang->zahost->osLang; +$lang->zahost->image->imageName = $lang->zahost->imageName; + +$lang->zahost->image->imageEmpty = '无镜像'; + +$lang->zahost->image->statusList['waiting'] = '未下载'; +$lang->zahost->image->statusList['downloading'] = '下载中'; +$lang->zahost->image->statusList['finish'] = '已下载'; +$lang->zahost->image->statusList['fail'] = '下载失败'; + + $lang->zahost->vmTemplate = new stdclass; $lang->zahost->vmTemplate->name = '名称'; $lang->zahost->vmTemplate->common = '虚拟机模板'; diff --git a/module/zahost/model.php b/module/zahost/model.php index 08f956f26e..cdeffe3196 100644 --- a/module/zahost/model.php +++ b/module/zahost/model.php @@ -171,39 +171,80 @@ class zahostModel extends model return common::createChanges($oldHost, $templateInfo); } + /** + * Get image by ID. + * + * @param int $imageID + * @access public + * @return object + */ + public function getImageByID($imageID) + { + return $this->dao->select('*')->from(TABLE_IMAGE)->where('deleted')->eq(0)->andWhere('id')->eq($imageID)->fetch(); + } + /** * Get image files from ZAgent server. * - * @param object $host - * @param int $templateID + * @param object $hostID * @access public * @return array */ - public function getImageList($host, $templateID = 0) + public function getImageList($hostID, $browseType = 'all', $param = 0, $orderBy = 'id', $pager = null) { - $result = json_decode(commonModel::http("http://{$host->publicIP}:8086/api/v1/kvm/listTmpl")); - if(empty($result) || $result->code != 200) - { - $result = new stdclass; - $result->data = array(); - } - - $usedImageList = $this->dao->select('imageName')->from(TABLE_VMTEMPLATE)->where('hostID')->eq($host->hostID)->fetchAll('imageName'); - - $imageList = array(); - foreach($result->data as $image) - { - if(array_key_exists($image->name, $usedImageList)) continue; - - $imageList[] = array('name' => $image->name); - } - - /* Template ID is not empty means editing template. */ - if($templateID) foreach($usedImageList as $usedImage) $imageList[] = array('name' => $usedImage->imageName); + $imageList = $this->dao->select('*')->from(TABLE_IMAGE) + ->where('hostID')->eq($hostID) + ->orderBy($orderBy) + ->page($pager) + ->fetchAll(); return $imageList; } + /** + * create image. + * + * @access public + * @return void + */ + public function createImage() + { + $vmImage = fixer::input('post') + ->get(); + + $this->dao->insert(TABLE_IMAGE)->data($vmImage) + ->batchCheck($this->config->zahost->createimage->requiredFields, 'notempty') + ->autoCheck()->exec(); + if(dao::isError()) return false; + + $imageID = $this->dao->lastInsertID(); + $this->loadModel('action')->create('image', $imageID, 'Created'); + } + + /** + * Send download image command to HOST. + * + * @param object $image + * @access public + * @return bool + */ + public function downloadImage($image) + { + $host = $this->getById($image->hostID); + $downloadApi = 'http://' . $host->address . '/api/v1/download/add'; + + $apiParams['md5'] = $image->md5; + $apiParams['task'] = 1; + $apiParams['url'] = $image->address; + + $response = json_decode(commonModel::http($downloadApi, $apiParams)); + + if($response->code == 200) return true; + + dao::$errors[] = $response->msg; + return false; + } + /** * Get host by id. * diff --git a/module/zahost/view/browseimage.html.php b/module/zahost/view/browseimage.html.php new file mode 100755 index 0000000000..8c6b5265c8 --- /dev/null +++ b/module/zahost/view/browseimage.html.php @@ -0,0 +1,82 @@ + + * @package zahost + * @version $Id$ + * @link http://www.zentao.net + */ +?> + + +
' data-module='vmTemplate'>
+
+ recTotal}&recPerPage={$pager->recPerPage}";?> + +
+

+ zahost->image->imageEmpty;?> + ' . $lang->zahost->image->createImage, '', 'class="btn btn-info"');?> +

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
idAB);?>zahost->image->name);?>zahost->image->memory);?>zahost->image->disk);?>zahost->image->osCategory);?>zahost->image->osType);?>zahost->image->osVersion);?>zahost->image->osLang);?>zahost->status;?>actions;?>
id;?>name;?>memory . zget($this->lang->zahost->unitList, 'GB');?>disk . zget($this->lang->zahost->unitList, 'GB');?>zahost->os->list, $image->osCategory);?>zahost->os->type[$image->osCategory], $image->osType);?>zahost->versionList[$image->osType], $image->osVersion);?>zahost->langList, $image->osLang);?>zahost->image->statusList, $image->status, '');?> + id}", $image, 'list', 'download');?> + createLink('zahost', 'downloadImage', "id={$image->id}"), '', 'hiddenwin', "title='{$lang->zahost->image->downloadImage}' class='btn'");?> + id}", $image, 'list', 'edit');?> + createLink('zahost', 'deleteImage', "id={$image->id}"), '', 'hiddenwin', "title='{$lang->zahost->delete}' class='btn'");?> +
+ + +
+getModuleRoot() . 'common/view/footer.html.php';?> diff --git a/module/zahost/view/createimage.html.php b/module/zahost/view/createimage.html.php new file mode 100644 index 0000000000..c78c79f70c --- /dev/null +++ b/module/zahost/view/createimage.html.php @@ -0,0 +1,79 @@ + + * @package zahost + * @version $Id$ + * @link http://www.zentao.net + */ +?> +getModuleRoot() . 'common/view/header.html.php';?> +zahost);?> +zahost);?> +hostID);?> +
+
+

zahost->image->createImage;?>

+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
zahost->common;?>name;?>
zahost->image->choseImage;?>
zahost->name;?>
zahost->memory;?> +
+ + MB +
+
zahost->diskSize;?> +
+ + GB +
+
zahost->image->osCategory;?>zahost->os->list, '', "class='form-control chosen'")?>
zahost->image->osType;?>
zahost->image->osVersion;?>
zahost->image->osLang;?>zahost->langList, '', "class='form-control chosen'")?>
+ + + id);?> +
+
+
+getModuleRoot() . 'common/view/footer.html.php';?> diff --git a/module/zahost/view/editimage.html.php b/module/zahost/view/editimage.html.php new file mode 100644 index 0000000000..e7faa05750 --- /dev/null +++ b/module/zahost/view/editimage.html.php @@ -0,0 +1,80 @@ + + * @package zahost + * @version $Id$ + * @link http://www.zentao.net + */ +?> +getModuleRoot() . 'common/view/header.html.php';?> +zahost);?> +zahost);?> + +hostID);?> +
+
+

zahost->createTemplate;?>

+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
zahost->name;?>name, "class='form-control'");?>
zahost->cpuCores;?>cpuCoreNum, "class='form-control'");?>
zahost->memory;?> +
+ memorySize, "class='form-control'");?> + MB +
+
zahost->diskSize;?> +
+ diskSize/1024), "class='form-control'");?> + GB +
+
zahost->vmTemplate->osCategory;?>zahost->os->list, $template->osCategory, "class='form-control chosen'")?>
zahost->vmTemplate->osType;?>osType, "class='form-control chosen'")?>
zahost->vmTemplate->osVersion;?>osVersion, "class='form-control chosen'")?>
zahost->vmTemplate->osLang;?>zahost->langList, $template->osLang, "class='form-control chosen'")?>
zahost->vmTemplate->imageName;?>imageName, "class='form-control chosen'")?>
+ + + +
+
+
+getModuleRoot() . 'common/view/footer.html.php';?>