From bf4e2d991e5683a28cb85abf430fe2797302325e Mon Sep 17 00:00:00 2001 From: xieqiyu Date: Wed, 27 Dec 2023 14:06:03 +0800 Subject: [PATCH] * Refactore zanodeModel::restoreSnapshot. --- module/zanode/control.php | 2 +- module/zanode/model.php | 30 +++++++++++-------- module/zanode/test/model/restoresnapshot.php | 24 +++++++++++++++ .../test/model/yaml/restoresnapshot/host.yaml | 28 +++++++++++++++++ .../model/yaml/restoresnapshot/image.yaml | 16 ++++++++++ module/zanode/test/zanode.class.php | 17 +++++++++++ 6 files changed, 103 insertions(+), 14 deletions(-) create mode 100644 module/zanode/test/model/restoresnapshot.php create mode 100644 module/zanode/test/model/yaml/restoresnapshot/host.yaml create mode 100644 module/zanode/test/model/yaml/restoresnapshot/image.yaml diff --git a/module/zanode/control.php b/module/zanode/control.php index e09f61a51d..72537659ef 100644 --- a/module/zanode/control.php +++ b/module/zanode/control.php @@ -425,7 +425,7 @@ class zanode extends control public function restoreSnapshot(int $nodeID, int $snapshotID) { $this->zanode->restoreSnapshot($nodeID, $snapshotID); - if(dao::isError()) return $this->sendError(dao::getError()); + if(dao::isError()) return $this->sendError(dao::getError(), true); return $this->sendSuccess(array('message' => $this->lang->zanode->actionSuccess, 'load' => true)); } diff --git a/module/zanode/model.php b/module/zanode/model.php index 1a1ef7eebd..a9cbcceee6 100644 --- a/module/zanode/model.php +++ b/module/zanode/model.php @@ -207,31 +207,32 @@ class zanodemodel extends model } /** - * Restore Snapshot to zanode. + * 将执行节点还原到此快照。 + * Restore zanode to snapshot. * * @param int $zanodeID * @param int $snapshotID * @access public * @return bool */ - public function restoreSnapshot($zanodeID = 0, $snapshotID = 0) + public function restoreSnapshot(int $zanodeID = 0, int $snapshotID = 0) { $node = $this->getNodeByID($zanodeID); $snap = $this->getImageByID($snapshotID); - $snap->status = ($snap->status == 'restoring' && time() - strtotime($snap->restoreDate) > 600) ? 'restore_failed' : $snap->status; + /* 检查快照的状态。*/ + /* Check snapshot status. */ + $snap->status = $snap->status == 'restoring' && time() - strtotime($snap->restoreDate) > 600 ? 'restore_failed' : $snap->status; if(!in_array($snap->status, array('completed', 'restoring', 'restore_failed', 'restore_completed'))) dao::$errors = $this->lang->zanode->snapStatusError; if($snap->status == 'restoring') dao::$errors = $this->lang->zanode->snapRestoring; if(dao::isError()) return false; - //update snapshot status - $this->dao->update(TABLE_IMAGE) - ->set('status')->eq('restoring') - ->set('restoreDate')->eq(helper::now()) - ->where('id')->eq($snapshotID)->exec(); + /* 更新快照状态。*/ + /* Update snapshot status. */ + $this->dao->update(TABLE_IMAGE)->set('status')->eq('restoring')->set('restoreDate')->eq(helper::now())->where('id')->eq($snapshotID)->exec(); - - /* Prepare create params. */ + /* 执行还原命令。*/ + /* Execute the restore command. */ $agnetUrl = 'http://' . $node->ip . ':' . $node->hzap; $param = array(array( 'name' => $snap->name, @@ -239,9 +240,10 @@ class zanodemodel extends model 'type' => 'revertSnap', 'vm' => $node->name )); + $result = json_decode(commonModel::http($agnetUrl . static::SNAPSHOT_CREATE_PATH, json_encode($param, JSON_NUMERIC_CHECK), array(), array("Authorization:$node->tokenSN"), 'data', 'POST', 10)); - $result = json_decode(commonModel::http($agnetUrl . static::SNAPSHOT_CREATE_PATH, json_encode($param,JSON_NUMERIC_CHECK), null, array("Authorization:$node->tokenSN"), 'data', 'POST', 10)); - + /* 若执行成功修改执行节点的状态。*/ + /* Change node status when success. */ if(!empty($result) and $result->code == 'success') { $this->dao->update(TABLE_ZAHOST)->set('status')->eq('restoring')->where('id')->eq($node->id)->exec(); @@ -249,8 +251,10 @@ class zanodemodel extends model return true; } + /* 执行失败时修改快照状态为完成。*/ + /* Change status to completed when fail. */ $this->dao->update(TABLE_IMAGE)->set('status')->eq('completed')->where('id')->eq($snapshotID)->exec(); - dao::$errors[] = (!empty($result) and !empty($result->msg)) ? $result->msg : $this->lang->zanode->apiError['fail']; + dao::$errors[] = !empty($result) && !empty($result->msg) ? $result->msg : $this->lang->zanode->apiError['fail']; return false; } diff --git a/module/zanode/test/model/restoresnapshot.php b/module/zanode/test/model/restoresnapshot.php new file mode 100644 index 0000000000..44cc84678d --- /dev/null +++ b/module/zanode/test/model/restoresnapshot.php @@ -0,0 +1,24 @@ +#!/usr/bin/env php +restoreSnapshot(). +cid=1 + +- 测试快照状态错误 @快照不可用 +- 测试正常还原属性status @restoring + +*/ + +include dirname(__FILE__, 5) . '/test/lib/init.php'; +include dirname(__FILE__, 2) . '/zanode.class.php'; + +zdTable('host')->config('host')->gen(2); +zdTable('image')->config('image')->gen(3); +zdTable('user')->gen(1); +su('admin'); + +$zanode = new zanodeTest(); +r($zanode->restoreSnapshotTest(2, 3)) && p(0) && e('快照不可用'); //测试快照状态错误 +r($zanode->restoreSnapshotTest(2, 1)) && p('status') && e('restoring'); //测试正常还原 diff --git a/module/zanode/test/model/yaml/restoresnapshot/host.yaml b/module/zanode/test/model/yaml/restoresnapshot/host.yaml new file mode 100644 index 0000000000..bc9db57589 --- /dev/null +++ b/module/zanode/test/model/yaml/restoresnapshot/host.yaml @@ -0,0 +1,28 @@ +title: zt_host +author: Qiyu Xie +version: "1.0" +fields: + - field: id + range: 1-1000 + - field: name + fields: + - field: name1 + range: zahost,zanode + - field: name2 + range: 1-1000 + - field: type + range: zahost,node + - field: hostType + range: 'physical,[]' + - field: status + range: online,running + - field: image + range: 1 + - field: parent + range: '0,1' + - field: tokenSN + range: f9f9220b37bd2a92061417118afe165c,[] + - field: zap + range: '55001,55156' + - field: extranet + range: "10.0.1.222,[]" diff --git a/module/zanode/test/model/yaml/restoresnapshot/image.yaml b/module/zanode/test/model/yaml/restoresnapshot/image.yaml new file mode 100644 index 0000000000..8a8ee06c86 --- /dev/null +++ b/module/zanode/test/model/yaml/restoresnapshot/image.yaml @@ -0,0 +1,16 @@ +title: zt_image +author: Qiyu Xie +version: "1.0" +fields: + - field: id + range: 1-1000 + - field: host + range: 2 + - field: name + fields: + - field: name1 + range: snapshot + - field: name2 + range: 1-1000 + - field: status + range: completed,restoring,running diff --git a/module/zanode/test/zanode.class.php b/module/zanode/test/zanode.class.php index ba58e8e730..cc2d74cee7 100644 --- a/module/zanode/test/zanode.class.php +++ b/module/zanode/test/zanode.class.php @@ -177,4 +177,21 @@ class zanodeTest return $this->objectModel->dao->select('*')->from(TABLE_IMAGE)->where('id')->eq($snapshotID)->fetch(); } + + /** + * 测试将执行节点还原到此快照。 + * Test restore zanode to snapshot. + * + * @param int $zanodeID + * @param int $snapshotID + * @access public + * @return bool + */ + public function restoreSnapshotTest(int $zanodeID = 0, int $snapshotID = 0) + { + $result = $this->restoreSnapshot($zanodeID, $snapshotID); + if(!$result) return dao::getError(); + + return $this->objectModel->dao->select('*')->from(TABLE_ZAHOST)->where('id')->eq($zanodeID)->fetch(); + } }